Panda3D
directdServer.cxx
1 // Filename: directdServer.cxx
2 // Created by: skyler 2002.04.08
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "directdServer.h"
16 
17 DirectDServer::DirectDServer() {
18 }
19 
20 DirectDServer::~DirectDServer() {
21 }
22 
23 void
24 DirectDServer::handle_command(const string& cmd) {
25  nout<<"DirectDServer::handle_command: "<<cmd<<", size="<<cmd.size()<<endl;
26  if (cmd.size()==1) {
27  switch (cmd[0]) {
28  case 'k':
29  kill_app(0);
30  break;
31  case 'q':
32  _shutdown=true;
33  break;
34  default:
35  cerr<<"unknown command: "<<cmd<<endl;
36  break;
37  }
38  } else {
39  switch (cmd[0]) {
40  case 'k':
41  if (cmd[1]=='a') {
42  kill_all();
43  } else {
44  int index = atoi(cmd.substr(1, string::npos).c_str());
45  kill_app(index);
46  }
47  break;
48  case '!': {
49  string c=cmd.substr(1, string::npos);
50  //read_command(c);
51  start_app(c);
52  }
53  break;
54  default:
55  start_app(cmd);
56  break;
57  }
58  }
59 }
60 
61 void
62 DirectDServer::read_command(string& cmd) {
63  try {
64  pifstream f;
65  f.open("directdCommand", ios::in | ios::binary);
66  stringstream ss;
67  const int buf_size=512;
68  char buf[buf_size];
69  f.getline(buf, buf_size);
70  if (f.gcount() > 0) {
71  cmd = buf;
72  cerr<<"read_command "<<cmd<<endl;
73  }
74  f.close();
75  } catch (...) {
76  // This could be bad, I suppose. But we're going to throw out
77  // any exceptions that happen during the above read.
78  cerr<<"DirectD::read_command() exception."<<endl;
79  }
80 }
81 
82 void
83 DirectDServer::run_server(int port) {
84  nout<<"server"<<endl;
85 
86  listen_to(port);
87 
88  while (!_shutdown) {
89  check_for_new_clients();
90  check_for_lost_connection();
91  check_for_datagrams();
92 
93  // Yield the timeslice before we poll again.
94  //PR_Sleep(PR_MillisecondsToInterval(200));
95  Sleep(200);
96  }
97 }
98 
99 int
100 main(int argc, char *argv[]) {
101  if (argc > 1 && strcmp(argv[1], "--help")==0) {
102  cerr<<"directd [<port>]\n"
103  " port default 8001\n";
104  return 1;
105  }
106 
107  cerr<<"directdServer "<<__DATE__<<" "<<__TIME__<<endl;
108  int port=8001;
109  if (argc > 1) {
110  port=(atoi(argv[argc-1]));
111  }
112  DirectDServer directd;
113  directd.run_server(port);
114 
115  return 0;
116 }
Start a directdServer on each of the machines you which to start panda on.
Definition: directdServer.h:25