Re: Tunnel Profile - Again

Rick Fujiyama <[email protected]> Fri, 16 May 2003 11:46:23 -0700 (PDT)
Newsgroups gmane.network.beep.beepcore.java.general
Message-ID <Pine.GSO.4.44.0305161142420.8130-200000@turing>
Apply the attached patch to the beepcore sources first.  Then grab
tunnel.tgz from our project page at
http://sourceforge.net/projects/beep-tunnel

You can find much more information from our homepage at
http://beep-tunnel.sourceforge.net/ including the results
of our interoperability testing.


-rick

On Fri, 16 May 2003, Harsh Daharwal wrote:

> How are we doing on Tunnel Profile?
> --
> Harsh Daharwal
> IOS Networks Inc.
> http://www.iosnetworks.com
>
> -Original Message---------------
>
> From: Harsh Daharwal <hdaharwal@io...>
>  Tunnel Profile
> 2003-03-08 13:27
>  Huston,
>
>  Is Tunnel Profile implemented into beepcore? If not, is there
>  any plan for doing so?
>
>  I'm trying to solve a peer to peer communication issue when peers
>  are behind NATs/Firewalls. Tunnel Profile is one of things I'm
>  looking at, although it does not solve the problem completely.
>
>  Regards,
>  --
>  Harsh Daharwal
>  IOS Networks Inc.
>  http://www.iosnetworks.com
>
>
> From: Tunnel Team <aero02b@cs...>.hmc.edu>
>  Re: Tunnel Profile
> 2003-03-08 14:08
>  On Sat, Mar 08, 2003 at 03:32:29PM -0600, Harsh Daharwal wrote:
>  > Huston,
>  >
>  > Is Tunnel Profile implemented into beepcore? If not, is there
>  > any plan for doing so?
>  >
>  > I'm trying to solve a peer to peer communication issue when peers
>  > are behind NATs/Firewalls. Tunnel Profile is one of things I'm
>  > looking at, although it does not solve the problem completely.
>  >
>  > Regards,
>  > --
>  > Harsh Daharwal
>  > IOS Networks Inc.
>  > http://www.iosnetworks.com
>  >
>
>  Hi,
>
>  The Tunnel Profile for beepcore-Java is currently under development.
>  We are working hard to get something working.  Expect something more solid
>  around May of this year.
>
>  --
>  ----------------------------
>  |       Nick Hertl         |
>  |   Harvey Mudd College    |
>  | Aerospace Clinic Manager |
>  ----------------------------
>
>
>
>
> -------------------------------------------------------
> Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara
> The only event dedicated to issues related to Linux enterprise solutions
> www.enterpriselinuxforum.com
>
> _______________________________________________
> Beepcore-java-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/beepcore-java-users
>
beepcore-0.9.07-tunnel.patch (text/plain, 5.5 KB)
diff -Naur beepcore-0.9.07/src/org/beepcore/beep/core/Session.java beepcore-0.9.07-tunnel/src/org/beepcore/beep/core/Session.java
--- beepcore-0.9.07/src/org/beepcore/beep/core/Session.java	Wed Apr 30 22:17:51 2003
+++ beepcore-0.9.07-tunnel/src/org/beepcore/beep/core/Session.java	Wed Apr 30 22:18:49 2003
@@ -1381,6 +1381,16 @@
                     String data = null;
                     Node dataNode = profile.getFirstChild();
 
+
+		    // added by RKF 04.14.2003 for compatibility w/RoadRunner
+		    // Essentially, the parser is turning a "\n" into a node.
+		    // The fix is to just skip over that extra node.
+		    if ((dataNode != null) && 
+			(dataNode.getNodeType() != 
+			 Node.CDATA_SECTION_NODE)) {
+			dataNode = dataNode.getNextSibling();
+		    }
+		    
                     if (dataNode != null) {
                         data = dataNode.getNodeValue();
 
@@ -1981,4 +1991,19 @@
                          "no longer active.");
         }
     }
+
+    /**
+     * Kill off all the listeners for this session.
+     * Used for preventing beepcore from processing messages on the session.
+     * This functionality is necessary for the Tunnel profile.
+     *
+     * Added by RKF (Will)
+     */
+    public void killAllListeners() {
+	channelListeners = null;
+	sessionListeners = null;
+	sessionListenerList = null;
+	channelListenerList = null;
+	channels = null;
+    }
 }
diff -Naur beepcore-0.9.07/src/org/beepcore/beep/transport/tcp/TCPSession.java beepcore-0.9.07-tunnel/src/org/beepcore/beep/transport/tcp/TCPSession.java
--- beepcore-0.9.07/src/org/beepcore/beep/transport/tcp/TCPSession.java	Wed Apr 30 22:17:51 2003
+++ beepcore-0.9.07-tunnel/src/org/beepcore/beep/transport/tcp/TCPSession.java	Wed Apr 30 22:18:49 2003
@@ -83,6 +83,27 @@
     private Thread thread;
 
     /**
+     * When Set to true, the processnext frame thread spins rather than blocks.
+     *
+     * spinIO true means use the ugly hack in processnextframe that allows
+     * a tunnel to work, since we can't kill the thread processing the
+     * next frame.
+     * ADDED by Tunnel Team 
+     */
+    private boolean spinIO = false;
+    
+    /**
+     * Whether to process the next frame.
+     *
+     * Tunnel proxies do not want to process the next frame when proxying
+     * and thus set this to false.
+     * 
+     * ADDED by Tunnel Team 
+     */
+     private boolean doProcessNextFrame = true;
+
+
+    /**
      * @param sock the Socket for this TCPConnection
      *
      * @registry the ProfileRegistry (set of profiles) to be used on
@@ -204,6 +225,35 @@
             socket = null;
         }
     }
+    /** 
+     * Terminate the session for a tunnel proxy.
+     * ADDED by Tunnel Team 
+     */
+    public void TunTerminate() {
+	// Disable IO (this should stop threads, but I don't believe it does)
+	disableIO();
+	
+	// Stop the next frame from being processed
+	doProcessNextFrame = false;
+	
+	// Kill all the message listeners
+	super.killAllListeners();
+
+	// Release the socket (it should later be garbage collected when
+	// it's not pointed to.
+	socket = null;
+    }
+
+    /** 
+     * Sets spinIO to true, causing ProcessNextFrame to spin rather than
+     * block.
+     * Set to true before using a tunnel proxy that recieved a non-piggybacked
+     * message.
+     * *ADDED by Tunnel Team *
+     */
+    public void setSpinIO(boolean spin) {
+	spinIO = spin;
+    }
 
     // Implementation of method declared in Session
     protected void disableIO()
@@ -231,7 +281,10 @@
                     }
                 };
 
-            thread.setDaemon(true);
+	    // *ADDED by Tunnel Team *
+	    // Why does this need to be a daemon, other than to keep it 
+	    // alive after the server is killed?
+	    //	    thread.setDaemon(true);
             thread.start();
         }
     }
@@ -421,7 +474,9 @@
         try {
             InputStream is = socket.getInputStream();
 
-            while (running) {
+	    // *CHANGED by Tunnel Team *
+	    // If we are set to process the next frame, and running, do so
+            while (running && doProcessNextFrame) {
                 if (Log.isLogged(Log.SEV_DEBUG_VERBOSE)) {
                     Log.logEntry(Log.SEV_DEBUG_VERBOSE, TCP_MAPPING,
                                  "Processing next frame");
@@ -430,8 +485,31 @@
                 int amountRead;
 
                 try {
-                    do {
-                        amountRead =
+		    // *CHANGED by Tunnel Team *
+		    // We need to use nonblocking reads so that the thread
+		    // can be killed before it has read the data off of the
+		    // socket. It would be annoying to use something like a
+		    // buffered inputstream, because a tunnel needs the socket
+		    // in addition to the streams, and you can't create a 
+		    // socket to use a buffered input stream by default.       
+		    do {
+			// this is quite possibly the nastiest hack ever,
+			// it is the worst way to do this, but is necessary 
+			// so that this thread can be killed before
+			// it reads data that will be passed to the tunnel.
+			if(spinIO){
+			    while(is.available() == 0) {
+				Thread.sleep(50);
+			    }
+			}
+			
+			// if we're not supposed to process the next frame
+			// then return
+			if(!doProcessNextFrame) {
+			    return;
+			}
+
+			amountRead =
                             is.read(headerBuffer, 0, MIN_SEQ_HEADER_SIZE);
 
                         if (amountRead == -1) {