Example usage for java.nio.channels Channel toString

List of usage examples for java.nio.channels Channel toString

Introduction

In this page you can find the example usage for java.nio.channels Channel toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.openhab.binding.tcp.protocol.internal.UDPBinding.java

@Override
protected boolean internalReceiveChanneledCommand(String itemName, Command command, Channel sChannel,
        String commandAsString) {

    ProtocolBindingProvider provider = findFirstMatchingBindingProvider(itemName);

    if (command != null) {

        String transformedMessage = transformResponse(provider.getProtocolCommand(itemName, command),
                commandAsString);/* w w w.ja  v a2s . c o m*/
        String UDPCommandName = preAmble + transformedMessage + postAmble;

        ByteBuffer outputBuffer = null;
        try {
            outputBuffer = ByteBuffer.allocate(UDPCommandName.getBytes(charset).length);
            outputBuffer.put(UDPCommandName.getBytes(charset));
        } catch (UnsupportedEncodingException e) {
            logger.warn("Exception while attempting an unsupported encoding scheme");
        }

        // send the buffer in an asynchronous way
        ByteBuffer result = null;
        try {
            result = writeBuffer(outputBuffer, sChannel, blocking, timeOut);
        } catch (Exception e) {
            logger.error("An exception occurred while writing a buffer to a channel: {}", e.getMessage());
        }

        if (result != null && blocking) {
            String resultString = "";
            try {
                resultString = new String(result.array(), charset);
            } catch (UnsupportedEncodingException e) {
                logger.warn("Exception while attempting an unsupported encoding scheme");
            }

            logger.info("Received {} from the remote end {}", resultString, sChannel.toString());
            String transformedResponse = transformResponse(provider.getProtocolCommand(itemName, command),
                    resultString);

            // if the remote-end does not send a reply in response to the string we just sent, then the abstract superclass will update
            // the openhab status of the item for us. If it does reply, then an additional update is done via parseBuffer.
            // since this TCP binding does not know about the specific protocol, there might be two state updates (the command, and if
            // the case, the reply from the remote-end)

            if (updateWithResponse) {

                List<Class<? extends State>> stateTypeList = provider.getAcceptedDataTypes(itemName, command);
                State newState = createStateFromString(stateTypeList, transformedResponse);

                if (newState != null) {
                    eventPublisher.postUpdate(itemName, newState);
                } else {
                    logger.warn("Can not parse transformed input " + transformedResponse
                            + " to match command {} on item {}  ", command, itemName);
                }

                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
    return false;
}