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;
|
|
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.Queue;
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorRef;
|
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.event.Logging;
|
|
|
|
|
import akka.event.LoggingAdapter;
|
|
|
|
|
import akka.io.Tcp.ConnectionClosed;
|
2013-05-26 10:58:55 +02:00
|
|
|
import akka.io.Tcp.Event;
|
2013-04-16 22:31:09 +02:00
|
|
|
import akka.io.Tcp.Received;
|
|
|
|
|
import akka.io.TcpMessage;
|
|
|
|
|
import akka.japi.Procedure;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
|
|
|
|
|
//#simple-echo-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 SimpleEchoHandler extends AbstractActor {
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
final LoggingAdapter log = Logging
|
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
|
|
|
.getLogger(getContext().system(), self());
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
final ActorRef connection;
|
|
|
|
|
final InetSocketAddress remote;
|
|
|
|
|
|
|
|
|
|
public static final long maxStored = 100000000;
|
|
|
|
|
public static final long highWatermark = maxStored * 5 / 10;
|
|
|
|
|
public static final long lowWatermark = maxStored * 2 / 10;
|
|
|
|
|
|
|
|
|
|
public SimpleEchoHandler(ActorRef connection, InetSocketAddress remote) {
|
|
|
|
|
this.connection = connection;
|
|
|
|
|
this.remote = remote;
|
|
|
|
|
|
|
|
|
|
// sign death pact: this actor stops when the connection is closed
|
|
|
|
|
getContext().watch(connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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();
|
|
|
|
|
buffer(data);
|
|
|
|
|
connection.tell(TcpMessage.write(data, ACK), self());
|
|
|
|
|
// now switch behavior to “waiting for acknowledgement”
|
|
|
|
|
getContext().become(buffering(), false);
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.match(ConnectionClosed.class, msg -> {
|
|
|
|
|
getContext().stop(self());
|
|
|
|
|
})
|
|
|
|
|
.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 final Receive buffering() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.match(Received.class, msg -> {
|
|
|
|
|
buffer(msg.data());
|
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
|
|
|
})
|
|
|
|
|
.match(Event.class, msg -> msg == ACK, msg -> {
|
2013-04-16 22:31:09 +02:00
|
|
|
acknowledge();
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
.match(ConnectionClosed.class, msg -> {
|
|
|
|
|
if (msg.isPeerClosed()) {
|
2013-04-16 22:31:09 +02:00
|
|
|
closing = true;
|
|
|
|
|
} else {
|
|
|
|
|
// could also be ErrorClosed, in which case we just give up
|
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
|
|
|
getContext().stop(self());
|
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
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
}
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
//#storage-omitted
|
|
|
|
|
public void postStop() {
|
|
|
|
|
log.info("transferred {} bytes from/to [{}]", transferred, remote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long transferred;
|
|
|
|
|
private long stored = 0;
|
|
|
|
|
private Queue<ByteString> storage = new LinkedList<ByteString>();
|
|
|
|
|
|
|
|
|
|
private boolean suspended = false;
|
|
|
|
|
private boolean closing = false;
|
|
|
|
|
|
2013-05-26 10:58:55 +02:00
|
|
|
private final Event ACK = new Event() {};
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
//#simple-helpers
|
|
|
|
|
protected void buffer(ByteString data) {
|
|
|
|
|
storage.add(data);
|
|
|
|
|
stored += data.size();
|
|
|
|
|
|
|
|
|
|
if (stored > maxStored) {
|
|
|
|
|
log.warning("drop connection to [{}] (buffer overrun)", remote);
|
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
|
|
|
getContext().stop(self());
|
2013-04-16 22:31:09 +02:00
|
|
|
|
|
|
|
|
} else if (stored > highWatermark) {
|
|
|
|
|
log.debug("suspending reading");
|
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
|
|
|
connection.tell(TcpMessage.suspendReading(), self());
|
2013-04-16 22:31:09 +02:00
|
|
|
suspended = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void acknowledge() {
|
|
|
|
|
final ByteString acked = storage.remove();
|
|
|
|
|
stored -= acked.size();
|
|
|
|
|
transferred += acked.size();
|
|
|
|
|
|
|
|
|
|
if (suspended && stored < lowWatermark) {
|
|
|
|
|
log.debug("resuming reading");
|
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
|
|
|
connection.tell(TcpMessage.resumeReading(), self());
|
2013-04-16 22:31:09 +02:00
|
|
|
suspended = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (storage.isEmpty()) {
|
|
|
|
|
if (closing) {
|
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
|
|
|
getContext().stop(self());
|
2013-04-16 22:31:09 +02:00
|
|
|
} else {
|
|
|
|
|
getContext().unbecome();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
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
|
|
|
connection.tell(TcpMessage.write(storage.peek(), ACK), self());
|
2013-04-16 22:31:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//#simple-helpers
|
|
|
|
|
//#storage-omitted
|
|
|
|
|
}
|
|
|
|
|
//#simple-echo-handler
|