-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathMultiProtocolJSONClient.java
More file actions
246 lines (220 loc) · 9.25 KB
/
MultiProtocolJSONClient.java
File metadata and controls
246 lines (220 loc) · 9.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* ChargeTime.eu - Java-OCA-OCPP
*
* MIT License
*
* Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
* Copyright (C) 2023 Robert Schlabbach <robert.schlabbach@ubitricity.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package eu.chargetime.ocpp;
import eu.chargetime.ocpp.feature.function.Function;
import eu.chargetime.ocpp.feature.profile.Profile;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.Request;
import eu.chargetime.ocpp.wss.BaseWssSocketBuilder;
import eu.chargetime.ocpp.wss.WssSocketBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
import java.util.zip.Deflater;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.extensions.IExtension;
import org.java_websocket.protocols.IProtocol;
import org.java_websocket.protocols.Protocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** multiple protocol versions capable JSON Web Socket implementation of the client. */
public class MultiProtocolJSONClient implements IMultiProtocolClientAPI {
private static final Logger logger = LoggerFactory.getLogger(MultiProtocolJSONClient.class);
private final String identity;
private final MultiProtocolFeatureRepository featureRepository;
private final MultiProtocolWebSocketTransmitter transmitter;
private final Client client;
/**
* Application composite root for a json client.
*
* @param protocolVersions list of protocol versions to offer.
*/
public MultiProtocolJSONClient(List<ProtocolVersion> protocolVersions) {
this(protocolVersions, null);
}
/**
* Application composite root for a json client.
*
* @param protocolVersions list of protocol versions to offer.
* @param identity identity if set, will append identity to url.
*/
public MultiProtocolJSONClient(List<ProtocolVersion> protocolVersions, String identity) {
this(protocolVersions, identity, JSONConfiguration.get());
}
/**
* Application composite root for a json client.
*
* @param protocolVersions list of protocol versions to offer.
* @param identity if set, will append identity to url.
* @param configuration network configuration for a json client.
*/
public MultiProtocolJSONClient(
List<ProtocolVersion> protocolVersions, String identity, JSONConfiguration configuration) {
this.identity = identity;
featureRepository = new MultiProtocolFeatureRepository(protocolVersions);
int maxFrameSize = configuration.getParameter(JSONConfiguration.WEBSOCKET_MAX_FRAME_SIZE, 0);
List<IExtension> inputExtensions = new ArrayList<>();
if (configuration.getParameter(JSONConfiguration.WEBSOCKET_COMPRESSION_SUPPORT, false)) {
PerMessageDeflateExtension perMessageDeflateExtension =
maxFrameSize > 0
? new PerMessageDeflateExtension(Deflater.BEST_COMPRESSION, maxFrameSize)
: new PerMessageDeflateExtension(Deflater.BEST_COMPRESSION);
perMessageDeflateExtension.setThreshold(64);
perMessageDeflateExtension.setServerNoContextTakeover(false);
perMessageDeflateExtension.setClientNoContextTakeover(false);
inputExtensions.add(perMessageDeflateExtension);
}
List<IProtocol> inputProtocols = new ArrayList<>(protocolVersions.size());
for (ProtocolVersion protocolVersion : protocolVersions) {
inputProtocols.add(new Protocol(protocolVersion.getSubProtocolName()));
}
Draft draft =
maxFrameSize > 0
? new Draft_6455(inputExtensions, inputProtocols, maxFrameSize)
: new Draft_6455(inputExtensions, inputProtocols);
transmitter = new MultiProtocolWebSocketTransmitter(featureRepository, configuration, draft);
JSONCommunicator communicator = new JSONCommunicator(transmitter, false);
ISessionFactory sessionFactory = new MultiProtocolSessionFactory(featureRepository);
ISession session = sessionFactory.createSession(communicator);
client = new Client(session, new PromiseRepository());
}
/**
* Application composite root for a json client.
*
* @param protocolVersions list of protocol versions to offer.
* @param identity if set, will append identity to url.
* @param wssSocketBuilder to build {@link java.net.Socket} to support wss://.
*/
public MultiProtocolJSONClient(
List<ProtocolVersion> protocolVersions, String identity, WssSocketBuilder wssSocketBuilder) {
this(protocolVersions, identity, wssSocketBuilder, JSONConfiguration.get());
}
/**
* Application composite root for a json client.
*
* @param protocolVersions list of protocol versions to offer.
* @param identity if set, will append identity to url.
* @param wssSocketBuilder to build {@link java.net.Socket} to support wss://.
* @param configuration network configuration for a json client.
*/
public MultiProtocolJSONClient(
List<ProtocolVersion> protocolVersions,
String identity,
WssSocketBuilder wssSocketBuilder,
JSONConfiguration configuration) {
this(protocolVersions, identity, configuration);
enableWSS(wssSocketBuilder);
}
// To ensure the exposed API is backward compatible
public void enableWSS(SSLContext sslContext) throws IOException {
WssSocketBuilder wssSocketBuilder =
BaseWssSocketBuilder.builder().sslSocketFactory(sslContext.getSocketFactory());
enableWSS(wssSocketBuilder);
}
/**
* Enables WSS connection to the endpoint. The {@code wssSocketBuilder} must be initialized at
* that step (as required parameters set might vary depending on implementation the {@link
* WssSocketBuilder#verify()} is used to ensure initialization).
*
* @param wssSocketBuilder builder to provide SSL socket
* @return instance of {@link MultiProtocolJSONClient}
* @throws IllegalStateException in case if the client is already connected
* @throws IllegalStateException in case {@code wssSocketBuilder} not initialized properly
*/
public MultiProtocolJSONClient enableWSS(WssSocketBuilder wssSocketBuilder) {
wssSocketBuilder.verify();
transmitter.enableWSS(wssSocketBuilder);
return this;
}
/**
* Applies JSONConfiguration changes when already connected. Specifically, the WebSocket ping
* interval can be changed without reconnecting by calling this method.
*/
public void reconfigure() {
transmitter.configure();
}
@Override
public void addFeatureProfile(Profile profile) {
addFeatureProfile(ProtocolVersion.OCPP1_6, profile);
}
@Override
public void addFeatureProfile(ProtocolVersion protocolVersion, Profile profile) {
featureRepository.addFeatureProfile(protocolVersion, profile);
}
@Override
public void addFunction(ProtocolVersion protocolVersion, Function function) {
featureRepository.addFeatureFunction(protocolVersion, function);
}
@Override
public void connect(String url, ClientEvents clientEvents) {
logger.debug("Feature repository: {}", featureRepository);
String identityUrl = (identity != null) ? String.format("%s/%s", url, identity) : url;
client.connect(identityUrl, clientEvents);
}
@Override
@Nullable
public ProtocolVersion getProtocolVersion() {
return featureRepository.getProtocolVersion();
}
@Override
public CompletionStage<Confirmation> send(Request request)
throws OccurenceConstraintException, UnsupportedFeatureException {
return client.send(request);
}
@Override
public boolean asyncCompleteRequest(String uniqueId, Confirmation confirmation)
throws UnsupportedFeatureException, OccurenceConstraintException {
return client.asyncCompleteRequest(uniqueId, confirmation);
}
@Override
public void disconnect() {
client.disconnect();
}
public double getCompressionRatio() {
IExtension extension = transmitter.getExtension();
if (extension instanceof PerMessageDeflateExtension) {
return ((PerMessageDeflateExtension) extension).getCompressionRatio();
}
return 1;
}
public Exception getLastError() {
return transmitter.getLastError();
}
@Override
public boolean isClosed() {
return transmitter.isClosed();
}
@Override
public UUID getSessionId() {
return client.getSessionId();
}
}