2013-02-15 11:47:46 +01:00
|
|
|
/**
|
2015-03-07 22:58:48 -08:00
|
|
|
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
2013-02-15 11:47:46 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.io;
|
|
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
import akka.actor.ActorRef;
|
2013-05-26 18:29:23 +02:00
|
|
|
import akka.actor.PoisonPill;
|
2013-02-15 11:47:46 +01:00
|
|
|
import akka.actor.UntypedActor;
|
2013-03-25 10:02:50 +01:00
|
|
|
import akka.io.Udp;
|
2013-05-26 18:29:23 +02:00
|
|
|
import akka.io.UdpConnected;
|
|
|
|
|
import akka.io.UdpConnectedMessage;
|
2013-03-25 10:02:50 +01:00
|
|
|
import akka.io.UdpMessage;
|
2013-05-26 18:29:23 +02:00
|
|
|
import akka.japi.Procedure;
|
2013-02-15 11:47:46 +01:00
|
|
|
import akka.util.ByteString;
|
|
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
//#imports
|
|
|
|
|
|
2013-03-25 10:02:50 +01:00
|
|
|
public class UdpDocTest {
|
2013-05-02 17:12:36 +02:00
|
|
|
|
2013-05-26 18:29:23 +02:00
|
|
|
//#sender
|
|
|
|
|
public static class SimpleSender extends UntypedActor {
|
|
|
|
|
final InetSocketAddress remote;
|
2013-05-02 17:12:36 +02:00
|
|
|
|
2013-05-26 18:29:23 +02:00
|
|
|
public SimpleSender(InetSocketAddress remote) {
|
|
|
|
|
this.remote = remote;
|
|
|
|
|
|
|
|
|
|
// request creation of a SimpleSender
|
|
|
|
|
final ActorRef mgr = Udp.get(getContext().system()).getManager();
|
|
|
|
|
mgr.tell(UdpMessage.simpleSender(), getSelf());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
if (msg instanceof Udp.SimpleSenderReady) {
|
|
|
|
|
getContext().become(ready(getSender()));
|
|
|
|
|
//#sender
|
|
|
|
|
getSender().tell(UdpMessage.send(ByteString.fromString("hello"), remote), getSelf());
|
|
|
|
|
//#sender
|
|
|
|
|
} else unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Procedure<Object> ready(final ActorRef send) {
|
|
|
|
|
return new Procedure<Object>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Object msg) throws Exception {
|
|
|
|
|
if (msg instanceof String) {
|
|
|
|
|
final String str = (String) msg;
|
|
|
|
|
send.tell(UdpMessage.send(ByteString.fromString(str), remote), getSelf());
|
|
|
|
|
//#sender
|
|
|
|
|
if (str.equals("world")) {
|
|
|
|
|
send.tell(PoisonPill.getInstance(), getSelf());
|
|
|
|
|
}
|
|
|
|
|
//#sender
|
|
|
|
|
|
|
|
|
|
} else unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-02-15 11:47:46 +01:00
|
|
|
}
|
2013-05-02 17:12:36 +02:00
|
|
|
}
|
2013-05-26 18:29:23 +02:00
|
|
|
//#sender
|
|
|
|
|
|
|
|
|
|
//#listener
|
|
|
|
|
public static class Listener extends UntypedActor {
|
|
|
|
|
final ActorRef nextActor;
|
|
|
|
|
|
|
|
|
|
public Listener(ActorRef nextActor) {
|
|
|
|
|
this.nextActor = nextActor;
|
|
|
|
|
|
|
|
|
|
// request creation of a bound listen socket
|
|
|
|
|
final ActorRef mgr = Udp.get(getContext().system()).getManager();
|
|
|
|
|
mgr.tell(
|
|
|
|
|
UdpMessage.bind(getSelf(), new InetSocketAddress("localhost", 0)),
|
|
|
|
|
getSelf());
|
|
|
|
|
}
|
2013-02-15 11:47:46 +01:00
|
|
|
|
2013-05-26 18:29:23 +02:00
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
if (msg instanceof Udp.Bound) {
|
|
|
|
|
final Udp.Bound b = (Udp.Bound) msg;
|
|
|
|
|
//#listener
|
|
|
|
|
nextActor.tell(b.localAddress(), getSender());
|
|
|
|
|
//#listener
|
|
|
|
|
getContext().become(ready(getSender()));
|
|
|
|
|
} else unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Procedure<Object> ready(final ActorRef socket) {
|
|
|
|
|
return new Procedure<Object>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Object msg) throws Exception {
|
|
|
|
|
if (msg instanceof Udp.Received) {
|
|
|
|
|
final Udp.Received r = (Udp.Received) msg;
|
|
|
|
|
// echo server example: send back the data
|
|
|
|
|
socket.tell(UdpMessage.send(r.data(), r.sender()), getSelf());
|
|
|
|
|
// or do some processing and forward it on
|
|
|
|
|
final Object processed = // parse data etc., e.g. using PipelineStage
|
|
|
|
|
//#listener
|
|
|
|
|
r.data().utf8String();
|
|
|
|
|
//#listener
|
|
|
|
|
nextActor.tell(processed, getSelf());
|
|
|
|
|
|
|
|
|
|
} else if (msg.equals(UdpMessage.unbind())) {
|
|
|
|
|
socket.tell(msg, getSelf());
|
|
|
|
|
|
|
|
|
|
} else if (msg instanceof Udp.Unbound) {
|
|
|
|
|
getContext().stop(getSelf());
|
|
|
|
|
|
|
|
|
|
} else unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#listener
|
|
|
|
|
|
|
|
|
|
//#connected
|
|
|
|
|
public static class Connected extends UntypedActor {
|
|
|
|
|
final InetSocketAddress remote;
|
|
|
|
|
|
|
|
|
|
public Connected(InetSocketAddress remote) {
|
|
|
|
|
this.remote = remote;
|
|
|
|
|
|
|
|
|
|
// create a restricted a.k.a. “connected” socket
|
|
|
|
|
final ActorRef mgr = UdpConnected.get(getContext().system()).getManager();
|
|
|
|
|
mgr.tell(UdpConnectedMessage.connect(getSelf(), remote), getSelf());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
if (msg instanceof UdpConnected.Connected) {
|
|
|
|
|
getContext().become(ready(getSender()));
|
|
|
|
|
//#connected
|
|
|
|
|
getSender()
|
|
|
|
|
.tell(UdpConnectedMessage.send(ByteString.fromString("hello")),
|
|
|
|
|
getSelf());
|
|
|
|
|
//#connected
|
|
|
|
|
} else unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Procedure<Object> ready(final ActorRef connection) {
|
|
|
|
|
return new Procedure<Object>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void apply(Object msg) throws Exception {
|
|
|
|
|
if (msg instanceof UdpConnected.Received) {
|
|
|
|
|
final UdpConnected.Received r = (UdpConnected.Received) msg;
|
|
|
|
|
// process data, send it on, etc.
|
|
|
|
|
// #connected
|
|
|
|
|
if (r.data().utf8String().equals("hello")) {
|
|
|
|
|
connection.tell(
|
|
|
|
|
UdpConnectedMessage.send(ByteString.fromString("world")),
|
|
|
|
|
getSelf());
|
|
|
|
|
}
|
|
|
|
|
// #connected
|
|
|
|
|
|
|
|
|
|
} else if (msg instanceof String) {
|
|
|
|
|
final String str = (String) msg;
|
|
|
|
|
connection
|
|
|
|
|
.tell(UdpConnectedMessage.send(ByteString.fromString(str)),
|
|
|
|
|
getSelf());
|
|
|
|
|
|
|
|
|
|
} else if (msg.equals(UdpConnectedMessage.disconnect())) {
|
|
|
|
|
connection.tell(msg, getSelf());
|
|
|
|
|
|
|
|
|
|
} else if (msg instanceof UdpConnected.Disconnected) {
|
|
|
|
|
getContext().stop(getSelf());
|
|
|
|
|
|
|
|
|
|
} else unhandled(msg);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2013-05-02 17:12:36 +02:00
|
|
|
}
|
2013-05-26 18:29:23 +02:00
|
|
|
//#connected
|
2013-02-15 11:47:46 +01:00
|
|
|
|
|
|
|
|
}
|