Search in sources :

Example 1 with ConnectException

use of jp.ossc.nimbus.service.publish.ConnectException in project nimbus by nimbus-org.

the class ClientConnectionImpl method connect.

private synchronized void connect(Object id, boolean isReconnect) throws ConnectException {
    if (socket != null) {
        return;
    }
    isConnected = false;
    try {
        try {
            if (socketFactory == null) {
                socket = new Socket(address, port, getBindAddress(), getBindPort());
            } else {
                socket = socketFactory.createSocket(address, port, getBindAddress(), getBindPort());
            }
            if (responseTimeout > 0) {
                socket.setSoTimeout((int) responseTimeout);
            }
            if (!isReconnect) {
                if (receiveAddress != null) {
                    receiveGroup = InetAddress.getByName(receiveAddress);
                    InetAddress bindAddress = getUDPBindAddress();
                    if (bindAddress == null) {
                        receiveSocket = receiveGroup.isMulticastAddress() ? new MulticastSocket(receivePort) : new DatagramSocket(receivePort);
                    } else {
                        final InetSocketAddress address = new InetSocketAddress(bindAddress, receivePort);
                        receiveSocket = receiveGroup.isMulticastAddress() ? new MulticastSocket(address) : new DatagramSocket(address);
                    }
                    if (receiveGroup.isMulticastAddress()) {
                        NetworkInterface[] networkInterfaces = getNetworkInterfaces();
                        if (networkInterfaces == null) {
                            ((MulticastSocket) receiveSocket).joinGroup(receiveGroup);
                        } else {
                            for (int i = 0; i < networkInterfaces.length; i++) {
                                ((MulticastSocket) receiveSocket).joinGroup(new InetSocketAddress(receiveGroup, receivePort), networkInterfaces[i]);
                            }
                        }
                    }
                } else {
                    InetAddress bindAddress = getUDPBindAddress();
                    if (bindAddress == null) {
                        receiveSocket = new DatagramSocket(receivePort);
                    } else {
                        receiveSocket = new DatagramSocket(new InetSocketAddress(bindAddress, receivePort));
                    }
                }
                if (receivePort == 0) {
                    receivePortReal = receiveSocket.getLocalPort();
                } else {
                    receivePortReal = receivePort;
                }
                if (receiveSocket != null) {
                    try {
                        int receiveBufferSize = receiveSocket.getReceiveBufferSize();
                        if (receiveBufferSize < windowSize) {
                            receiveSocket.setReceiveBufferSize(windowSize);
                        }
                    } catch (SocketException e) {
                    }
                }
            }
        } catch (UnknownHostException e) {
            throw new ConnectException(e);
        } catch (NumberFormatException e) {
            throw new ConnectException(e);
        } catch (IOException e) {
            throw new ConnectException(e);
        }
        if (receivePacketQueue == null) {
            receivePacketQueue = new DefaultQueueService();
            try {
                receivePacketQueue.create();
                receivePacketQueue.start();
            } catch (Exception e) {
                throw new ConnectException(e);
            }
        }
        if (packetReceiveDaemon == null) {
            packetReceiveDaemon = new Daemon(new PacketReceiver());
            packetReceiveDaemon.setDaemon(true);
            packetReceiveDaemon.setName("Nimbus Publish(UDP) ClientConnection PacketReceiver " + socket.getLocalSocketAddress());
            packetReceiveDaemon.start();
        }
        if (replyReceiveDaemon == null) {
            replyReceiveDaemon = new Daemon(new ReplyReceiver());
            replyReceiveDaemon.setDaemon(true);
            replyReceiveDaemon.setName("Nimbus Publish(UDP) ClientConnection ReplyReceiver " + socket.getLocalSocketAddress());
            replyReceiveDaemon.start();
        }
        if (messageReceiveDaemon == null) {
            messageReceiveDaemon = new Daemon(new MessageReceiver());
            messageReceiveDaemon.setDaemon(true);
            messageReceiveDaemon.setName("Nimbus Publish(UDP) ClientConnection MessageReceiver " + socket.getLocalSocketAddress());
        }
        if (missingWindowCheckDaemon == null) {
            missingWindowCheckDaemon = new Daemon(new MissingWindowChecker((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()));
            missingWindowCheckDaemon.setDaemon(true);
            missingWindowCheckDaemon.setName("Nimbus Publish(UDP) ClientConnection MissingWindowChecker " + socket.getLocalSocketAddress());
            ((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).setPacketReceiver((PacketReceiver) packetReceiveDaemon.getDaemonRunnable());
            ((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).setMissingWindowChecker((MissingWindowChecker) missingWindowCheckDaemon.getDaemonRunnable());
        }
        messageReceiveDaemon.start();
        missingWindowCheckDaemon.start();
        this.id = id == null ? socket.getLocalSocketAddress() : id;
        try {
            IdMessage message = new IdMessage(this.id);
            message.setReceivePort(receivePortReal);
            send(message, isAcknowledge);
        } catch (IOException e) {
            throw new ConnectException(e);
        } catch (ClassNotFoundException e) {
            throw new ConnectException(e);
        }
        if (serverServiceName != null) {
            ServiceManager manager = ServiceManagerFactory.findManager(serviceManagerName == null ? serverServiceName.getServiceManagerName() : serviceManagerName);
            if (manager != null) {
                final ClientConnectionService ccs = new ClientConnectionService();
                try {
                    String name = serverServiceName.getServiceName() + '$' + socket.getLocalSocketAddress();
                    name = name.replaceAll(":", "\\$");
                    if (!manager.isRegisteredService(name) && manager.registerService(name, ccs)) {
                        serviceName = ccs.getServiceNameObject();
                        manager.createService(ccs.getServiceName());
                        manager.startService(ccs.getServiceName());
                    }
                } catch (Exception e) {
                    throw new ConnectException(e);
                }
            }
        }
    } catch (ConnectException e) {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e2) {
            }
            socket = null;
        }
        if (!isReconnect && receiveSocket != null) {
            receiveSocket.close();
            receiveSocket = null;
        }
        throw e;
    }
    isConnected = true;
    isServerClosed = false;
}
Also used : MulticastSocket(java.net.MulticastSocket) SocketException(java.net.SocketException) InetSocketAddress(java.net.InetSocketAddress) ServiceManager(jp.ossc.nimbus.core.ServiceManager) ConnectException(jp.ossc.nimbus.service.publish.ConnectException) UnknownHostException(java.net.UnknownHostException) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) ConnectException(jp.ossc.nimbus.service.publish.ConnectException) EOFException(java.io.EOFException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Daemon(jp.ossc.nimbus.daemon.Daemon) DatagramSocket(java.net.DatagramSocket) InetAddress(java.net.InetAddress) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) MulticastSocket(java.net.MulticastSocket)

Example 2 with ConnectException

use of jp.ossc.nimbus.service.publish.ConnectException in project nimbus by nimbus-org.

the class ClientConnectionImpl method reconnect.

private void reconnect() throws ConnectException, MessageSendException {
    boolean isNowReconnecting = isReconnecting;
    synchronized (this) {
        if (isNowReconnecting) {
            return;
        }
        isReconnecting = true;
        try {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                }
                socket = null;
            }
            int tryCount = 0;
            boolean isSuccess = false;
            while (!isSuccess) {
                tryCount++;
                try {
                    connect(id);
                    if (subjects != null) {
                        Object[] subjectArray = subjects.keySet().toArray();
                        for (int i = 0; i < subjectArray.length; i++) {
                            Object subject = subjectArray[i];
                            Set keySet = (Set) subjects.get(subject);
                            if (keySet != null) {
                                String[] keys = (String[]) keySet.toArray(new String[keySet.size()]);
                                boolean containsNull = false;
                                List keyList = new ArrayList();
                                for (int j = 0; j < keys.length; j++) {
                                    if (keys[j] == null) {
                                        containsNull = true;
                                    } else {
                                        keyList.add(keys[j]);
                                    }
                                }
                                if (containsNull) {
                                    addSubject((String) subject);
                                    keys = (String[]) keyList.toArray(new String[keyList.size()]);
                                }
                                if (keys != null && keys.length != 0) {
                                    addSubject((String) subject, keys);
                                }
                            }
                        }
                    }
                    if (isStartReceive) {
                        long time = -1;
                        if (latestMessage != null) {
                            time = latestMessage.getReceiveTime() - reconnectBufferTime;
                        }
                        startReceive(time, true);
                    }
                    isSuccess = true;
                } catch (ConnectException e) {
                    if (tryCount >= reconnectCount) {
                        throw e;
                    } else {
                        if (receiveWarnMessageId != null) {
                            ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
                        }
                    }
                } catch (MessageSendException e) {
                    if (tryCount >= reconnectCount) {
                        throw e;
                    } else {
                        if (receiveWarnMessageId != null) {
                            ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
                        }
                    }
                }
                if (!isSuccess && reconnectInterval > 0) {
                    try {
                        Thread.sleep(reconnectInterval);
                    } catch (InterruptedException e) {
                        throw new ConnectException(e);
                    }
                }
            }
        } finally {
            isReconnecting = false;
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) ArrayList(java.util.ArrayList) List(java.util.List) ConnectException(jp.ossc.nimbus.service.publish.ConnectException)

Example 3 with ConnectException

use of jp.ossc.nimbus.service.publish.ConnectException in project nimbus by nimbus-org.

the class ClientConnectionImpl method connect.

public synchronized void connect(Object id) throws ConnectException {
    if (socket != null) {
        return;
    }
    isConnected = false;
    InetAddress bindAddress = null;
    int bindPort = 0;
    try {
        bindAddress = getBindAddress();
        bindPort = getBindPort();
        if (socketFactory == null) {
            socket = new Socket(address, port, bindAddress, bindPort);
        } else {
            socket = socketFactory.createSocket(address, port, bindAddress, bindPort);
        }
    } catch (UnknownHostException e) {
        throw new ConnectException("address=" + address + ", port=" + port + ", bindAddress=" + bindAddress + ", bindPort=" + bindPort, e);
    } catch (NumberFormatException e) {
        throw new ConnectException(e);
    } catch (IOException e) {
        throw new ConnectException("address=" + address + ", port=" + port + ", bindAddress=" + bindAddress + ", bindPort=" + bindPort, e);
    }
    try {
        if (messageReceiveDaemon == null) {
            messageReceiveDaemon = new Daemon(this);
            messageReceiveDaemon.setDaemon(true);
            messageReceiveDaemon.setName("Nimbus Publish(TCP) ClientConnection SocketReader " + socket.getLocalSocketAddress());
            messageReceiveDaemon.start();
        }
        this.id = id == null ? socket.getLocalSocketAddress() : id;
        try {
            send(new IdMessage(this.id));
        } catch (IOException e) {
            throw new ConnectException(e);
        } catch (MessageSendException e) {
            throw new ConnectException(e);
        }
        if (serviceManagerName != null && serverServiceName != null) {
            ServiceManager manager = ServiceManagerFactory.findManager(serviceManagerName);
            if (manager != null) {
                final ClientConnectionService ccs = new ClientConnectionService();
                try {
                    String name = serverServiceName.getServiceName() + '$' + socket.getLocalSocketAddress();
                    name = name.replaceAll(":", "\\$");
                    if (!manager.isRegisteredService(name) && manager.registerService(name, ccs)) {
                        serviceName = ccs.getServiceNameObject();
                        manager.createService(ccs.getServiceName());
                        manager.startService(ccs.getServiceName());
                    }
                } catch (Exception e) {
                    throw new ConnectException(e);
                }
            }
        }
    } catch (ConnectException e) {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e2) {
            }
            socket = null;
        }
        throw e;
    }
    isConnected = true;
    isServerClosed = false;
}
Also used : UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) SocketException(java.net.SocketException) MessageCommunicateException(jp.ossc.nimbus.service.publish.MessageCommunicateException) ConnectException(jp.ossc.nimbus.service.publish.ConnectException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) MessageException(jp.ossc.nimbus.service.publish.MessageException) EOFException(java.io.EOFException) UnknownHostException(java.net.UnknownHostException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) Daemon(jp.ossc.nimbus.daemon.Daemon) ServiceManager(jp.ossc.nimbus.core.ServiceManager) InetAddress(java.net.InetAddress) Socket(java.net.Socket) ConnectException(jp.ossc.nimbus.service.publish.ConnectException)

Example 4 with ConnectException

use of jp.ossc.nimbus.service.publish.ConnectException in project nimbus by nimbus-org.

the class ClientConnectionImpl method connect.

public synchronized void connect(Object id) throws ConnectException {
    if (isConnected) {
        return;
    }
    isConnected = false;
    this.id = id == null ? clientNo : id;
    serverConnection.connect(this.id, this);
    if (serverServiceName != null) {
        ServiceManager manager = ServiceManagerFactory.findManager(serviceManagerName == null ? serverServiceName.getServiceManagerName() : serviceManagerName);
        if (manager != null) {
            final ClientConnectionService ccs = new ClientConnectionService();
            try {
                String name = serverServiceName.getServiceName() + '$' + clientNo;
                name = name.replaceAll(":", "\\$");
                if (!manager.isRegisteredService(name) && manager.registerService(name, ccs)) {
                    serviceName = ccs.getServiceNameObject();
                    manager.createService(ccs.getServiceName());
                    manager.startService(ccs.getServiceName());
                }
            } catch (Exception e) {
                throw new ConnectException(e);
            }
        }
    }
    isConnected = true;
    isServerClosed = false;
}
Also used : ServiceManager(jp.ossc.nimbus.core.ServiceManager) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) ConnectException(jp.ossc.nimbus.service.publish.ConnectException) ConnectException(jp.ossc.nimbus.service.publish.ConnectException)

Example 5 with ConnectException

use of jp.ossc.nimbus.service.publish.ConnectException in project nimbus by nimbus-org.

the class ClientConnectionImpl method reconnect.

private void reconnect() throws ConnectException, MessageSendException {
    boolean isNowReconnecting = isReconnecting;
    synchronized (this) {
        if (isNowReconnecting) {
            return;
        }
        isReconnecting = true;
        try {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                }
                socket = null;
            }
            ((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).reset();
            int tryCount = 0;
            boolean isSuccess = false;
            while (!isSuccess) {
                tryCount++;
                try {
                    connect(id, true);
                    if (subjects != null) {
                        Object[] subjectArray = subjects.keySet().toArray();
                        for (int i = 0; i < subjectArray.length; i++) {
                            Object subject = subjectArray[i];
                            Set keySet = (Set) subjects.get(subject);
                            if (keySet != null) {
                                String[] keys = (String[]) keySet.toArray(new String[keySet.size()]);
                                boolean containsNull = false;
                                List keyList = new ArrayList();
                                for (int j = 0; j < keys.length; j++) {
                                    if (keys[j] == null) {
                                        containsNull = true;
                                    } else {
                                        keyList.add(keys[j]);
                                    }
                                }
                                if (containsNull) {
                                    addSubject((String) subject);
                                    keys = (String[]) keyList.toArray(new String[keyList.size()]);
                                }
                                if (keys != null && keys.length != 0) {
                                    addSubject((String) subject, keys);
                                }
                            }
                        }
                    }
                    if (isStartReceive) {
                        long time = -1;
                        MessageId latestMessageId = ((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).getLatestMessageId();
                        if (latestMessageId != null) {
                            time = ((MessageReceiver) messageReceiveDaemon.getDaemonRunnable()).getLatestMessageReceiveTime() - reconnectBufferTime;
                        }
                        startReceive(time, true);
                    }
                    isSuccess = true;
                } catch (ConnectException e) {
                    if (tryCount >= reconnectCount) {
                        throw e;
                    } else {
                        if (receiveWarnMessageId != null) {
                            ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
                        }
                    }
                } catch (MessageSendException e) {
                    if (tryCount >= reconnectCount) {
                        throw e;
                    } else {
                        if (receiveWarnMessageId != null) {
                            ServiceManagerFactory.getLogger().write(receiveWarnMessageId, new Object[] { this }, e);
                        }
                    }
                }
                if (!isSuccess && reconnectInterval > 0) {
                    try {
                        Thread.sleep(reconnectInterval);
                    } catch (InterruptedException e) {
                        throw new ConnectException(e);
                    }
                }
            }
        } finally {
            isReconnecting = false;
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MessageSendException(jp.ossc.nimbus.service.publish.MessageSendException) List(java.util.List) ArrayList(java.util.ArrayList) ConnectException(jp.ossc.nimbus.service.publish.ConnectException)

Aggregations

ConnectException (jp.ossc.nimbus.service.publish.ConnectException)5 MessageSendException (jp.ossc.nimbus.service.publish.MessageSendException)5 IOException (java.io.IOException)4 ServiceManager (jp.ossc.nimbus.core.ServiceManager)3 EOFException (java.io.EOFException)2 InetAddress (java.net.InetAddress)2 Socket (java.net.Socket)2 SocketException (java.net.SocketException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 Daemon (jp.ossc.nimbus.daemon.Daemon)2 DatagramSocket (java.net.DatagramSocket)1 InetSocketAddress (java.net.InetSocketAddress)1 MulticastSocket (java.net.MulticastSocket)1 NetworkInterface (java.net.NetworkInterface)1 MessageCommunicateException (jp.ossc.nimbus.service.publish.MessageCommunicateException)1