GizmoDistribution

evaluators.cpp

Evaluators example application.

This example illustrates how to use GizmoDistribution evaluators. A client add, update and remove objects and an evaluator accept or reject the clients actions.

The source code can also be found in the examples directory of the GizmoDistribution installation.

.

00001 //*****************************************************************************
00002 // File         : evaluators.cpp
00003 // Module       : evaluators
00004 // Description  : evaluators (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  040421  Created file    
00019 //
00020 //*****************************************************************************
00021 
00022 // Includes
00023 #include <stdio.h>
00024 #include "gzBaseLibrary.h"
00025 #include "gzDistLibrary.h"
00026 
00027 
00028 // Common constants
00029 static const gzString EXAMPLE_SESSION = "example";
00030 
00031 
00032 //-----------------------------------------------------------------------------
00033 
00034 // A client
00035 class Client : public gzDistClientInterface
00036 {
00037     public:
00038 
00039         Client();
00040 
00041         virtual ~Client();
00042 
00043 
00044     private:
00045 
00046         // callbacks
00047 
00048         gzVoid onClientTick();
00049 
00050         gzVoid onNewObject(gzDistObject* object, gzDistSession* session);
00051 
00052         gzVoid onRemoveObject(gzDistObject* object, gzDistSession* session);
00053 
00054         gzVoid onSystemShutdown();
00055 
00056 
00057     private:
00058 
00059         // members
00060 
00061         gzDistSessionPtr m_session;
00062 
00063         gzDistObjectPtr m_object;
00064 
00065         gzULong m_tickCount;
00066 
00067 
00068         enum actions
00069         {
00070             ADD     = 0,
00071             REMOVE  = 9
00072         };
00073 
00074 };
00075 
00076 
00077 Client::Client()
00078  :  gzDistClientInterface("example_client"),
00079     m_tickCount(0)
00080 {
00081 
00082     // initialize client
00083     initialize(2);
00084 
00085     // create an join session
00086     m_session = getSession(EXAMPLE_SESSION, TRUE);
00087     joinSession(m_session);
00088 
00089     // subscribe for objects
00090     subscribeObjects(gzDistObject::getClassType(), m_session);
00091 
00092 }
00093 
00094 
00095 Client::~Client()
00096 {
00097     // unsubscribe objects
00098     unsubscribeObjects(gzDistObject::getClassType(), m_session);
00099 
00100     // resign the session
00101     resignSession(m_session);
00102 
00103     // uninitialize client
00104     uninitialize();
00105 }
00106 
00107 
00108 gzVoid Client::onNewObject(gzDistObject* object, gzDistSession* session)
00109 {
00110     m_object = object;
00111 }
00112 
00113 
00114 gzVoid Client::onRemoveObject(gzDistObject* object, gzDistSession* session)
00115 {
00116     m_object = NULL;
00117 }
00118 
00119 
00120 gzVoid Client::onClientTick()
00121 {
00122     if (!m_session)
00123     {
00124         return;
00125     }
00126 
00127     gzULong action = m_tickCount%10;
00128 
00129     switch (action)
00130     {
00131         case ADD:
00132         {
00133             GZMESSAGE(GZ_MESSAGE_NOTICE, "Adding object");
00134             if (addObject( new gzDistObject(gzString(m_tickCount)), m_session, GZ_DIST_SYNCHRONOUS))
00135             {
00136                 GZMESSAGE(GZ_MESSAGE_NOTICE, "Accepted");
00137             }
00138             else
00139             {
00140                 gzDistReportLastError(TRUE);
00141             }
00142 
00143         }
00144         break;
00145 
00146         case REMOVE:
00147         {
00148             GZMESSAGE(GZ_MESSAGE_NOTICE, "Removing object");
00149             if (removeObject(m_object))
00150             {
00151                 GZMESSAGE(GZ_MESSAGE_NOTICE, "Accepted");
00152             }
00153             else
00154             {
00155                 gzDistReportLastError(TRUE);
00156             }
00157         }
00158         break;
00159 
00160         default:
00161         {
00162             GZMESSAGE(GZ_MESSAGE_NOTICE, "Updating object");
00163             if (updateObject(gzString(action%5), gzString(m_tickCount), m_object))
00164             {
00165                 GZMESSAGE(GZ_MESSAGE_NOTICE, "Accepted");
00166             }
00167             else
00168             {
00169                 gzDistReportLastError(TRUE);
00170             }
00171         }
00172         break;
00173     }
00174     
00175     ++m_tickCount;
00176 }
00177 
00178 
00179 gzVoid Client::onSystemShutdown()
00180 {
00181     // in case of pre-mature shutdown
00182     m_object = NULL;
00183     m_session = NULL;   
00184 }
00185 
00186 
00187 //-----------------------------------------------------------------------------
00188 
00189 // An evaluator
00190 class Evaluator : public gzDistEvaluatorInterface
00191 {
00192     public:
00193 
00194         Evaluator();
00195 
00196         virtual ~Evaluator();
00197 
00198 
00199     private:
00200 
00201         // callbacks
00202 
00203         gzBool onNewObject(gzDistObject* object, gzDistSession* session);
00204 
00205         gzBool onRemoveObject(gzDistObject* object);
00206 
00207         gzBool onUpdateObject(gzDistTransaction* transaction, gzDistObject* object);
00208 
00209         gzBool onRemoveAttributes(gzDistTransaction* transaction, gzDistObject* object);
00210 
00211 };
00212 
00213 
00214 Evaluator::Evaluator()
00215 :   gzDistEvaluatorInterface(gzDistObject::getClassType())
00216 {
00217 }
00218 
00219 
00220 Evaluator::~Evaluator()
00221 {
00222 }
00223 
00224         
00225 gzBool Evaluator::onNewObject(gzDistObject* object, gzDistSession* session)
00226 {
00227     GZMESSAGE(GZ_MESSAGE_NOTICE, "Evaluating new object");
00228     
00229     return TRUE; // accept new object (return FALSE to reject)
00230 }
00231 
00232 
00233 gzBool Evaluator::onRemoveObject(gzDistObject* object)
00234 {
00235     GZMESSAGE(GZ_MESSAGE_NOTICE, "Evaluating remove object");
00236 
00237     return TRUE; // accept object remove (return FALSE to reject)
00238 }
00239 
00240 
00241 gzBool Evaluator::onUpdateObject(gzDistTransaction* transaction, gzDistObject* object)
00242 {
00243     GZMESSAGE(GZ_MESSAGE_NOTICE, "Evaluating update object");
00244 
00245     return TRUE; // accept object update (return FALSE to reject)
00246 }
00247 
00248 
00249 gzBool Evaluator::onRemoveAttributes(gzDistTransaction* transaction, gzDistObject* object)
00250 {
00251     GZMESSAGE(GZ_MESSAGE_NOTICE, "Evaluating remove attributes");
00252 
00253     return TRUE; // accept attribute remove (return FALSE to reject)
00254 }
00255 
00256         
00257 //-----------------------------------------------------------------------------
00258 
00259 // Application entry point
00260 int main(int argc, char* argv[])
00261 {
00262     // setup default message receiver
00263     gzMessage::setMessageLevel(GZ_MESSAGE_NOTICE);
00264 
00265     // create manager and start
00266     gzDistManagerPtr manager = gzDistManager::getManager(TRUE);
00267     manager->start();
00268 
00269     GZMESSAGE(GZ_MESSAGE_NOTICE, "Hit return to quit!\n");  
00270     
00271     // create session
00272     gzDistSessionPtr session = manager->getSession(EXAMPLE_SESSION, TRUE, FALSE, GZ_DIST_SERVER_PRIO_NORMAL);
00273 
00274     // add evaluator
00275     gzDistEvaluatorInterfacePtr evaluator = new Evaluator;
00276     session->registerEvaluator(evaluator);
00277     
00278     // create client
00279     Client* client = new Client;
00280 
00281 
00282     // wait for return
00283     getchar();
00284     
00285 
00286     // delete the client
00287     delete client;
00288 
00289     // unregister evaluator
00290     session->unregisterEvaluator(evaluator);
00291     evaluator = NULL;
00292 
00293     // release session
00294     session = NULL;
00295     
00296     // shut down and release manager
00297     manager->shutDown();
00298     manager = NULL;
00299 
00300     gzSleep(GZ_SLEEP_SECOND);
00301 
00302     return 0;
00303 
00304 }
00305 

Documentation for GizmoDistribution generated at Wed Feb 20 11:59:20 2008 by   Saab Training Systems AB, ¸ (c) 2003-and beyond