1 //------------------------------------------------------------------------------
2 // Copyright (c) 2001-2002, OpenBeOS
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
22 // File Name: WatchingService.cpp
23 // Author: Ingo Weinhold (bonefish@users.sf.net)
24 // Description: Features everything needed to provide a watching service.
25 //------------------------------------------------------------------------------
30 #include "WatchingService.h"
34 /*! \class WatchingService
35 \brief Features everything needed to provide a watching service.
37 A watcher is represented by an object of the Watcher or a derived class.
38 It is identified by its target BMessenger. The service can't contain
39 more than one watcher with the same target BMessenger at a time.
41 New watchers can be registered with AddWatcher(), registered ones
42 unregistered via RemoveRegister(). NotifyWatchers() sends a specified
43 message to all registered watchers, or, if an optional WatcherFilter is
44 supplied, only to those watchers it selects.
47 /*! \var typedef map<BMessenger,Watcher*> WatchingService::watcher_map
48 \brief Watcher container type.
50 Defined for convenience.
53 /*! \var WatchingService::watcher_map WatchingService::fWatchers
54 \brief Container for the watchers registered to the service.
56 For each registered watcher \code Watcher *watcher \endcode, the map
57 contains an entry \code (watcher->Target(), watcher) \endcode.
62 /*! \brief Creates a new watching service.
64 The list of watchers is initially empty.
66 WatchingService::WatchingService()
72 /*! \brief Frees all resources associated with the object.
74 All registered watchers are deleted.
76 WatchingService::~WatchingService()
78 // delete the watchers
79 for (watcher_map::iterator it
= fWatchers
.begin();
80 it
!= fWatchers
.end();
87 /*! \brief Registers a new watcher to the watching service.
89 The ownership of \a watcher is transfered to the watching service, that
90 is the caller must not delete it after the method returns.
92 If the service already contains a Watcher with the same target BMessenger
93 (Watcher::Target()), the old watcher is removed and deleted before the
96 \param watcher The watcher to be registered.
97 \return \c true, if \a watcher is not \c NULL and adding was successfully,
101 WatchingService::AddWatcher(Watcher
*watcher
)
103 bool result
= (watcher
);
105 RemoveWatcher(watcher
->Target(), true);
106 fWatchers
[watcher
->Target()] = watcher
;
112 /*! \brief Registers a new watcher to the watching service.
114 A new \c Watcher is created with \a target as its target and added to
115 the watching service. The caller retains ownership of \a target, but the
116 newly created Watcher is owned by the watching service.
118 If the service already contains a Watcher with the same target BMessenger
119 (Watcher::Target()), the old watcher is removed and deleted before the
122 \param target The target BMessenger a Watcher shall be registered for.
123 \return \c true, if a new Watcher could be created and added successfully,
127 WatchingService::AddWatcher(const BMessenger
&target
)
129 return AddWatcher(new(nothrow
) Watcher(target
));
133 /*! \brief Unregisters a watcher from the watching service and optionally
136 If \a deleteWatcher is \c false, the ownership of \a watcher is transfered
137 to the caller, otherwise it is deleted.
139 \param watcher The watcher to be unregistered.
140 \param deleteWatcher If \c true, the watcher is deleted after being
142 \return \c true, if \a watcher was not \c NULL and registered to the
143 watching service before, \c false otherwise.
146 WatchingService::RemoveWatcher(Watcher
*watcher
, bool deleteWatcher
)
148 watcher_map::iterator it
= fWatchers
.find(watcher
->Target());
149 bool result
= (it
!= fWatchers
.end() && it
->second
== watcher
);
159 /*! \brief Unregisters a watcher from the watching service and optionally
162 The watcher is identified by its target BMessenger.
164 If \a deleteWatcher is \c false, the ownership of the concerned watcher is
165 transfered to the caller, otherwise it is deleted.
167 \param target The target BMessenger identifying the watcher to be
169 \param deleteWatcher If \c true, the watcher is deleted after being
171 \return \c true, if a watcher with the specified target was registered to
172 the watching service before, \c false otherwise.
175 WatchingService::RemoveWatcher(const BMessenger
&target
, bool deleteWatcher
)
177 watcher_map::iterator it
= fWatchers
.find(target
);
178 bool result
= (it
!= fWatchers
.end());
188 /*! \brief Sends a notification message to all watcher targets selected by a
191 If no filter is supplied the message is sent to all watchers. Otherwise
192 each watcher (and the notification message) is passed to its
193 WatcherFilter::Filter() method and the message is sent only to those
194 watchers for which \c true is returned.
196 If a sending a message to a watcher's target failed, because it became
197 invalid, the watcher is unregistered and deleted.
199 \param message The message to be sent to the watcher targets.
200 \param filter The filter selecting the watchers to which the message
201 is be sent. May be \c NULL.
204 WatchingService::NotifyWatchers(BMessage
*message
, WatcherFilter
*filter
)
208 // deliver the message
209 for (watcher_map::iterator it
= fWatchers
.begin();
210 it
!= fWatchers
.end();
212 Watcher
*watcher
= it
->second
;
213 // TODO: If a watcher is invalid, but the filter never selects it, it will
215 if (!filter
|| filter
->Filter(watcher
, message
)) {
216 status_t error
= watcher
->SendMessage(message
);
217 if (error
!= B_OK
&& !watcher
->Target().IsValid())
218 staleWatchers
.AddItem(watcher
);
221 // remove the stale watchers
223 Watcher
*watcher
= (Watcher
*)staleWatchers
.ItemAt(i
);
225 RemoveWatcher(watcher
, true);