@Override public void onReceive(Object msg) throws Exception { if (msg instanceof Connect) { InetSocketAddress address = ((Connect) msg).getAddress(); log.debug("connecting to " + address); ActorRef tcp = Tcp.get(getContext().system()).manager(); tcp.tell(TcpMessage.connect(address), getSelf()); } else if (msg instanceof CommandFailed) { log.error(msg.toString()); app.tell(new ConnectFailed((CommandFailed) msg), getSelf()); } else if (msg instanceof Connected) { log.info("connected"); ActorRef listener = getContext() .actorOf(ClientListener.props(config), nameGenerator.getName(ClientListener.class)); listener.tell(msg, getSender()); getContext().watch(listener); } else if (msg instanceof Terminated) { log.warning("connection closed"); app.tell(new ConnectionClosed(), getSelf()); } else { unhandled(msg); } }
@Override public void preStart() throws Exception { // #pull-mode-bind tcp = Tcp.get(getContext().system()).manager(); final List<Inet.SocketOption> options = new ArrayList<Inet.SocketOption>(); tcp.tell( TcpMessage.bind(getSelf(), new InetSocketAddress("localhost", 0), 100, options, true), getSelf()); // #pull-mode-bind }
private void startServer(int port) { final InetSocketAddress endpoint = new InetSocketAddress("localhost", port); final Object bindCmd = TcpMessage.bind(getSelf(), endpoint, 100); Tcp.get(getContext().system()).getManager().tell(bindCmd, getSelf()); }