Skip to content

Commit

Permalink
Merge pull request #35 from ChargeTimeEU/revert-33-master
Browse files Browse the repository at this point in the history
Revert "Changing Ping interval, added debugging message, fixed Sec-We…
  • Loading branch information
TVolden authored Mar 13, 2018
2 parents eb26cf6 + 4a06df2 commit 84f2f79
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ synchronized public void sendCall(String uniqueId, String action, Request reques
radio.send(call);
}
} catch (NotConnectedException ex) {
logger.warn("sendCall() failed: not connected");
logger.warn("sendCall() failed", ex);
if (request.transactionRelated()) {
transactionQueue.add(call);
} else {
Expand Down Expand Up @@ -234,7 +234,6 @@ public void connected() {

@Override
public void receivedMessage(Object input) {
logger.debug("Recieved: " + input);
Message message = parse(input);
if (message instanceof CallResultMessage) {
events.onCallResult(message.getId(), message.getAction(), message.getPayload());
Expand Down
28 changes: 13 additions & 15 deletions ocpp-v1_6/src/main/java/eu/chargetime/ocpp/JSONClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import eu.chargetime.ocpp.model.Request;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.util.concurrent.CompletionStage;

/*
Expand Down Expand Up @@ -39,32 +40,29 @@
*/
public class JSONClient implements IClientAPI {

protected final WebSocketTransmitter transmitter;
private final WebSocketTransmitter transmitter;
private final FeatureRepository featureRepository;
private final Client client;

public JSONClient(SSLContext sslContext, ClientCoreProfile coreProfile) {
transmitter = new WebSocketTransmitter(sslContext);
JSONCommunicator communicator = new JSONCommunicator(transmitter);
AsyncPromiseFulfilerDecorator promiseFulfiler = new AsyncPromiseFulfilerDecorator(new SimplePromiseFulfiller());
featureRepository = new FeatureRepository();
Session session = new Session(communicator, new Queue(), promiseFulfiler, featureRepository);
client = new Client(session, featureRepository, new PromiseRepository());
featureRepository.addFeatureProfile(coreProfile);
}


/**
* Application composite root for a json client.
* The core feature profile is required as a minimum.
*
* @param coreProfile implementation of the core feature profile.
*/
public JSONClient(ClientCoreProfile coreProfile) {
this(null, coreProfile);
transmitter = new WebSocketTransmitter(new OcppDraft());
JSONCommunicator communicator = new JSONCommunicator(transmitter);
AsyncPromiseFulfilerDecorator promiseFulfiler = new AsyncPromiseFulfilerDecorator(new SimplePromiseFulfiller());
featureRepository = new FeatureRepository();
Session session = new Session(communicator, new Queue(), promiseFulfiler, featureRepository);
client = new Client(session, featureRepository, new PromiseRepository());
featureRepository.addFeatureProfile(coreProfile);
}
public void setPingInterval(int interval) {
transmitter.setPingInterval(interval);

public void enableWSS(SSLContext sslContext) throws IOException {
transmitter.enableWSS(sslContext);
}

@Override
Expand Down
69 changes: 69 additions & 0 deletions ocpp-v1_6/src/main/java/eu/chargetime/ocpp/OcppDraft.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package eu.chargetime.ocpp;
/*
ChargeTime.eu - Java-OCA-OCPP
MIT License
Copyright (C) 2016-2018 Thomas Volden <[email protected]>
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.
*/

import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.exceptions.InvalidHandshakeException;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ClientHandshakeBuilder;
import org.java_websocket.handshake.HandshakeBuilder;
import org.java_websocket.handshake.ServerHandshakeBuilder;

public class OcppDraft extends Draft_6455 {

private final String SUBPROTOCOL = "ocpp1.6";

@Override
public ClientHandshakeBuilder postProcessHandshakeRequestAsClient(ClientHandshakeBuilder request) {
ClientHandshakeBuilder clientHandshakeBuilder = postProcessHandshakeRequestAsClient(request);
clientHandshakeBuilder.put("Sec-WebSocket-Protocol", SUBPROTOCOL);
return clientHandshakeBuilder;
}

@Override
public HandshakeBuilder postProcessHandshakeResponseAsServer(ClientHandshake request, ServerHandshakeBuilder response) throws InvalidHandshakeException {
HandshakeBuilder handshakeBuilder = postProcessHandshakeResponseAsServer(request, response);
String subProtocol = request.getFieldValue("Sec-WebSocket-Protocol");
if (subProtocol.length() > 0) {
handshakeBuilder.put("Sec-WebSocket-Protocol", subProtocol);
}
return handshakeBuilder;
}

@Override
public int hashCode() {
int hash = super.hashCode();
hash = 31 * hash + SUBPROTOCOL.hashCode();
return hash;
}

@Override
public String toString() {
String result = super.toString();
result += " sub protocol: " + SUBPROTOCOL;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,32 @@ of this software and associated documentation files (the "Software"), to deal
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.drafts.Draft;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.extensions.IExtension;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.protocols.IProtocol;
import org.java_websocket.protocols.Protocol;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URI;
import java.util.Collections;
/**
* Web Socket implementation of the Transmitter.
*/
public class WebSocketTransmitter implements Transmitter
{
private static final Logger logger = LogManager.getLogger(WebSocketTransmitter.class);
private SSLContext sslContext;
private final Draft draft;

private WebSocketClient client;

public WebSocketTransmitter(SSLContext sslContext) {
this.sslContext = sslContext;
}

public WebSocketTransmitter() {
this(null);
public WebSocketTransmitter(Draft draft) {
this.draft = draft;
}

@Override
public void connect(String uri, RadioEvents events) {
Draft_6455 draft = new Draft_6455(Collections.<IExtension>emptyList(), Collections.<IProtocol>singletonList(new Protocol("ocpp1.6")));

client = new WebSocketClient(URI.create(uri), draft) {
@Override
public void onOpen(ServerHandshake serverHandshake)
Expand All @@ -76,11 +68,10 @@ public void onMessage(String s)
{
events.receivedMessage(s);
}

@Override
public void onClose(int i, String s, boolean b)
{
logger.debug("WebSocketClient.onClose: code = " + i + ", message = " + s + ", host closed = " + b);
events.disconnected();
}

Expand All @@ -94,25 +85,16 @@ public void onError(Exception ex)
}
}
};

if(sslContext != null) {
try {
SSLSocketFactory factory = sslContext.getSocketFactory();
client.setSocket(factory.createSocket());
} catch (IOException ex) {
logger.error("client.setSocket() failed", ex);
}
}

try {
client.connectBlocking();
} catch (Exception ex) {
logger.warn("client.connectBlocking() failed", ex);
}
}

public void setPingInterval(int interval) {
client.setConnectionLostTimeout(interval);

public void enableWSS(SSLContext sslContext) throws IOException {
SSLSocketFactory factory = sslContext.getSocketFactory();
client.setSocket(factory.createSocket());
}

@Override
Expand All @@ -127,7 +109,6 @@ public void disconnect()

@Override
public void send(Object request) throws NotConnectedException {
logger.debug("Sending: " + request);
try {
client.send(request.toString());
} catch (WebsocketNotConnectedException ex) {
Expand Down

0 comments on commit 84f2f79

Please sign in to comment.