コード例 #1
0
 @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());
   }
 }
コード例 #2
0
 @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());
   }
 }
コード例 #3
0
ファイル: Client.java プロジェクト: robstek/geo-publisher
  @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);
    }
  }
コード例 #4
0
 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
 }
コード例 #5
0
 @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
 }
コード例 #6
0
ファイル: Handler.java プロジェクト: solq360/myblog
  @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());
    }
  }
コード例 #7
0
 // #pull-reading-echo
 @Override
 public void preStart() throws Exception {
   connection.tell(TcpMessage.resumeReading(), getSelf());
 }
コード例 #8
0
ファイル: TcpServer.java プロジェクト: solq360/myblog
 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());
 }
コード例 #9
0
ファイル: TcpServer.java プロジェクト: solq360/myblog
 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());
 }