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 00041 #ifndef __JAUS_CORE_SERVICE__H 00042 #define __JAUS_CORE_SERVICE__H 00043 00044 #include "jaus/core/message.h" 00045 #include <string> 00046 #include <set> 00047 #include <map> 00048 00049 namespace JAUS 00050 { 00051 class Transport; 00052 class Component; 00053 00061 class JAUS_CORE_DLL Service 00062 { 00063 friend class Transport; 00064 public: 00076 class JAUS_CORE_DLL ID 00077 { 00078 public: 00079 typedef std::vector<ID> List; 00080 typedef std::set<ID> Set; 00081 ID(const std::string& name = "", const double verion = 1.0); 00082 ID(const ID& id); 00083 ~ID(); 00084 int Write(Packet& packet) const; 00085 int Read(const Packet& packet); 00086 void Clear(); 00087 std::string ToString(const bool nameOnly = true, const bool trim = true) const; 00088 ID& operator=(const ID& id); 00089 bool operator<(const ID& id) const { return mName < id.mName; } 00090 std::string mName; 00091 double mVersion; 00092 }; 00093 static const int NoBroadcast = 0; // No broadcasting over IP (default) 00094 static const int LocalBroadcast = 1; // Use local broadcast transport layer options for sending. 00095 static const int GlobalBroadcast = 2; // Use global broadcast transport layer options for sending. 00096 static const unsigned int DefaultWaitMs = 100; 00097 typedef std::map<std::string, Service*> Map; 00098 typedef Mutex::ScopedPtr<Service> Ptr; 00099 // Constructor, initializes ID, and any parent service we inherit from. 00100 Service(const ID& serviceIdentifier, const ID& parentServiceIdentifier); 00101 // Destructor. 00102 virtual ~Service(); 00103 // Called on component initialization. 00104 virtual void Initialize() {} 00105 // Shutsdown the Service. 00106 virtual void Shutdown() {} 00107 // Returns true if components are allowed to discover this serivice (used bye Discovery). 00108 virtual bool IsDiscoverable() const = 0; 00109 // Load settings from a file. 00110 virtual bool LoadSettings(const std::string& filename) { return true; } 00111 // Sends a message. 00112 virtual bool Send(const Message* message, const int broadcastFlags = NoBroadcast) const; 00113 // Sends a message, then waits for a response. 00114 virtual bool Send(const Message* message, 00115 Message* response, 00116 const unsigned int waitTimeMs = DefaultWaitMs) const; 00117 // Sends a message and waits for one of multiple responses. 00118 virtual bool Send(const Message* message, 00119 Message::List& possibleResponses, 00120 const unsigned int waitTimeMs = Service::DefaultWaitMs) const; 00121 // Method called whenever a message is received/given to the Service for processing. 00122 virtual void Receive(const Message* message); 00123 // Create a message based on the message code. 00124 virtual Message* CreateMessage(const UShort messageCode) const = 0; 00125 // Gets the Service ID information. 00126 inline ID GetServiceID() const { return mServiceID; } 00127 // Sets the compondent ID. 00128 bool SetComponentID(const Address& id); 00129 // Gets the component ID. 00130 inline Address GetComponentID() const { return mComponentID; } 00131 // Checks if this Service inherits from another. 00132 bool InheritsFrom(const Service::ID& id) const; 00133 // Adds a child service connection if it inherits from this Service. 00134 bool AddChildService(Service* childService); 00135 // Sets the parent service (if this Service inherits from it). 00136 bool SetParentService(Service* parentService); 00137 // Gets the parent Service. 00138 Service* GetParentService(); 00139 // Gets the parent Service. 00140 const Service* GetParentService() const; 00141 // Gets a pointer to a child Service. 00142 Service* GetChildService(const Service::ID& id = Service::ID()); 00143 // Gets a pointer to a child Service. 00144 const Service* GetChildService(const Service::ID& id = Service::ID()) const; 00145 // Gets all the service information for this service and its children. 00146 Service::ID::Set GetServices() const; 00147 // Turn on Debug Messages. 00148 virtual void EnableDebugMessages(const bool on = true) { mDebugMessagesFlag = on; } 00149 // Method called periodically by Component or other Services, can be used for periodic udpdates. 00150 virtual void CheckServiceStatus(const unsigned int timeSinceLastCheckMs) { } 00151 // Toggles a service on or off. 00152 void EnableService(const bool enable) { mServiceEnabledFlag = enable; } 00153 // Turns a service on or off. 00154 bool IsEnabled() const { return mServiceEnabledFlag; } 00155 // Shutsdown all children of this Service first, followed by itself. 00156 virtual void RecursiveShutdown(); 00157 // Sets a pointer to the component the service (and children) belong to. 00158 void SetComponent(Component* component); 00159 // Gets a pointer to the component the service belongs to. 00160 inline Component* GetComponent() { return mpComponent; } 00161 // Gets a pointer to the component the service belongs to. 00162 inline const Component* GetComponent() const { return mpComponent; } 00163 // Prints information about the service. 00164 virtual void PrintStatus() const {} 00165 // Signal to services they must shutdown. 00166 void SignalServiceToShutdown(const bool enable = true) { mShutdownServiceFlag = enable; } 00167 // Get the service shutdown flag. 00168 bool IsServiceShuttingDown() const { return mShutdownServiceFlag; } 00169 protected: 00170 // Passes unsuported message to inheriting Services. 00171 void PushMessageToChildren(const Message* message); 00172 //Map* GetChildServices() { return &mJausChildServices; } 00173 Map GetChildServices(); 00174 const Map GetChildServices() const; 00175 Transport* GetTransportService() { return mpTransportService; } 00176 const Transport* GetTransportService() const { return mpTransportService; } 00177 static Mutex mDebugMessagesMutex; 00178 volatile bool mDebugMessagesFlag; 00179 Address mComponentID; 00180 Component* mpComponent; 00181 volatile bool mShutdownServiceFlag; 00182 private: 00183 volatile bool mServiceEnabledFlag; 00184 Mutex mJausServiceMutex; 00185 Service::ID mServiceID; 00186 Service::ID mParentServiceID; 00187 Map mJausChildServices; 00188 Service* mpJausParentService; 00189 Transport* mpTransportService; 00190 }; 00191 } 00192 00193 #endif 00194 /* End of File */