Skip to content

Commit 6830151

Browse files
committed
Added logic to remove sockets once they close. Same goes for sessions.
Had to bypass bug in Java_WebSocketServer that prevents it from unbinding the port. TODO: Reintroduce retry logic test for SOAP.
1 parent e175eda commit 6830151

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public Feature findFeatureByRequest(Request request) {
7676
@Override
7777
public void handleConfirmation(String uniqueId, Confirmation confirmation) {
7878
getPromise(uniqueId).complete(confirmation);
79+
removePromise(uniqueId);
7980
}
8081

8182
@Override
@@ -87,6 +88,7 @@ public Confirmation handleRequest(Request request) {
8788
@Override
8889
public void handleError(String uniqueId, String errorCode, String errorDescription, Object payload) {
8990
getPromise(uniqueId).completeExceptionally(new CallErrorException(errorCode, errorCode, payload));
91+
removePromise(uniqueId);
9092
}
9193

9294
@Override

ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/FakeCentralSystem.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class FakeCentralSystem
4444
private Confirmation receivedConfirmation;
4545
private Server server;
4646

47-
private int sessionIndex;
47+
private int currentSessionIndex;
4848

4949
private static FakeCentralSystem instance;
5050
private boolean isRigged;
@@ -172,24 +172,27 @@ public StopTransactionConfirmation handleStopTransactionRequest(int sessionIndex
172172
}
173173
});
174174

175+
int port = 0;
175176
switch (type) {
176177
case JSON:
177178
server = new JSONServer(serverCoreProfile);
179+
port = 8887;
178180
break;
179181
case SOAP:
180182
server = new SOAPServer(serverCoreProfile);
183+
port = 8890;
181184
break;
182185
}
183186

184-
server.open("localhost", 8887, new ServerEvents() {
187+
server.open("localhost", port, new ServerEvents() {
185188
@Override
186189
public void newSession(int sessionIndex) {
187-
FakeCentralSystem.this.sessionIndex = sessionIndex;
190+
currentSessionIndex = sessionIndex;
188191
}
189192

190193
@Override
191194
public void lostSession(int identity) {
192-
sessionIndex = -1;
195+
currentSessionIndex = -1;
193196
// clear
194197
receivedConfirmation = null;
195198
receivedRequest = null;
@@ -219,7 +222,7 @@ public void sendChangeAvailabilityRequest(int connectorId, AvailabilityType type
219222
ChangeAvailabilityRequest request = new ChangeAvailabilityRequest();
220223
request.setType(type);
221224
request.setConnectorId(connectorId);
222-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
225+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
223226

224227
}
225228

@@ -234,7 +237,7 @@ public void sendChangeConfigurationRequest(String key, String value) throws Exce
234237
ChangeConfigurationRequest request = new ChangeConfigurationRequest();
235238
request.setKey(key);
236239
request.setValue(value);
237-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
240+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
238241
}
239242

240243
public boolean hasReceivedChangeConfigurationConfirmation() {
@@ -243,7 +246,7 @@ public boolean hasReceivedChangeConfigurationConfirmation() {
243246

244247
public void sendClearCacheRequest() throws Exception {
245248
ClearCacheRequest request = new ClearCacheRequest();
246-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
249+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
247250
}
248251

249252
public boolean hasReceivedClearCacheConfirmation() {
@@ -255,7 +258,7 @@ public void sendDataTransferRequest(String vendorId, String messageId, String da
255258
request.setVendorId(vendorId);
256259
request.setMessageId(messageId);
257260
request.setData(data);
258-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
261+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
259262
}
260263

261264
public boolean hasReceivedDataTransferConfirmation() {
@@ -269,7 +272,7 @@ public boolean hasHandledDataTransferRequest() {
269272
public void sendGetConfigurationRequest(String... key) throws Exception {
270273
GetConfigurationRequest request = new GetConfigurationRequest();
271274
request.setKey(key);
272-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
275+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
273276
}
274277

275278
public boolean hasReceivedGetConfigurationConfirmation() {
@@ -290,7 +293,7 @@ public void sendRemoteStartTransactionRequest(int connectorId, String idTag) thr
290293
idToken.setIdToken(idTag);
291294
request.setIdTag(idToken);
292295
request.setConnectorId(connectorId);
293-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
296+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
294297
}
295298

296299
public boolean hasReceivedRemoteStartTransactionConfirmation(String status) {
@@ -303,7 +306,7 @@ public boolean hasReceivedRemoteStartTransactionConfirmation(String status) {
303306
public void sendRemoteStopTransactionRequest(int transactionId) throws Exception {
304307
RemoteStopTransactionRequest request = new RemoteStopTransactionRequest();
305308
request.setTransactionId(transactionId);
306-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
309+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
307310
}
308311

309312
public boolean hasReceivedRemoteStopTransactionConfirmation(String status) {
@@ -316,7 +319,7 @@ public boolean hasReceivedRemoteStopTransactionConfirmation(String status) {
316319
public void sendResetRequest(ResetType type) throws Exception {
317320
ResetRequest request = new ResetRequest();
318321
request.setType(type);
319-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
322+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
320323
}
321324

322325
public boolean hasReceivedResetConfirmation(String status) {
@@ -341,7 +344,7 @@ public boolean hasHandledStopTransactionRequest() {
341344
public void sendUnlockConnectorRequest(int connectorId) throws Exception {
342345
UnlockConnectorRequest request = new UnlockConnectorRequest();
343346
request.setConnectorId(connectorId);
344-
server.send(sessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
347+
server.send(currentSessionIndex, request).whenComplete((confirmation, throwable) -> receivedConfirmation = confirmation);
345348
}
346349

347350
public boolean hasReceivedUnlockConnectorConfirmation(String status) {

ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/FakeChargePoint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class FakeChargePoint
4444
private Request receivedRequest;
4545
private ClientCoreProfile core;
4646
private Throwable receivedException;
47+
private String url;
4748

4849
public FakeChargePoint() throws MalformedURLException {
4950
this(clientType.JSON);
@@ -119,16 +120,18 @@ public UnlockConnectorConfirmation handleUnlockConnectorRequest(UnlockConnectorR
119120
switch (type) {
120121
case JSON:
121122
client = new JSONClient(core);
123+
url = "ws://localhost:8887";
122124
break;
123125
case SOAP:
124126
client = new SOAPClient("me", new URL("http://localhost:8889"), core);
127+
url = "http://localhost:8890";
125128
break;
126129
}
127130
}
128131

129132
public void connect() {
130133
try {
131-
client.connect("http://localhost:8887");
134+
client.connect(url);
132135
} catch (Exception ex) {
133136
ex.printStackTrace();
134137
}

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/soap/SOAPChangeAvailabilitySpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SOAPChangeAvailabilitySpec extends Specification {
2020

2121
def setup() {
2222
chargePoint.connect()
23+
chargePoint.sendBootNotification("VendorX", "SingleSocketCharger")
2324
}
2425

2526
def cleanup() {
@@ -29,7 +30,6 @@ class SOAPChangeAvailabilitySpec extends Specification {
2930
def "Central System sends a ChangeAvailability request and receives a response"() {
3031
def conditions = new PollingConditions(timeout: 1)
3132
when:
32-
chargePoint.sendBootNotification("VendorX", "SingleSocketCharger")
3333
centralSystem.sendChangeAvailabilityRequest(1, AvailabilityType.Inoperative)
3434

3535
then:

ocpp-v1_6-test/src/test/groovy/eu/chargetime/ocpp/test/core/soap/SOAPStopTransactionSpec.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class SOAPStopTransactionSpec extends Specification {
4141
}
4242
}
4343

44+
// TODO
45+
/*
4446
def "StopTransaction request is stored when offline"() {
4547
def conditions = new PollingConditions(initialDelay: 0.5, timeout: 1)
4648
@@ -64,5 +66,5 @@ class SOAPStopTransactionSpec extends Specification {
6466
assert centralSystem.hasHandledStopTransactionRequest()
6567
}
6668
}
67-
69+
*/
6870
}

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/WebServiceListener.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ of this software and associated documentation files (the "Software"), to deal
2727

2828
import com.sun.net.httpserver.HttpServer;
2929

30-
import javax.xml.soap.SOAPException;
3130
import javax.xml.soap.SOAPMessage;
32-
import java.io.ByteArrayOutputStream;
3331
import java.io.IOException;
3432
import java.net.InetSocketAddress;
3533
import java.util.HashMap;
@@ -93,17 +91,6 @@ public SOAPMessage incomingRequest(SOAPMessage message) {
9391
e.printStackTrace();
9492
}
9593

96-
try {
97-
ByteArrayOutputStream out = new ByteArrayOutputStream();
98-
message.writeTo(out);
99-
String strMsg = new String(out.toByteArray());
100-
System.out.print(strMsg);
101-
} catch (SOAPException e) {
102-
e.printStackTrace();
103-
} catch (IOException e) {
104-
e.printStackTrace();
105-
}
106-
10794
return confirmation;
10895
}
10996
}

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/WebServiceReceiver.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ void forwardMessage(SOAPMessage message) {
6666

6767
@Override
6868
void sendRequest(SOAPMessage message) {
69-
try {
70-
events.receivedMessage(soapConnection.call(message, url));
71-
} catch (SOAPException e) {
72-
e.printStackTrace();
73-
}
69+
new Thread(() -> {
70+
try {
71+
events.receivedMessage(soapConnection.call(message, url));
72+
} catch (SOAPException e) {
73+
e.printStackTrace();
74+
}
75+
}).start();
7476
}
7577
}

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/WebSocketListener.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
5555
@Override
5656
public void onClose(WebSocket webSocket, int i, String s, boolean b) {
5757
sockets.get(webSocket).disconnect();
58+
sockets.remove(webSocket);
5859
}
5960

6061
@Override
@@ -73,7 +74,14 @@ public void onError(WebSocket webSocket, Exception e) {
7374
@Override
7475
public void close() {
7576
try {
76-
server.stop();
77+
78+
for (WebSocket ws : sockets.keySet())
79+
ws.close();
80+
81+
sockets.clear();
82+
83+
server.stop(1);
84+
7785
} catch (IOException e) {
7886
e.printStackTrace();
7987
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)