-
Notifications
You must be signed in to change notification settings - Fork 73
/
Ponger.java
executable file
·52 lines (44 loc) · 1.74 KB
/
Ponger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.agilewiki.jactor.pingpong.stackoverflow;
import org.agilewiki.jactor.Actor;
import org.agilewiki.jactor.JAFuture;
import org.agilewiki.jactor.Mailbox;
import org.agilewiki.jactor.RP;
import org.agilewiki.jactor.lpc.JLPCActor;
import org.agilewiki.jactor.lpc.Request;
/**
* Receives Pings, and send Pongs back.
*/
public class Ponger extends JLPCActor {
/** Some mutable data of Ponger, which must be access in a thread-safe way. */
private int pings;
/** A Ping request, targeted at Ponger. */
private static class PingRequest2 extends Request<String,Ponger> {
private final String from;
public PingRequest2(final String _from) {
from = _from;
}
/** Processes the ping(String) request, from within the Thread of the Ponger. */
@SuppressWarnings("unchecked")
@Override
public void processRequest(final JLPCActor targetActor,
@SuppressWarnings("rawtypes") final RP responseProcessor)
throws Exception {
final Ponger ponger = (Ponger) targetActor;
responseProcessor.processResponse("Pong " + (ponger.pings++) + " to "
+ from + "!");
}
@Override
public boolean isTargetType(Actor targetActor) {
return Ponger.class.isInstance(targetActor);
}
}
/** Creates a Ponger, with it's own mailbox.
* @throws Exception */
public Ponger(final Mailbox mbox) throws Exception {
initialize(mbox);
}
/** Sends a ping(String) request to the Ponger. Blocks and returns response. */
public void ping(final Pinger pinger, final RP<String> rp) throws Exception {
new PingRequest2(pinger.getName()).send(pinger, this, rp);
}
}