2013-04-16 22:31:09 +02:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2013-04-16 22:31:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.io.japi;
|
|
|
|
|
|
|
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2016-02-11 16:39:25 +01:00
|
|
|
import docs.AbstractJavaTest;
|
2013-05-02 17:12:36 +02:00
|
|
|
import org.junit.ClassRule;
|
2013-04-16 22:31:09 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
//#imports
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
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
|
|
|
import akka.actor.AbstractActor;
|
2013-04-16 22:31:09 +02:00
|
|
|
import akka.io.Tcp;
|
|
|
|
|
import akka.io.Tcp.Bound;
|
|
|
|
|
import akka.io.Tcp.CommandFailed;
|
|
|
|
|
import akka.io.Tcp.Connected;
|
|
|
|
|
import akka.io.Tcp.ConnectionClosed;
|
|
|
|
|
import akka.io.Tcp.Received;
|
|
|
|
|
import akka.io.TcpMessage;
|
|
|
|
|
import akka.japi.Procedure;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
//#imports
|
|
|
|
|
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
|
|
|
|
import akka.testkit.AkkaSpec;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class IODocTest extends AbstractJavaTest {
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#server
|
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
|
|
|
public class Server extends AbstractActor {
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
final ActorRef manager;
|
|
|
|
|
|
|
|
|
|
public Server(ActorRef manager) {
|
|
|
|
|
this.manager = manager;
|
|
|
|
|
}
|
2015-02-14 20:36:51 +03:30
|
|
|
|
|
|
|
|
public static Props props(ActorRef manager) {
|
|
|
|
|
return Props.create(Server.class, manager);
|
|
|
|
|
}
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() throws Exception {
|
|
|
|
|
final ActorRef tcp = Tcp.get(getContext().system()).manager();
|
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
|
|
|
tcp.tell(TcpMessage.bind(self(),
|
|
|
|
|
new InetSocketAddress("localhost", 0), 100), self());
|
2013-04-16 22:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
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
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Bound.class, msg -> {
|
|
|
|
|
manager.tell(msg, self());
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.match(CommandFailed.class, msg -> {
|
|
|
|
|
getContext().stop(self());
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.match(Connected.class, conn -> {
|
|
|
|
|
manager.tell(conn, self());
|
|
|
|
|
final ActorRef handler = getContext().actorOf(
|
|
|
|
|
Props.create(SimplisticHandler.class));
|
|
|
|
|
sender().tell(TcpMessage.register(handler), self());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-04-16 22:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#server
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#simplistic-handler
|
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
|
|
|
public class SimplisticHandler extends AbstractActor {
|
2013-04-16 22:31:09 +02:00
|
|
|
@Override
|
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
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Received.class, msg -> {
|
|
|
|
|
final ByteString data = msg.data();
|
|
|
|
|
System.out.println(data);
|
|
|
|
|
sender().tell(TcpMessage.write(data), self());
|
|
|
|
|
})
|
|
|
|
|
.match(ConnectionClosed.class, msg -> {
|
|
|
|
|
getContext().stop(self());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-04-16 22:31:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#simplistic-handler
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
//#client
|
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
|
|
|
public class Client extends AbstractActor {
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
final InetSocketAddress remote;
|
|
|
|
|
final ActorRef listener;
|
2015-02-14 20:36:51 +03:30
|
|
|
|
|
|
|
|
public static Props props(InetSocketAddress remote, ActorRef listener) {
|
|
|
|
|
return Props.create(Client.class, remote, listener);
|
|
|
|
|
}
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
public Client(InetSocketAddress remote, ActorRef listener) {
|
|
|
|
|
this.remote = remote;
|
|
|
|
|
this.listener = listener;
|
|
|
|
|
|
|
|
|
|
final ActorRef tcp = Tcp.get(getContext().system()).manager();
|
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
|
|
|
tcp.tell(TcpMessage.connect(remote), self());
|
2013-04-16 22:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
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
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(CommandFailed.class, msg -> {
|
|
|
|
|
listener.tell("failed", self());
|
|
|
|
|
getContext().stop(self());
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.match(Connected.class, msg -> {
|
|
|
|
|
listener.tell(msg, self());
|
|
|
|
|
sender().tell(TcpMessage.register(self()), self());
|
|
|
|
|
getContext().become(connected(sender()));
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-04-16 22:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
private Receive connected(final ActorRef connection) {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(ByteString.class, msg -> {
|
|
|
|
|
connection.tell(TcpMessage.write((ByteString) msg), self());
|
|
|
|
|
})
|
|
|
|
|
.match(CommandFailed.class, msg -> {
|
|
|
|
|
// OS kernel socket buffer was full
|
|
|
|
|
})
|
|
|
|
|
.match(Received.class, msg -> {
|
|
|
|
|
listener.tell(msg.data(), self());
|
|
|
|
|
})
|
|
|
|
|
.matchEquals("close", msg -> {
|
|
|
|
|
connection.tell(TcpMessage.close(), self());
|
|
|
|
|
})
|
|
|
|
|
.match(ConnectionClosed.class, msg -> {
|
|
|
|
|
getContext().stop(self());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2013-04-16 22:31:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//#client
|
2013-05-02 17:12:36 +02:00
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
|
|
|
|
new AkkaJUnitActorSystemResource("IODocTest", AkkaSpec.testConf());
|
|
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
|
|
|
|
|
2013-04-16 22:31:09 +02:00
|
|
|
@Test
|
|
|
|
|
public void testConnection() {
|
|
|
|
|
new JavaTestKit(system) {
|
|
|
|
|
{
|
|
|
|
|
@SuppressWarnings("unused")
|
2015-02-14 20:36:51 +03:30
|
|
|
final ActorRef server = system.actorOf(Server.props(getRef()), "server1");
|
2013-04-16 22:31:09 +02:00
|
|
|
final InetSocketAddress listen = expectMsgClass(Bound.class).localAddress();
|
2015-02-14 20:36:51 +03:30
|
|
|
final ActorRef client = system.actorOf(Client.props(listen, getRef()), "client1");
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
final Connected c1 = expectMsgClass(Connected.class);
|
|
|
|
|
final Connected c2 = expectMsgClass(Connected.class);
|
|
|
|
|
assert c1.localAddress().equals(c2.remoteAddress());
|
|
|
|
|
assert c2.localAddress().equals(c1.remoteAddress());
|
|
|
|
|
|
|
|
|
|
client.tell(ByteString.fromString("hello"), getRef());
|
|
|
|
|
final ByteString reply = expectMsgClass(ByteString.class);
|
|
|
|
|
assert reply.utf8String().equals("hello");
|
|
|
|
|
|
|
|
|
|
watch(client);
|
|
|
|
|
client.tell("close", getRef());
|
|
|
|
|
expectTerminated(client);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|