
"Hello World" for GizmoDistribution events.
The source code can also be found in the examples directory of the GizmoDistribution installation.
.
00001 //***************************************************************************** 00002 // File : helloevent.cpp 00003 // Module : helloevent 00004 // Description : helloevent (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_ATTRIBUTE = "hello"; 00030 00031 00032 //------------------------------ EventSender ---------------------------------- 00033 00034 // A client that send events 00035 class EventSender : public gzDistClientInterface 00036 { 00037 public: 00038 00039 EventSender(); 00040 00041 virtual ~EventSender(); 00042 00043 00044 private: 00045 00046 // thread ticker callback 00047 00048 gzVoid onClientTick(); 00049 00050 private: 00051 00052 // members 00053 00054 gzDistSessionPtr m_session; 00055 00056 gzULong m_counter; 00057 00058 }; 00059 00060 00061 EventSender::EventSender() : gzDistClientInterface("sender"), m_counter(0) 00062 { 00063 // Initialize client with tick interval approx 1 sec 00064 initialize(1); 00065 00066 // Get a local session (create it if it does not exist) 00067 m_session = getSession(HELLO_SESSION, TRUE); 00068 00069 // Join the session 00070 joinSession(m_session); 00071 00072 } 00073 00074 00075 EventSender::~EventSender() 00076 { 00077 // Uninitialize client 00078 uninitialize(); 00079 } 00080 00081 00082 gzVoid EventSender::onClientTick() 00083 { 00084 // Create an event 00085 gzDistEventPtr event = new gzDistEvent; 00086 00087 // Add an attribute 00088 event->setAttributeValue(HELLO_ATTRIBUTE, gzString("Hello ")+gzString(m_counter)); 00089 00090 // Send the event 00091 sendEvent(event, m_session); 00092 00093 ++m_counter; 00094 00095 } 00096 00097 //------------------------------ EventReceiver ---------------------------------- 00098 00099 // A client that receive events 00100 class EventReceiver : public gzDistClientInterface 00101 { 00102 public: 00103 00104 EventReceiver(); 00105 00106 virtual ~EventReceiver(); 00107 00108 00109 private: 00110 00111 // notification callbacks 00112 00113 gzVoid onEvent(gzDistEvent* event); 00114 00115 00116 private: 00117 00118 // members 00119 00120 gzDistSessionPtr m_session; 00121 00122 }; 00123 00124 00125 EventReceiver::EventReceiver() : gzDistClientInterface("receiver") 00126 { 00127 // Initialize client (no ticker) 00128 initialize(); 00129 00130 // Get a local session (create it if it does not exist) 00131 m_session = getSession(HELLO_SESSION, TRUE); 00132 00133 // Join the session 00134 joinSession(m_session); 00135 00136 // Subscribe for events 00137 subscribeEvents(gzDistEvent::getClassType(), m_session); 00138 } 00139 00140 00141 EventReceiver::~EventReceiver() 00142 { 00143 // Uninitialize client 00144 uninitialize(); 00145 } 00146 00147 00148 gzVoid EventReceiver::onEvent(gzDistEvent* event) 00149 { 00150 gzString attributeValue = event->getAttributeString(HELLO_ATTRIBUTE); 00151 00152 GZMESSAGE(GZ_MESSAGE_NOTICE, attributeValue); 00153 } 00154 00155 00156 //--------------------------------- main -------------------------------------- 00157 00158 // Application entry point 00159 int main(int argc, char* argv[]) 00160 { 00161 // Setup default message receiver 00162 gzMessage::setMessageLevel(GZ_MESSAGE_NOTICE); 00163 00164 // Create manager and start 00165 gzDistManagerPtr manager = gzDistManager::getManager(TRUE); 00166 manager->start(); 00167 00168 // Create receiver client 00169 EventReceiver* receiver = new EventReceiver; 00170 00171 // Create sender client 00172 EventSender* sender = new EventSender; 00173 00174 00175 // Run a while 00176 gzSleep(10 * GZ_SLEEP_SECOND); 00177 00178 00179 // Delete the sender 00180 delete sender; 00181 00182 // Delete the receiver 00183 delete receiver; 00184 00185 // Shut down and release manager 00186 manager->shutDown(); 00187 manager = NULL; 00188 00189 gzSleep(GZ_SLEEP_SECOND); 00190 00191 return 0; 00192 00193 } 00194