!act #3812: Remove Pipelines
This commit is contained in:
parent
8d2bc2bc40
commit
293dd0b9d2
31 changed files with 12 additions and 4400 deletions
|
|
@ -1,16 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
import akka.actor.ActorContext;
|
||||
import akka.io.PipelineContext;
|
||||
|
||||
//#actor-context
|
||||
public interface HasActorContext extends PipelineContext {
|
||||
|
||||
public ActorContext getContext();
|
||||
|
||||
}
|
||||
//#actor-context
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import akka.io.PipelineContext;
|
||||
|
||||
public interface HasByteOrder extends PipelineContext {
|
||||
|
||||
public ByteOrder byteOrder();
|
||||
|
||||
}
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
//#frame
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import scala.util.Either;
|
||||
import akka.io.AbstractSymmetricPipePair;
|
||||
import akka.io.PipePairFactory;
|
||||
import akka.io.PipelineContext;
|
||||
import akka.io.SymmetricPipePair;
|
||||
import akka.io.SymmetricPipelineStage;
|
||||
import akka.util.ByteString;
|
||||
import akka.util.ByteStringBuilder;
|
||||
|
||||
public class LengthFieldFrame extends
|
||||
SymmetricPipelineStage<PipelineContext, ByteString, ByteString> {
|
||||
|
||||
final int maxSize;
|
||||
|
||||
public LengthFieldFrame(int maxSize) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SymmetricPipePair<ByteString, ByteString> apply(final PipelineContext ctx) {
|
||||
return PipePairFactory
|
||||
.create(ctx, new AbstractSymmetricPipePair<ByteString, ByteString>() {
|
||||
|
||||
final ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
|
||||
ByteString buffer = null;
|
||||
|
||||
@Override
|
||||
public Iterable<Either<ByteString, ByteString>> onCommand(
|
||||
ByteString cmd) {
|
||||
final int length = cmd.length() + 4;
|
||||
if (length > maxSize) {
|
||||
return new ArrayList<Either<ByteString, ByteString>>(0);
|
||||
}
|
||||
final ByteStringBuilder bb = new ByteStringBuilder();
|
||||
bb.putInt(length, byteOrder);
|
||||
bb.append(cmd);
|
||||
return singleCommand(bb.result());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Either<ByteString, ByteString>> onEvent(
|
||||
ByteString event) {
|
||||
final ArrayList<Either<ByteString, ByteString>> res =
|
||||
new ArrayList<Either<ByteString, ByteString>>();
|
||||
ByteString current = buffer == null ? event : buffer.concat(event);
|
||||
while (true) {
|
||||
if (current.length() == 0) {
|
||||
buffer = null;
|
||||
return res;
|
||||
} else if (current.length() < 4) {
|
||||
buffer = current;
|
||||
return res;
|
||||
} else {
|
||||
final int length = current.iterator().getInt(byteOrder);
|
||||
if (length > maxSize)
|
||||
throw new IllegalArgumentException(
|
||||
"received too large frame of size " + length + " (max = "
|
||||
+ maxSize + ")");
|
||||
if (current.length() < length) {
|
||||
buffer = current;
|
||||
return res;
|
||||
} else {
|
||||
res.add(makeEvent(current.slice(4, length)));
|
||||
current = current.drop(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
//#frame
|
||||
|
|
@ -1,118 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Collections;
|
||||
|
||||
import scala.concurrent.duration.Duration;
|
||||
import scala.concurrent.duration.FiniteDuration;
|
||||
import scala.util.Either;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.io.AbstractSymmetricPipePair;
|
||||
import akka.io.PipePairFactory;
|
||||
import akka.io.SymmetricPipePair;
|
||||
import akka.io.SymmetricPipelineStage;
|
||||
import akka.util.ByteIterator;
|
||||
import akka.util.ByteString;
|
||||
import akka.util.ByteStringBuilder;
|
||||
|
||||
//#format
|
||||
public class MessageStage extends
|
||||
SymmetricPipelineStage<HasByteOrder, Message, ByteString> {
|
||||
|
||||
@Override
|
||||
public SymmetricPipePair<Message, ByteString> apply(final HasByteOrder context) {
|
||||
|
||||
return PipePairFactory
|
||||
.create(context, new AbstractSymmetricPipePair<Message, ByteString>() {
|
||||
|
||||
final ByteOrder byteOrder = context.byteOrder();
|
||||
|
||||
private void putString(ByteStringBuilder builder, String str) {
|
||||
final byte[] bytes = ByteString.fromString(str, "UTF-8").toArray();
|
||||
builder.putInt(bytes.length, byteOrder);
|
||||
builder.putBytes(bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Either<Message, ByteString>> onCommand(Message cmd) {
|
||||
final ByteStringBuilder builder = new ByteStringBuilder();
|
||||
|
||||
builder.putInt(cmd.getPersons().length, byteOrder);
|
||||
for (Message.Person p : cmd.getPersons()) {
|
||||
putString(builder, p.getFirst());
|
||||
putString(builder, p.getLast());
|
||||
}
|
||||
|
||||
builder.putInt(cmd.getHappinessCurve().length, byteOrder);
|
||||
builder.putDoubles(cmd.getHappinessCurve(), byteOrder);
|
||||
|
||||
return singleCommand(builder.result());
|
||||
}
|
||||
|
||||
//#decoding-omitted
|
||||
//#decoding
|
||||
private String getString(ByteIterator iter) {
|
||||
final int length = iter.getInt(byteOrder);
|
||||
final byte[] bytes = new byte[length];
|
||||
iter.getBytes(bytes);
|
||||
return ByteString.fromArray(bytes).utf8String();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Either<Message, ByteString>> onEvent(ByteString evt) {
|
||||
final ByteIterator iter = evt.iterator();
|
||||
|
||||
final int personLength = iter.getInt(byteOrder);
|
||||
final Message.Person[] persons = new Message.Person[personLength];
|
||||
for (int i = 0; i < personLength; ++i) {
|
||||
persons[i] = new Message.Person(getString(iter), getString(iter));
|
||||
}
|
||||
|
||||
final int curveLength = iter.getInt(byteOrder);
|
||||
final double[] curve = new double[curveLength];
|
||||
iter.getDoubles(curve, byteOrder);
|
||||
|
||||
// verify that this was all; could be left out to allow future
|
||||
// extensions
|
||||
assert iter.isEmpty();
|
||||
|
||||
return singleEvent(new Message(persons, curve));
|
||||
}
|
||||
//#decoding
|
||||
|
||||
ActorRef target = null;
|
||||
|
||||
//#mgmt-ticks
|
||||
private FiniteDuration lastTick = Duration.Zero();
|
||||
|
||||
@Override
|
||||
public Iterable<Either<Message, ByteString>> onManagementCommand(Object cmd) {
|
||||
//#omitted
|
||||
if (cmd instanceof PipelineTest.SetTarget) {
|
||||
target = ((PipelineTest.SetTarget) cmd).getRef();
|
||||
} else if (cmd instanceof TickGenerator.Tick && target != null) {
|
||||
target.tell(cmd, ActorRef.noSender());
|
||||
}
|
||||
//#omitted
|
||||
if (cmd instanceof TickGenerator.Tick) {
|
||||
final FiniteDuration timestamp = ((TickGenerator.Tick) cmd)
|
||||
.getTimestamp();
|
||||
System.out.println("time since last tick: "
|
||||
+ timestamp.minus(lastTick));
|
||||
lastTick = timestamp;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
//#mgmt-ticks
|
||||
//#decoding-omitted
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
//#format
|
||||
|
|
@ -1,157 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import akka.testkit.AkkaJUnitActorSystemResource;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import scala.concurrent.duration.Duration;
|
||||
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.PoisonPill;
|
||||
import akka.actor.Props;
|
||||
import akka.io.AbstractPipelineContext;
|
||||
import akka.io.PipelineFactory;
|
||||
import akka.io.PipelineInjector;
|
||||
import akka.io.PipelineSink;
|
||||
import akka.io.PipelineStage;
|
||||
import akka.testkit.JavaTestKit;
|
||||
import akka.testkit.TestProbe;
|
||||
import akka.util.ByteString;
|
||||
|
||||
public class PipelineTest {
|
||||
|
||||
//#message
|
||||
final Message msg = new Message(
|
||||
new Message.Person[] {
|
||||
new Message.Person("Alice", "Gibbons"),
|
||||
new Message.Person("Bob", "Sparseley")
|
||||
},
|
||||
new double[] { 1.0, 3.0, 5.0 });
|
||||
//#message
|
||||
|
||||
//#byteorder
|
||||
class Context extends AbstractPipelineContext implements HasByteOrder {
|
||||
|
||||
@Override
|
||||
public ByteOrder byteOrder() {
|
||||
return java.nio.ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
}
|
||||
final Context ctx = new Context();
|
||||
//#byteorder
|
||||
|
||||
@ClassRule
|
||||
public static AkkaJUnitActorSystemResource actorSystemResource =
|
||||
new AkkaJUnitActorSystemResource("PipelineTest");
|
||||
|
||||
private final ActorSystem system = actorSystemResource.getSystem();
|
||||
|
||||
@Test
|
||||
public void demonstratePipeline() throws Exception {
|
||||
final TestProbe probe = TestProbe.apply(system);
|
||||
final ActorRef commandHandler = probe.ref();
|
||||
final ActorRef eventHandler = probe.ref();
|
||||
//#build-sink
|
||||
final PipelineStage<Context, Message, ByteString, Message, ByteString> stages =
|
||||
PipelineStage.sequence(
|
||||
new MessageStage(),
|
||||
new LengthFieldFrame(10000)
|
||||
);
|
||||
|
||||
final PipelineSink<ByteString, Message> sink =
|
||||
new PipelineSink<ByteString, Message>() {
|
||||
|
||||
@Override
|
||||
public void onCommand(ByteString cmd) throws Throwable {
|
||||
commandHandler.tell(cmd, ActorRef.noSender());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(Message evt) throws Throwable {
|
||||
eventHandler.tell(evt, ActorRef.noSender());
|
||||
}
|
||||
};
|
||||
|
||||
final PipelineInjector<Message, ByteString> injector =
|
||||
PipelineFactory.buildWithSink(ctx, stages, sink);
|
||||
|
||||
injector.injectCommand(msg);
|
||||
//#build-sink
|
||||
final ByteString encoded = probe.expectMsgClass(ByteString.class);
|
||||
injector.injectEvent(encoded);
|
||||
final Message decoded = probe.expectMsgClass(Message.class);
|
||||
assert msg == decoded;
|
||||
}
|
||||
|
||||
static class SetTarget {
|
||||
final ActorRef ref;
|
||||
|
||||
public SetTarget(ActorRef ref) {
|
||||
super();
|
||||
this.ref = ref;
|
||||
}
|
||||
|
||||
public ActorRef getRef() {
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTick() {
|
||||
new JavaTestKit(system) {
|
||||
{
|
||||
class P extends Processor {
|
||||
public P(ActorRef cmds, ActorRef evts) throws Exception {
|
||||
super(cmds, evts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Object obj) throws Exception {
|
||||
if (obj.equals("fail!")) {
|
||||
throw new RuntimeException("FAIL!");
|
||||
}
|
||||
super.onReceive(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final ActorRef proc = system.actorOf(Props.create(
|
||||
P.class, this, getRef(), getRef()), "processor");
|
||||
expectMsgClass(TickGenerator.Tick.class);
|
||||
proc.tell(msg, ActorRef.noSender());
|
||||
final ByteString encoded = expectMsgClass(ByteString.class);
|
||||
proc.tell(encoded, ActorRef.noSender());
|
||||
final Message decoded = expectMsgClass(Message.class);
|
||||
assert msg == decoded;
|
||||
|
||||
new Within(Duration.create(1500, TimeUnit.MILLISECONDS),
|
||||
Duration.create(3, TimeUnit.SECONDS)) {
|
||||
protected void run() {
|
||||
expectMsgClass(TickGenerator.Tick.class);
|
||||
expectMsgClass(TickGenerator.Tick.class);
|
||||
}
|
||||
};
|
||||
proc.tell("fail!", ActorRef.noSender());
|
||||
new Within(Duration.create(1700, TimeUnit.MILLISECONDS),
|
||||
Duration.create(3, TimeUnit.SECONDS)) {
|
||||
protected void run() {
|
||||
expectMsgClass(TickGenerator.Tick.class);
|
||||
expectMsgClass(TickGenerator.Tick.class);
|
||||
proc.tell(PoisonPill.getInstance(), ActorRef.noSender());
|
||||
expectNoMsg();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import akka.actor.ActorContext;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.io.AbstractPipelineContext;
|
||||
import akka.io.PipelineFactory;
|
||||
import akka.io.PipelineInjector;
|
||||
import akka.io.PipelineSink;
|
||||
import akka.io.PipelineStage;
|
||||
import akka.util.ByteString;
|
||||
import scala.concurrent.duration.*;
|
||||
|
||||
//#actor
|
||||
public class Processor extends UntypedActor {
|
||||
|
||||
private class Context extends AbstractPipelineContext
|
||||
implements HasByteOrder, HasActorContext {
|
||||
|
||||
@Override
|
||||
public ActorContext getContext() {
|
||||
return Processor.this.getContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteOrder byteOrder() {
|
||||
return java.nio.ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final Context ctx = new Context();
|
||||
|
||||
final FiniteDuration interval = Duration.apply(1, TimeUnit.SECONDS);
|
||||
|
||||
final PipelineStage<Context, Message, ByteString, Message, ByteString> stages =
|
||||
PipelineStage.sequence(
|
||||
// Java 7 can infer these types, Java 6 cannot
|
||||
PipelineStage.<Context, Message, Message, ByteString, Message, Message,
|
||||
ByteString> sequence( //
|
||||
new TickGenerator<Message, Message>(interval), //
|
||||
new MessageStage()), //
|
||||
new LengthFieldFrame(10000));
|
||||
|
||||
private final ActorRef evts;
|
||||
private final ActorRef cmds;
|
||||
|
||||
final PipelineInjector<Message, ByteString> injector = PipelineFactory
|
||||
.buildWithSink(ctx, stages, new PipelineSink<ByteString, Message>() {
|
||||
|
||||
@Override
|
||||
public void onCommand(ByteString cmd) {
|
||||
cmds.tell(cmd, getSelf());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(Message evt) {
|
||||
evts.tell(evt, getSelf());
|
||||
}
|
||||
});
|
||||
|
||||
public Processor(ActorRef cmds, ActorRef evts) throws Exception {
|
||||
this.cmds = cmds;
|
||||
this.evts = evts;
|
||||
}
|
||||
|
||||
//#omitted
|
||||
@Override
|
||||
public void preStart() throws Exception {
|
||||
injector.managementCommand(new PipelineTest.SetTarget(cmds));
|
||||
}
|
||||
//#omitted
|
||||
|
||||
@Override
|
||||
public void onReceive(Object obj) throws Exception {
|
||||
if (obj instanceof Message) {
|
||||
injector.injectCommand((Message) obj);
|
||||
} else if (obj instanceof ByteString) {
|
||||
injector.injectEvent((ByteString) obj);
|
||||
} else if (obj instanceof TickGenerator.Trigger) {
|
||||
injector.managementCommand(obj);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//#actor
|
||||
|
||||
|
|
@ -1,202 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLEngine;
|
||||
|
||||
import akka.testkit.AkkaJUnitActorSystemResource;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
|
||||
import akka.actor.ActorContext;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
import akka.actor.UntypedActor;
|
||||
import akka.event.Logging;
|
||||
import akka.event.LoggingAdapter;
|
||||
import akka.io.AbstractPipelineContext;
|
||||
import akka.io.BackpressureBuffer;
|
||||
import akka.io.DelimiterFraming;
|
||||
import akka.io.HasLogging;
|
||||
import akka.io.PipelineStage;
|
||||
import static akka.io.PipelineStage.sequence;
|
||||
import akka.io.SslTlsSupport;
|
||||
import akka.io.StringByteStringAdapter;
|
||||
import akka.io.Tcp;
|
||||
import akka.io.Tcp.Bound;
|
||||
import akka.io.Tcp.Command;
|
||||
import akka.io.Tcp.CommandFailed;
|
||||
import akka.io.Tcp.Connected;
|
||||
import akka.io.Tcp.Event;
|
||||
import akka.io.Tcp.Received;
|
||||
import akka.io.TcpMessage;
|
||||
import akka.io.TcpPipelineHandler;
|
||||
import akka.io.TcpPipelineHandler.Init;
|
||||
import akka.io.TcpPipelineHandler.WithinActorContext;
|
||||
import akka.io.TcpReadWriteAdapter;
|
||||
import akka.io.ssl.SslTlsSupportSpec;
|
||||
import akka.testkit.AkkaSpec;
|
||||
import akka.testkit.JavaTestKit;
|
||||
import akka.util.ByteString;
|
||||
|
||||
public class SslDocTest {
|
||||
|
||||
static
|
||||
//#client
|
||||
public class SslClient extends UntypedActor {
|
||||
final InetSocketAddress remote;
|
||||
final SSLContext sslContext;
|
||||
final ActorRef listener;
|
||||
|
||||
final LoggingAdapter log = Logging
|
||||
.getLogger(getContext().system(), getSelf());
|
||||
|
||||
public SslClient(InetSocketAddress remote, SSLContext sslContext,
|
||||
ActorRef listener) {
|
||||
this.remote = remote;
|
||||
this.sslContext = sslContext;
|
||||
this.listener = listener;
|
||||
|
||||
// open a connection to the remote TCP port
|
||||
Tcp.get(getContext().system()).getManager()
|
||||
.tell(TcpMessage.connect(remote), getSelf());
|
||||
}
|
||||
|
||||
// this will hold the pipeline handler’s context
|
||||
Init<WithinActorContext, String, String> init = null;
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof CommandFailed) {
|
||||
getContext().stop(getSelf());
|
||||
|
||||
} else if (msg instanceof Connected) {
|
||||
// create a javax.net.ssl.SSLEngine for our peer in client mode
|
||||
final SSLEngine engine = sslContext.createSSLEngine(
|
||||
remote.getHostName(), remote.getPort());
|
||||
engine.setUseClientMode(true);
|
||||
|
||||
// build pipeline and set up context for communicating with TcpPipelineHandler
|
||||
init = TcpPipelineHandler.withLogger(log, sequence(sequence(sequence(sequence(
|
||||
new StringByteStringAdapter("utf-8"),
|
||||
new DelimiterFraming(1024, ByteString.fromString("\n"), true)),
|
||||
new TcpReadWriteAdapter()),
|
||||
new SslTlsSupport(engine)),
|
||||
new BackpressureBuffer(1000, 10000, 1000000)));
|
||||
|
||||
// create handler for pipeline, setting ourselves as payload recipient
|
||||
final ActorRef handler = getContext().actorOf(
|
||||
TcpPipelineHandler.props(init, getSender(), getSelf()));
|
||||
|
||||
// register the SSL handler with the connection
|
||||
getSender().tell(TcpMessage.register(handler), getSelf());
|
||||
|
||||
// and send a message across the SSL channel
|
||||
handler.tell(init.command("hello\n"), getSelf());
|
||||
|
||||
} else if (msg instanceof Init.Event) {
|
||||
// unwrap TcpPipelineHandler’s event into a Tcp.Event
|
||||
final String recv = init.event(msg);
|
||||
// and inform someone of the received payload
|
||||
listener.tell(recv, getSelf());
|
||||
}
|
||||
}
|
||||
}
|
||||
//#client
|
||||
|
||||
static
|
||||
//#server
|
||||
public class SslServer extends UntypedActor {
|
||||
final SSLContext sslContext;
|
||||
final ActorRef listener;
|
||||
|
||||
final LoggingAdapter log = Logging
|
||||
.getLogger(getContext().system(), getSelf());
|
||||
|
||||
public SslServer(SSLContext sslContext, ActorRef listener) {
|
||||
this.sslContext = sslContext;
|
||||
this.listener = listener;
|
||||
|
||||
// bind to a socket, registering ourselves as incoming connection handler
|
||||
Tcp.get(getContext().system()).getManager().tell(
|
||||
TcpMessage.bind(getSelf(), new InetSocketAddress("localhost", 0), 100),
|
||||
getSelf());
|
||||
}
|
||||
|
||||
// this will hold the pipeline handler’s context
|
||||
Init<WithinActorContext, String, String> init = null;
|
||||
|
||||
@Override
|
||||
public void onReceive(Object msg) {
|
||||
if (msg instanceof CommandFailed) {
|
||||
getContext().stop(getSelf());
|
||||
|
||||
} else if (msg instanceof Bound) {
|
||||
listener.tell(msg, getSelf());
|
||||
|
||||
} else if (msg instanceof Connected) {
|
||||
// create a javax.net.ssl.SSLEngine for our peer in server mode
|
||||
final InetSocketAddress remote = ((Connected) msg).remoteAddress();
|
||||
final SSLEngine engine = sslContext.createSSLEngine(
|
||||
remote.getHostName(), remote.getPort());
|
||||
engine.setUseClientMode(false);
|
||||
|
||||
// build pipeline and set up context for communicating with TcpPipelineHandler
|
||||
init = TcpPipelineHandler.withLogger(log, sequence(sequence(sequence(sequence(
|
||||
new StringByteStringAdapter("utf-8"),
|
||||
new DelimiterFraming(1024, ByteString.fromString("\n"), true)),
|
||||
new TcpReadWriteAdapter()),
|
||||
new SslTlsSupport(engine)),
|
||||
new BackpressureBuffer(1000, 10000, 1000000)));
|
||||
|
||||
// create handler for pipeline, setting ourselves as payload recipient
|
||||
final ActorRef handler = getContext().actorOf(
|
||||
TcpPipelineHandler.props(init, getSender(), getSelf()));
|
||||
|
||||
// register the SSL handler with the connection
|
||||
getSender().tell(TcpMessage.register(handler), getSelf());
|
||||
|
||||
} else if (msg instanceof Init.Event) {
|
||||
// unwrap TcpPipelineHandler’s event to get a Tcp.Event
|
||||
final String recv = init.event(msg);
|
||||
// inform someone of the received message
|
||||
listener.tell(recv, getSelf());
|
||||
// and reply (sender is the SSL handler created above)
|
||||
getSender().tell(init.command("world\n"), getSelf());
|
||||
}
|
||||
}
|
||||
}
|
||||
//#server
|
||||
|
||||
@ClassRule
|
||||
public static AkkaJUnitActorSystemResource actorSystemResource =
|
||||
new AkkaJUnitActorSystemResource("SslDocTest", AkkaSpec.testConf());
|
||||
|
||||
private final ActorSystem system = actorSystemResource.getSystem();
|
||||
|
||||
@Test
|
||||
public void demonstrateSslClient() {
|
||||
new JavaTestKit(system) {
|
||||
{
|
||||
final SSLContext ctx = SslTlsSupportSpec.createSslContext("/keystore", "/truststore", "changeme");
|
||||
|
||||
final ActorRef server = system.actorOf(Props.create(SslServer.class, ctx, getRef()));
|
||||
final Bound bound = expectMsgClass(Bound.class);
|
||||
assert getLastSender() == server;
|
||||
|
||||
final ActorRef client = system.actorOf(Props.create(SslClient.class, bound.localAddress(), ctx, getRef()));
|
||||
expectMsgEquals("hello\n");
|
||||
assert getLastSender() == server;
|
||||
expectMsgEquals("world\n");
|
||||
assert getLastSender() == client;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.io.japi;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import scala.concurrent.duration.Deadline;
|
||||
import scala.concurrent.duration.FiniteDuration;
|
||||
import scala.util.Either;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.io.AbstractPipePair;
|
||||
import akka.io.PipePair;
|
||||
import akka.io.PipePairFactory;
|
||||
import akka.io.PipelineStage;
|
||||
|
||||
//#tick-generator
|
||||
public class TickGenerator<Cmd, Evt> extends
|
||||
PipelineStage<HasActorContext, Cmd, Cmd, Evt, Evt> {
|
||||
|
||||
public static interface Trigger {};
|
||||
|
||||
public static class Tick implements Trigger {
|
||||
final FiniteDuration timestamp;
|
||||
|
||||
public Tick(FiniteDuration timestamp) {
|
||||
super();
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public FiniteDuration getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
private final FiniteDuration interval;
|
||||
|
||||
public TickGenerator(FiniteDuration interval) {
|
||||
this.interval = interval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PipePair<Cmd, Cmd, Evt, Evt> apply(final HasActorContext ctx) {
|
||||
return PipePairFactory.create(ctx,
|
||||
new AbstractPipePair<Cmd, Cmd, Evt, Evt>() {
|
||||
|
||||
private final Trigger trigger = new Trigger() {
|
||||
public String toString() {
|
||||
return "Tick[" + ctx.getContext().self().path() + "]";
|
||||
}
|
||||
};
|
||||
|
||||
private void schedule() {
|
||||
final ActorSystem system = ctx.getContext().system();
|
||||
system.scheduler().scheduleOnce(interval,
|
||||
ctx.getContext().self(), trigger, system.dispatcher(), null);
|
||||
}
|
||||
|
||||
{
|
||||
schedule();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Either<Evt, Cmd>> onCommand(Cmd cmd) {
|
||||
return singleCommand(cmd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Either<Evt, Cmd>> onEvent(Evt evt) {
|
||||
return singleEvent(evt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Either<Evt, Cmd>> onManagementCommand(Object cmd) {
|
||||
if (cmd == trigger) {
|
||||
ctx.getContext().self().tell(new Tick(Deadline.now().time()),
|
||||
ActorRef.noSender());
|
||||
schedule();
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
//#tick-generator
|
||||
Loading…
Add table
Add a link
Reference in a new issue