JBoss Remoting SVN: r5304 - remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket.
by jboss-remoting-commits@lists.jboss.org
Author: david.lloyd(a)jboss.com
Date: 2009-07-07 20:09:13 -0400 (Tue, 07 Jul 2009)
New Revision: 5304
Modified:
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/Bisocket.java
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketClientInvoker.java
remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketServerInvoker.java
Log:
JBREM-1140 - Add an "enablePingReplies" config option which causes the server to reply to bisocket control socket pings
Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/Bisocket.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/Bisocket.java 2009-07-08 00:05:49 UTC (rev 5303)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/Bisocket.java 2009-07-08 00:09:13 UTC (rev 5304)
@@ -53,8 +53,15 @@
*/
public static final String PING_WINDOW_FACTOR = "pingWindowFactor";
public static final int PING_WINDOW_FACTOR_DEFAULT = 2;
-
+
/**
+ * Configuration key for enabling ping replies, so that the client can more
+ * actively detect connection death. Must be enabled on both client and
+ * server.
+ */
+ public static final String ENABLE_PING_REPLIES = "enablePingReplies";
+
+ /**
* Configuration key and default value for number of retries
* BisocketServerInvoker.ControlConnectionThread and
* BisocketServerInvoker.createControlConnection should attempt while creating
Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketClientInvoker.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketClientInvoker.java 2009-07-08 00:05:49 UTC (rev 5303)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketClientInvoker.java 2009-07-08 00:09:13 UTC (rev 5304)
@@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.OutputStream;
+import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Collections;
@@ -84,12 +85,13 @@
private int maxRetries = Bisocket.MAX_RETRIES_DEFAULT;
private Socket controlSocket;
private OutputStream controlOutputStream;
+ private InputStream controlInputStream;
private Object controlLock = new Object();
private PingTimerTask pingTimerTask;
protected boolean isCallbackInvoker;
protected BooleanHolder pingFailed = new BooleanHolder(false);
+ private boolean enablePingReplies = false;
-
/**
* @param listenerId
* @return
@@ -201,6 +203,14 @@
" value of " + val + " to an int value.");
}
}
+
+ val = configuration.get(Bisocket.ENABLE_PING_REPLIES);
+ if (val != null)
+ {
+ // parseBoolean doesn't throw exceptions :-)
+ boolean bVal = Boolean.parseBoolean((String) val);
+ enablePingReplies = bVal;
+ }
}
}
@@ -277,6 +287,7 @@
try
{
controlOutputStream = controlSocket.getOutputStream();
+ controlInputStream = controlSocket.getInputStream();
}
catch (IOException e1)
{
@@ -522,6 +533,7 @@
controlSocket = socket;
log.debug(this + " control socket replaced by: " + socket);
controlOutputStream = controlSocket.getOutputStream();
+ controlInputStream = controlSocket.getInputStream();
}
if (pingTimerTask != null)
@@ -604,18 +616,16 @@
{
private Object controlLock;
private OutputStream controlOutputStream;
- private int maxRetries;
- private Exception savedException;
- private boolean pingSent;
+ private InputStream controlInputStream;
private BooleanHolder pingFailed;
-
+ private boolean enablePingReplies;
+
PingTimerTask(BisocketClientInvoker invoker)
{
controlLock = invoker.controlLock;
controlOutputStream = invoker.controlOutputStream;
- maxRetries = invoker.getMaxRetries();
- pingFailed = invoker.pingFailed;
- pingFailed.flag = false;
+ controlInputStream = invoker.controlInputStream;
+ enablePingReplies = invoker.enablePingReplies;
}
public void shutDown()
@@ -623,6 +633,7 @@
synchronized (controlLock)
{
controlOutputStream = null;
+ controlInputStream = null;
}
cancel();
try
@@ -637,42 +648,54 @@
}
public void run()
- {
- pingSent = false;
-
- for (int i = 0; i < maxRetries; i++)
+ {
+ boolean ok = false;
+ try
{
- try
+ synchronized (controlLock)
{
- synchronized (controlLock)
+ if (controlOutputStream == null || controlInputStream == null)
+ return;
+
+ controlOutputStream.write(Bisocket.PING);
+ if (enablePingReplies)
{
- if (controlOutputStream == null)
+ int rep = controlInputStream.read();
+ if (rep == -1)
+ {
+ // socket was closed!
+ shutDown();
return;
-
- controlOutputStream.write(Bisocket.PING);
+ }
+ if (rep != Bisocket.PING)
+ {
+ // um - protocol error
+ log.warn("Protocol error: received unexpected reply to ping on control socket (are ping replies enabled on the server?); shutting down PingTimerTask");
+ shutDown();
+ return;
+ }
}
- pingSent = true;
- break;
+ ok = true;
}
- catch (Exception e)
+ }
+ catch (Exception e)
+ {
+ log.warn("Unable to send ping: shutting down PingTimerTask", e);
+ }
+ finally
+ {
+ if (! ok)
{
- savedException = e;
- log.debug("Unable to send ping: trying again");
+ pingFailed.flag = true;
+ shutDown();
}
}
-
- if (!pingSent)
- {
- log.warn("Unable to send ping: shutting down PingTimerTask", savedException);
- pingFailed.flag = true;
- shutDown();
- }
}
}
static class BooleanHolder
{
- public boolean flag;
+ public volatile boolean flag;
public BooleanHolder(boolean flag)
{
Modified: remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketServerInvoker.java
===================================================================
--- remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketServerInvoker.java 2009-07-08 00:05:49 UTC (rev 5303)
+++ remoting2/branches/2.2/src/main/org/jboss/remoting/transport/bisocket/BisocketServerInvoker.java 2009-07-08 00:09:13 UTC (rev 5304)
@@ -82,6 +82,7 @@
private int socketCreationRetries = Bisocket.MAX_RETRIES_DEFAULT;
private int controlConnectionRestarts = Bisocket.MAX_CONTROL_CONNECTION_RESTARTS_DEFAULT;
private ControlMonitorTimerTask controlMonitorTimerTask;
+ private boolean enablePingReplies = false;
protected boolean isCallbackServer = false;
protected int secondaryBindPort = -1;
protected int secondaryConnectPort = -1;
@@ -202,6 +203,13 @@
secondaryServerSocketThread.start();
log.debug("started secondary port: " + host + ":" + secondaryBindPort);
}
+ Object val = configuration.get(Bisocket.ENABLE_PING_REPLIES);
+ if (val != null)
+ {
+ // parseBoolean doesn't throw exceptions :-)
+ boolean bVal = Boolean.parseBoolean((String) val);
+ enablePingReplies = bVal;
+ }
}
@@ -799,6 +807,10 @@
break;
case Bisocket.PING:
+ if (enablePingReplies)
+ {
+ socket.getOutputStream().write(Bisocket.PING);
+ }
continue;
case -1: