midi_server: cleanup.

* Style cleanup.
* Moved documentation to source file.
* Improved variable/member naming, added '_' prefix for private methods.
This commit is contained in:
Axel Dörfler 2015-10-14 21:15:38 +02:00
parent 8a3ae7a49d
commit 83f803c6ff
2 changed files with 423 additions and 399 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009, Haiku, Inc. All rights reserved.
* Copyright 2002-2015, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
@ -8,129 +8,92 @@
#ifndef MIDI_SERVER_APP_H
#define MIDI_SERVER_APP_H
#include <Application.h>
#include <List.h>
#include "DeviceWatcher.h"
struct app_t;
struct endpoint_t;
// The heart of the midi_server. This BApplication subclass
// keeps the roster of endpoints and applications, processes
// incoming messages from libmidi2.so, and notifies the apps
// when something interesting happens.
class MidiServerApp : public BApplication
{
/*! The heart of the midi_server. This BApplication subclass
keeps the roster of endpoints and applications, processes
incoming messages from libmidi2.so, and notifies the apps
when something interesting happens.
*/
class MidiServerApp : public BApplication {
public:
MidiServerApp();
virtual ~MidiServerApp();
MidiServerApp();
virtual ~MidiServerApp();
virtual void AboutRequested();
virtual void MessageReceived(BMessage* msg);
virtual void AboutRequested();
virtual void MessageReceived(BMessage* msg);
private:
typedef BApplication super;
void OnRegisterApp(BMessage* msg);
void OnCreateEndpoint(BMessage* msg);
void OnDeleteEndpoint(BMessage* msg);
void OnPurgeEndpoint(BMessage* msg);
void OnChangeEndpoint(BMessage* msg);
void OnConnectDisconnect(BMessage* msg);
void _OnRegisterApp(BMessage* msg);
void _OnCreateEndpoint(BMessage* msg);
void _OnDeleteEndpoint(BMessage* msg);
void _OnPurgeEndpoint(BMessage* msg);
void _OnChangeEndpoint(BMessage* msg);
void _OnConnectDisconnect(BMessage* msg);
// Sends an app MSG_ENDPOINT_CREATED notifications for
// all current endpoints. Used when the app registers.
bool SendAllEndpoints(app_t* app);
bool _SendAllEndpoints(app_t* app);
bool _SendAllConnections(app_t* app);
// Sends an app MSG_ENDPOINTS_CONNECTED notifications for
// all current connections. Used when the app registers.
bool SendAllConnections(app_t* app);
void _AddEndpoint(BMessage* msg, endpoint_t* endp);
void _RemoveEndpoint(app_t* app, endpoint_t* endp);
// Adds the specified endpoint to the roster, and notifies
// all other applications about this event.
void AddEndpoint(BMessage* msg, endpoint_t* endp);
void _DisconnectDeadConsumer(endpoint_t* cons);
// Removes an endpoint from the roster, and notifies all
// other apps about this event. "app" is the application
// that the endpoint belongs to; if it is NULL, the app
// no longer exists and we're purging the endpoint.
void RemoveEndpoint(app_t* app, endpoint_t* endp);
void _MakeCreatedNotification(BMessage* msg,
endpoint_t* endp);
void _MakeConnectedNotification(BMessage* msg,
endpoint_t* prod, endpoint_t* cons,
bool mustConnect);
// Removes a consumer from the list of connections of
// all the producers it is connected to, just before
// we remove it from the roster.
void DisconnectDeadConsumer(endpoint_t* cons);
app_t* _WhichApp(BMessage* msg);
endpoint_t* _WhichEndpoint(BMessage* msg, app_t* app);
endpoint_t* _FindEndpoint(int32 id);
// Fills up a MSG_ENDPOINT_CREATED message.
void MakeCreatedNotification(BMessage* msg, endpoint_t* endp);
void _NotifyAll(BMessage* msg, app_t* except);
bool _SendNotification(app_t* app, BMessage* msg);
bool _SendReply(app_t* app, BMessage* msg,
BMessage* reply);
// Fills up a MSG_ENDPOINTS_(DIS)CONNECTED message.
void MakeConnectedNotification(
BMessage* msg, endpoint_t* prod, endpoint_t* cons, bool mustConnect);
void _DeliveryError(app_t* app);
// Figures out which application a message came from.
// Returns NULL if the application is not registered.
app_t* WhichApp(BMessage* msg);
int32 _CountApps();
app_t* _AppAt(int32 index);
// Looks at the "midi:id" field from a message, and returns
// the endpoint object that corresponds to that ID. It also
// checks whether the application specified by "app" really
// owns the endpoint. Returns NULL on error.
endpoint_t* WhichEndpoint(BMessage* msg, app_t* app);
int32 _CountEndpoints();
endpoint_t* _EndpointAt(int32 index);
// Returns the endpoint with the specified ID, or
// NULL if no such endpoint exists on the roster.
endpoint_t* FindEndpoint(int32 id);
int32 _CountConnections(endpoint_t* prod);
endpoint_t* _ConnectionAt(endpoint_t* prod, int32 index);
// Sends notification messages to all registered apps,
// except to the application that triggered the event.
// The "except" app is allowed to be NULL.
void NotifyAll(BMessage* msg, app_t* except);
#ifdef DEBUG
void _DumpApps();
void _DumpEndpoints();
#endif
// Sends a notification message to an application, which is
// not necessarily registered yet. Applications never reply
// to such notification messages.
bool SendNotification(app_t* app, BMessage* msg);
private:
//! The registered applications.
BList fApps;
// Sends a reply to a request made by an application.
// If "app" is NULL, the application is not registered
// (and the reply should contain an error code).
bool SendReply(app_t* app, BMessage* msg, BMessage* reply);
//! All the endpoints in the system.
BList fEndpoints;
// Removes an app and all of its endpoints from the roster
// if a reply or notification message cannot be delivered.
// (Waiting for communications to fail is actually our only
// way to get rid of stale endpoints.)
void DeliveryError(app_t* app);
//! The ID we will assign to the next new endpoint.
int32 fNextID;
int32 CountApps();
app_t* AppAt(int32 index);
int32 CountEndpoints();
endpoint_t* EndpointAt(int32 index);
int32 CountConnections(endpoint_t* prod);
endpoint_t* ConnectionAt(endpoint_t* prod, int32 index);
// The registered applications.
BList apps;
// All the endpoints in the system.
BList endpoints;
// The ID we will assign to the next new endpoint.
int32 nextId;
// Watch endpoints from /dev/midi drivers.
DeviceWatcher* fDeviceWatcher;
#ifdef DEBUG
void DumpApps();
void DumpEndpoints();
#endif
//! Watch endpoints from /dev/midi drivers.
DeviceWatcher* fDeviceWatcher;
};
#endif // MIDI_SERVER_APP_H