Skip to content

Commit 26e2fcc

Browse files
committed
Refactured Client, extracted Feature logic to reuse in Server.
1 parent 4b2c43b commit 26e2fcc

File tree

7 files changed

+384
-79
lines changed

7 files changed

+384
-79
lines changed

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

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import eu.chargetime.ocpp.model.Confirmation;
66
import eu.chargetime.ocpp.model.Request;
77

8-
import java.util.ArrayList;
9-
import java.util.HashMap;
108
import java.util.concurrent.CompletableFuture;
119
/*
1210
ChargeTime.eu - Java-OCA-OCPP
@@ -38,16 +36,14 @@ of this software and associated documentation files (the "Software"), to deal
3836
/**
3937
* Abstract class.
4038
* Handles basic client logic:
41-
* Hold s list of supported features.
39+
* Holds a list of supported features.
4240
* Keeps track of outgoing request.
4341
* Calls back when a confirmation is received.
4442
* <p>
4543
* Must be overloaded in order to support specific protocols and formats.
4644
*/
47-
public abstract class Client
45+
public abstract class Client extends FeatureHandler
4846
{
49-
private HashMap<String, CompletableFuture<Confirmation>> promises;
50-
private ArrayList<Feature> featureList;
5147
private Session session;
5248

5349
/**
@@ -57,23 +53,9 @@ public abstract class Client
5753
* @see Session
5854
*/
5955
public Client(Session session) {
60-
this.promises = new HashMap<>();
61-
this.featureList = new ArrayList<>();
6256
this.session = session;
6357
}
6458

65-
/**
66-
* Add {@link Profile} to support a group of features.
67-
*
68-
* @param profile supported feature {@link Profile}
69-
* @see Profile
70-
*/
71-
public void addFeatureProfile(Profile profile) {
72-
Feature[] features = profile.getFeatureList();
73-
for (Feature feature: features)
74-
featureList.add(feature);
75-
}
76-
7759
/**
7860
* Connect to server
7961
*
@@ -94,7 +76,7 @@ public Feature findFeatureByRequest(Request request) {
9476

9577
@Override
9678
public void handleConfirmation(String uniqueId, Confirmation confirmation) {
97-
promises.get(uniqueId).complete(confirmation);
79+
getPromise(uniqueId).complete(confirmation);
9880
}
9981

10082
@Override
@@ -132,51 +114,6 @@ public void disconnect()
132114
}
133115
}
134116

135-
/**
136-
* Search for supported features added with the addProfile.
137-
* If no supported feature is found, null is returned
138-
*
139-
* Can take multiple inputs:
140-
* {@link String}, search for the action name of the feature.
141-
* {@link Request}/{@link Confirmation}, search for a feature that matches.
142-
* Anything else will return null.
143-
*
144-
* @param needle Object supports {@link String}, {@link Request} or {@link Confirmation}
145-
* @return Instance of the supported Feature
146-
*/
147-
private Feature findFeature(Object needle) {
148-
Feature output = null;
149-
150-
for(Feature feature: featureList) {
151-
if (featureContains(feature, needle)) {
152-
output = feature;
153-
break;
154-
}
155-
}
156-
157-
return output;
158-
}
159-
160-
/**
161-
* Tries to match {@link Feature} with an {@link Object}.
162-
* Different outcome based on the type:
163-
* {@link String}, matches the action name of the feature.
164-
* {@link Request}, matches the request type of the feature.
165-
* {@link Confirmation}, matches the confirmation type of the feature.
166-
* Other wise returns false.
167-
*
168-
* @param feature to match
169-
* @param object to match with, supports {@link String}, {@link Request} or {@link Confirmation}
170-
* @return true if the {@link Feature} matches the {@link Object}
171-
*/
172-
private boolean featureContains(Feature feature, Object object) {
173-
boolean contains = false;
174-
contains |= object instanceof String && feature.getAction().equals(object);
175-
contains |= object instanceof Request && feature.getRequestType() == object.getClass();
176-
contains |= object instanceof Confirmation && feature.getConfirmationType() == object.getClass();
177-
return contains;
178-
}
179-
180117
/**
181118
* Send a {@link Request} to the server.
182119
* Can only send {@link Request} that the client supports, see {@link #addFeatureProfile(Profile)}
@@ -195,17 +132,4 @@ public CompletableFuture<Confirmation> send(Request request) throws UnsupportedF
195132
CompletableFuture<Confirmation> promise = createPromise(id);
196133
return promise;
197134
}
198-
199-
/**
200-
* Creates call back {@link CompletableFuture} for later use
201-
*
202-
* @param uniqueId identification for the {@link Request}
203-
* @return call back {@link CompletableFuture}
204-
*/
205-
private CompletableFuture<Confirmation> createPromise(String uniqueId) {
206-
CompletableFuture<Confirmation> promise = new CompletableFuture<>();
207-
promises.put(uniqueId, promise);
208-
return promise;
209-
}
210-
211135
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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.feature.Feature;
28+
import eu.chargetime.ocpp.feature.profile.Profile;
29+
import eu.chargetime.ocpp.model.Confirmation;
30+
import eu.chargetime.ocpp.model.Request;
31+
32+
import java.util.ArrayList;
33+
import java.util.HashMap;
34+
import java.util.concurrent.CompletableFuture;
35+
36+
public abstract class FeatureHandler {
37+
38+
private HashMap<String, CompletableFuture<Confirmation>> promises;
39+
private ArrayList<Feature> featureList;
40+
41+
FeatureHandler() {
42+
this.promises = new HashMap<>();
43+
this.featureList = new ArrayList<>();
44+
}
45+
46+
/**
47+
* Add {@link Profile} to support a group of features.
48+
*
49+
* @param profile supported feature {@link Profile}
50+
* @see Profile
51+
*/
52+
public void addFeatureProfile(Profile profile) {
53+
Feature[] features = profile.getFeatureList();
54+
for (Feature feature : features)
55+
featureList.add(feature);
56+
}
57+
58+
/**
59+
* Search for supported features added with the addProfile.
60+
* If no supported feature is found, null is returned
61+
* <p>
62+
* Can take multiple inputs:
63+
* {@link String}, search for the action name of the feature.
64+
* {@link Request}/{@link Confirmation}, search for a feature that matches.
65+
* Anything else will return null.
66+
*
67+
* @param needle Object supports {@link String}, {@link Request} or {@link Confirmation}
68+
* @return Instance of the supported Feature
69+
*/
70+
protected Feature findFeature(Object needle) {
71+
Feature output = null;
72+
73+
for (Feature feature : featureList) {
74+
if (featureContains(feature, needle)) {
75+
output = feature;
76+
break;
77+
}
78+
}
79+
80+
return output;
81+
}
82+
83+
/**
84+
* Tries to match {@link Feature} with an {@link Object}.
85+
* Different outcome based on the type:
86+
* {@link String}, matches the action name of the feature.
87+
* {@link Request}, matches the request type of the feature.
88+
* {@link Confirmation}, matches the confirmation type of the feature.
89+
* Other wise returns false.
90+
*
91+
* @param feature to match
92+
* @param object to match with, supports {@link String}, {@link Request} or {@link Confirmation}
93+
* @return true if the {@link Feature} matches the {@link Object}
94+
*/
95+
protected boolean featureContains(Feature feature, Object object) {
96+
boolean contains = false;
97+
contains |= object instanceof String && feature.getAction().equals(object);
98+
contains |= object instanceof Request && feature.getRequestType() == object.getClass();
99+
contains |= object instanceof Confirmation && feature.getConfirmationType() == object.getClass();
100+
return contains;
101+
}
102+
103+
/**
104+
* Creates call back {@link CompletableFuture} for later use
105+
*
106+
* @param uniqueId identification for the {@link Request}
107+
* @return call back {@link CompletableFuture}
108+
*/
109+
protected CompletableFuture<Confirmation> createPromise(String uniqueId) {
110+
CompletableFuture<Confirmation> promise = new CompletableFuture<>();
111+
promises.put(uniqueId, promise);
112+
return promise;
113+
}
114+
115+
/**
116+
* Get stored call back {@link CompletableFuture}.
117+
*
118+
* @param uniqueId identification for the {@link Request}
119+
* @return call back {@link CompletableFuture}
120+
*/
121+
protected CompletableFuture<Confirmation> getPromise(String uniqueId) {
122+
return promises.get(uniqueId);
123+
}
124+
125+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package eu.chargetime.ocpp;
2+
/*
3+
ChargeTime.eu - Java-OCA-OCPP
4+
5+
MIT License
6+
7+
Copyright (C) 2016 Thomas Volden <[email protected]>
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
*/
27+
28+
public interface Listener {
29+
void open(ListenerEvents listenerEvents);
30+
31+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
public interface ListenerEvents {
28+
void newSession(Session session);
29+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package eu.chargetime.ocpp;
2+
3+
import eu.chargetime.ocpp.feature.Feature;
4+
import eu.chargetime.ocpp.model.Confirmation;
5+
import eu.chargetime.ocpp.model.Request;
6+
7+
import java.util.ArrayList;
8+
import java.util.concurrent.CompletableFuture;
9+
/*
10+
ChargeTime.eu - Java-OCA-OCPP
11+
12+
MIT License
13+
14+
Copyright (C) 2016 Thomas Volden <[email protected]>
15+
16+
Permission is hereby granted, free of charge, to any person obtaining a copy
17+
of this software and associated documentation files (the "Software"), to deal
18+
in the Software without restriction, including without limitation the rights
19+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+
copies of the Software, and to permit persons to whom the Software is
21+
furnished to do so, subject to the following conditions:
22+
23+
The above copyright notice and this permission notice shall be included in all
24+
copies or substantial portions of the Software.
25+
26+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
SOFTWARE.
33+
*/
34+
35+
/**
36+
* Handles basic server logic:
37+
* Holds a list of supported features.
38+
* Keeps track of outgoing requests.
39+
* Calls back when a confirmation is received.
40+
*/
41+
public class Server extends FeatureHandler {
42+
43+
private ArrayList<Session> sessions;
44+
45+
public Server() {
46+
this.sessions = new ArrayList<>();
47+
}
48+
49+
public void open(Listener listener, ServerEvents serverEvents) {
50+
listener.open(session -> {
51+
sessions.add(session);
52+
serverEvents.newSession(sessions.indexOf(session));
53+
});
54+
}
55+
56+
public CompletableFuture<Confirmation> send(int sessionIndex, Request request) throws UnsupportedFeatureException {
57+
Feature feature = findFeature(request);
58+
if (feature == null)
59+
throw new UnsupportedFeatureException();
60+
61+
String id = sessions.get(sessionIndex).sendRequest(feature.getAction(), request);
62+
CompletableFuture<Confirmation> promise = createPromise(id);
63+
return promise;
64+
}
65+
}

0 commit comments

Comments
 (0)