Browse Source

*** empty log message ***

Dave Schuyler 24 years ago
parent
commit
351446cb2f
3 changed files with 92 additions and 45 deletions
  1. 62 27
      direct/src/directd/directd.cxx
  2. 24 13
      direct/src/directd/directd.h
  3. 6 5
      direct/src/directd/directd.py

+ 62 - 27
direct/src/directd/directd.cxx

@@ -144,6 +144,7 @@ DirectD::~DirectD() {
     _cm.close_connection((*ci));
     _cm.close_connection((*ci));
   }
   }
   _connections.clear();
   _connections.clear();
+  cerr<<"DirectD dtor"<<endl;
 }
 }
 
 
 
 
@@ -169,39 +170,61 @@ DirectDClient::handle_command(const string& cmd) {
   }
   }
 }
 }
 
 
-
-#if 0 //[
-void 
-DirectD::add_server(const string& server_host, int port) {
-  string* s = new String(server_host);
-  _servers.push_back(pair<const string*, int>(&s, port));
+int 
+DirectD::client_is_ready(const string& client_host, int port) {
+  connect_to(client_host, port);
+  send_command("s");
+  disconnect_from(client_host, port);
+  return 0;
 }
 }
 
 
-// Description: free memory and earse all elements from _servers.
-void
-DirectD::clear_server_list() {
-  ServerList::const_iterator i = _servers.begin();
-  for (; i!=_servers.end(); ++i) {
-    string* name=(*i).first;
-    delete name;
+bool
+DirectD::wait_for_servers(int count, int timeout_ms) {
+  if (count <= 0) {
+    return true;
   }
   }
-  _servers.clear();
-}
+  // The timeout is a rough estimate, we may wait slightly longer.
+  const wait_ms=200;
+  int cycles=timeout_ms/wait_ms;
+  while (cycles--) {
+    check_for_new_clients();
+    check_for_lost_connection();
+    /*
+        The following can be more generalized with a little work.
+        check_for_datagrams() could take a handler function as an arugment, maybe.
+    */
+    ////check_for_datagrams();
+    // Process all available datagrams.
+    while (_reader.data_available()) {
+      NetDatagram datagram;
+      if (_reader.get_data(datagram)) {
+        if (_verbose) {
+          nout << "Got datagram " /*<< datagram <<*/ "from "
+               << datagram.get_address() << endl;
+          datagram.dump_hex(nout);
+        }
+        //handle_datagram(datagram);
+        DatagramIterator di(datagram);
+        string s=di.get_string();
+        if (s=="r" && --count) {
+          return true;
+        }
+      }
+    }
 
 
-int 
-DirectD::connect() {
-  ServerList::const_iterator i = _servers;
-  for (; i!=_servers.end(); ++i) {
-    string* name=(*i).first;
-    int port=(*i).second;
-    cerr<<"connecting to "<<name<<" "<<port<<endl;
+    // Yield the timeslice before we poll again.
+    PR_Sleep(PR_MillisecondsToInterval(wait_ms));
   }
   }
-  return 0;
+  // We've waited long enough, assume they're not going to be
+  // ready in the time we want them:
+  return false;
 }
 }
-#endif //]
 
 
 int 
 int 
-DirectD::tell_client_the_server_is_ready(const string& client_host, int port) {
+DirectD::server_is_ready(const string& client_host, int port) {
+  connect_to(client_host, port);
+  send_command("r");
+  disconnect_from(client_host, port);
   return 0;
   return 0;
 }
 }
 
 
@@ -350,6 +373,19 @@ DirectD::connect_to(const string& host_name, int port) {
   _connections.insert(c);
   _connections.insert(c);
 }
 }
 
 
+void
+DirectD::disconnect_from(const string& host_name, int port) {
+  for (ConnectionSet::iterator i=_connections.begin(); i != _connections.end(); ++i) {
+    if ((*i)->get_address().get_ip_string()==host_name 
+        && (*i)->get_address().get_port()==port) {
+      _cm.close_connection((*i));
+      _reader.remove_connection((*i));
+      _connections.erase(i);
+      break;
+    }
+  }
+}
+
 void
 void
 DirectD::check_for_lost_connection() {
 DirectD::check_for_lost_connection() {
   while (_cm.reset_connection_available()) {
   while (_cm.reset_connection_available()) {
@@ -386,8 +422,7 @@ DirectD::spawn_background_server() {
 }
 }
 
 
 void
 void
-DirectD::listen_to(int port) {
-  const backlog=8;
+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()) {
   if (rendezvous.is_null()) {
     nout << "Cannot grab port " << _port << ".\n";
     nout << "Cannot grab port " << _port << ".\n";

+ 24 - 13
direct/src/directd/directd.h

@@ -64,20 +64,35 @@ PUBLISHED:
   // Description: Call connect_to from client for each server.
   // Description: Call connect_to from client for each server.
   void connect_to(const string& server_host, int port);
   void connect_to(const string& server_host, int port);
   
   
+  // Description: 
+  void disconnect_from(const string& server_host, int port);
+  
   // Description: Call listen_to in the server.
   // Description: Call listen_to in the server.
   //              port is a rendezvous port.
   //              port is a rendezvous port.
-  void listen_to(int port);
-  
-  // Description: 
-  ////void disconnect() {
+  //              
+  //              backlog refers to how many connections can queue up
+  //              before you handle them.  Consider setting backlog to
+  //              the count you send to wait_for_servers(); or higher.
+  void listen_to(int port, int backlog=8);
   
   
-  // process command string.
+  // Description: process command string.
   void send_command(const string& cmd);
   void send_command(const string& cmd);
 
 
-  // Description: This function is overly named, but that's what it does.
-  //              Call this function from the server when import ShowbaseGlobal is
-  //              nearly finished.
-  int tell_client_the_server_is_ready(const string& client_host, int port);
+  // Description: Call this function from the client when
+  //              import ShowbaseGlobal is nearly finished.
+  int client_is_ready(const string& client_host, int port);
+
+  // Description: Call this function from the client after
+  //              calling <count> client_is_ready() calls.
+  //              
+  //              Call listen_to(port) prior to calling
+  //              wait_for_servers() (or better yet, prior
+  //              to calling client_is_ready()).
+  bool wait_for_servers(int count, int timeout_ms);
+
+  // Description: Call this function from the server when
+  //              import ShowbaseGlobal is nearly finished.
+  int server_is_ready(const string& client_host, int port);
 
 
 public:
 public:
   void set_host_name(const string& host_name);
   void set_host_name(const string& host_name);
@@ -107,10 +122,6 @@ protected:
   string _host_name;
   string _host_name;
   int _port;
   int _port;
   intptr_t _app_pid;
   intptr_t _app_pid;
-  #if 0 //[
-  typedef pvector<pair<const string*, int> > ServerList;
-  ServerList _servers;
-  #endif //]
   typedef pset< PT(Connection) > ConnectionSet;
   typedef pset< PT(Connection) > ConnectionSet;
   ConnectionSet _connections;
   ConnectionSet _connections;
 
 

+ 6 - 5
direct/src/directd/directd.py

@@ -10,13 +10,13 @@ import DatagramIterator
 print "hello"
 print "hello"
 
 
 
 
-class DirectDServer:
+class DirectD:
     notify = DirectNotifyGlobal.directNotify.newCategory("DirectD")
     notify = DirectNotifyGlobal.directNotify.newCategory("DirectD")
 
 
     def __init__(self):
     def __init__(self):
         pass
         pass
 
 
-    def connect(self, ip, port, timeout=35000):
+    def connect(self, ip, port, timeout=5000):
         self.qcm=QueuedConnectionManager()
         self.qcm=QueuedConnectionManager()
         self.tcpConn=self.qcm.openTCPClientConnection(ip, port, timeout)
         self.tcpConn=self.qcm.openTCPClientConnection(ip, port, timeout)
         
         
@@ -29,7 +29,7 @@ class DirectDServer:
             self.qcr=QueuedConnectionReader(self.qcm, 0)
             self.qcr=QueuedConnectionReader(self.qcm, 0)
             self.qcr.addConnection(self.tcpConn)
             self.qcr.addConnection(self.tcpConn)
             self.cw=ConnectionWriter(self.qcm, 0)
             self.cw=ConnectionWriter(self.qcm, 0)
-            while 1:
+            while 0:
                 s=raw_input("send: ")
                 s=raw_input("send: ")
                 d=Datagram.Datagram()
                 d=Datagram.Datagram()
                 d.addString(s)
                 d.addString(s)
@@ -45,5 +45,6 @@ class DirectDServer:
             #self.registerForChannel(self.ourChannel)
             #self.registerForChannel(self.ourChannel)
 
 
 def foo():
 def foo():
-    dds = DirectDServer()
-    dds.connect("127.0.0.1", 8000)
+    dd = DirectD()
+    dd.connect("127.0.0.1", 8001)
+    dd.connect("127.0.0.1", 8002)