Beaming Scene Service  2.0
RakNet wrapper for managing data communications between multiple Beaming clients
netServer/server.cpp
Go to the documentation of this file.
00001 //Server Application - Raknet link and logfile recording
00002 
00003 #include "ClientServer.h"
00004 #ifdef WIN32
00005 #include <windows.h>
00006 #include <stdio.h>
00007 #include <tchar.h>
00008 #endif
00009 
00010 int main(int argc, char *argv[])
00011 {
00012         char ch;
00013         SocketDescriptor sd;
00014         //char ip[128];
00015         static int SERVER_PORT=12050;
00016 
00017         // ReplicaManager3 requires NetworkIDManager to lookup pointers from numbers.
00018         NetworkIDManager networkIdManager;
00019         // Each application has one instance of RakPeerInterface
00020         RakPeerInterface *rakPeer;
00021         // The system that performs most of our functionality for this server
00022         ReplicaManager3Beaming replicaManager;
00023 
00024         printf("NetServer handles objects creation and destruction and automatic serialization of data members.\n");
00025         ch='s'; 
00026 
00027         rakPeer = RakNetworkFactory::GetRakPeerInterface();
00028         topology=SERVER;
00029         if (argc > 1)
00030                 SERVER_PORT = atoi(argv[1]);
00031         else
00032                 printf("To specify an alternative port (default=12050), run: server port_no\n");
00033         sd.port=SERVER_PORT;
00034         printf("press key 'l' - Starts logging.\n");
00035 
00036         // ObjectMemberRPC, AutoRPC for objects, and ReplicaManager3 require that you call SetNetworkIDManager()
00037         rakPeer->SetNetworkIDManager(&networkIdManager);
00038         // The network ID authority is the system that creates the common numerical identifier used to lookup pointers.
00039         // For client/server this is the server
00040         // For peer to peer this would be true on every system, and NETWORK_ID_SUPPORTS_PEER_TO_PEER should be defined in RakNetDefines.h
00041         networkIdManager.SetIsNetworkIDAuthority(true);
00042         // Start RakNet, up to 32 connections if the server
00043         rakPeer->Startup(32,10,&sd,1);
00044         rakPeer->AttachPlugin(&replicaManager);
00045         rakPeer->SetMaximumIncomingConnections(32);
00046 
00047         printf("Started server on port %i\n", SERVER_PORT);
00048         printf("Server GUID is %s \n",rakPeer->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).ToString());
00049 
00050                 //printf("\n");
00051 
00052 #ifdef WIN32    
00053         STARTUPINFO si;                                 //startup handle for audio process
00054         PROCESS_INFORMATION pi;                 //process handle for audio process
00055 #endif
00056 
00057         // Enter infinite loop to run the system
00058         Packet *packet;
00059         bool quit=false;
00060         //std::map<std::string, std::vector<node_info*> >::iterator it;
00061         while (!quit)
00062         {
00063                 for (packet = rakPeer->Receive(); packet; rakPeer->DeallocatePacket(packet), packet = rakPeer->Receive())
00064                 {
00065                         switch (packet->data[0])
00066                         {
00067                         case ID_CONNECTION_ATTEMPT_FAILED:
00068                                 printf("ID_CONNECTION_ATTEMPT_FAILED\n");
00069                                 quit=true;
00070                                 break;
00071                         case ID_NO_FREE_INCOMING_CONNECTIONS:
00072                                 printf("ID_NO_FREE_INCOMING_CONNECTIONS\n");
00073                                 quit=true;
00074                                 break;
00075                         case ID_CONNECTION_REQUEST_ACCEPTED:
00076                                 printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
00077                                 break;
00078                         case ID_NEW_INCOMING_CONNECTION:
00079                                 printf("ID_NEW_INCOMING_CONNECTION from %s, guid %s\n", packet->systemAddress.ToString(),packet->guid.ToString());
00080                                 /*it = nodes_map.find(packet->guid.ToString());
00081                                 if (it != nodes_map.end()) //if id exists in node database, clear out
00082                                         nodes_map[packet->guid.ToString()].clear();
00083                                 //avatar_joint_replicas.clear();*/
00084                                 break;
00085                         case ID_DISCONNECTION_NOTIFICATION:
00086                                 printf("ID_DISCONNECTION_NOTIFICATION\n");
00087                                 break;
00088                         case ID_CONNECTION_LOST:
00089                                 printf("ID_CONNECTION_LOST\n");
00090                                 break;
00091                         }
00092                 }
00093 
00094                 if (kbhit())
00095                 {
00096                         ch=getch();
00097                         if (ch=='q' || ch=='Q')
00098                         {
00099                                 printf("Quitting.\n");
00100                                 quit=true;
00101                         }
00102                         //for debugging
00103                         if (ch=='n' || ch=='N')
00104                         {
00105                                 for ( std::map<std::string, std::vector<node_info*> >::iterator cIter = nodes_map.begin(); cIter!=nodes_map.end(); cIter++ )
00106                                         printf("%d, ",cIter->second.size());
00107                                 printf("\n");
00108                         }
00109                         if (ch=='l' || ch=='L')
00110                         {
00111                         #ifdef WIN32    
00112                                 logging=!logging;
00113                                 if (logging==true)
00114                                 {
00115                                         printf("Logging Enabled.\n");
00116                                         
00117                                         int logstartTime = (int) RakNet::GetTime();             //get time for log name
00118                                                                                 
00119                                         char commandline[256];                                                  //set command line for audio record
00120                                         sprintf(commandline, "Harddisk.exe -preset beaming.hdp -filter beaming.hfs -output BeamingLog_%i.ogg -minimize -record",logstartTime);
00121                                         
00122                                         ZeroMemory( &si, sizeof(si) );          //prepare for audio recording process
00123                                         si.cb = sizeof(si);                                     //..
00124                                         ZeroMemory( &pi, sizeof(pi) );          //..
00125 
00126                                         if( !CreateProcess( NULL,               // No module name (use command line)
00127                                                 commandline,                                    // Command line for audio client exe
00128                                                 NULL,                                           // Process handle not inheritable
00129                                                 NULL,                                           // Thread handle not inheritable
00130                                                 FALSE,                                          // Set handle inheritance to FALSE
00131                                                 0,                                              // No creation flags
00132                                                 NULL,                                           // Use parent's environment block
00133                                                 NULL,                                           // Use parent's starting directory 
00134                                                 &si,                                            // Pointer to STARTUPINFO structure
00135                                                 &pi )                                           // Pointer to PROCESS_INFORMATION structure
00136                                                 ) 
00137                                         {                                                                       //warn on fail
00138                                                 printf("Create Audio Process failed (%d).\n", GetLastError() ); 
00139                                         }
00140                                         
00141                                         rakPeer->AttachPlugin(&logfileHandler);
00142                                         logfileHandler.StartLog("BeamingLog", logstartTime); //to test -  standard output should be automatically written
00143                                         char starttime[128];
00144                                         char startstr[128];
00145                                         logfileHandler.GetLocalTime(starttime);
00146                                         sprintf(startstr,"S;%s",starttime);
00147                                         logfileHandler.WriteLog(startstr);
00148                                         //logfileHandler.WriteLog("Wole is testing"); //writes additional lines
00149                                         //logfileHandler.WriteMiscellaneous("audiotime","234678"); //should enable writing additional information - not on the same line?
00150         
00151                                         char localtime[128];
00152                                         char logstr[1024];
00153                                         logfileHandler.GetLocalTime(localtime);
00154                                         RakNetTimeMS time = RakNet::GetTime();
00155                                         //NOTE: This is out of date and doesn't work anymore. Need to update, as it only logged avatars. 
00156                                         for ( std::map<RakNetGUID, std::vector<BeamingAvatarJointReplica*> >::iterator cIter = avatar_joint_replicas.begin(); cIter!=avatar_joint_replicas.end(); cIter++ )
00157                                         {
00158                                                 for (int i=0; i<=cIter->second.size();i++)
00159                                                 {
00160                                                         sprintf(logstr,"C;%"PRINTF_64_BIT_MODIFIER"u;%s;%s;%i;%i",(unsigned long long)time,localtime,cIter->first.ToString(),i,avatar_joint_replicas.size());
00161                                                         logfileHandler.WriteLog(logstr);
00162                                                 }
00163                                         }
00164                                 }
00165                                 else
00166                                 {
00167                                         rakPeer->DetachPlugin(&logfileHandler);
00168                                         printf("Logging Disabled.\n");
00169                                         DWORD exitCode=0;                                                               //kill the audio recording process
00170                                         GetExitCodeProcess(pi.hProcess, &exitCode);             //..
00171                                         TerminateProcess(pi.hProcess, exitCode);                //..
00172                                         CloseHandle(pi.hProcess);                                               //..
00173                                         char endtime[128];
00174                                         char endstr[128];
00175                                         logfileHandler.GetLocalTime(endtime);
00176                                         sprintf(endstr,"E;%s",endtime);
00177                                         logfileHandler.WriteLog(endstr);
00178                                 }
00179                         #endif
00180                         }
00181                 }
00182 
00183                 RakSleep(1);
00184         }
00185 
00186         #ifdef WIN32    
00187         if (logging==true)
00188         {
00189                 rakPeer->DetachPlugin(&logfileHandler);
00190                 printf("Logging Disabled.\n");
00191                 DWORD exitCode=0;                                                               //kill the audio recording process
00192                 GetExitCodeProcess(pi.hProcess, &exitCode);             //..
00193                 TerminateProcess(pi.hProcess, exitCode);                //..
00194                 CloseHandle(pi.hProcess);                                               //..
00195                 char endtime[128];
00196                 char endstr[128];
00197                 logfileHandler.GetLocalTime(endtime);
00198                 sprintf(endstr,"E;%s",endtime);
00199                 logfileHandler.WriteLog(endstr);
00200         }
00201         #endif
00202 
00203         //Tells clients to destroy local copies when server exits.
00204         DataStructures::Multilist<ML_STACK, Replica3*> replicaListOut;
00205         // ClearPointers is needed, as I don't track which objects have and have not been allocated at the application level. 
00206         // So ClearPointers will call delete on every object in the returned list, 
00207         // which is every object that the application has created. Another way to put it is
00208         //      A. Send a packet to tell other systems to delete these objects
00209         //      B. Delete these objects on my own system
00210         replicaManager.GetReferencedReplicaList(replicaListOut);
00211         replicaManager.BroadcastDestructionList(replicaListOut, UNASSIGNED_SYSTEM_ADDRESS);
00212         replicaListOut.ClearPointers( true, __FILE__, __LINE__ );
00213         rakPeer->Shutdown(100,0);
00214         RakNetworkFactory::DestroyRakPeerInterface(rakPeer);
00215 
00216         return 1;
00217 }
 All Classes Files Functions Variables Enumerations Enumerator Defines