Panda3D
directd.h
1 // Filename: directd.h
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 <process.h>
16 #include "pandabase.h"
17 #include "directsymbols.h"
18 #include "queuedConnectionManager.h"
19 #include "queuedConnectionReader.h"
20 #include "connectionWriter.h"
21 #include "queuedConnectionListener.h"
22 #include <windows.h>
23 
24 
25 #ifdef CPPPARSER //[
26 // hack for interrogate
27 typedef int intptr_t;
28 typedef int HANDLE;
29 #endif //]
30 
31 
32 // Description: DirectD is a client/server app for starting panda/direct.
33 //
34 // Usage:
35 // Start a directd server on each of the machines you
36 // which to start panda on.
37 //
38 // Start a directd client on the controlling machine or
39 // import ShowBaseGlobal with the xxxxx flag in your
40 // Configrc. The client will connect each of the servers
41 // in the xxxxx list in your Configrc.
42 //
43 // There are two API groups in this class, they are:
44 //
45 // listen_to()
46 // client_ready() or tell_server()
47 // wait_for_servers()
48 // server_ready()
49 //
50 // and:
51 //
52 // connect_to()
53 // send_command()
54 // disconnect_from()
55 //
56 // The second group was from a more general implementation
57 // of DirectD. The first group summarizes the main intents
58 // of DirectD.
59 // Both groups are presented in order chronologically by their
60 // intended usage.
61 // The first group will probably provide everthing needed for
62 // DirectD.
63 class EXPCL_DIRECT DirectD {
64 PUBLISHED:
65  DirectD();
66  ~DirectD();
67 
68  // Description: Call listen_to in the server.
69  // port is a rendezvous port.
70  //
71  // backlog refers to how many connections can queue up
72  // before you handle them. Consider setting backlog to
73  // the count you send to wait_for_servers(); or higher.
74  void listen_to(int port, int backlog=8);
75 
76  // Description: Call this function from the client when
77  // import ShowbaseGlobal is nearly finished.
78  // cmd: a cli command that will be executed on the remote
79  // machine.
80  // A new connection will be created and closed. If you
81  // want to send more than one command, you should use
82  // connect_to(), send_command(), and disconnect_from().
83  int client_ready(const string& server_host, int port, const string& cmd);
84 
85  // Description: Tell the server to do the command cmd.
86  // cmd is one of the following:
87  // "k[<n>]" Kill the most recent application
88  // started with client_ready() or "!".
89  // Or kill the nth most recent or 'a' for All.
90  // E.g. "k", "k0", "k2", "ka".
91  // "q" Tell the server to quit.
92  // "!cmd" Exectue the cmd on the server (this
93  // is a dos shell command; if you want
94  // a bash command, include bash in the
95  // command e.g. "!bash pwd"). When you call
96  // client_ready(), it prefixes "!" for you.
97  // A new connection will be created and closed.
98  int tell_server(const string& server_host, int port, const string& cmd);
99 
100  // Description: Call this function from the client after
101  // calling <count> client_ready() calls.
102  //
103  // Call listen_to(port) prior to calling
104  // wait_for_servers() (or better yet, prior
105  // to calling client_ready()).
106  //
107  // timeout_ms defaults to two minutes.
108  bool wait_for_servers(int count, int timeout_ms=2*60*1000);
109 
110  // Description: Call this function from the server when
111  // import ShowbaseGlobal is nearly finished.
112  int server_ready(const string& client_host, int port);
113 
114  // Description: Call connect_to from client for each server.
115  // returns the port number of the connection (which
116  // is different from the rendezvous port used in the
117  // second argument). The return value can be used
118  // for the port arguemnt in disconnect_from().
119  int connect_to(const string& server_host, int port);
120 
121  // Description: This is the counterpart to connect_to(). Pass
122  // the same server_host as for connect_to(), but pass
123  // the return value from connect_to() for the port,
124  // not the port passed to connect_to().
125  void disconnect_from(const string& server_host, int port);
126 
127  // Description: Send the same command string to all current
128  // connections.
129  void send_command(const string& cmd);
130 
131 protected:
132  void start_app(const string& cmd);
133  void kill_app(int index);
134  void kill_all();
135  virtual void handle_command(const string& cmd);
136  void handle_datagram(NetDatagram& datagram);
137  void send_one_message(const string& host_name,
138  int port, const string& message);
139 
141  QueuedConnectionReader _reader;
142  ConnectionWriter _writer;
143  QueuedConnectionListener _listener;
144 
145  // Start of old stuff:
146  // This is used to switch to the original method of
147  // starting applications. It can be used on old systems
148  // that don't support job objects. Eventually this stuff
149  // should be removed.
150  bool _useOldStuff;
151  typedef pvector< long /*intptr_t*/ > PidStack;
152  PidStack _pids;
153  // End of old stuff
154 
156  ConnectionSet _connections;
157  HANDLE _jobObject;
158  bool _shutdown;
159 
160  void check_for_new_clients();
161  void check_for_datagrams();
162  void check_for_lost_connection();
163 };
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:43
This flavor of ConnectionManager will queue up all of the reset-connection messages from the Connecti...
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
This class handles threaded delivery of datagrams to various TCP or UDP sockets.
DirectD is a client/server app for starting panda/direct.
Definition: directd.h:63
This flavor of ConnectionReader will read from its sockets and queue up all of the datagrams read for...
This flavor of ConnectionListener will queue up all of the TCP connections it established for later d...