Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __JAUS_CORE_EVENTS_EVENTS__H
00041 #define __JAUS_CORE_EVENTS_EVENTS__H
00042
00043 #include "jaus/core/service.h"
00044 #include "jaus/core/transport/transport.h"
00045 #include <cxutils/timer.h>
00046 #include <map>
00047
00048 namespace JAUS
00049 {
00063 class JAUS_CORE_DLL Events : public Service
00064 {
00065 public:
00066 const static std::string Name;
00067
00068 enum Type
00069 {
00070 Periodic = 0,
00071 EveryChange
00072 };
00080 class JAUS_CORE_DLL Subscription
00081 {
00082 friend class Events;
00083 public:
00084 typedef std::map<UInt, Subscription> Map;
00085 typedef std::vector<Subscription> List;
00086 Subscription();
00087 Subscription(const Subscription& data);
00088 ~Subscription();
00089 Subscription& operator=(const Subscription& data);
00090 Byte mID;
00091 Byte mSequenceNumber;
00092 double mPeriodicRate;
00093 Message* mpQueryMessage;
00094 Events::Type mType;
00095 Time::Stamp mUpdateTimeMs;
00096 Address mProducer;
00097 Address::Set mClients;
00098 private:
00099 double mTriggerTimeSeconds;
00100 Service* mpEventService;
00101 };
00114 class JAUS_CORE_DLL Child : public Service
00115 {
00116 public:
00117 Child(const ID& serviceIdentifier, const ID& parentServiceIdentifier);
00118 ~Child();
00119
00120 void SignalEvent(const UShort reportMessageCode);
00121
00122 void SignalEvent(const Subscription& info);
00123
00124 virtual bool GenerateEvent(const Events::Subscription& info) const = 0;
00125
00126 virtual bool IsEventSupported(const Events::Type type,
00127 const double requestedPeriodicRate,
00128 const Message* queryMessage,
00129 double& confirmedPeriodicRate,
00130 std::string& errorMessage) const = 0;
00131
00132 void SendEvent(const Events::Subscription& info,
00133 const Message* payload) const;
00134
00135 Events* EventsService();
00136
00137 const Events* EventsService() const;
00138 };
00146 class JAUS_CORE_DLL Callback : public Transport::Callback
00147 {
00148 public:
00149 Callback() {}
00150 virtual ~Callback() {}
00161 virtual void ProcessEvent(const Events::Type type,
00162 const Byte eventID,
00163 const Byte sequenceNumber,
00164 const Message* message) {}
00165 };
00166 Events();
00167 ~Events();
00168
00169 virtual void Shutdown();
00170
00171 bool RequestEveryChangeEvent(const Address& provider,
00172 const Message* query,
00173 const unsigned int waitTimeMs = Service::DefaultWaitMs);
00174
00175 bool RequestPeriodicEvent(const Address& provider,
00176 const Message* query,
00177 const double desiredPeriodicRate = 1.0,
00178 const double minimumPeriodicRate = 0.9,
00179 const unsigned int waitTimeMs = DefaultWaitMs);
00180
00181 void SignalEvent(const UShort reportMessageCode, const bool changeOnly = true);
00182
00183 void SignalEvent(const Subscription& info, const bool changeOnly = true);
00184
00185 bool CancelSubscription(const Address& id,
00186 const UShort reportMessageCode,
00187 const unsigned int waitTimeMs = Service::DefaultWaitMs);
00188
00189 bool CancelSubscription(const Address& id,
00190 const UShort reportMessageCode,
00191 Byte eventID,
00192 const unsigned int waitTimeMs);
00193
00194 bool HaveSubscription(const UShort reportMessageCode,
00195 const Address& id,
00196 const bool verifyWithQuery = false,
00197 const unsigned int waitTimeMs = Service::DefaultWaitMs) const;
00198
00199 bool HaveSubscribers(const UShort reportMessageCode) const;
00200
00201 virtual bool IsDiscoverable() const { return true; }
00202
00203 virtual void Receive(const Message* message);
00204
00205 virtual Message* CreateMessage(const UShort messageCode) const;
00206
00207 virtual void CheckServiceStatus(const unsigned int timeSinceLastCheckMs);
00208
00209 void SetEventSubscriptionCheckThreshold(const Time::Stamp timeMs = 5000) { mCheckEventsTimeMs = timeMs; }
00210
00211 Subscription::List GetSubscriptions(const Address& source = Address(),
00212 const UShort reportType = 0) const;
00213
00214 Subscription::List GetSubscriptions(const UShort reportType) const;
00215
00216 Subscription::List GetProducedEvents(const UShort reportType) const;
00217
00218 void RegisterCallback(Events::Callback* callback);
00219
00220 virtual void PrintStatus() const;
00221 private:
00222 bool CancelSubscription(Subscription& sub, const unsigned int waitTimeMs);
00223 static void PeriodicEvent(void* args);
00224 CxUtils::Timer mPeriodicTimer;
00225 Mutex mEventsMutex;
00226 Subscription::Map mEvents;
00227 Subscription::List mSubscriptions;
00228 std::set<Byte> mRequestSet;
00229 Mutex mRequestMutex;
00230 Time::Stamp mCheckEventDelayMs;
00231 Time::Stamp mCheckEventsTimeMs;
00232 volatile bool mShutdownFlag;
00233 Mutex mEventCallbackMutex;
00234 Callback::Set mEventCallbacks;
00235 };
00236 }
00237
00238 #endif
00239