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