Skip to content

Commit

Permalink
Merge pull request #33 from steven-smpct/master
Browse files Browse the repository at this point in the history
Changing Ping interval, added debugging message, fixed Sec-WebSocket-Protocol, and fixed wss
  • Loading branch information
TVolden authored Mar 1, 2018
2 parents f8df205 + 9f9c9e2 commit eb26cf6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 93 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", ex);
logger.warn("sendCall() failed: not connected");
if (request.transactionRelated()) {
transactionQueue.add(call);
} else {
Expand Down Expand Up @@ -234,6 +234,7 @@ 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: 15 additions & 13 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,7 +6,6 @@
import eu.chargetime.ocpp.model.Request;

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

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

private final WebSocketTransmitter transmitter;
protected 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) {
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);
this(null, coreProfile);
}

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

@Override
Expand Down
69 changes: 0 additions & 69 deletions ocpp-v1_6/src/main/java/eu/chargetime/ocpp/OcppDraft.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,40 @@ 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;
import org.java_websocket.drafts.Draft_6455;
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 final Draft draft;
private SSLContext sslContext;

private WebSocketClient client;

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

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

@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 @@ -68,10 +76,11 @@ 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 @@ -85,16 +94,25 @@ 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 enableWSS(SSLContext sslContext) throws IOException {
SSLSocketFactory factory = sslContext.getSocketFactory();
client.setSocket(factory.createSocket());

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

@Override
Expand All @@ -109,6 +127,7 @@ 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 eb26cf6

Please sign in to comment.