Skip to content

Commit e778fd0

Browse files
committedOct 9, 2017
Fix half of #8, an exception is thrown we can't send a message.
Interface.popDispatch now throw an exception if Combox.send length is less than 0, which mean that something went wrong somewhere. General cleanup an class fixes.
1 parent dad4160 commit e778fd0

9 files changed

+63
-53
lines changed
 

‎workspace/popjava/src/popjava/base/MessageHeader.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
*/
77
public class MessageHeader {
88

9-
static public final int REQUEST = 0;
10-
static public final int RESPONSE = 1;
11-
static public final int EXCEPTION = 2;
9+
public static final int REQUEST = 0;
10+
public static final int RESPONSE = 1;
11+
public static final int EXCEPTION = 2;
1212

13-
static public final int BIND_STATUS_CALL = 0;
14-
static public final int ADD_REF_CALL = 1;
15-
static public final int DEC_REF_CALL = 2;
16-
static public final int GET_ENCODING_CALL = 3;
17-
static public final int KILL_ALL = 4;
18-
static public final int OBJECT_ALIVE_CALL = 5;
19-
20-
static public final int HEADER_LENGTH = 24;
13+
public static final int BIND_STATUS_CALL = 0;
14+
public static final int ADD_REF_CALL = 1;
15+
public static final int DEC_REF_CALL = 2;
16+
public static final int GET_ENCODING_CALL = 3;
17+
public static final int KILL_ALL = 4;
18+
public static final int OBJECT_ALIVE_CALL = 5;
19+
20+
public static final int HEADER_LENGTH = 24;
2121

2222
protected int requestType;
2323
protected int classId;

‎workspace/popjava/src/popjava/base/POPException.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static POPException createReflectMethodNotFoundException(
128128
*/
129129
public static POPException throwBufferNotAvailableException()
130130
throws POPException {
131-
throw new POPException(POPErrorCode.POP_BUFFER_NOT_AVAILABLE,
131+
return new POPException(POPErrorCode.POP_BUFFER_NOT_AVAILABLE,
132132
"The buffer hasn't been initialized");
133133
}
134134

@@ -139,7 +139,7 @@ public static POPException throwBufferNotAvailableException()
139139
*/
140140
public static POPException throwComboxNotAvailableException()
141141
throws POPException {
142-
throw new POPException(POPErrorCode.POP_COMBOX_NOT_AVAILABLE,
142+
return new POPException(POPErrorCode.POP_COMBOX_NOT_AVAILABLE,
143143
"The combox hasn't been initialized");
144144
}
145145

@@ -156,7 +156,7 @@ public static POPException throwAccessPointNotAvailableException(POPAccessPoint
156156
info = accesspoint.toString();
157157
}
158158

159-
throw new POPException(POPErrorCode.POP_ACCESSPOINT_NOT_AVAILABLE,
159+
return new POPException(POPErrorCode.POP_ACCESSPOINT_NOT_AVAILABLE,
160160
"The accesspoint hasn't been initialized: "+info);
161161
}
162162

@@ -166,7 +166,7 @@ public static POPException throwAccessPointNotAvailableException(POPAccessPoint
166166
* @throws POPException exception thrown by this method
167167
*/
168168
public static POPException throwNullObjectNotAllowException() throws POPException {
169-
throw new POPException(POPErrorCode.NOT_ALLOW_PUT_NULL_OBJECT_TP_BUFFER,
169+
return new POPException(POPErrorCode.NOT_ALLOW_PUT_NULL_OBJECT_TP_BUFFER,
170170
"Not allowed to put null object to buffer except a null array");
171171
}
172172

‎workspace/popjava/src/popjava/broker/Broker.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ private boolean invokeConstructor(Request request) {
268268

269269
POPClass annotation = popObject.getClass().getAnnotation(POPClass.class);
270270
if(annotation != null){
271-
for (ComboxServer comboxServer : comboxServers) {
272-
comboxServer.getRequestQueue().setMaxQueue(annotation.maxRequestQueue());
273-
}
271+
requestQueue.setMaxQueue(annotation.maxRequestQueue());
274272
}
275273
} catch (Exception e) {
276274
exception = POPException.createReflectException(
@@ -978,7 +976,7 @@ public boolean initialize(List<String> argvs) {
978976
}
979977
}
980978

981-
comboxServers = liveServers.toArray(new ComboxServer[0]);
979+
comboxServers = liveServers.toArray(new ComboxServer[liveServers.size()]);
982980
return true;
983981
}
984982

‎workspace/popjava/src/popjava/combox/socket/ComboxSocket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ public int send(POPBuffer buffer) {
251251
return length;
252252
} catch (IOException e) {
253253
if (conf.isDebugCombox()){
254-
e.printStackTrace();
255254
LogWriter.writeDebugInfo(this.getClass().getName()
256255
+ "-Send: Error while sending data - " + e.getMessage() +" "+outputStream);
256+
LogWriter.writeExceptionLog(e);
257257
}
258258
return -1;
259259
}

‎workspace/popjava/src/popjava/combox/socket/ComboxSocketFactory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import popjava.combox.ComboxAllocate;
99
import popjava.combox.ComboxFactory;
1010
import popjava.combox.ComboxServer;
11+
import popjava.util.Configuration;
1112

1213
/**
1314
* This class is the factory for all combox socket
@@ -17,6 +18,7 @@ public class ComboxSocketFactory extends ComboxFactory {
1718
* Name of the implemented protocol
1819
*/
1920
public static final String PROTOCOL = "socket";
21+
private static final Configuration conf = Configuration.getInstance();
2022

2123
@Override
2224
public String getComboxName() {
@@ -25,7 +27,7 @@ public String getComboxName() {
2527

2628
@Override
2729
public Combox createClientCombox(POPAccessPoint accessPoint) {
28-
return new ComboxSocket(accessPoint, 0);
30+
return createClientCombox(accessPoint, conf.getConnectionTimeout());
2931
}
3032

3133
@Override
@@ -36,7 +38,7 @@ public Combox createClientCombox(POPAccessPoint accessPoint, int timeout) {
3638
@Override
3739
public ComboxServer createServerCombox(AccessPoint accessPoint,
3840
POPBuffer buffer, Broker broker) {
39-
return new ComboxServerSocket(accessPoint, 0, buffer, broker);
41+
return createServerCombox(accessPoint, conf.getConnectionTimeout(), buffer, broker);
4042
}
4143

4244
@Override

‎workspace/popjava/src/popjava/combox/ssl/ComboxAcceptSecureSocket.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import popjava.broker.Broker;
44
import popjava.broker.RequestQueue;
55
import popjava.util.LogWriter;
6+
import popjava.combox.ComboxReceiveRequest;
67

7-
import java.net.*;
8+
import java.io.IOException;
89
import java.util.LinkedList;
9-
import java.io.*;
10-
import popjava.combox.ComboxReceiveRequest;
10+
import javax.net.ssl.SSLServerSocket;
11+
import javax.net.ssl.SSLSocket;
1112

1213
/**
1314
* This class is responsible to accept the new connection for the associated server combox socket
@@ -21,19 +22,19 @@ public class ComboxAcceptSecureSocket implements Runnable {
2122

2223
protected Broker broker;
2324
protected RequestQueue requestQueue;
24-
protected ServerSocket serverSocket;
25+
protected SSLServerSocket serverSocket;
2526
protected int status = EXIT;
26-
protected final LinkedList<Socket> concurentConnections = new LinkedList<>();
27+
protected final LinkedList<SSLSocket> concurentConnections = new LinkedList<>();
2728

2829
/**
2930
* Create a new instance of the ComboxAccept socket
3031
* @param broker The associated broker
3132
* @param requestQueue The associated request queue
32-
* @param socket The associated combox socket
33+
* @param serverSocket The associated combox socket
3334
*/
3435
public ComboxAcceptSecureSocket(Broker broker, RequestQueue requestQueue,
35-
ServerSocket socket) {
36-
serverSocket = socket;
36+
SSLServerSocket serverSocket) {
37+
this.serverSocket = serverSocket;
3738
this.broker = broker;
3839
this.requestQueue = requestQueue;
3940
}
@@ -43,34 +44,39 @@ public ComboxAcceptSecureSocket(Broker broker, RequestQueue requestQueue,
4344
*/
4445
public void run() {
4546
while (status != EXIT) {
46-
Socket connection = null;
47+
SSLSocket connection = null;
4748
try {
48-
connection = serverSocket.accept();
49-
LogWriter.writeDebugInfo("Connection accepted "+connection.getLocalPort()+" local:"+connection.getPort());
49+
connection = (SSLSocket) serverSocket.accept();
50+
LogWriter.writeDebugInfo("[SSL Accept] Connection accepted "+connection.getLocalPort()+" local:"+connection.getPort());
5051
if(broker != null){
5152
broker.onNewConnection();
5253
}
54+
55+
// force srart of handshake from server side
56+
connection.startHandshake();
57+
5358
synchronized (concurentConnections) {
5459
concurentConnections.add(connection);
5560
}
5661

5762
Runnable runnable = new ComboxReceiveRequest(broker, requestQueue, new ComboxSecureSocket(connection));
5863
Thread thread = new Thread(runnable, "Combox request acceptance");
5964
thread.start();
60-
} catch (IOException e) {
65+
} catch (IOException e) {
66+
LogWriter.writeDebugInfo("[SSL Accept] Error while setting up connection: %s", e.getMessage());
6167
break;
6268
}
6369
}
6470

65-
LogWriter.writeDebugInfo("Combox Server finished");
71+
LogWriter.writeDebugInfo("[SSL Accept] Combox Server finished");
6672
this.close();
6773
}
6874

6975
/**
7076
* Close the current connection
7177
*/
7278
public void close() {
73-
for (Socket s : concurentConnections) {
79+
for (SSLSocket s : concurentConnections) {
7480
try {
7581
s.close();
7682
} catch (IOException e) {

‎workspace/popjava/src/popjava/combox/ssl/ComboxSecureSocket.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.io.InputStream;
88
import java.io.OutputStream;
99
import java.net.InetSocketAddress;
10-
import java.net.Socket;
1110
import java.net.SocketAddress;
1211
import java.security.cert.Certificate;
1312
import javax.net.ssl.SSLContext;
@@ -28,7 +27,7 @@
2827
*/
2928
public class ComboxSecureSocket extends Combox {
3029

31-
protected Socket peerConnection = null;
30+
protected SSLSocket peerConnection = null;
3231
protected byte[] receivedBuffer;
3332
public static final int BUFFER_LENGTH = 1024 * 1024 * 8;
3433
protected InputStream inputStream = null;
@@ -43,7 +42,7 @@ public class ComboxSecureSocket extends Combox {
4342
* @param socket The socket to create the combox
4443
* @throws IOException Thrown is any IO exception occurred during the creation
4544
*/
46-
public ComboxSecureSocket(Socket socket) throws IOException {
45+
public ComboxSecureSocket(SSLSocket socket) throws IOException {
4746
peerConnection = socket;
4847
receivedBuffer = new byte[BUFFER_LENGTH];
4948
inputStream = new BufferedInputStream(peerConnection.getInputStream(), STREAM_BUFFER_SIZE);
@@ -84,12 +83,14 @@ public void close() {
8483
}finally{
8584
try {
8685
outputStream.close();
86+
} catch (IOException e) {}
87+
try {
8788
inputStream.close();
88-
if(peerConnection != null){
89+
} catch (IOException e) {}
90+
if(peerConnection != null){
91+
try {
8992
peerConnection.close();
90-
}
91-
} catch (IOException e) {
92-
LogWriter.writeExceptionLog(e);
93+
} catch (IOException e) {}
9394
}
9495
}
9596
}
@@ -119,13 +120,13 @@ public boolean connect() {
119120
if (timeOut > 0) {
120121
SocketAddress sockaddress = new InetSocketAddress(host,
121122
port);
122-
peerConnection = factory.createSocket();
123+
peerConnection = (SSLSocket) factory.createSocket();
123124
peerConnection.connect(sockaddress, timeOut);
124125

125126
//LogWriter.writeExceptionLog(new Exception());
126127
//LogWriter.writeExceptionLog(new Exception("Open connection to "+host+":"+port+" remote: "+peerConnection.getLocalPort()));
127128
} else {
128-
peerConnection = factory.createSocket(host, port);
129+
peerConnection = (SSLSocket) factory.createSocket(host, port);
129130
}
130131
inputStream = new BufferedInputStream(peerConnection.getInputStream());
131132
outputStream = new BufferedOutputStream(peerConnection.getOutputStream());

‎workspace/popjava/src/popjava/combox/ssl/ComboxSecureSocketFactory.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package popjava.combox.ssl;
22

3-
import popjava.util.ssl.SSLUtils;
43
import javax.net.ssl.SSLContext;
4+
import popjava.util.ssl.SSLUtils;
55
import popjava.baseobject.AccessPoint;
66
import popjava.baseobject.POPAccessPoint;
77
import popjava.broker.Broker;
@@ -10,6 +10,7 @@
1010
import popjava.combox.ComboxAllocate;
1111
import popjava.combox.ComboxFactory;
1212
import popjava.combox.ComboxServer;
13+
import popjava.util.Configuration;
1314
import popjava.util.LogWriter;
1415

1516
/**
@@ -21,6 +22,7 @@ public class ComboxSecureSocketFactory extends ComboxFactory {
2122
* Name of the implemented protocol
2223
*/
2324
public static final String PROTOCOL = "ssl";
25+
private static final Configuration conf = Configuration.getInstance();
2426

2527
@Override
2628
public String getComboxName() {
@@ -29,7 +31,7 @@ public String getComboxName() {
2931

3032
@Override
3133
public Combox createClientCombox(POPAccessPoint accessPoint) {
32-
return new ComboxSecureSocket(accessPoint, 0);
34+
return createClientCombox(accessPoint, conf.getConnectionTimeout());
3335
}
3436

3537
@Override
@@ -40,7 +42,7 @@ public Combox createClientCombox(POPAccessPoint accessPoint, int timeout) {
4042
@Override
4143
public ComboxServer createServerCombox(AccessPoint accessPoint,
4244
POPBuffer buffer, Broker broker) {
43-
return new ComboxServerSecureSocket(accessPoint, 0, buffer, broker);
45+
return createServerCombox(accessPoint, conf.getConnectionTimeout(), buffer, broker);
4446
}
4547

4648
@Override

‎workspace/popjava/src/popjava/interfacebase/Interface.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ protected boolean bind(POPAccessPoint accesspoint) throws POPException {
325325
switch (bindStatus.getCode()) {
326326
case BindStatus.BIND_OK:
327327
this.getOD().setPlatform(bindStatus.getPlatform());
328-
negotiateEncoding(conf.getSelectedEncoding(), bindStatus
329-
.getPlatform());
328+
negotiateEncoding(conf.getSelectedEncoding(), bindStatus.getPlatform());
330329
return true;
331330
case BindStatus.BIND_FORWARD_SESSION:
332331
case BindStatus.BIND_FORWARD_PERMANENT:
@@ -337,7 +336,7 @@ protected boolean bind(POPAccessPoint accesspoint) throws POPException {
337336
} else {
338337
POPException.throwObjectBindException(accesspoint);
339338
}
340-
339+
341340
return true;
342341
}
343342

@@ -362,7 +361,6 @@ private void bindStatus(BindStatus bindStatus) throws POPException {
362361
POPBuffer responseBuffer = combox.getBufferFactory().createBuffer();
363362
popResponse(responseBuffer, messageHeader.getRequestID());
364363
errorcode = responseBuffer.getInt();
365-
366364
bindStatus.setCode(errorcode);
367365
String platform = responseBuffer.getString();
368366
String info = responseBuffer.getString();
@@ -895,7 +893,10 @@ private static int localExec(String hostname, String codeFile,
895893
* @param buffer Buffer to send
896894
*/
897895
protected void popDispatch(POPBuffer buffer) {
898-
combox.send(buffer);
896+
int length = combox.send(buffer);
897+
if (length < 0) {
898+
throw POPException.throwComboxNotAvailableException();
899+
}
899900
}
900901

901902
/**

0 commit comments

Comments
 (0)
Please sign in to comment.