Revision: 44170
          http://brlcad.svn.sourceforge.net/brlcad/?rev=44170&view=rev
Author:   davidloman
Date:     2011-04-04 10:47:33 +0000 (Mon, 04 Apr 2011)

Log Message:
-----------
GSConnection need not extend Thread.  Limits implementation and restricts the 
end user's design.

Modified Paths:
--------------
    
geomcore/trunk/src/interfaces/java/src/org/brlcad/geometryservice/net/GSConnection.java

Modified: 
geomcore/trunk/src/interfaces/java/src/org/brlcad/geometryservice/net/GSConnection.java
===================================================================
--- 
geomcore/trunk/src/interfaces/java/src/org/brlcad/geometryservice/net/GSConnection.java
     2011-04-01 21:58:50 UTC (rev 44169)
+++ 
geomcore/trunk/src/interfaces/java/src/org/brlcad/geometryservice/net/GSConnection.java
     2011-04-04 10:47:33 UTC (rev 44170)
@@ -27,11 +27,14 @@
 import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.Socket;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
 import java.nio.BufferOverflowException;
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.UUID;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.brlcad.geometryservice.GSStatics;
 import org.brlcad.geometryservice.GeometryServiceException;
@@ -40,7 +43,7 @@
 import org.brlcad.geometryservice.net.msg.RemoteNodeNameSetMsg;
 import org.brlcad.geometryservice.net.msg.SessionInfoMsg;
 
-public class GSConnection {
+public class GSConnection extends Thread {
 
        public static final int CONN_INITIAL_READBUF_SIZE = 1024 * 1024;
 
@@ -48,6 +51,9 @@
 
        public static final long MAX_HANDSHAKE_WAIT_TIME_MS = 1000 * 5;
 
+       private AtomicBoolean recvRunStatus = new AtomicBoolean(false);
+       private AtomicBoolean recvRunCmd = new AtomicBoolean(false);
+
        /**
         * Static method that will connect to the provided addy:port and 
attempt to
         * log in.
@@ -65,7 +71,8 @@
 
                /* Validate args */
                if (addy == null || port < 1)
-                       throw new GeometryServiceException("NULL address or 
port value is <1");
+                       throw new GeometryServiceException(
+                                       "NULL address or port value is <1");
                if (uname == null || uname.length() < 1)
                        throw new GeometryServiceException("NULL or zero length 
uname");
                if (passwd == null || passwd.length() < 1)
@@ -84,11 +91,12 @@
                if (!GSConnection.authenticate(conn, uname, passwd)) {
                        throw new GeometryServiceException("Authentication 
Failed.");
                }
-               
+
                return conn;
        }
 
-       private static AbstractNetMsg waitForMsg(GSConnection conn) throws 
GeometryServiceException {
+       private static AbstractNetMsg waitForMsg(GSConnection conn)
+                       throws GeometryServiceException {
                AbstractNetMsg newMsg = null;
                int totalRead = 0;
                long startTime = System.currentTimeMillis();
@@ -101,18 +109,22 @@
                                if (totalRead == 0) {
 
                                        if ((System.currentTimeMillis() - 
startTime) >= MAX_HANDSHAKE_WAIT_TIME_MS)
-                                               throw new 
GeometryServiceException("Timeout on handshake.  Waited " + 
MAX_HANDSHAKE_WAIT_TIME_MS + "ms.");
+                                               throw new 
GeometryServiceException(
+                                                               "Timeout on 
handshake.  Waited "
+                                                                               
+ MAX_HANDSHAKE_WAIT_TIME_MS + "ms.");
 
                                        try {
                                                Thread.sleep(15);
                                        } catch (InterruptedException e) {
-                                               throw new 
GeometryServiceException("Thread Interruption");
+                                               throw new 
GeometryServiceException(
+                                                               "Thread 
Interruption");
                                        }
                                        continue;
                                }
 
                                if (totalRead < 0)
-                                       throw new 
GeometryServiceException("Socket closed before handshake complete.");
+                                       throw new GeometryServiceException(
+                                                       "Socket closed before 
handshake complete.");
 
                        } // End while (totalRead < 1) {
 
@@ -127,7 +139,8 @@
 
        }
 
-       private static final boolean handshake(GSConnection conn, String uname) 
throws GeometryServiceException {
+       private static final boolean handshake(GSConnection conn, String uname)
+                       throws GeometryServiceException {
                String localNodeName = uname + "-" + conn.getNodename();
                conn.send(new RemoteNodeNameSetMsg(localNodeName));
 
@@ -151,10 +164,11 @@
                return true;
        }
 
-       private static final boolean authenticate(GSConnection conn, String 
uname, String passwd) throws GeometryServiceException {
+       private static final boolean authenticate(GSConnection conn, String 
uname,
+                       String passwd) throws GeometryServiceException {
                NewSessionReqMsg reqMsg = new NewSessionReqMsg(uname, passwd);
                conn.send(reqMsg);
-               
+
                AbstractNetMsg inMsg = GSConnection.waitForMsg(conn);
 
                if (inMsg == null)
@@ -168,7 +182,7 @@
                SessionInfoMsg infoMsg = (SessionInfoMsg) inMsg;
 
                conn.setSessionID(infoMsg.getSessionID());
-               return true;            
+               return true;
        }
 
        private final Socket sock;
@@ -184,7 +198,7 @@
        private String remoteNodename;
 
        private UUID sessionID;
-       
+
        private GSConnection(Socket sock) {
                this.sock = sock;
                this.connReadBuf = 
ByteBuffer.allocate(CONN_INITIAL_READBUF_SIZE);
@@ -202,7 +216,7 @@
 
                int totalRead = this.pullInFromSocket();
 
-               if (totalRead < 0)
+               if (totalRead < 0) /* Error */
                        return null;
 
                /* Now try to make netMsgs */
@@ -241,12 +255,25 @@
                /* pull in all the data off the socket */
                int bytesReadLast = 0;
 
+               int timeout;
+               
+               /* Try to set the read Timeout to 100ms */ 
+               try {
+                       timeout = this.sock.getSoTimeout();
+                       if (timeout < 1)
+                               this.sock.setSoTimeout(100);
+               } catch (SocketException e) {
+                       return -1;
+               }
+
                do {
                        bytesReadLast = this.read(this.socketReadBuf);
 
                        if (bytesReadLast == 0)
                                break;
 
+                       System.out.println("Got " + bytesReadLast + " bytes.");
+
                        if (bytesReadLast < 0)
                                return -1;
 
@@ -256,12 +283,22 @@
                         * make sure incoming data will fit into connection's 
BB, if not,
                         * expand buffer
                         */
-                       if ((this.connReadBuf.position() + bytesReadLast) > 
this.connReadBuf.capacity())
-                               this.connReadBuf = 
GSConnection.doubleBufCapacity(this.connReadBuf);
+                       if ((this.connReadBuf.position() + bytesReadLast) > 
this.connReadBuf
+                                       .capacity())
+                               this.connReadBuf = GSConnection
+                                               
.doubleBufCapacity(this.connReadBuf);
 
                        this.connReadBuf.put(this.socketReadBuf, 0, 
bytesReadLast);
 
                } while (bytesReadLast > 0);
+               
+               try {
+                       /* Return timeout to zero if applicable */
+                       if (timeout < 1)
+                               this.sock.setSoTimeout(0);
+               } catch (SocketException e) {
+                       return -1;
+               }
                return total;
        }
 
@@ -333,6 +370,10 @@
                        is = this.sock.getInputStream();
                        bytesRead = is.read(ba);
 
+               } catch (SocketTimeoutException ste){
+                       /* No worries here*/
+                       return 0;
+                       
                } catch (IOException ioe) {
                        this.disconnect();
                        return -1;
@@ -384,7 +425,7 @@
                        return false;
                }
        }
-       
+
        private final void write(ByteBuffer bb) throws IOException {
                OutputStream os = this.sock.getOutputStream();
 
@@ -402,8 +443,8 @@
                        os.write(b);
                        out += Integer.toString(b & 0xff, 16).toUpperCase();
                }
-       
-       System.out.println("Sending Data: " + out);
+
+               System.out.println("Sending Data (" + amtToWrite + " bytes): " 
+ out);
        }
 
        /**
@@ -427,8 +468,6 @@
                return remoteNodename;
        }
 
-       
-       
        /**
         * @return the sessionID
         */
@@ -437,7 +476,8 @@
        }
 
        /**
-        * @param sessionID the sessionID to set
+        * @param sessionID
+        *            the sessionID to set
         */
        private final void setSessionID(UUID sessionID) {
                this.sessionID = sessionID;
@@ -458,4 +498,27 @@
                return newBB;
        }
 
+       @Override
+       public void run() {
+               System.out.println("GS Connection.run() entered.");
+
+               this.recvRunStatus.set(true);
+               this.recvRunCmd.set(true);
+
+               while (this.recvRunCmd.get()) {
+                       
+               }
+
+               this.recvRunStatus.set(false);
+               System.out.println("GS Connection.run() exiting.");
+       }
+
+       public void stopReceiving() {
+               this.recvRunCmd.set(false);
+       }
+
+       public boolean isReceiving() {
+               return this.recvRunStatus.get();
+       }
+
 }


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

------------------------------------------------------------------------------
Create and publish websites with WebMatrix
Use the most popular FREE web apps or write code yourself; 
WebMatrix provides all the features you need to develop and 
publish your website. http://p.sf.net/sfu/ms-webmatrix-sf
_______________________________________________
BRL-CAD Source Commits mailing list
brlcad-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to