Search in sources :

Example 31 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class HeaderExchangeHandler method received.

@Override
public void received(Channel channel, Object message) throws RemotingException {
    final ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
    if (message instanceof Request) {
        // handle request.
        Request request = (Request) message;
        if (request.isEvent()) {
            handlerEvent(channel, request);
        } else {
            if (request.isTwoWay()) {
                handleRequest(exchangeChannel, request);
            } 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);
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Request(org.apache.dubbo.remoting.exchange.Request) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) RemotingException(org.apache.dubbo.remoting.RemotingException) ExecutionException(org.apache.dubbo.remoting.ExecutionException)

Example 32 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class AbstractClient method send.

@Override
public void send(Object message, boolean sent) throws RemotingException {
    if (needReconnect && !isConnected()) {
        connect();
    }
    Channel channel = getChannel();
    // TODO Can the value returned by getChannel() be null? need improvement.
    if (channel == null || !channel.isConnected()) {
        throw new RemotingException(this, "message can not send, because channel is closed . url:" + getUrl());
    }
    channel.send(message, sent);
}
Also used : Channel(org.apache.dubbo.remoting.Channel) RemotingException(org.apache.dubbo.remoting.RemotingException)

Example 33 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class HeaderExchangeServer method sendChannelReadOnlyEvent.

private void sendChannelReadOnlyEvent() {
    Request request = new Request();
    request.setEvent(READONLY_EVENT);
    request.setTwoWay(false);
    request.setVersion(Version.getProtocolVersion());
    Collection<Channel> channels = getChannels();
    for (Channel channel : channels) {
        try {
            if (channel.isConnected()) {
                channel.send(request, getUrl().getParameter(Constants.CHANNEL_READONLYEVENT_SENT_KEY, true));
            }
        } catch (RemotingException e) {
            logger.warn("send cannot write message error.", e);
        }
    }
}
Also used : ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) RemotingException(org.apache.dubbo.remoting.RemotingException) Request(org.apache.dubbo.remoting.exchange.Request)

Example 34 with RemotingException

use of org.apache.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 (CollectionUtils.isEmpty(history)) {
            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 (CollectionUtils.isNotEmpty(history) && 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(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException) RemotingException(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LinkedList(java.util.LinkedList)

Example 35 with RemotingException

use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.

the class MulticastExchangeNetworkerTest method testJoin.

@Test
public void testJoin() throws RemotingException, InterruptedException {
    final String groupURL = "multicast://224.5.6.7:1234";
    MulticastExchangeNetworker multicastExchangeNetworker = new MulticastExchangeNetworker();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    Peer peer1 = multicastExchangeNetworker.lookup(URL.valueOf(groupURL)).join(URL.valueOf("exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header"), new ExchangeHandlerAdapter() {

        @Override
        public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) throws RemotingException {
            countDownLatch.countDown();
            return super.reply(channel, msg);
        }
    });
    Peer peer2 = multicastExchangeNetworker.lookup(URL.valueOf(groupURL)).join(URL.valueOf("exchange://0.0.0.0:" + NetUtils.getAvailablePort() + "?exchanger=header"), mock(ExchangeHandler.class));
    while (true) {
        for (Channel channel : peer1.getChannels()) {
            channel.send("hello multicast exchange network!");
        }
        TimeUnit.MILLISECONDS.sleep(50);
        long count = countDownLatch.getCount();
        if (count > 0) {
            break;
        }
    }
    Group lookup = Networkers.lookup(groupURL);
    assertThat(lookup, not(nullValue()));
    assertThat(peer1, instanceOf(ExchangeServerPeer.class));
    peer1.close();
    peer2.close();
}
Also used : ExchangeHandlerAdapter(org.apache.dubbo.remoting.exchange.support.ExchangeHandlerAdapter) Group(org.apache.dubbo.remoting.p2p.Group) Peer(org.apache.dubbo.remoting.p2p.Peer) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) CountDownLatch(java.util.concurrent.CountDownLatch) ExchangeHandler(org.apache.dubbo.remoting.exchange.ExchangeHandler) CompletableFuture(java.util.concurrent.CompletableFuture) RemotingException(org.apache.dubbo.remoting.RemotingException) Test(org.junit.jupiter.api.Test)

Aggregations

RemotingException (org.apache.dubbo.remoting.RemotingException)39 IOException (java.io.IOException)13 Request (org.apache.dubbo.remoting.exchange.Request)7 Response (org.apache.dubbo.remoting.exchange.Response)6 RpcException (org.apache.dubbo.rpc.RpcException)6 Channel (org.apache.dubbo.remoting.Channel)5 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)5 Test (org.junit.jupiter.api.Test)5 InetSocketAddress (java.net.InetSocketAddress)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeoutException (org.apache.dubbo.remoting.TimeoutException)3 Transporter (org.apache.dubbo.remoting.Transporter)3 ExchangeClient (org.apache.dubbo.remoting.exchange.ExchangeClient)3 AppResponse (org.apache.dubbo.rpc.AppResponse)3 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)3 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)3 ChannelFuture (io.netty.channel.ChannelFuture)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2