Skip to content

Commit 1a5266b

Browse files
committed
Performs progressive wait to perform remote connect if effort fails.
Start at 2 seconds, doubling until a max of 30 seconds is reached, then exception thrown. Essentially, if ithis is actuated something is probably wrong, but at least one sees the failures writ large.
1 parent 9ced957 commit 1a5266b

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

src/main/java/org/ros/internal/node/server/ServerInvokeMethod.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import org.apache.commons.logging.Log;
66
import org.apache.commons.logging.LogFactory;
7-
import org.ros.internal.transport.tcp.TcpRosServer;
87

98
/**
109
* The remote call mechanism depends on Java reflection to provide access to methods that can be
@@ -13,8 +12,8 @@
1312
* This class handles reflection of the user requests to call designated methods in the server side classes,
1413
* It starts by populating a table of those methods, and at runtime, creates a method call transport for client,
1514
* and provides for server-side invocation of those methods.
16-
* Option to skip leading arguments for whatever reason is provided.
17-
* @author Groff Copyright (C) NeoCoreTechs 1998-2000, 2015
15+
* Option to skip leading arguments for whatever reason is provided.
16+
* @author Groff Copyright (C) NeoCoreTechs 1998-2000, 2015, 2017
1817
*/
1918
public final class ServerInvokeMethod {
2019
private static final boolean DEBUG = false;
@@ -30,7 +29,7 @@ public final class ServerInvokeMethod {
3029
* designated class. Reflect hierarchy in reverse (to get proper
3130
* overload) and look for methods
3231
* @param tclass The class name we are targeting
33-
* @param skipArgs > 0 if we want to skip first args.
32+
* @param tskipArgs > 0 if we want to skip first args.
3433
*/
3534
public ServerInvokeMethod(String tclass, int tskipArgs) throws ClassNotFoundException {
3635
pkmnap.classClass = Class.forName(tclass);
@@ -41,9 +40,9 @@ public ServerInvokeMethod(String tclass, int tskipArgs) throws ClassNotFoundExce
4140
Method m[];
4241
m = pkmnap.classClass.getMethods();
4342
for(int i = m.length-1; i >= 0 ; i--) {
44-
//if( m[i].getName().startsWith("Relatrix_") ) {
45-
pkmnap.methodNames.add(m[i].getName()/*.substring(9)*/);
46-
log.info("Method :"+m[i].getName()/*.substring(9)*/);
43+
//if( m[i].getName().startsWith("Reflect_") ) {
44+
pkmnap.methodNames.add(m[i].getName()/*.substring(8)*/);
45+
log.info("Method :"+m[i].getName()/*.substring(8)*/);
4746
//}
4847
}
4948
// create arrays
@@ -54,14 +53,14 @@ public ServerInvokeMethod(String tclass, int tskipArgs) throws ClassNotFoundExce
5453
int methCnt = 0;
5554
//
5655
for(int i = m.length-1; i >= 0 ; i--) {
57-
//if( m[i].getName().startsWith("Relatrix_") ) {
56+
//if( m[i].getName().startsWith("Reflect_") ) {
5857
pkmnap.methodParams[methCnt] = m[i].getParameterTypes();
5958
pkmnap.methodSigs[methCnt] = m[i].toString();
6059
pkmnap.returnTypes[methCnt] = m[i].getReturnType();
6160
if( pkmnap.returnTypes[methCnt] == void.class )
6261
pkmnap.returnTypes[methCnt] = Void.class;
63-
//int ind1 = pkmnap.methodSigs[methCnt].indexOf("Relatrix_");
64-
//pkmnap.methodSigs[methCnt] = pkmnap.methodSigs[methCnt].substring(0,ind1)+pkmnap.methodSigs[methCnt].substring(ind1+9);
62+
//int ind1 = pkmnap.methodSigs[methCnt].indexOf("Reflect_");
63+
//pkmnap.methodSigs[methCnt] = pkmnap.methodSigs[methCnt].substring(0,ind1)+pkmnap.methodSigs[methCnt].substring(ind1+8);
6564
if( skipArgs > 0) {
6665
try {
6766
int ind1 = pkmnap.methodSigs[methCnt].indexOf("(");
@@ -88,18 +87,20 @@ public Object invokeMethod(RemoteRequestInterface tmc) throws Exception {
8887
return invokeMethod(tmc, null);
8988
}
9089
/**
91-
* For an incoming RelatrixStatement, verify and invoke the proper
92-
* method. We assume there is a table of class names and this and
93-
* it has been used to locate this object.
90+
* For an incoming RemoteRequestInterface implementor of a concrete composition, verify and invoke the proper
91+
* method. We assume there is a table of class names and method params properly populated. Compare the
92+
* method signatures in the RemoteRequest to the table, if a match, invoke the method on localObject.
93+
* If skipArgs, a class variable, is not zero, the first number of skipArgs parameters in the method
94+
* described by RemoteRequest will not be compared against table entry, but the values will be passed into
95+
* the method upon invocation.
9496
* @return Object of result of method invocation
9597
*/
9698
public Object invokeMethod(RemoteRequestInterface tmc, Object localObject) throws Exception {
97-
//NoSuchMethodException, InvocationTargetException, IllegalAccessException, PowerSpaceException {
99+
//NoSuchMethodException, InvocationTargetException, IllegalAccessException {
98100
String targetMethod = tmc.getMethodName();
99101
int methodIndex = pkmnap.methodNames.indexOf(targetMethod);
100102
String whyNotFound = "No such method";
101103
while( methodIndex != -1 && methodIndex < pkmnap.methodNames.size()) {
102-
// System.out.println(jj);
103104
Class[] params = tmc.getParams();
104105
//
105106
//

src/main/java/org/ros/internal/transport/ChannelHandlerContextImpl.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import java.io.OutputStream;
88
import java.net.Socket;
99
import java.net.SocketAddress;
10-
1110
import java.nio.channels.CompletionHandler;
12-
1311
import java.util.HashSet;
1412
import java.util.Set;
1513
import java.util.concurrent.Executor;
1614

15+
import org.apache.commons.logging.Log;
16+
import org.apache.commons.logging.LogFactory;
1717
import org.ros.internal.transport.tcp.ChannelGroup;
1818

19+
1920
/**
2021
* A handler context contains all the executor, the channel group, the channel, and the pipeline with the handlers.
2122
* There is one channel per context.
@@ -32,6 +33,8 @@
3233
*
3334
*/
3435
public class ChannelHandlerContextImpl implements ChannelHandlerContext {
36+
private static final boolean DEBUG = false;
37+
private static final Log log = LogFactory.getLog(ChannelHandlerContextImpl.class);
3538
/*Asynchronous*/ChannelGroup channelGroup;
3639
Executor executor;
3740
/*Asynchronous*/Socket/*Channel*/ channel;
@@ -53,8 +56,7 @@ public ChannelHandlerContextImpl(/*Asynchronous*/ChannelGroup channelGroup2, /*A
5356
public void setChannel(/*Asynchronous*/Socket/*Channel*/ sock) {
5457
this.channel = sock;
5558
}
56-
57-
59+
5860
@Override
5961
public Executor executor() {
6062
return executor;
@@ -73,10 +75,24 @@ public String name() {
7375

7476
@Override
7577
public void connect(SocketAddress remoteAddress) throws IOException {
76-
channel.connect(remoteAddress);
77-
is = channel.getInputStream();
78-
os = channel.getOutputStream();
79-
78+
int try_num = 1;
79+
int secondsMax = 30;
80+
int secondsToWait = 1;
81+
while(secondsToWait < secondsMax) {
82+
try {
83+
channel.connect(remoteAddress);
84+
//is = channel.getInputStream();
85+
//os = channel.getOutputStream();
86+
return;
87+
} catch(Exception e) { }
88+
secondsToWait = (int) Math.min(secondsMax, Math.pow(2, try_num++));
89+
log.info("Failed to connect to remote address "+remoteAddress+", waiting "+secondsToWait+" seconds for retry...");
90+
try {
91+
Thread.sleep(secondsToWait*1000);
92+
} catch (InterruptedException e) {}
93+
}
94+
// Exceeded maximum wait, bomb
95+
throw new IOException("failed to connect to remote address "+remoteAddress+" after maximum retries");
8096
}
8197

8298
@Override
@@ -99,6 +115,7 @@ public void close() throws IOException {
99115

100116
@Override
101117
public Object read() throws IOException {
118+
is = channel.getInputStream();
102119
ObjectInputStream ois = new ObjectInputStream(is);
103120
try {
104121
return ois.readObject();
@@ -109,6 +126,7 @@ public Object read() throws IOException {
109126

110127
@Override
111128
public void write(Object msg) throws IOException {
129+
os = channel.getOutputStream();
112130
ObjectOutputStream oos = new ObjectOutputStream(os);
113131
oos.writeObject(msg);
114132
oos.flush();

0 commit comments

Comments
 (0)