All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Communication.TcpChannel Maven / Gradle / Ivy

package Alachisoft.NCache.Common.Communication;

import Alachisoft.NCache.Common.Communication.Exceptions.ChannelException;
import Alachisoft.NCache.Common.Communication.Exceptions.ConnectionException;
import Alachisoft.NCache.Common.GenericCopier;

import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;

public class TcpChannel implements IChannel {

    private static final int DATA_SIZE_BUFFER_LENGTH = 10; //(in bytes)
    private IConnection _connection;
    private String _serverIP;
    private String _bindIP;
    private int _port;
    //C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: byte[] _sizeBuffer = new byte[DATA_SIZE_BUFFER_LENGTH];
    private byte[] _sizeBuffer = new byte[DATA_SIZE_BUFFER_LENGTH];
    private IChannelFormatter _formatter;
    private IChannelEventListener _eventListener;
    private Thread _receiverThread;
    private ITraceProvider _traceProvider;
    private boolean stopThread;
    private String privateName;

    public TcpChannel(String serverIP, int port, String bindingIP, ITraceProvider traceProvider) {
        if (tangible.DotNetToJavaStringHelper.isNullOrEmpty(serverIP)) {
            throw new IllegalArgumentException("Value cannot be null or empty."+System.lineSeparator()+"Parameter name: serverIP");
        }

        _serverIP = serverIP;
        _port = port;
        _bindIP = bindingIP;
        _traceProvider = traceProvider;
    }

    public final String getName() {
        return privateName;
    }

    public final void setName(String value) {
        privateName = value;
    }

    @Override
    public boolean getIsConnected() {
        return _connection != null ? _connection.getIsConnected() : false;
    }


    public final boolean Connect() throws Exception {

        if (_formatter == null) {
            throw new Exception("Channel formatter is not specified");
        }

        if (_eventListener == null) {
            throw new Exception("There is no channel event listener specified");
        }

        try {
            if (_connection == null) {
                _connection = new TcpConnection();

                if (!tangible.DotNetToJavaStringHelper.isNullOrEmpty(_bindIP)) {
                    _connection.Bind(_bindIP);
                }

                _connection.Connect(_serverIP, _port);

                _receiverThread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Run();
                        } catch (UnsupportedEncodingException unsupportedEncodingException) {
                            Logger.getLogger(GenericCopier.class.getName()).warning("UnsupportedEncodingException + " + unsupportedEncodingException.getMessage());
                        }
                    }
                });
                //_receiverThread.IsBackground = true;
                //Muneeb: Thread = Daemon
                _receiverThread.setDaemon(true);
                _receiverThread.start();
                return true;
            }
        } catch (ConnectionException ce) {
            if (_traceProvider != null) {
                _traceProvider.TraceError(getName() + ".Connect", ce.toString());
            }
            throw new ChannelException(ce.getMessage(), ce);
        }

        return false;
    }

    public final void Disconnect() {
        if (_connection != null) {
            _connection.Disconnect();
            if (_receiverThread != null) {
                this.stopThread = true;
                _receiverThread.interrupt();
            }
            _connection = null;
        }
    }

    @Override
    public final boolean SendMessage(Object message) throws ChannelException, java.io.UnsupportedEncodingException {
        if (message == null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: message");
        }

        byte[] serailizedMessage = null;
        try {
            serailizedMessage = _formatter.Serialize(message);
        } catch (Exception exception) {
            throw new ChannelException(exception.getMessage());
        }

        byte[] msgLength = (serailizedMessage.length + "").getBytes("UTF-8");

        //message is written in a specific order as expected by Socket server

        byte[] finalBuffer = null;
        try {


            finalBuffer = new byte[30 + serailizedMessage.length];
            System.arraycopy(msgLength, 0, finalBuffer, 20, msgLength.length);
            System.arraycopy(serailizedMessage, 0, finalBuffer, 30, serailizedMessage.length);

        }
        catch (Exception e) {
            Logger.getLogger(GenericCopier.class.getName()).warning("Exception + " + e.getMessage());
        }

        try {
            if (EnsureConnected()) {
                try {
                    _connection.Send(finalBuffer, 0, finalBuffer.length);
                    return true;
                } catch (ConnectionException e) {
                    if (EnsureConnected()) {
                        _connection.Send(finalBuffer, 0, finalBuffer.length);
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            throw new ChannelException(e.getMessage());
        }

        return false;
    }

    private boolean EnsureConnected() throws Exception {
        if (_connection != null && !_connection.getIsConnected()) {
            Disconnect();
            Connect();
        }

        return _connection.getIsConnected();
    }

    public final void RegisterEventListener(IChannelEventListener listener) {
        if (listener == null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: listener");
        }

        _eventListener = listener;
    }

    public final void UnRegisterEventListener(IChannelEventListener listener) {
        if (listener != null && listener.equals(_eventListener)) {
            _eventListener = null;
        }
    }

    public final IChannelFormatter getFormatter() {
        return _formatter;
    }

    public final void setFormatter(IChannelFormatter value) {
        _formatter = value;
    }

    /**
     *
     */
    private void Run() throws UnsupportedEncodingException {

        while (!stopThread && !Thread.currentThread().isInterrupted()) {
            try {

                //receive data size for the response
                if (_connection != null) {
                    _connection.Receive(_sizeBuffer, DATA_SIZE_BUFFER_LENGTH);

                    String resp = new String(_sizeBuffer);
                    int rspLength = Integer.parseInt(resp.trim());
                    if (rspLength > 0) {
                        byte[] dataBuffer = new byte[rspLength];
                        _connection.Receive(dataBuffer, rspLength);

                        //deserialize the message
                        IResponse response = null;
                        if (_formatter != null) {
                            Object tempVar = _formatter.Deserialize(dataBuffer);
                            response = (IResponse) ((tempVar instanceof IResponse) ? tempVar : null);
                        }

                        if (_eventListener != null) {
                            _eventListener.ReceiveResponse(response);
                        }
                    }

                }

            }
            catch (ConnectionException ce) {
                if (_traceProvider != null) {
                    _traceProvider.TraceError(getName() + ".Run", ce.toString());
                }
                if (_eventListener != null) {
                    _eventListener.ChannelDisconnected(ce.getMessage());
                }
                break;
            } catch (Exception e) {
                if (_traceProvider != null) {
                    _traceProvider.TraceError(getName() + ".Run", e.toString());
                }
                //new ChannelException();

            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy