
"Hello World" for GizmoDistribution objects.
The source code can also be found in the examples directory of the GizmoDistribution installation.
.
00001 //***************************************************************************** 00002 // File : helloobject.cpp 00003 // Module : helloobject 00004 // Description : helloobject (example application) 00005 // Author : Anders Sandblad 00006 // Product : GizmoDistribution 00007 // 00008 // Copyright © 2004- Saab Training Systems AB, Sweden 00009 // 00010 // NOTE: GizmoDistribution is a toolkit used for implementing distributed 00011 // objects and events in C++ 00012 // 00013 // 00014 // Revision History... 00015 // 00016 // Who Date Description 00017 // 00018 // XAA 040416 Created file 00019 // 00020 //***************************************************************************** 00021 00022 // Includes 00023 #include "gzBaseLibrary.h" 00024 #include "gzDistLibrary.h" 00025 00026 00027 // Common constants 00028 static const gzString HELLO_SESSION = "hello"; 00029 static const gzString HELLO_OBJECT = "hello"; 00030 static const gzString HELLO_ATTRIBUTE = "hello"; 00031 00032 00033 //----------------------------------------------------------------------------- 00034 00035 // A client that publish objects 00036 class ObjectPublisher : public gzDistClientInterface 00037 { 00038 public: 00039 00040 ObjectPublisher(); 00041 00042 virtual ~ObjectPublisher(); 00043 00044 00045 private: 00046 00047 // callbacks 00048 00049 gzVoid onClientTick(); 00050 00051 gzVoid onNewObject(gzDistObject* object, gzDistSession* session); 00052 00053 gzVoid onRemoveObject(gzDistObject* object, gzDistSession* session); 00054 00055 gzVoid onSystemShutdown(); 00056 00057 00058 private: 00059 00060 // members 00061 00062 gzDistSessionPtr m_session; 00063 00064 gzDistObjectPtr m_object; 00065 00066 gzULong m_counter; 00067 00068 }; 00069 00070 00071 ObjectPublisher::ObjectPublisher() : gzDistClientInterface("publisher"), m_counter(0) 00072 { 00073 // initialize client 00074 initialize(1); 00075 00076 // get the session (create it if it does not exist) 00077 m_session = getSession(HELLO_SESSION, TRUE); 00078 00079 // join the session 00080 joinSession(m_session); 00081 00082 // subscribe for objects 00083 subscribeObjects(gzDistObject::getClassType(), m_session); 00084 00085 // create an object 00086 gzDistObjectPtr object = new gzDistObject(HELLO_OBJECT); 00087 object->setAttributeValue(HELLO_ATTRIBUTE, gzString("Hello")); 00088 00089 // add the object 00090 // this object instance is not necessarily identical to 00091 // the object actuallty added to the session 00092 // the added instance is captured in the onNewObject() callback 00093 addObject(object, m_session); 00094 00095 } 00096 00097 00098 ObjectPublisher::~ObjectPublisher() 00099 { 00100 // remove the object 00101 removeObject(m_object); 00102 00103 // unsubscribe objects 00104 unsubscribeObjects(gzDistObject::getClassType(), m_session); 00105 00106 // resign the session 00107 resignSession(m_session); 00108 00109 // uninitialize client 00110 uninitialize(); 00111 } 00112 00113 00114 gzVoid ObjectPublisher::onNewObject(gzDistObject* object, gzDistSession* session) 00115 { 00116 if (!m_object) 00117 { 00118 m_object = object; 00119 } 00120 } 00121 00122 00123 gzVoid ObjectPublisher::onRemoveObject(gzDistObject* object, gzDistSession* session) 00124 { 00125 if (object == m_object) 00126 { 00127 m_object = NULL; 00128 } 00129 } 00130 00131 00132 gzVoid ObjectPublisher::onClientTick() 00133 { 00134 if (m_object) 00135 { 00136 updateObject(HELLO_ATTRIBUTE, gzString("Hello ")+gzString(m_counter), m_object); 00137 ++m_counter; 00138 } 00139 } 00140 00141 00142 gzVoid ObjectPublisher::onSystemShutdown() 00143 { 00144 // in case of pre-mature shutdown 00145 m_object = NULL; 00146 m_session = NULL; 00147 } 00148 00149 00150 //----------------------------------------------------------------------------- 00151 00152 // A client that subscribe for objects and attributes 00153 class ObjectSubscriber : public gzDistClientInterface 00154 { 00155 public: 00156 00157 ObjectSubscriber(); 00158 00159 virtual ~ObjectSubscriber(); 00160 00161 00162 private: 00163 00164 // callbacks 00165 00166 gzVoid onNewObject(gzDistObject* object, gzDistSession* session); 00167 00168 gzVoid onRemoveObject(gzDistObject* object, gzDistSession* session); 00169 00170 gzVoid onUpdateAttributes(const gzDistNotificationSet& attributes, gzDistObject* object, gzDistSession* session); 00171 00172 gzVoid onSystemShutdown(); 00173 00174 00175 private: 00176 00177 // members 00178 00179 gzDistSessionPtr m_session; 00180 00181 }; 00182 00183 00184 ObjectSubscriber::ObjectSubscriber() : gzDistClientInterface("subscriber") 00185 { 00186 // initialize client 00187 initialize(); 00188 00189 // get the session (create it if it does not exist) 00190 m_session = getSession(HELLO_SESSION, TRUE); 00191 00192 // join the session 00193 joinSession(m_session); 00194 00195 // subscribe for objects 00196 subscribeObjects(gzDistObject::getClassType(), m_session); 00197 } 00198 00199 00200 ObjectSubscriber::~ObjectSubscriber() 00201 { 00202 // Unsubscribe objects. Not really needed when the uninitialize() does it for us. 00203 unsubscribeObjects(gzDistObject::getClassType(), m_session); 00204 00205 // Resign the session. Not really needed when the uninitialize() does it for us. 00206 resignSession(m_session); 00207 00208 // Uninitialize client 00209 uninitialize(); 00210 } 00211 00212 00213 gzVoid ObjectSubscriber::onNewObject(gzDistObject* object, gzDistSession* session) 00214 { 00215 GZMESSAGE(GZ_MESSAGE_NOTICE, "New object: %s", (const char*)object->getName()); 00216 subscribeAttributeValue(HELLO_ATTRIBUTE, object); 00217 } 00218 00219 00220 gzVoid ObjectSubscriber::onRemoveObject(gzDistObject* object, gzDistSession* session) 00221 { 00222 GZMESSAGE(GZ_MESSAGE_NOTICE, "Removed object: %s", (const char*)object->getName()); 00223 } 00224 00225 00226 gzVoid ObjectSubscriber::onUpdateAttributes(const gzDistNotificationSet& attributes, gzDistObject* object, gzDistSession* session) 00227 { 00228 gzDistNotificationSetIterator iter(attributes); 00229 00230 gzDistNotificationData* data = NULL; 00231 00232 while (data = iter()) 00233 { 00234 GZMESSAGE(GZ_MESSAGE_NOTICE, (const char*)data->getAttributeValue().getString()); 00235 } 00236 } 00237 00238 00239 gzVoid ObjectSubscriber::onSystemShutdown() 00240 { 00241 // in case of pre-mature shutdown 00242 m_session = NULL; 00243 } 00244 00245 00246 //----------------------------------------------------------------------------- 00247 00248 // Application entry point 00249 int main(int argc, char* argv[]) 00250 { 00251 // setup default message receiver 00252 gzMessage::setMessageLevel(GZ_MESSAGE_NOTICE); 00253 00254 // create manager and start 00255 gzDistManagerPtr manager = gzDistManager::getManager(TRUE); 00256 manager->start(); 00257 00258 // create subscriber client 00259 ObjectSubscriber* subscriber = new ObjectSubscriber; 00260 00261 gzSleep(GZ_SLEEP_SECOND); 00262 00263 // create publisher client 00264 ObjectPublisher* publisher = new ObjectPublisher; 00265 00266 00267 // run a while 00268 gzSleep(10 * GZ_SLEEP_SECOND); 00269 00270 00271 // delete the publisher 00272 delete publisher; 00273 00274 gzSleep(GZ_SLEEP_SECOND); 00275 00276 // delete the subscriber 00277 delete subscriber; 00278 00279 // shut down and release manager 00280 manager->shutDown(); 00281 manager = NULL; 00282 00283 gzSleep(GZ_SLEEP_SECOND); 00284 00285 return 0; 00286 00287 } 00288