2013-02-15 11:22:39 +01:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2013-02-15 11:22:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.io;
|
|
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
|
|
|
|
import org.junit.ClassRule;
|
2013-02-15 11:22:39 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.UntypedActor;
|
|
|
|
|
//#imports
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.io.Inet;
|
2013-03-25 10:02:50 +01:00
|
|
|
import akka.io.UdpConnected;
|
|
|
|
|
import akka.io.UdpConnectedMessage;
|
2013-02-15 11:22:39 +01:00
|
|
|
import akka.io.UdpSO;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
//#imports
|
|
|
|
|
|
2013-03-25 10:02:50 +01:00
|
|
|
public class UdpConnectedDocTest {
|
2013-02-15 11:22:39 +01:00
|
|
|
|
|
|
|
|
static public class Demo extends UntypedActor {
|
|
|
|
|
ActorRef connectionActor = null;
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
ActorRef handler = self();
|
|
|
|
|
ActorSystem system = getContext().system();
|
2013-02-15 11:22:39 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onReceive(Object msg) {
|
|
|
|
|
if ("connect".equals(msg)) {
|
|
|
|
|
//#manager
|
2013-03-25 10:02:50 +01:00
|
|
|
final ActorRef udp = UdpConnected.get(system).manager();
|
2013-02-15 11:22:39 +01:00
|
|
|
//#manager
|
|
|
|
|
//#connect
|
|
|
|
|
final InetSocketAddress remoteAddr =
|
|
|
|
|
new InetSocketAddress("127.0.0.1", 12345);
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
udp.tell(UdpConnectedMessage.connect(handler, remoteAddr), self());
|
2013-02-17 13:38:06 +13:00
|
|
|
//#connect
|
|
|
|
|
//#connect-with-options
|
2013-02-15 11:22:39 +01:00
|
|
|
final InetSocketAddress localAddr =
|
|
|
|
|
new InetSocketAddress("127.0.0.1", 1234);
|
|
|
|
|
final List<Inet.SocketOption> options =
|
|
|
|
|
new ArrayList<Inet.SocketOption>();
|
|
|
|
|
options.add(UdpSO.broadcast(true));
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
udp.tell(UdpConnectedMessage.connect(handler, remoteAddr, localAddr, options), self());
|
2013-02-17 13:38:06 +13:00
|
|
|
//#connect-with-options
|
2013-02-15 11:22:39 +01:00
|
|
|
} else
|
|
|
|
|
//#connected
|
2013-03-25 10:02:50 +01:00
|
|
|
if (msg instanceof UdpConnected.Connected) {
|
|
|
|
|
final UdpConnected.Connected conn = (UdpConnected.Connected) msg;
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
connectionActor = sender(); // Save the worker ref for later use
|
2013-02-15 11:22:39 +01:00
|
|
|
}
|
|
|
|
|
//#connected
|
|
|
|
|
else
|
|
|
|
|
//#received
|
2013-03-25 10:02:50 +01:00
|
|
|
if (msg instanceof UdpConnected.Received) {
|
|
|
|
|
final UdpConnected.Received recv = (UdpConnected.Received) msg;
|
2013-02-15 11:22:39 +01:00
|
|
|
final ByteString data = recv.data();
|
|
|
|
|
// and do something with the received data ...
|
2013-03-25 10:02:50 +01:00
|
|
|
} else if (msg instanceof UdpConnected.CommandFailed) {
|
|
|
|
|
final UdpConnected.CommandFailed failed = (UdpConnected.CommandFailed) msg;
|
|
|
|
|
final UdpConnected.Command command = failed.cmd();
|
2013-02-15 11:22:39 +01:00
|
|
|
// react to failed connect, etc.
|
2013-03-25 10:02:50 +01:00
|
|
|
} else if (msg instanceof UdpConnected.Disconnected) {
|
2013-02-15 11:22:39 +01:00
|
|
|
// do something on disconnect
|
|
|
|
|
}
|
|
|
|
|
//#received
|
|
|
|
|
else
|
|
|
|
|
if ("send".equals(msg)) {
|
|
|
|
|
ByteString data = ByteString.empty();
|
|
|
|
|
//#send
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
connectionActor.tell(UdpConnectedMessage.send(data), self());
|
2013-02-15 11:22:39 +01:00
|
|
|
//#send
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateConnect() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|