Example usage for io.netty.channel Channel remoteAddress

List of usage examples for io.netty.channel Channel remoteAddress

Introduction

In this page you can find the example usage for io.netty.channel Channel remoteAddress.

Prototype

SocketAddress remoteAddress();

Source Link

Document

Returns the remote address where this channel is connected to.

Usage

From source file:alluxio.util.CommonUtils.java

License:Apache License

/**
 * Unwraps a {@link alluxio.proto.dataserver.Protocol.Response} associated with a channel.
 *
 * @param response the response/*w  ww. j  av  a2 s .  co m*/
 * @param channel the channel that receives this response
 */
public static void unwrapResponseFrom(Protocol.Response response, Channel channel)
        throws AlluxioStatusException {
    Status status = ProtoUtils.fromProto(response.getStatus());
    if (status != Status.OK) {
        throw AlluxioStatusException.from(status.withDescription(
                String.format("Channel to %s: %s", channel.remoteAddress(), response.getMessage())));
    }
}

From source file:ca.lambtoncollege.hauntedhouse.server.ServerHandler.java

License:Apache License

public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
    // Send the received message to all channels but the current one.
    //        for (Channel c: channels) {
    //            if (c != ctx.channel()) {
    //                c.writeAndFlush("[" + ctx.channel().remoteAddress() + "] " + msg + '\n');
    //            } else {
    //                c.writeAndFlush("[you] " + msg + '\n');
    //            }
    //        }/*ww  w.  j av  a2 s .  c o m*/
    String str = "";
    Channel clientA = ctx.channel();
    Channel clientB = getClient(clientA);
    String[] array = PropertyMgr.split(msg);
    if (clientB == null) {
        if (array[0].equals(Integer.toString(PropertyMgr.BYE))) {
            GlobalMap.resetWaiting();
            ctx.close();
        }
    } else {
        if (array[0].equals(Integer.toString(PropertyMgr.DURING_THE_GAME))) {
            str = String.format("%d,%s,%s,%s,%s", PropertyMgr.DURING_THE_GAME, array[1], array[2], array[3],
                    clientA.remoteAddress());
            clientB.writeAndFlush(str + "\n");
        } else if (array[0].equals(Integer.toString(PropertyMgr.BYE))) {
            str = String.format("%d,,%s", PropertyMgr.ANOTHER_PLAYER_LOST_THE_CONNECTION,
                    clientA.remoteAddress());
            map.remove(Integer.toString(clientA.hashCode()));
            map.remove(Integer.toString(clientB.hashCode()));
            clientB.writeAndFlush(str + "\n");
            ctx.close();
        }
    }

}

From source file:ca.lambtoncollege.netty.chat.SecureChatServerHandler.java

License:Apache License

public void messageReceived(ChannelHandlerContext ctx, String msg, Channel channel) throws Exception {
    // Send the received message to all channels but the current one.
    //        for (Channel c: channels) {
    //            if (c != ctx.channel()) {
    //                c.writeAndFlush("[" + ctx.channel().remoteAddress() + "] " + msg + '\n');
    //            } else {
    //                c.writeAndFlush("[you] " + msg + '\n');
    //            }
    //        }/*from   ww w  .  j ava 2  s .c  o  m*/

    System.out.println(channel);
    if (channel != null) {
        channel.writeAndFlush("[" + channel.remoteAddress() + "] " + msg + '\n');
    }
    //        ctx.channel().writeAndFlush("[you] " + msg + '\n');

    // Close the connection if the client has sent 'bye'.
    if ("bye".equals(msg.toLowerCase())) {
        ctx.close();
    }
}

From source file:ChatServer.ChatServerHandler.java

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    Channel incomming = ctx.channel();
    for (Channel channel : channels) {
        channel.writeAndFlush(new NewUser(1, incomming.remoteAddress().toString(), null, null));
    }/* w ww.j a  v a 2s. c om*/
    channels.add(ctx.channel());
}

From source file:co.rsk.net.discovery.PacketDecoderTest.java

License:Open Source License

@Test(expected = PeerDiscoveryException.class)
public void decodeInvalidMessage() throws Exception {

    PacketDecoder decoder = new PacketDecoder();
    ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
    InetSocketAddress sender = new InetSocketAddress("localhost", 44035);
    Channel channel = Mockito.mock(Channel.class);
    Mockito.when(ctx.channel()).thenReturn(channel);
    Mockito.when(channel.remoteAddress()).thenReturn(sender);

    decoder.decodeMessage(ctx, new byte[] { 11 }, sender);

    Assert.fail();//  w  w w.  j  ava2s .  c  o m
}

From source file:com.addthis.meshy.Meshy.java

License:Apache License

protected void channelConnected(Channel channel, ChannelState channelState) {
    synchronized (connectedChannels) {
        connectedChannels.add(channelState);
    }/*from w  w  w. ja v a  2  s.c  o m*/
    log.debug("{} channelConnected @ {}", this, channel.remoteAddress());
}

From source file:com.addthis.meshy.Meshy.java

License:Apache License

protected void channelClosed(Channel channel, ChannelState channelState) {
    synchronized (connectedChannels) {
        connectedChannels.remove(channelState);
        inPeering.remove(channelState.getName());
    }//w  w  w  .  j a va2 s . com
    synchronized (channelCloseListeners) {
        for (ChannelCloseListener channelCloseListener : channelCloseListeners) {
            channelCloseListener.channelClosed(channel);
        }
    }
    log.debug("{} channelClosed @ {}", this, channel.remoteAddress());
}

From source file:com.addthis.meshy.MeshyServer.java

License:Apache License

@Override
protected void channelConnected(Channel channel, ChannelState channelState) {
    super.channelConnected(channel, channelState);
    /* servers peer with other servers once a channel comes up */
    // assign unique id (local or remote inferred)
    channelState.setName("temp-uuid-" + nextSession.incrementAndGet());
    InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();
    if (channel.parent() == null) {
        log.debug("{} >>> starting peering with {}", MeshyServer.this, address);
        new PeerSource(this, channelState.getName());
    }//  ww  w . j  a  va 2 s  .co  m
}

From source file:com.addthis.meshy.service.file.FileTarget.java

License:Apache License

private static String peersToString(Iterable<Channel> peers) {
    try {/*  ww  w  . j av a 2 s .c o m*/
        StringBuilder sb = new StringBuilder();
        for (Channel peer : peers) {
            if (sb.length() > 0) {
                sb.append(',');
            }
            sb.append(((InetSocketAddress) peer.remoteAddress()).getHostName());
        }
        return sb.toString();
    } catch (Exception e) {
        return e.getMessage();
    }
}

From source file:com.addthis.meshy.SourceHandler.java

License:Apache License

public String getPeerString() {
    StringBuilder sb = new StringBuilder(10 * channels.size());
    synchronized (channels) {
        for (Channel channel : channels) {
            if (sb.length() > 0) {
                sb.append(",");
            }/*from ww  w . j ava2s. c  om*/
            sb.append(channel.remoteAddress());
        }
    }
    return sb.toString();
}