From 7a0335c8a35bff97a843dff14eb85b70fa281483 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Fri, 18 Oct 2002 14:05:25 +0000 Subject: [PATCH] Added support for roster watching. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1578 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/servers/registrar/Jamfile | 3 + src/servers/registrar/Registrar.cpp | 6 ++ src/servers/registrar/TRoster.cpp | 113 ++++++++++++++++++++++++++++ src/servers/registrar/TRoster.h | 7 ++ 4 files changed, 129 insertions(+) diff --git a/src/servers/registrar/Jamfile b/src/servers/registrar/Jamfile index 7aea2466bc..14bad0b3d4 100644 --- a/src/servers/registrar/Jamfile +++ b/src/servers/registrar/Jamfile @@ -8,6 +8,7 @@ Server obos_registrar : AppInfoList.cpp ClipboardHandler.cpp Event.cpp + EventMaskWatcher.cpp EventQueue.cpp MessageEvent.cpp MessageRunnerManager.cpp @@ -15,6 +16,8 @@ Server obos_registrar : Registrar.cpp RosterAppInfo.cpp TRoster.cpp + Watcher.cpp + WatchingService.cpp ; LinkSharedOSLibs obos_registrar : libopenbeos.so diff --git a/src/servers/registrar/Registrar.cpp b/src/servers/registrar/Registrar.cpp index eb8f6e8938..3616203b1a 100644 --- a/src/servers/registrar/Registrar.cpp +++ b/src/servers/registrar/Registrar.cpp @@ -131,6 +131,12 @@ Registrar::MessageReceived(BMessage *message) case B_REG_BROADCAST: fRoster->HandleBroadcast(message); break; + case B_REG_START_WATCHING: + fRoster->HandleStartWatching(message); + break; + case B_REG_STOP_WATCHING: + fRoster->HandleStopWatching(message); + break; // message runner requests case B_REG_REGISTER_MESSAGE_RUNNER: fMessageRunnerManager->HandleRegisterRunner(message); diff --git a/src/servers/registrar/TRoster.cpp b/src/servers/registrar/TRoster.cpp index f4f9676482..c76def36c4 100644 --- a/src/servers/registrar/TRoster.cpp +++ b/src/servers/registrar/TRoster.cpp @@ -34,6 +34,7 @@ #include "RegistrarDefs.h" #include "RosterAppInfo.h" #include "TRoster.h" +#include "EventMaskWatcher.h" /*! \class TRoster @@ -74,6 +75,7 @@ TRoster::TRoster() fEarlyPreRegisteredApps(), fIAPRRequests(), fActiveApp(NULL), + fWatchingService(), fLastToken(0) { } @@ -685,6 +687,79 @@ TRoster::HandleBroadcast(BMessage *request) FUNCTION_END(); } +// HandleStartWatching +/*! \brief Handles a StartWatching() request. + \param request The request message +*/ +void +TRoster::HandleStartWatching(BMessage *request) +{ + FUNCTION_START(); + + status_t error = B_OK; + // get the parameters + BMessenger target; + uint32 events; + if (error == B_OK && request->FindMessenger("target", &target) != B_OK) + error = B_BAD_VALUE; + if (request->FindInt32("events", (int32*)&events) != B_OK) + error = B_BAD_VALUE; + // add the new watcher + if (error == B_OK) { + Watcher *watcher = new(nothrow) EventMaskWatcher(target, events); + if (watcher) { + if (!fWatchingService.AddWatcher(watcher)) { + error = B_NO_MEMORY; + delete watcher; + } + } else + error = B_NO_MEMORY; + } + // reply to the request + if (error == B_OK) { + BMessage reply(B_REG_SUCCESS); + request->SendReply(&reply); + } else { + BMessage reply(B_REG_ERROR); + reply.AddInt32("error", error); + request->SendReply(&reply); + } + + FUNCTION_END(); +} + +// HandleStopWatching +/*! \brief Handles a StopWatching() request. + \param request The request message +*/ +void +TRoster::HandleStopWatching(BMessage *request) +{ + FUNCTION_START(); + + status_t error = B_OK; + // get the parameters + BMessenger target; + if (error == B_OK && request->FindMessenger("target", &target) != B_OK) + error = B_BAD_VALUE; + // remove the watcher + if (error == B_OK) { + if (!fWatchingService.RemoveWatcher(target)) + error = B_BAD_VALUE; + } + // reply to the request + if (error == B_OK) { + BMessage reply(B_REG_SUCCESS); + request->SendReply(&reply); + } else { + BMessage reply(B_REG_ERROR); + reply.AddInt32("error", error); + request->SendReply(&reply); + } + + FUNCTION_END(); +} + // Init /*! \brief Initializes the roster. @@ -827,6 +902,11 @@ TRoster::CheckSanity() void TRoster::_AppAdded(RosterAppInfo *info) { + // notify the watchers + BMessage message(B_SOME_APP_LAUNCHED); + _AddMessageWatchingInfo(&message, info); + EventMaskWatcherFilter filter(B_REQUEST_LAUNCHED); + fWatchingService.NotifyWatchers(&message, &filter); } // _AppRemoved @@ -840,6 +920,11 @@ TRoster::_AppRemoved(RosterAppInfo *info) // deactivate the app, if it was the active one if (info == fActiveApp) ActivateApp(NULL); + // notify the watchers + BMessage message(B_SOME_APP_QUIT); + _AddMessageWatchingInfo(&message, info); + EventMaskWatcherFilter filter(B_REQUEST_QUIT); + fWatchingService.NotifyWatchers(&message, &filter); } } @@ -858,6 +943,11 @@ TRoster::_AppActivated(RosterAppInfo *info) BMessage message(B_APP_ACTIVATED); message.AddBool("active", true); messenger.SendMessage(&message); + // notify the watchers + BMessage watcherMessage(B_SOME_APP_ACTIVATED); + _AddMessageWatchingInfo(&watcherMessage, info); + EventMaskWatcherFilter filter(B_REQUEST_ACTIVATED); + fWatchingService.NotifyWatchers(&watcherMessage, &filter); } } } @@ -908,6 +998,29 @@ TRoster::_AddMessageAppInfo(BMessage *message, const app_info *info) sizeof(flat_app_info)); } +// _AddMessageWatchingInfo +/*! \brief Adds application monitoring related fields to a message. + \param message The message. + \param info The app_info of the concerned application. + \return \c B_OK if everything went fine, an error code otherwise. +*/ +status_t +TRoster::_AddMessageWatchingInfo(BMessage *message, const app_info *info) +{ + status_t error = B_OK; + if (error == B_OK) + error = message->AddString("be:signature", info->signature); + if (error == B_OK) + error = message->AddInt32("be:team", info->team); + if (error == B_OK) + error = message->AddInt32("be:thread", info->thread); + if (error == B_OK) + error = message->AddInt32("be:flags", (int32)info->flags); + if (error == B_OK) + error = message->AddRef("be:ref", &info->ref); + return error; +} + // _NextToken /*! \brief Returns the next available token. \return The token. diff --git a/src/servers/registrar/TRoster.h b/src/servers/registrar/TRoster.h index 33dbe3984b..b2737a00c7 100644 --- a/src/servers/registrar/TRoster.h +++ b/src/servers/registrar/TRoster.h @@ -33,8 +33,10 @@ #include #include "AppInfoList.h" +#include "WatchingService.h" class BMessage; +class WatchingService; struct IAPRRequest { entry_ref ref; @@ -63,6 +65,8 @@ public: void HandleGetAppList(BMessage *request); void HandleActivateApp(BMessage *request); void HandleBroadcast(BMessage *request); + void HandleStartWatching(BMessage *request); + void HandleStopWatching(BMessage *request); status_t Init(); @@ -82,6 +86,8 @@ private: // helper functions static status_t _AddMessageAppInfo(BMessage *message, const app_info *info); + static status_t _AddMessageWatchingInfo(BMessage *message, + const app_info *info); uint32 _NextToken(); void _ReplyToIAPRRequest(BMessage *request, const RosterAppInfo *info); @@ -90,6 +96,7 @@ private: AppInfoList fEarlyPreRegisteredApps; IAPRRequestMap fIAPRRequests; RosterAppInfo *fActiveApp; + WatchingService fWatchingService; uint32 fLastToken; };