diff --git a/src/servers/registrar/EventMaskWatcher.cpp b/src/servers/registrar/EventMaskWatcher.cpp new file mode 100644 index 0000000000..7ce025cebb --- /dev/null +++ b/src/servers/registrar/EventMaskWatcher.cpp @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: EventMaskWatcher.cpp +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: EventMaskWatcher is a Watcher extended by an event mask. +// EventMaskWatcherFilter filters EventMaskWatchers with +// respect to their event mask and a specific event. +//------------------------------------------------------------------------------ + +#include "EventMaskWatcher.h" + +// EventMaskWatcher + +// constructor +EventMaskWatcher::EventMaskWatcher(const BMessenger &target, uint32 eventMask) + : Watcher(target), + fEventMask(eventMask) +{ +} + +// EventMask +uint32 +EventMaskWatcher::EventMask() const +{ + return fEventMask; +} + + +// EventMaskWatcherFilter + +// constructor +EventMaskWatcherFilter::EventMaskWatcherFilter(uint32 event) + : WatcherFilter(), + fEvent(event) +{ +} + +// Filter +bool +EventMaskWatcherFilter::Filter(Watcher *watcher, BMessage *message) +{ + EventMaskWatcher *emWatcher = dynamic_cast(watcher); + return (emWatcher && (emWatcher->EventMask() & fEvent)); +} + diff --git a/src/servers/registrar/EventMaskWatcher.h b/src/servers/registrar/EventMaskWatcher.h new file mode 100644 index 0000000000..2a2511d8e4 --- /dev/null +++ b/src/servers/registrar/EventMaskWatcher.h @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: EventMaskWatcher.h +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: EventMaskWatcher is a Watcher extended by an event mask. +// EventMaskWatcherFilter filters EventMaskWatchers with +// respect to their event mask and a specific event. +//------------------------------------------------------------------------------ + +#ifndef EVENT_MASK_WATCHER_H +#define EVENT_MASK_WATCHER_H + +#include "Watcher.h" + +// EventMaskWatcher +class EventMaskWatcher : public Watcher { +public: + EventMaskWatcher(const BMessenger &target, uint32 eventMask); + + uint32 EventMask() const; + +private: + uint32 fEventMask; +}; + +// EventMaskWatcherFilter +class EventMaskWatcherFilter : public WatcherFilter { +public: + EventMaskWatcherFilter(uint32 event); + + virtual bool Filter(Watcher *watcher, BMessage *message); + +private: + uint32 fEvent; +}; + +#endif // EVENT_MASK_WATCHER_H diff --git a/src/servers/registrar/Watcher.cpp b/src/servers/registrar/Watcher.cpp new file mode 100644 index 0000000000..d3a857a39d --- /dev/null +++ b/src/servers/registrar/Watcher.cpp @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: Watcher.cpp +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: A Watcher represents a target of a watching service. +// A WatcherFilter represents a predicate on a Watcher. +//------------------------------------------------------------------------------ + +#include + +#include "Watcher.h" + +// Watcher + +// constructor +Watcher::Watcher(const BMessenger &target) + : fTarget(target) +{ +} + +// destructor +Watcher::~Watcher() +{ +} + +// Target +const BMessenger& +Watcher::Target() const +{ + return fTarget; +} + +// SendMessage +status_t +Watcher::SendMessage(BMessage *message) +{ + return fTarget.SendMessage(message, (BHandler*)NULL, 0); +} + + +// WatcherFilter + +// constructor +WatcherFilter::WatcherFilter() +{ +} + +// destructor +WatcherFilter::~WatcherFilter() +{ +} + +// Filter +bool +WatcherFilter::Filter(Watcher *watcher, BMessage *message) +{ + return true; +} + diff --git a/src/servers/registrar/Watcher.h b/src/servers/registrar/Watcher.h new file mode 100644 index 0000000000..764dd440fb --- /dev/null +++ b/src/servers/registrar/Watcher.h @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: Watcher.h +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: A Watcher represents a target of a watching service. +// A WatcherFilter represents a predicate on a Watcher. +//------------------------------------------------------------------------------ + +#ifndef WATCHER_H +#define WATCHER_H + +#include + +// Watcher +class Watcher { +public: + Watcher(const BMessenger &target); + virtual ~Watcher(); + + const BMessenger &Target() const; + + status_t SendMessage(BMessage *message); + +private: + BMessenger fTarget; +}; + +// WatcherFilter +class WatcherFilter { +public: + WatcherFilter(); + virtual ~WatcherFilter(); + + virtual bool Filter(Watcher *watcher, BMessage *message); +}; + +#endif // WATCHER_H diff --git a/src/servers/registrar/WatchingService.cpp b/src/servers/registrar/WatchingService.cpp new file mode 100644 index 0000000000..df21d29772 --- /dev/null +++ b/src/servers/registrar/WatchingService.cpp @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: WatchingService.cpp +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: Features everything needed to provide a watching service. +//------------------------------------------------------------------------------ + +#include + +#include "Watcher.h" +#include "WatchingService.h" + +// constructor +WatchingService::WatchingService() + : fWatchers() +{ +} + +// destructor +WatchingService::~WatchingService() +{ + // delete the watchers + for (watcher_map::iterator it = fWatchers.begin(); + it != fWatchers.end(); + ++it) { + delete it->second; + } +} + +// AddWatcher +bool +WatchingService::AddWatcher(Watcher *watcher) +{ + bool result = (watcher); + if (result) { + RemoveWatcher(watcher->Target(), true); + fWatchers[watcher->Target()] = watcher; + } + return result; +} + +// AddWatcher +bool +WatchingService::AddWatcher(const BMessenger &target) +{ + return AddWatcher(new(nothrow) Watcher(target)); +} + +// RemoveWatcher +bool +WatchingService::RemoveWatcher(Watcher *watcher, bool deleteWatcher) +{ + return (watcher && RemoveWatcher(watcher->Target(), deleteWatcher)); +} + +// RemoveWatcher +bool +WatchingService::RemoveWatcher(const BMessenger &target, bool deleteWatcher) +{ + watcher_map::iterator it = fWatchers.find(target); + bool result = (it != fWatchers.end()); + if (result) { + if (deleteWatcher) + delete it->second; + fWatchers.erase(it); + } + return result; +} + +// NotifyWatchers +void +WatchingService::NotifyWatchers(BMessage *message, WatcherFilter *filter) +{ + BList staleWatchers; + // deliver the message + for (watcher_map::iterator it = fWatchers.begin(); + it != fWatchers.end(); + ++it) { + Watcher *watcher = it->second; +// TODO: If a watcher is invalid, but the filter never selects it, it will +// not be removed. + if (!filter || filter->Filter(watcher, message)) { + status_t error = watcher->SendMessage(message); + if (error != B_OK && error != B_WOULD_BLOCK) + staleWatchers.AddItem(watcher); + } + } + // remove the stale watchers + for (int32 i = 0; + Watcher *watcher = (Watcher*)staleWatchers.ItemAt(i); + i++) { + RemoveWatcher(watcher, true); + } +} + diff --git a/src/servers/registrar/WatchingService.h b/src/servers/registrar/WatchingService.h new file mode 100644 index 0000000000..1d1a9c1ea1 --- /dev/null +++ b/src/servers/registrar/WatchingService.h @@ -0,0 +1,56 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: WatchingService.h +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: Features everything needed to provide a watching service. +//------------------------------------------------------------------------------ + +#ifndef WATCHING_SERVICE_H +#define WATCHING_SERVICE_H + +#include + +#include + +class Watcher; +class WatcherFilter; + +class WatchingService { +public: + WatchingService(); + virtual ~WatchingService(); + + bool AddWatcher(Watcher *watcher); + bool AddWatcher(const BMessenger &target); + bool RemoveWatcher(Watcher *watcher, bool deleteWatcher = true); + bool RemoveWatcher(const BMessenger &target, bool deleteWatcher = true); + + void NotifyWatchers(BMessage *message, WatcherFilter *filter = NULL); + +private: + typedef map watcher_map; + +private: + watcher_map fWatchers; +}; + +#endif // WATCHING_SERVICE_H