Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

direct/src/directdServer/directdServer.cxx

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

Generated on Fri May 2 01:37:21 2003 for Direct by doxygen1.3