@Override
 public void onReceive(Object message) throws Exception {
   if (message instanceof Tcp.Received) {
     ByteString data = ((Tcp.Received) message).data();
     connection.tell(TcpMessage.write(data, new Ack()), getSelf());
   } else if (message instanceof Ack) {
     connection.tell(TcpMessage.resumeReading(), getSelf());
   }
 }
 @Override
 // #pull-accepting
 public void onReceive(Object message) throws Exception {
   if (message instanceof Tcp.Bound) {
     listener = getSender();
     // Accept connections one by one
     listener.tell(TcpMessage.resumeAccepting(1), getSelf());
   } else if (message instanceof Tcp.Connected) {
     ActorRef handler = getContext().actorOf(Props.create(PullEcho.class, getSender()));
     getSender().tell(TcpMessage.register(handler), getSelf());
     // Resume accepting connections
     listener.tell(TcpMessage.resumeAccepting(1), getSelf());
   }
 }
Example #3
0
  @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);
    }
  }
 private void demonstrateConnect() {
   // #pull-mode-connect
   final List<Inet.SocketOption> options = new ArrayList<Inet.SocketOption>();
   tcp.tell(
       TcpMessage.connect(new InetSocketAddress("localhost", 3000), null, options, null, true),
       getSelf());
   // #pull-mode-connect
 }
 @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
 }
Example #6
0
  @Override
  public void onReceive(Object msg) throws Exception {
    System.out.println("Handler received:" + msg);

    if (msg instanceof Received) {
      final ByteString data = ((Received) msg).data();
      getSender().tell(TcpMessage.write(data), getSelf());
    } else if (msg instanceof ConnectionClosed) {
      getContext().stop(getSelf());
    }
  }
 // #pull-reading-echo
 @Override
 public void preStart() throws Exception {
   connection.tell(TcpMessage.resumeReading(), getSelf());
 }
Example #8
0
 private void registerCodec(ActorRef connection) {
   final Props codecProps = Props.create(MsgCodec.class, connection, msgHandler);
   final ActorRef codec = getContext().actorOf(codecProps);
   connection.tell(TcpMessage.register(codec), getSelf());
 }
Example #9
0
 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());
 }