List of usage examples for io.netty.channel ServerChannel localAddress
SocketAddress localAddress();
From source file:me.ferrybig.p2pnetwork.Test.java
public static void main(String... arguments) throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try {/* w ww .j a v a 2s. c o m*/ final Peer main = new Peer(group, true); //main.startIncomingConnectionThread(5000); Scanner in = new Scanner(System.in); mainLoop: while (in.hasNextLine()) { String[] args = in.nextLine().split(" "); try { switch (args[0]) { case "exit": break mainLoop; case "connect": { InetAddress to = InetAddress.getByName(args[1]); int port = Integer.parseInt(args[2]); main.startOutgomingConnectionThread(to, port); } break; case "listen": { int port = Integer.parseInt(args[1]); main.startIncomingConnectionThread(port); } break; case "myaddr": { LOG.log(Level.INFO, "My address: {0}", main.getAddress()); } break; case "connections": { synchronized (main.servers) { for (ServerChannel s : main.servers) { LOG.log(Level.INFO, "Server channel: {0}", s); } } synchronized (main.clientsIn) { for (Channel s : main.clientsIn) { LOG.log(Level.INFO, "Incoming channel: {0}", s); } } synchronized (main.clientsOut) { for (Channel s : main.clientsOut) { LOG.log(Level.INFO, "Outgoing channel: {0}", s); } } } break; case "routing": { for (Map.Entry<Address, Byte> routes : main.routingTable.generateDelegatedRoutingMap() .entrySet()) { LOG.log(Level.INFO, "Routes to {0}: {1}", new Object[] { routes.getKey(), routes.getValue() }); } } break; case "send": { Address to = new Address(javax.xml.bind.DatatypeConverter.parseHexBinary(args[1])); LOG.log(Level.INFO, "Sending to: {0}", to); byte[] message = args[2].getBytes(); if (!main.routePacket(to, new PingPacket(message))) { LOG.log(Level.INFO, "Failed sending to: {0}", to); } } break; case "trace": { Address to = new Address(javax.xml.bind.DatatypeConverter.parseHexBinary(args[1])); LOG.log(Level.INFO, "Sending to: {0}", to); for (int i = 0; i < 10; i++) { if (!main.routePacket(to, new PingPacket(new byte[10]), (byte) i)) { LOG.log(Level.INFO, "Failed sending to: {0}", to); } } } break; case "ip": { Set<Integer> ports; synchronized (main.servers) { ports = main.servers.stream().map(s -> ((InetSocketAddress) s.localAddress()).getPort()) .collect(Collectors.toSet()); } Set<InetAddress> addresses = new HashSet<>(); for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InetAddress inetAddress : Collections.list(intf.getInetAddresses())) { if (inetAddress instanceof Inet6Address) { Inet6Address inet6Address = (Inet6Address) inetAddress; if (inet6Address.isMulticastAddress()) { continue; } if (inet6Address.isLinkLocalAddress()) { continue; } if (inet6Address.isIPv4CompatibleAddress()) { continue; } } else if (inetAddress instanceof Inet4Address) { Inet4Address inet4Address = (Inet4Address) inetAddress; if (inet4Address.isLoopbackAddress() || inet4Address.isLinkLocalAddress()) { continue; } } else { continue; } addresses.add(inetAddress); } } addresses.stream().flatMap(a -> ports.stream().map(p -> new InetSocketAddress(a, p))) .forEach(s -> LOG.log(Level.INFO, "Known ip: {0}", s)); } break; default: { LOG.log(Level.WARNING, "Unknown command: " + args[0]); } break; } } catch (Exception e) { LOG.log(Level.WARNING, "Exception caugth:", e); } } } finally { group.shutdownGracefully(); LOG.info("Shutting down..."); group.awaitTermination(1, TimeUnit.MINUTES); LOG.info("Shutdown!"); } }