Skip to content

Commit b0e6c43

Browse files
committed
Merge branch 'FirmwareManagement'
# Conflicts: # ocpp-v1_6/src/test/java/eu/chargetime/ocpp/test/TimeoutSessionTest.java
2 parents 59f3aa6 + e0e1761 commit b0e6c43

File tree

13 files changed

+154
-51
lines changed

13 files changed

+154
-51
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ of this software and associated documentation files (the "Software"), to deal
2727

2828
public interface Listener {
2929
void open(String hostname, int port, ListenerEvents listenerEvents);
30-
3130
void close();
31+
32+
void setAsyncRequestHandler(boolean async);
3233
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package eu.chargetime.ocpp;/*
2+
ChargeTime.eu - Java-OCA-OCPP
3+
4+
MIT License
5+
6+
Copyright (C) 2016 Thomas Volden <[email protected]>
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
import eu.chargetime.ocpp.model.Confirmation;
28+
import eu.chargetime.ocpp.model.Request;
29+
30+
import java.util.concurrent.CompletableFuture;
31+
32+
abstract class RequestDispatcher {
33+
34+
protected SessionEvents eventHandler;
35+
36+
public CompletableFuture<Confirmation> handleRequest(Request request)
37+
{
38+
CompletableFuture<Confirmation> promise = new CompletableFuture<>();
39+
fulfillPromis(promise, request);
40+
return promise;
41+
}
42+
43+
protected abstract void fulfillPromis(CompletableFuture<Confirmation> promise, Request request);
44+
45+
public void setEventHandler(SessionEvents eventHandler) {
46+
this.eventHandler = eventHandler;
47+
}
48+
}
49+
50+
class AsyncRequestDispatcher extends SimpleRequestDispatcher {
51+
52+
@Override
53+
protected void fulfillPromis(CompletableFuture<Confirmation> promise, Request request) {
54+
new Thread(() -> {
55+
this.fulfillPromis(promise, request);
56+
}).start();
57+
}
58+
}
59+
60+
class SimpleRequestDispatcher extends RequestDispatcher {
61+
62+
@Override
63+
protected void fulfillPromis(CompletableFuture<Confirmation> promise, Request request) {
64+
try {
65+
Confirmation conf = eventHandler.handleRequest(request);
66+
promise.complete(conf);
67+
} catch (Exception ex) {
68+
ex.printStackTrace();
69+
promise.completeExceptionally(ex);
70+
}
71+
}
72+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ public CompletableFuture<Confirmation> send(UUID sessionIndex, Request request)
143143
* @param sessionIndex Session index of the client.
144144
*/
145145
public void closeSession(UUID sessionIndex) {
146-
sessions.get(sessionIndex).close();
146+
Session session = sessions.get(sessionIndex);
147+
if (session != null)
148+
session.close();
149+
}
150+
151+
public void setAsyncRequestHandler(boolean asyncRequestHandler) {
152+
listener.setAsyncRequestHandler(asyncRequestHandler);
147153
}
148154
}

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,24 @@ public class Session {
4141

4242
private Communicator communicator;
4343
private Queue queue;
44+
private RequestDispatcher dispatcher;
4445
private SessionEvents events;
4546

4647
/**
4748
* Handles required injections.
4849
*
49-
* @param communicator send and receive messages.
50-
* @param queue store and restore requests based on unique ids.
50+
* @param communicator send and receive messages.
51+
* @param queue store and restore requests based on unique ids.
52+
* @param handleRequestAsync toggle if requests are handled async or not.
5153
*/
52-
public Session(Communicator communicator, Queue queue) {
54+
public Session(Communicator communicator, Queue queue, boolean handleRequestAsync) {
5355
this.communicator = communicator;
5456
this.queue = queue;
57+
58+
if (handleRequestAsync)
59+
dispatcher = new AsyncRequestDispatcher();
60+
else
61+
dispatcher = new SimpleRequestDispatcher();
5562
}
5663

5764
/**
@@ -94,6 +101,7 @@ private Class<? extends Confirmation> getConfirmationType(String uniqueId) throw
94101
*/
95102
public void open(String uri, SessionEvents eventHandler) {
96103
this.events = eventHandler;
104+
dispatcher.setEventHandler(eventHandler);
97105
communicator.connect(uri, new CommunicatorEventHandler());
98106
}
99107

@@ -106,6 +114,7 @@ public void close() {
106114

107115
public void accept(SessionEvents eventHandler) {
108116
this.events = eventHandler;
117+
dispatcher.setEventHandler(eventHandler);
109118
communicator.accept(new CommunicatorEventHandler());
110119
}
111120

@@ -142,7 +151,7 @@ synchronized public void onCall(String id, String action, Object payload) {
142151
try {
143152
Request request = communicator.unpackPayload(payload, feature.getRequestType());
144153
if (request.validate()) {
145-
CompletableFuture<Confirmation> promise = handleIncomingRequest(request);
154+
CompletableFuture<Confirmation> promise = dispatcher.handleRequest(request);
146155
promise.whenComplete(new ConfirmationHandler(id, action, communicator));
147156
} else {
148157
communicator.sendCallError(id, action, "OccurenceConstraintViolation", "Payload for Action is syntactically correct but at least one of the fields violates occurence constraints");
@@ -173,18 +182,5 @@ public void onConnected() {
173182
events.handleConnectionOpened();
174183
}
175184

176-
private CompletableFuture<Confirmation> handleIncomingRequest(Request request) {
177-
CompletableFuture<Confirmation> promise = new CompletableFuture<>();
178-
new Thread(() -> {
179-
try {
180-
Confirmation conf = events.handleRequest(request);
181-
promise.complete(conf);
182-
} catch (Exception ex) {
183-
ex.printStackTrace();
184-
promise.completeExceptionally(ex);
185-
}
186-
}).start();
187-
return promise;
188-
}
189185
}
190186
}

ocpp-common/src/test/java/eu/chargetime/ocpp/test/SessionTest.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class SessionTest {
6060
@Before
6161
public void setup() throws Exception {
6262
when(sessionEvents.findFeatureByAction(any())).thenReturn(feature);
63-
session = new Session(communicator, queue);
63+
session = new Session(communicator, queue, false);
6464
doAnswer(invocation -> eventHandler = invocation.getArgumentAt(1, CommunicatorEvents.class)).when(communicator).connect(any(), any());
6565
session.open(null, sessionEvents);
6666
}
@@ -149,7 +149,6 @@ public void onCall_unhandledCallback_callSendCallError() throws Exception {
149149

150150
// When
151151
eventHandler.onCall(someId, null, null);
152-
try { Thread.sleep(10); } catch (Exception ex) {} // TODO make async invoker injectable
153152

154153
// then
155154
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
@@ -159,18 +158,12 @@ public void onCall_unhandledCallback_callSendCallError() throws Exception {
159158
public void onCall_handledCallback_callSendCallResult() throws Exception {
160159
// Given
161160
String someId = "Some id";
162-
Confirmation aConfirmation = new Confirmation() {
163-
@Override
164-
public boolean validate() {
165-
return true;
166-
}
167-
};
161+
Confirmation aConfirmation = () -> true;
168162
when(sessionEvents.handleRequest(any())).thenReturn(aConfirmation);
169163
when(communicator.unpackPayload(any(), any())).thenReturn(new TestRequest());
170164

171165
// When
172166
eventHandler.onCall(someId, null, null);
173-
try { Thread.sleep(10); } catch (Exception ex) {} // TODO make async invoker injectable
174167

175168
// then
176169
verify(communicator, times(1)).sendCallResult(anyString(), anyString(), eq(aConfirmation));
@@ -185,7 +178,6 @@ public void onCall_callbackThrowsException_callSendCallResult() throws Exception
185178

186179
// When
187180
eventHandler.onCall(someId, null, null);
188-
try { Thread.sleep(10); } catch (Exception ex) {} // TODO make async invoker injectable
189181

190182
// then
191183
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
@@ -222,10 +214,6 @@ public void onCall_sessionAccepted_callIsForwarded() throws Exception {
222214

223215
// When
224216
eventHandler.onCall(someId, null, null);
225-
try {
226-
Thread.sleep(10);
227-
} catch (Exception ex) {
228-
} // TODO make async invoker injectable
229217

230218
// Then
231219
verify(sessionEvents, times(1)).handleRequest(any());

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class FakeCentralSystem {
5858
} else {
5959
server = new SOAPServer(serverCoreProfile);
6060
}
61+
server.setAsyncRequestHandler(false);
6162

6263
initializeServer();
6364
isStarted = false;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ public GetDiagnosticsConfirmation handleGetDiagnosticsRequest(GetDiagnosticsRequ
153153

154154
switch (type) {
155155
case JSON:
156-
client = new JSONClient(core, "test");
156+
client = new JSONClient(core, "test", false);
157157
url = "ws://localhost:8887";
158158
break;
159159
case SOAP:
160-
client = new SOAPClient("me", new URL("http://localhost:8889"), core);
160+
client = new SOAPClient("me", new URL("http://localhost:8889"), core, false);
161161
url = "http://localhost:8890";
162162
break;
163163
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,22 @@ public class JSONClient extends Client {
3636
/**
3737
* The core feature profile is required as a minimum.
3838
*
39-
* @param coreProfile implementation of the core feature profile.
39+
* @param coreProfile implementation of the core feature profile.
40+
* @param identity identity of the charge point.
4041
*/
4142
public JSONClient(ClientCoreProfile coreProfile, String identity) {
42-
super(new Session(new JSONCommunicator(new WebSocketTransmitter()), new Queue()));
43+
this(coreProfile, identity, true);
44+
}
45+
46+
/**
47+
* The core feature profile is required as a minimum.
48+
*
49+
* @param coreProfile implementation of the core feature profile.
50+
* @param identity identity of the charge point. Not used for JSON protocol.
51+
* @param handleRequestAsync sets the session request handler in async or blocking mode.
52+
*/
53+
public JSONClient(ClientCoreProfile coreProfile, String identity, boolean handleRequestAsync) {
54+
super(new Session(new JSONCommunicator(new WebSocketTransmitter()), new Queue(), handleRequestAsync));
4355
addFeatureProfile(coreProfile);
4456
}
4557
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,31 @@ public class SOAPClient extends Client {
5555
* @param coreProfile implementation of the core feature profile.
5656
*/
5757
public SOAPClient(String chargeBoxIdentity, URL callback, ClientCoreProfile coreProfile) {
58-
this(chargeBoxIdentity, callback, new WebServiceTransmitter());
58+
this(chargeBoxIdentity, callback, coreProfile, true);
59+
}
60+
61+
/**
62+
* The core feature profile is required.
63+
* The client will use the information taken from the callback parameter to open a HTTP based Web Service.
64+
*
65+
* @param chargeBoxIdentity required identity used in message header.
66+
* @param callback call back info that the server can send requests to.
67+
* @param coreProfile implementation of the core feature profile.
68+
* @param handleRequestAsync sets the session request handler in async or blocking mode.
69+
*/
70+
public SOAPClient(String chargeBoxIdentity, URL callback, ClientCoreProfile coreProfile, boolean handleRequestAsync) {
71+
this(chargeBoxIdentity, callback, new WebServiceTransmitter(), handleRequestAsync);
5972
this.callback = callback;
6073
addFeatureProfile(coreProfile);
6174
}
6275

63-
private SOAPClient(String chargeBoxIdentity, URL callback, WebServiceTransmitter transmitter) {
64-
this(new SOAPCommunicator(chargeBoxIdentity, callback.toString(), transmitter));
76+
private SOAPClient(String chargeBoxIdentity, URL callback, WebServiceTransmitter transmitter, boolean handleRequestAsync) {
77+
this(new SOAPCommunicator(chargeBoxIdentity, callback.toString(), transmitter), handleRequestAsync);
6578
this.transmitter = transmitter;
6679
}
6780

68-
private SOAPClient(SOAPCommunicator communicator) {
69-
super(new Session(communicator, new Queue()));
81+
private SOAPClient(SOAPCommunicator communicator, boolean handleRequestAsync) {
82+
super(new Session(communicator, new Queue(), handleRequestAsync));
7083
this.communicator = communicator;
7184
}
7285

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ public class TimeoutSession extends Session {
3939
/**
4040
* Handles required injections.
4141
*
42-
* @param communicator send and receive messages.
43-
* @param queue store and restore requests based on unique ids.
42+
* @param communicator send and receive messages.
43+
* @param queue store and restore requests based on unique ids.
44+
* @param handleRequestAsync toggle if requests are handled async or not.
4445
*/
45-
public TimeoutSession(Communicator communicator, Queue queue) {
46-
super(communicator, queue);
46+
public TimeoutSession(Communicator communicator, Queue queue, boolean handleRequestAsync) {
47+
super(communicator, queue, handleRequestAsync);
4748
}
4849

4950
public void setTimeoutTimer(TimeoutTimer timeoutTimer) {

0 commit comments

Comments
 (0)