فهرست منبع

*** empty log message ***

Dave Schuyler 24 سال پیش
والد
کامیت
15477e84b7
2فایلهای تغییر یافته به همراه48 افزوده شده و 25 حذف شده
  1. 10 18
      direct/src/directd/directd.cxx
  2. 38 7
      direct/src/directd/directd.h

+ 10 - 18
direct/src/directd/directd.cxx

@@ -122,7 +122,7 @@ namespace {
       CloseHandle(pi.hProcess);
       CloseHandle(pi.hThread);
     } else {
-      cerr<<"CreateProcess failed: "<<cmd<<endl;
+      nout<<"CreateProcess failed: "<<cmd<<endl;
     }
     return pid;
   }
@@ -131,8 +131,7 @@ namespace {
 
 
 DirectD::DirectD() :
-    _host_name("localhost"),
-    _port(8001), _app_pid(0),
+    _app_pid(0),
     _reader(&_cm, 1), _writer(&_cm, 1), _listener(&_cm, 0),
     _shutdown(false) {
 }
@@ -144,7 +143,6 @@ DirectD::~DirectD() {
     _cm.close_connection((*ci));
   }
   _connections.clear();
-  cerr<<"DirectD dtor"<<endl;
 }
 
 int 
@@ -182,7 +180,7 @@ DirectD::wait_for_servers(int count, int timeout_ms) {
         //handle_datagram(datagram);
         DatagramIterator di(datagram);
         string s=di.get_string();
-        cerr<<"wait_for_servers() count="<<count<<", s="<<s<<endl;
+        nout<<"wait_for_servers() count="<<count<<", s="<<s<<endl;
         if (s=="r" && !--count) {
           return true;
         }
@@ -214,7 +212,7 @@ DirectD::start_app(const string& cmd) {
 void
 DirectD::kill_app() {
   if (_app_pid) {
-    cerr<<"trying k "<<_app_pid<<endl;
+    nout<<"trying kill "<<_app_pid<<endl;
     TerminateApp(_app_pid, 1000);
   }
 }
@@ -274,7 +272,7 @@ DirectD::send_one_message(const string& host_name,
   _cm.close_connection(c);
 }
 
-void
+int
 DirectD::connect_to(const string& host_name, int port) {
   NetAddress host;
   if (!host.set_host(host_name, port)) {
@@ -285,7 +283,7 @@ DirectD::connect_to(const string& host_name, int port) {
   PT(Connection) c = _cm.open_TCP_client_connection(host, timeout_ms);
   if (c.is_null()) {
     nout << "No connection.\n";
-    return;
+    return 0;
   }
 
   nout << "Successfully opened TCP connection to " << host_name
@@ -295,6 +293,7 @@ DirectD::connect_to(const string& host_name, int port) {
 
   _reader.add_connection(c);
   _connections.insert(c);
+  return  c->get_address().get_port();
 }
 
 void
@@ -338,21 +337,14 @@ DirectD::check_for_datagrams(){
   }
 }
 
-void
-DirectD::spawn_background_server() {
-  stringstream ss;
-  ss<<"directd -s "<<_host_name.c_str()<<" "<<_port;
-  DWORD serverPID = StartApp(ss.str());
-}
-
 void
 DirectD::listen_to(int port, int backlog) {
-  PT(Connection) rendezvous = _cm.open_TCP_server_rendezvous(_port, backlog);
+  PT(Connection) rendezvous = _cm.open_TCP_server_rendezvous(port, backlog);
   if (rendezvous.is_null()) {
-    nout << "Cannot grab port " << _port << ".\n";
+    nout << "Cannot grab port " << port << ".\n";
     exit(1);
   }
-  nout << "Listening for connections on port " << _port << "\n";
+  nout << "Listening for connections on port " << port << "\n";
   _listener.add_connection(rendezvous);
 }
 

+ 38 - 7
direct/src/directd/directd.h

@@ -39,8 +39,29 @@ typedef int intptr_t;
 //              
 //              Start a directd client on the controlling machine or 
 //              import ShowBaseGlobal with the xxxxx flag in your
-//              Configrc.  The client will connact each of the servers 
+//              Configrc.  The client will connect each of the servers 
 //              in the xxxxx list in your Configrc.
+//              
+//              There are two API groups in this class, they are:
+//              
+//                listen_to()
+//                client_ready()
+//                wait_for_servers()
+//                server_ready()
+//              
+//              and:
+//              
+//                connect_to()
+//                send_command()
+//                disconnect_from()
+//              
+//              The second group was from a more general implementation
+//              of DirectD.  The first group summarizes the main intents
+//              of DirectD.
+//              Both groups are presented in order chronologically by their
+//              intended usage.
+//              The first group will probably provide everthing needed for
+//              DirectD.
 class EXPCL_DIRECT DirectD {
 PUBLISHED:
   DirectD();
@@ -58,6 +79,9 @@ PUBLISHED:
   //              import ShowbaseGlobal is nearly finished.
   //              cmd: a cli command that will be executed on the remote
   //                   machine.
+  //              A new connection will be created and closed.  If you
+  //              want to send more than one command, you should use
+  //              connect_to(), send_command(), and disconnect_from().
   int client_ready(const string& client_host, int port, const string& cmd);
 
   // Description: Call this function from the client after
@@ -66,6 +90,8 @@ PUBLISHED:
   //              Call listen_to(port) prior to calling
   //              wait_for_servers() (or better yet, prior
   //              to calling client_ready()).
+  //              
+  //              timeout_ms defaults to two minutes.
   bool wait_for_servers(int count, int timeout_ms=2*60*1000);
 
   // Description: Call this function from the server when
@@ -73,16 +99,23 @@ PUBLISHED:
   int server_ready(const string& client_host, int port);
   
   // Description: Call connect_to from client for each server.
-  void connect_to(const string& server_host, int port);
+  //              returns the port number of the connection (which
+  //              is different from the rendezvous port used in the
+  //              second argument).  The return value can be used
+  //              for the port arguemnt in disconnect_from().
+  int connect_to(const string& server_host, int port);
   
-  // Description: 
+  // Description: This is the counterpart to connect_to().  Pass
+  //              the same server_host as for connect_to(), but pass
+  //              the return value from connect_to() for the port,
+  //              not the port passed to connect_to().
   void disconnect_from(const string& server_host, int port);
   
-  // Description: process command string.
+  // Description: Send the same command string to all current
+  //              connections.
   void send_command(const string& cmd);
 
 protected:
-  void spawn_background_server();
   void start_app(const string& cmd);
   void kill_app();
   virtual void handle_command(const string& cmd);
@@ -95,8 +128,6 @@ protected:
   ConnectionWriter _writer;
   QueuedConnectionListener _listener;
 
-  string _host_name;
-  int _port;
   intptr_t _app_pid;
   typedef pset< PT(Connection) > ConnectionSet;
   ConnectionSet _connections;