Revision: 40702
          http://brlcad.svn.sourceforge.net/brlcad/?rev=40702&view=rev
Author:   davidloman
Date:     2010-09-27 17:30:41 +0000 (Mon, 27 Sep 2010)

Log Message:
-----------
Made 'listening' on a PortalManager optional.  Allows for a testServer and 
testClient to be run on the same machine without bashing your head against an 
IP Bind issue.  Also put in NULL checking in PortalManager::connectToHost() so 
as to prevent segfaults.

Modified Paths:
--------------
    rt^3/trunk/include/PortalManager.h
    rt^3/trunk/src/libNet/PortalManager.cxx

Modified: rt^3/trunk/include/PortalManager.h
===================================================================
--- rt^3/trunk/include/PortalManager.h  2010-09-27 17:25:05 UTC (rev 40701)
+++ rt^3/trunk/include/PortalManager.h  2010-09-27 17:30:41 UTC (rev 40702)
@@ -40,7 +40,7 @@
 class PortalManager : public ControlledThread
 {
 public:
-       PortalManager(quint16 port);
+       PortalManager(quint16 port = 0);
        ~PortalManager();
 
        Portal* connectToHost(QString host, quint16 port);

Modified: rt^3/trunk/src/libNet/PortalManager.cxx
===================================================================
--- rt^3/trunk/src/libNet/PortalManager.cxx     2010-09-27 17:25:05 UTC (rev 
40701)
+++ rt^3/trunk/src/libNet/PortalManager.cxx     2010-09-27 17:30:41 UTC (rev 
40702)
@@ -45,15 +45,22 @@
 PortalManager::connectToHost(QString host, quint16 port)
 {
        PkgTcpClient* pkgc = (PkgTcpClient* 
)this->tcpServer->connectToHost(host.toStdString(), port);
-       return this->makeNewPortal(pkgc);
+
+       if (pkgc == NULL) {
+               return NULL;
+       } else {
+               return this->makeNewPortal(pkgc);
+       }
 }
 
 void
 PortalManager::_run()
 {
+  struct timeval timeout;
   fd_set readfds;
   fd_set writefds;
   fd_set exceptionfds;
+  int listener;
 
   this->masterFDSLock.lock();
   FD_ZERO(&masterfds);
@@ -63,19 +70,25 @@
   FD_ZERO(&writefds);
   FD_ZERO(&exceptionfds);
 
-  int listener = this->tcpServer->listen(this->port);
-  if (listener < 0) {
-         this->log->logERROR("PortalManager", "Failed to listen");
-      return;
-  }
+  if (this->port != 0) {
+               listener = this->tcpServer->listen(this->port);
+               if (listener < 0) {
+                       this->log->logERROR("PortalManager", "Failed to 
listen");
+                       return;
+               }
 
-  this->masterFDSLock.lock();
-  FD_SET(listener, &masterfds);
-  fdmax = listener;
-  this->masterFDSLock.unlock();
+               this->masterFDSLock.lock();
+               FD_SET(listener, &masterfds);
+               fdmax = listener;
+               this->masterFDSLock.unlock();
+       }
 
   while (this->runCmd) {
+         //Set values EVERY loop since select() on *nix modifies this.
+         timeout.tv_sec = 1;
+         timeout.tv_usec = 0;
 
+
        this->masterFDSLock.lock();
     readfds = masterfds;
     writefds = masterfds;
@@ -83,7 +96,17 @@
     this->masterFDSLock.unlock();
 
     //Shelect!!
-    int retval = select(fdmax+1, &readfds, &writefds, &exceptionfds, NULL);
+    int retval = select(fdmax+1, &readfds, &writefds, NULL, &timeout);
+
+    QString out("Loop start.  Select returned: ");
+    out.append(QString::number(retval));
+    this->log->logINFO("PortalManager", out);
+
+    //Save time on the loop:
+    if (retval == 0) {
+       continue;
+    }
+
     if(retval <0) {
       //got a selector error
 
@@ -103,14 +126,18 @@
     }
 
       for (int i = 0; i <= fdmax; ++i) {
+         /*
                        if (FD_ISSET(i, &exceptionfds)) {
                                //TODO handle exceptions
                                perror("Exception on FileDescriptor");
                        }
+*/
 
                        if (FD_ISSET(i, &readfds)) {
+                               this->log->logINFO("PortalManager", "Read On 
Listener.");
+
                                //If we are 'reading' on listener
-                               if (i == listener) {
+                               if (port != 0 && i == listener) {
                                        PkgTcpClient* client = (PkgTcpClient*) 
this->tcpServer->waitForClient(42);
 
                                        if (client == 0) {
@@ -122,6 +149,7 @@
 
                                        //else we are plain reading.
                                } else {
+                                       this->log->logINFO("PortalManager", 
"Read On Normal FD.");
                                        //Portal->read here.
                                        if (this->fdPortalMap->contains(i)) {
                                                this->portalsLock->lock();
@@ -146,8 +174,11 @@
 
                        /*
                         * Do we really need Write checking?
-                        *
+                        */
                        if (FD_ISSET(i, &writefds)) {
+                       this->log->logINFO("PortalManager", "Write.");
+
+                               /*
                                //Portal->write here.
                                if (this->fdPortalMap->contains(i)) {
                                        this->portalsLock->lock();
@@ -167,8 +198,9 @@
                                        this->closeFD(i,"Attempting to write to 
FD not associated with a Portal, dropping connection to remote host.", 
&masterfds);
                                        continue;
                                }
+                               */
                        }
-                       */
+
                } //end FOR
     } //end while
 }//end fn


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to