1*76bb8030Smahlzeit /* 2*76bb8030Smahlzeit * Copyright (c) 2002-2003 Matthijs Hollemans 37c27cd32Smahlzeit */ 437192de9Smahlzeit 537192de9Smahlzeit #ifndef MIDI_SERVER_APP_H 637192de9Smahlzeit #define MIDI_SERVER_APP_H 737192de9Smahlzeit 837192de9Smahlzeit #include <Application.h> 953284a9fSmahlzeit #include <List.h> 1037192de9Smahlzeit 1153284a9fSmahlzeit struct app_t; 1253284a9fSmahlzeit struct endpoint_t; 1327d74562Smahlzeit 14*76bb8030Smahlzeit // The heart of the midi_server. This BApplication subclass 15*76bb8030Smahlzeit // keeps the roster of endpoints and applications, processes 16*76bb8030Smahlzeit // incoming messages from libmidi2.so, and notifies the apps 17*76bb8030Smahlzeit // when something interesting happens. 1837192de9Smahlzeit class MidiServerApp : public BApplication 1937192de9Smahlzeit { 2037192de9Smahlzeit public: 2153284a9fSmahlzeit 2237192de9Smahlzeit MidiServerApp(); 2327d74562Smahlzeit virtual ~MidiServerApp(); 2427d74562Smahlzeit 2537192de9Smahlzeit virtual void AboutRequested(); 2627d74562Smahlzeit virtual void MessageReceived(BMessage* msg); 2796462df1Sjerl1 2896462df1Sjerl1 private: 2996462df1Sjerl1 3027d74562Smahlzeit typedef BApplication super; 3127d74562Smahlzeit 3253284a9fSmahlzeit void OnRegisterApp(BMessage* msg); 3353284a9fSmahlzeit void OnCreateEndpoint(BMessage* msg); 3453284a9fSmahlzeit void OnDeleteEndpoint(BMessage* msg); 3553284a9fSmahlzeit void OnPurgeEndpoint(BMessage* msg); 3653284a9fSmahlzeit void OnChangeEndpoint(BMessage* msg); 3753284a9fSmahlzeit void OnConnectDisconnect(BMessage* msg); 3853284a9fSmahlzeit 39*76bb8030Smahlzeit // Sends an app MSG_ENDPOINT_CREATED notifications for 40*76bb8030Smahlzeit // all current endpoints. Used when the app registers. 4153284a9fSmahlzeit bool SendAllEndpoints(app_t* app); 4253284a9fSmahlzeit 43*76bb8030Smahlzeit // Sends an app MSG_ENDPOINTS_CONNECTED notifications for 44*76bb8030Smahlzeit // all current connections. Used when the app registers. 4553284a9fSmahlzeit bool SendAllConnections(app_t* app); 4653284a9fSmahlzeit 47*76bb8030Smahlzeit // Adds the specified endpoint to the roster, and notifies 48*76bb8030Smahlzeit // all other applications about this event. 4953284a9fSmahlzeit void AddEndpoint(BMessage* msg, endpoint_t* endp); 5053284a9fSmahlzeit 51*76bb8030Smahlzeit // Removes an endpoint from the roster, and notifies all 52*76bb8030Smahlzeit // other apps about this event. "app" is the application 53*76bb8030Smahlzeit // that the endpoint belongs to; if it is NULL, the app 54*76bb8030Smahlzeit // no longer exists and we're purging the endpoint. 5553284a9fSmahlzeit void RemoveEndpoint(app_t* app, endpoint_t* endp); 5653284a9fSmahlzeit 57*76bb8030Smahlzeit // Removes a consumer from the list of connections of 58*76bb8030Smahlzeit // all the producers it is connected to, just before 59*76bb8030Smahlzeit // we remove it from the roster. 6053284a9fSmahlzeit void DisconnectDeadConsumer(endpoint_t* cons); 6153284a9fSmahlzeit 62*76bb8030Smahlzeit // Fills up a MSG_ENDPOINT_CREATED message. 6353284a9fSmahlzeit void MakeCreatedNotification(BMessage* msg, endpoint_t* endp); 6453284a9fSmahlzeit 65*76bb8030Smahlzeit // Fills up a MSG_ENDPOINTS_(DIS)CONNECTED message. 6653284a9fSmahlzeit void MakeConnectedNotification( 6753284a9fSmahlzeit BMessage* msg, endpoint_t* prod, endpoint_t* cons, bool mustConnect); 6853284a9fSmahlzeit 69*76bb8030Smahlzeit // Figures out which application a message came from. 70*76bb8030Smahlzeit // Returns NULL if the application is not registered. 7153284a9fSmahlzeit app_t* WhichApp(BMessage* msg); 7253284a9fSmahlzeit 73*76bb8030Smahlzeit // Looks at the "midi:id" field from a message, and returns 74*76bb8030Smahlzeit // the endpoint object that corresponds to that ID. It also 75*76bb8030Smahlzeit // checks whether the application specified by "app" really 76*76bb8030Smahlzeit // owns the endpoint. Returns NULL on error. 7753284a9fSmahlzeit endpoint_t* WhichEndpoint(BMessage* msg, app_t* app); 7853284a9fSmahlzeit 79*76bb8030Smahlzeit // Returns the endpoint with the specified ID, or 80*76bb8030Smahlzeit // NULL if no such endpoint exists on the roster. 8153284a9fSmahlzeit endpoint_t* FindEndpoint(int32 id); 8253284a9fSmahlzeit 83*76bb8030Smahlzeit // Sends notification messages to all registered apps, 84*76bb8030Smahlzeit // except to the application that triggered the event. 85*76bb8030Smahlzeit // The "except" app is allowed to be NULL. 8653284a9fSmahlzeit void NotifyAll(BMessage* msg, app_t* except); 8753284a9fSmahlzeit 88*76bb8030Smahlzeit // Sends a notification message to an application, which is 89*76bb8030Smahlzeit // not necessarily registered yet. Applications never reply 90*76bb8030Smahlzeit // to such notification messages. 9153284a9fSmahlzeit bool SendNotification(app_t* app, BMessage* msg); 9253284a9fSmahlzeit 93*76bb8030Smahlzeit // Sends a reply to a request made by an application. 94*76bb8030Smahlzeit // If "app" is NULL, the application is not registered 95*76bb8030Smahlzeit // (and the reply should contain an error code). 9653284a9fSmahlzeit bool SendReply(app_t* app, BMessage* msg, BMessage* reply); 9753284a9fSmahlzeit 98*76bb8030Smahlzeit // Removes an app and all of its endpoints from the roster 99*76bb8030Smahlzeit // if a reply or notification message cannot be delivered. 100*76bb8030Smahlzeit // (Waiting for communications to fail is actually our only 101*76bb8030Smahlzeit // way to get rid of stale endpoints.) 10253284a9fSmahlzeit void DeliveryError(app_t* app); 10353284a9fSmahlzeit 10453284a9fSmahlzeit int32 CountApps(); 10553284a9fSmahlzeit app_t* AppAt(int32 index); 10653284a9fSmahlzeit 10753284a9fSmahlzeit int32 CountEndpoints(); 10853284a9fSmahlzeit endpoint_t* EndpointAt(int32 index); 10953284a9fSmahlzeit 11053284a9fSmahlzeit int32 CountConnections(endpoint_t* prod); 11153284a9fSmahlzeit endpoint_t* ConnectionAt(endpoint_t* prod, int32 index); 11253284a9fSmahlzeit 113*76bb8030Smahlzeit // The registered applications. 11453284a9fSmahlzeit BList apps; 11553284a9fSmahlzeit 116*76bb8030Smahlzeit // All the endpoints in the system. 11753284a9fSmahlzeit BList endpoints; 11853284a9fSmahlzeit 119*76bb8030Smahlzeit // The ID we will assign to the next new endpoint. 12053284a9fSmahlzeit int32 nextId; 12153284a9fSmahlzeit 12253284a9fSmahlzeit #ifdef DEBUG 12353284a9fSmahlzeit void DumpApps(); 12453284a9fSmahlzeit void DumpEndpoints(); 12553284a9fSmahlzeit #endif 12637192de9Smahlzeit }; 12737192de9Smahlzeit 12837192de9Smahlzeit #endif // MIDI_SERVER_APP_H 129