Search in sources :

Example 31 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class FileExchangeGroup method leave.

@Override
public void leave(URL url) throws RemotingException {
    super.leave(url);
    try {
        String full = url.toFullString();
        String[] lines = IOUtils.readLines(file);
        List<String> saves = new ArrayList<String>();
        for (String line : lines) {
            if (full.equals(line)) {
                return;
            }
            saves.add(line);
        }
        IOUtils.appendLines(file, saves.toArray(new String[0]));
    } catch (IOException e) {
        throw new RemotingException(new InetSocketAddress(NetUtils.getLocalHost(), 0), getUrl().toInetSocketAddress(), e.getMessage(), e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) RemotingException(com.alibaba.dubbo.remoting.RemotingException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 32 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class FileGroup method join.

public Peer join(URL url, ChannelHandler handler) throws RemotingException {
    Peer peer = super.join(url, handler);
    try {
        String full = url.toFullString();
        String[] lines = IOUtils.readLines(file);
        for (String line : lines) {
            if (full.equals(line)) {
                return peer;
            }
        }
        IOUtils.appendLines(file, new String[] { full });
    } catch (IOException e) {
        throw new RemotingException(new InetSocketAddress(NetUtils.getLocalHost(), 0), getUrl().toInetSocketAddress(), e.getMessage(), e);
    }
    return peer;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Peer(com.alibaba.dubbo.remoting.p2p.Peer) RemotingException(com.alibaba.dubbo.remoting.RemotingException) IOException(java.io.IOException)

Example 33 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class PeerMain method main.

public static void main(String[] args) throws Throwable {
    // Group address, supporting two groups of multicast and file, extensible
    String groupURL = "multicast://224.5.6.7:9911";
    // The native server address for cross networking
    final String peerURL = "dubbo://0.0.0.0:" + (((int) (Math.random() * 10000)) + 20000);
    // Join the group and get the peer reference
    Peer peer = Networkers.join(groupURL, peerURL, new ChannelHandlerAdapter() {

        @Override
        public void received(Channel channel, Object message) throws RemotingException {
            System.out.println("Received: " + message + " in " + peerURL);
        }
    });
    // Sending messages to other peers in the network
    for (int i = 0; i < Integer.MAX_VALUE; i++) {
        // Access to channels with all other peers, this list changes dynamically
        Collection<Channel> channels = peer.getChannels();
        if (channels != null && channels.size() > 0) {
            for (Channel channel : channels) {
                // Sending messages to a specified peer
                channel.send("(" + i + ") " + peerURL);
            }
        }
        Thread.sleep(1000);
    }
    // leave the network
    peer.leave();
}
Also used : ChannelHandlerAdapter(com.alibaba.dubbo.remoting.transport.ChannelHandlerAdapter) Channel(com.alibaba.dubbo.remoting.Channel) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 34 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class HeaderExchangeHandler method received.

public void received(Channel channel, Object message) throws RemotingException {
    channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis());
    ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    try {
        if (message instanceof Request) {
            // handle request.
            Request request = (Request) message;
            if (request.isEvent()) {
                handlerEvent(channel, request);
            } else {
                if (request.isTwoWay()) {
                    Response response = handleRequest(exchangeChannel, request);
                    channel.send(response);
                } else {
                    handler.received(exchangeChannel, request.getData());
                }
            }
        } else if (message instanceof Response) {
            handleResponse(channel, (Response) message);
        } else if (message instanceof String) {
            if (isClientSide(channel)) {
                Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl());
                logger.error(e.getMessage(), e);
            } else {
                String echo = handler.telnet(channel, (String) message);
                if (echo != null && echo.length() > 0) {
                    channel.send(echo);
                }
            }
        } else {
            handler.received(exchangeChannel, message);
        }
    } finally {
        HeaderExchangeChannel.removeChannelIfDisconnected(channel);
    }
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) ExecutionException(com.alibaba.dubbo.remoting.ExecutionException) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 35 with RemotingException

use of com.alibaba.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class TelnetCodec method decode.

@SuppressWarnings("unchecked")
protected Object decode(Channel channel, ChannelBuffer buffer, int readable, byte[] message) throws IOException {
    if (isClientSide(channel)) {
        return toString(message, getCharset(channel));
    }
    checkPayload(channel, readable);
    if (message == null || message.length == 0) {
        return DecodeResult.NEED_MORE_INPUT;
    }
    if (message[message.length - 1] == '\b') {
        // Windows backspace echo
        try {
            // double byte char
            boolean doublechar = message.length >= 3 && message[message.length - 3] < 0;
            channel.send(new String(doublechar ? new byte[] { 32, 32, 8, 8 } : new byte[] { 32, 8 }, getCharset(channel).name()));
        } catch (RemotingException e) {
            throw new IOException(StringUtils.toString(e));
        }
        return DecodeResult.NEED_MORE_INPUT;
    }
    for (Object command : EXIT) {
        if (isEquals(message, (byte[]) command)) {
            if (logger.isInfoEnabled()) {
                logger.info(new Exception("Close channel " + channel + " on exit command: " + Arrays.toString((byte[]) command)));
            }
            channel.close();
            return null;
        }
    }
    boolean up = endsWith(message, UP);
    boolean down = endsWith(message, DOWN);
    if (up || down) {
        LinkedList<String> history = (LinkedList<String>) channel.getAttribute(HISTORY_LIST_KEY);
        if (history == null || history.isEmpty()) {
            return DecodeResult.NEED_MORE_INPUT;
        }
        Integer index = (Integer) channel.getAttribute(HISTORY_INDEX_KEY);
        Integer old = index;
        if (index == null) {
            index = history.size() - 1;
        } else {
            if (up) {
                index = index - 1;
                if (index < 0) {
                    index = history.size() - 1;
                }
            } else {
                index = index + 1;
                if (index > history.size() - 1) {
                    index = 0;
                }
            }
        }
        if (old == null || !old.equals(index)) {
            channel.setAttribute(HISTORY_INDEX_KEY, index);
            String value = history.get(index);
            if (old != null && old >= 0 && old < history.size()) {
                String ov = history.get(old);
                StringBuilder buf = new StringBuilder();
                for (int i = 0; i < ov.length(); i++) {
                    buf.append("\b");
                }
                for (int i = 0; i < ov.length(); i++) {
                    buf.append(" ");
                }
                for (int i = 0; i < ov.length(); i++) {
                    buf.append("\b");
                }
                value = buf.toString() + value;
            }
            try {
                channel.send(value);
            } catch (RemotingException e) {
                throw new IOException(StringUtils.toString(e));
            }
        }
        return DecodeResult.NEED_MORE_INPUT;
    }
    for (Object command : EXIT) {
        if (isEquals(message, (byte[]) command)) {
            if (logger.isInfoEnabled()) {
                logger.info(new Exception("Close channel " + channel + " on exit command " + command));
            }
            channel.close();
            return null;
        }
    }
    byte[] enter = null;
    for (Object command : ENTER) {
        if (endsWith(message, (byte[]) command)) {
            enter = (byte[]) command;
            break;
        }
    }
    if (enter == null) {
        return DecodeResult.NEED_MORE_INPUT;
    }
    LinkedList<String> history = (LinkedList<String>) channel.getAttribute(HISTORY_LIST_KEY);
    Integer index = (Integer) channel.getAttribute(HISTORY_INDEX_KEY);
    channel.removeAttribute(HISTORY_INDEX_KEY);
    if (history != null && !history.isEmpty() && index != null && index >= 0 && index < history.size()) {
        String value = history.get(index);
        if (value != null) {
            byte[] b1 = value.getBytes();
            byte[] b2 = new byte[b1.length + message.length];
            System.arraycopy(b1, 0, b2, 0, b1.length);
            System.arraycopy(message, 0, b2, b1.length, message.length);
            message = b2;
        }
    }
    String result = toString(message, getCharset(channel));
    if (result.trim().length() > 0) {
        if (history == null) {
            history = new LinkedList<String>();
            channel.setAttribute(HISTORY_LIST_KEY, history);
        }
        if (history.isEmpty()) {
            history.addLast(result);
        } else if (!result.equals(history.getLast())) {
            history.remove(result);
            history.addLast(result);
            if (history.size() > 10) {
                history.removeFirst();
            }
        }
    }
    return result;
}
Also used : RemotingException(com.alibaba.dubbo.remoting.RemotingException) IOException(java.io.IOException) IOException(java.io.IOException) RemotingException(com.alibaba.dubbo.remoting.RemotingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LinkedList(java.util.LinkedList)

Aggregations

RemotingException (com.alibaba.dubbo.remoting.RemotingException)40 IOException (java.io.IOException)13 Request (com.alibaba.dubbo.remoting.exchange.Request)7 RpcException (com.alibaba.dubbo.rpc.RpcException)7 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)5 Response (com.alibaba.dubbo.remoting.exchange.Response)5 Channel (com.alibaba.dubbo.remoting.Channel)4 ExchangeClient (com.alibaba.dubbo.remoting.exchange.ExchangeClient)4 InetSocketAddress (java.net.InetSocketAddress)4 TimeoutException (com.alibaba.dubbo.remoting.TimeoutException)3 Transporter (com.alibaba.dubbo.remoting.Transporter)3 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)3 Test (org.junit.Test)3 ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)2 Serialization (com.alibaba.dubbo.common.serialize.Serialization)2 ExchangeServer (com.alibaba.dubbo.remoting.exchange.ExchangeServer)2 Result (com.alibaba.dubbo.rpc.Result)2 RpcResult (com.alibaba.dubbo.rpc.RpcResult)2 ChannelFuture (io.netty.channel.ChannelFuture)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2