Search in sources :

Example 1 with ConnectedEvent

use of org.apache.plc4x.java.spi.events.ConnectedEvent in project plc4x by apache.

the class Plc4xAbEthProtocol method decode.

@Override
protected void decode(ChannelHandlerContext ctx, CIPEncapsulationPacket packet, List<Object> out) throws Exception {
    logger.trace("Received {}, decoding...", packet);
    if (packet instanceof CIPEncapsulationConnectionResponse) {
        CIPEncapsulationConnectionResponse connectionResponse = (CIPEncapsulationConnectionResponse) packet;
        // Save the session handle
        sessionHandle = connectionResponse.getSessionHandle();
        // Tell Netty we're finished connecting
        ctx.channel().pipeline().fireUserEventTriggered(new ConnectedEvent());
    } else {
        // We're currently just expecting responses.
        if (!(packet instanceof CIPEncapsulationReadResponse)) {
            return;
        }
        CIPEncapsulationReadResponse cipResponse = (CIPEncapsulationReadResponse) packet;
        int transactionCounter = cipResponse.getResponse().getTransactionCounter();
        if (!requests.containsKey(transactionCounter)) {
            ctx.fireExceptionCaught(new PlcProtocolException("Couldn't find request for response with transaction counter " + transactionCounter));
            return;
        }
        PlcRequestContainer requestContainer = requests.remove(transactionCounter);
        PlcRequest request = requestContainer.getRequest();
        PlcResponse response = null;
        if (request instanceof PlcReadRequest) {
            response = decodeReadResponse(cipResponse, requestContainer);
        } else {
            ctx.fireExceptionCaught(new PlcProtocolException("Unsupported request type " + request.getClass().getName()));
        }
        // Confirm the response being handled.
        if (response != null) {
            requestContainer.getResponseFuture().complete(response);
        }
    }
}
Also used : PlcRequestContainer(org.apache.plc4x.java.spi.messages.PlcRequestContainer) PlcResponse(org.apache.plc4x.java.api.messages.PlcResponse) PlcProtocolException(org.apache.plc4x.java.api.exceptions.PlcProtocolException) PlcRequest(org.apache.plc4x.java.api.messages.PlcRequest) ConnectedEvent(org.apache.plc4x.java.spi.events.ConnectedEvent) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest)

Aggregations

PlcProtocolException (org.apache.plc4x.java.api.exceptions.PlcProtocolException)1 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)1 PlcRequest (org.apache.plc4x.java.api.messages.PlcRequest)1 PlcResponse (org.apache.plc4x.java.api.messages.PlcResponse)1 ConnectedEvent (org.apache.plc4x.java.spi.events.ConnectedEvent)1 PlcRequestContainer (org.apache.plc4x.java.spi.messages.PlcRequestContainer)1