Main Page | Class Hierarchy | Class List | File List | Class Members

demoavatar.cxx

00001 
00002 //
00003 // $Id: demoavatar.cxx,v 1.11 2004/04/02 21:44:15 scharver Exp $
00004 //
00005 // Author: Chris Scharver
00006 // Email: scharver@evl.uic.edu
00007 // Copyright (c) 2003 Electronic Visualization Laboratory,
00008 //                    University of Illinois at Chicago
00009 //
00010 // Permission is hereby granted, free of charge, to any person
00011 // obtaining a copy of this software and associated documentation
00012 // files (the "Software"), to deal in the Software without
00013 // restriction, including without limitation the rights to use, copy,
00014 // modify, merge, publish, distribute, sublicense, and/or sell copies
00015 // of the Software, and to permit persons to whom the Software is
00016 // furnished to do so, subject to the following conditions:
00017 //
00018 // The above copyright notice and this permission notice shall be
00019 // included in all copies or substantial portions of the Software.
00020 //
00021 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00024 // NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00025 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00026 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00028 // DEALINGS IN THE SOFTWARE.
00029 //
00031 
00032 /*
00033 Simple non-graphic demo program to test CAVERN support template for avatars.
00034 Run the program by typing:
00035 
00036 demoavatar site lifetime
00037 
00038 where:
00039 
00040 site is the IP address of a remote IRB or another running demoavatar program.
00041 
00042 lifetime (in seconds) is the amount of time to run this program before quiting.
00043 By setting different times you can watch as avatars appear and disapper.
00044 
00045 */
00046 
00047 #include <QUANTA/QUANTAglobals.hxx>
00048 #include <QUANTA/QUANTAinit.hxx>
00049 #include <QUANTA/QUANTAmisc_debug.hxx>
00050 #include <QUANTA/QUANTAnet_datapack_c.hxx>
00051 
00052 #include <QAvatar.h>
00053 #include <QAvatarFactory.h>
00054 #include <QAvatarListener.h>
00055 #include <QAvatarManager.h>
00056 
00057 #include <signal.h>
00058 
00059 #ifdef USE_STL_NAMESPACE
00060 #include <iostream>
00061 using std::cerr;
00062 using std::cout;
00063 using std::endl;
00064 #else /* USE_STL_NAMESPACE */
00065 #include <iostream.h>
00066 #endif /* USE_STL_NAMESPACE */
00067 
00068 static const int HELLO_SIZE=256;
00069 static const int TRACKER_SIZE=QUANTAnet_datapack_c::sizeof_int()*2;
00070 static const int AUX_SIZE=256;
00071 
00072 class DemoFactory : public QAvatarFactory
00073 {
00074 public:
00075   virtual QAvatar* createAvatar(const long& newID)
00076   {
00077     // The new avatar MUST specify the data sizes fpr hello, aux, and
00078     // tracker data! Without these values, the data transfer will not
00079     // be correct. These data sizes are essential, and as constants
00080     // they may only be initialized when the new QAvatar instance is
00081     // created by the factory.
00082     return new QAvatar(newID, HELLO_SIZE, AUX_SIZE, TRACKER_SIZE);
00083   }
00084 };
00085 
00086 class DemoListener : public QAvatarListener
00087 {
00088 public:
00089   virtual ~DemoListener() {}
00090   virtual void avatarArrive(QAvatar* avatar)
00091   {
00092     printf("WELCOME!!! AVATAR %i ARRIVED: %s\n",
00093            avatar->getID(), avatar->getHelloData());
00094   }
00095   virtual void avatarAux(QAvatar* avatar)
00096   {
00097     printf("AVATAR %i AUX: %s\n", avatar->getID(), avatar->getAuxData());
00098   }
00099   virtual void avatarBye(QAvatar* avatar)
00100   {
00101     printf("GOODBYE!! AVATAR %i LEFT\n", avatar->getID());
00102   }
00103   virtual void avatarHello(QAvatar* avatar)
00104   {
00105     printf("AVATAR %i HELLO: %s\n",
00106            avatar->getID(), avatar->getHelloData());
00107   }
00108   virtual void avatarTracker(QAvatar* avatar)
00109   {
00110     QUANTAnet_datapack_c unpacker;
00111     int x, y;
00112     unpacker.initUnpack(avatar->getTrackerData(), TRACKER_SIZE);
00113     unpacker.unpackInt(&x);
00114     unpacker.unpackInt(&y);    
00115     printf("AVATAR %i TRACKER: %i %i\n", avatar->getID(), x, y);
00116   }
00117 };
00118 
00119 volatile bool keepRunning = true;
00120 
00121 void interruptHandler(int);
00122 
00123 int
00124 main(int argc, char** argv)
00125 {
00126   if (argc < 2) {
00127     cerr << "Usage: server" << endl;
00128     return 1;
00129   }
00130 
00131   short hailingPort = 7000;
00132   short trackerPort = 7001;
00133 
00134   // Assign a signal callback to catch Ctrl-C.
00135   signal(SIGINT, interruptHandler);
00136 
00137   QUANTAinit();
00138 
00139   DemoFactory* avatarFactory = new DemoFactory;
00140   QAvatarManager* avatarManager = new QAvatarManager(avatarFactory);
00141   QAvatar* selfAvatar = avatarManager->getSelfAvatar();
00142 
00143   // Create the listener object and attach it as an observer of the
00144   // manager. This allows the listener to receive notifications.
00145   DemoListener* avatarListener = new DemoListener;
00146   avatarManager->attach(avatarListener);
00147   avatarManager->setTrackerUpdateDelay(1.0);
00148 
00149   // Connect to the avatar server.
00150   if (!avatarManager->connect(argv[1], hailingPort, trackerPort)) {
00151     cerr << "Unable to initialize the avatar manager!" << endl;
00152     delete avatarManager;
00153     return 1;
00154   }
00155 
00156   // Pack hello data with data about your avatar and then send it.
00157   char* helloData = new char[HELLO_SIZE];
00158   strcpy(helloData, "THIS IS HELLO DATA");
00159   selfAvatar->setHelloData(helloData, strlen(helloData)+1);
00160   avatarManager->sendHello();
00161 
00162   char* trackerData = new char[TRACKER_SIZE];
00163   char* auxData = new char[AUX_SIZE];
00164 
00165   int loops;
00166   if (argc > 2) {
00167     loops = atoi(argv[2]);
00168   } else {
00169     loops = 1000;
00170   }
00171 
00172   QUANTAnet_datapack_c packer;
00173   for (int i = 0; (i < loops) && keepRunning; i++) {
00174 
00175     cerr << i << endl;
00176     // Pack tracker data with whatever data you want and then send it. Note
00177     // that it is your responsibility to pack all data before calling this
00178     // function and unpack when received. For this example, pack the current
00179     // loop count and the number of remaining loops.
00180     packer.initPack(trackerData, TRACKER_SIZE);
00181     packer.packInt(i);
00182     packer.packInt(loops-i);
00183     // Update the self avatar's tracker data buffer.
00184     selfAvatar->setTrackerData(trackerData, TRACKER_SIZE);
00185     // Send the tracker data over the network to other users.
00186     avatarManager->sendTracker();
00187 
00188     // Every fourth loop iteration, send aux data.
00189     if ((i % 4) == 0) {
00190       sprintf(auxData, "THIS IS AUX DATA %d", i);
00191       selfAvatar->setAuxData(auxData, strlen(auxData)+1);
00192       avatarManager->sendAux();
00193     }
00194     avatarManager->process();
00195     QUANTAsleep(5);
00196   }
00197 
00198   // Shut down when the loop is finished, and make sure to send a bye
00199   // message so that other avatars know this client has left.
00200   cerr << "\nShutting down..." << endl;
00201   avatarManager->sendBye();
00202 
00203   delete avatarManager;
00204   delete avatarFactory;
00205   delete avatarListener;
00206 
00207   delete[] helloData;
00208   delete[] trackerData;
00209   delete[] auxData;
00210 
00211   return 0;
00212 }
00213 
00214 void
00215 interruptHandler(int arg)
00216 {
00217   cout << "Ctrl-C caught..." << endl;
00218   keepRunning = false;
00219 }

Generated on Tue Apr 13 16:42:25 2004 for QAvatar by doxygen 1.3.6