Panda3D
|
00001 // Filename: directdServer.cxx 00002 // Created by: skyler 2002.04.08 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "directdServer.h" 00016 00017 DirectDServer::DirectDServer() { 00018 } 00019 00020 DirectDServer::~DirectDServer() { 00021 } 00022 00023 void 00024 DirectDServer::handle_command(const string& cmd) { 00025 nout<<"DirectDServer::handle_command: "<<cmd<<", size="<<cmd.size()<<endl; 00026 if (cmd.size()==1) { 00027 switch (cmd[0]) { 00028 case 'k': 00029 kill_app(0); 00030 break; 00031 case 'q': 00032 _shutdown=true; 00033 break; 00034 default: 00035 cerr<<"unknown command: "<<cmd<<endl; 00036 break; 00037 } 00038 } else { 00039 switch (cmd[0]) { 00040 case 'k': 00041 if (cmd[1]=='a') { 00042 kill_all(); 00043 } else { 00044 int index = atoi(cmd.substr(1, string::npos).c_str()); 00045 kill_app(index); 00046 } 00047 break; 00048 case '!': { 00049 string c=cmd.substr(1, string::npos); 00050 //read_command(c); 00051 start_app(c); 00052 } 00053 break; 00054 default: 00055 start_app(cmd); 00056 break; 00057 } 00058 } 00059 } 00060 00061 void 00062 DirectDServer::read_command(string& cmd) { 00063 try { 00064 pifstream f; 00065 f.open("directdCommand", ios::in | ios::binary); 00066 stringstream ss; 00067 const int buf_size=512; 00068 char buf[buf_size]; 00069 f.getline(buf, buf_size); 00070 if (f.gcount() > 0) { 00071 cmd = buf; 00072 cerr<<"read_command "<<cmd<<endl; 00073 } 00074 f.close(); 00075 } catch (...) { 00076 // This could be bad, I suppose. But we're going to throw out 00077 // any exceptions that happen during the above read. 00078 cerr<<"DirectD::read_command() exception."<<endl; 00079 } 00080 } 00081 00082 void 00083 DirectDServer::run_server(int port) { 00084 nout<<"server"<<endl; 00085 00086 listen_to(port); 00087 00088 while (!_shutdown) { 00089 check_for_new_clients(); 00090 check_for_lost_connection(); 00091 check_for_datagrams(); 00092 00093 // Yield the timeslice before we poll again. 00094 //PR_Sleep(PR_MillisecondsToInterval(200)); 00095 Sleep(200); 00096 } 00097 } 00098 00099 int 00100 main(int argc, char *argv[]) { 00101 if (argc > 1 && strcmp(argv[1], "--help")==0) { 00102 cerr<<"directd [<port>]\n" 00103 " port default 8001\n"; 00104 return 1; 00105 } 00106 00107 cerr<<"directdServer "<<__DATE__<<" "<<__TIME__<<endl; 00108 int port=8001; 00109 if (argc > 1) { 00110 port=(atoi(argv[argc-1])); 00111 } 00112 DirectDServer directd; 00113 directd.run_server(port); 00114 00115 return 0; 00116 }