/
RemoteActorRef.java
150 lines (124 loc) · 5.06 KB
/
RemoteActorRef.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Quasar: lightweight threads and actors for the JVM.
* Copyright (c) 2013-2014, Parallel Universe Software Co. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 3.0
* as published by the Free Software Foundation.
*/
package co.paralleluniverse.actors;
import co.paralleluniverse.common.util.ServiceUtil;
import co.paralleluniverse.fibers.SuspendExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class should be extended by implementations of remote actors.
*
* @author pron
*/
public class RemoteActorRef<Message> extends ActorRefImpl<Message> {
private static final Logger LOG = LoggerFactory.getLogger(RemoteActorRef.class);
private static LifecycleListenerProxy lifecycleListenerProxy = ServiceUtil.loadSingletonService(LifecycleListenerProxy.class);
private final transient ActorRefImpl<Message> actor;
protected RemoteActorRef(ActorRef<Message> actor) {
super(actor.getName(), ((ActorRefImpl) actor).mailbox());
this.actor = (ActorRefImpl) actor;
}
protected void handleAdminMessage(RemoteActorAdminMessage msg) {
if (msg instanceof RemoteActorRegisterListenerAdminMessage) {
final RemoteActorRegisterListenerAdminMessage m = (RemoteActorRegisterListenerAdminMessage) msg;
actor.addLifecycleListener(((RemoteActorRegisterListenerAdminMessage) msg).getListener());
} else if (msg instanceof RemoteActorUnregisterListenerAdminMessage) {
RemoteActorUnregisterListenerAdminMessage unreg = (RemoteActorUnregisterListenerAdminMessage) msg;
if (unreg.getObserver() != null)
actor.removeObserverListeners(unreg.getObserver());
else
actor.removeLifecycleListener(unreg.getListener());
} else if (msg instanceof RemoteActorInterruptAdminMessage) {
actor.interrupt();
} else if (msg instanceof RemoteActorThrowInAdminMessage) {
actor.throwIn(((RemoteActorThrowInAdminMessage) msg).getException());
}
}
@Override
protected void internalSend(Object message) throws SuspendExecution {
actor.internalSend(message);
}
@Override
protected void internalSendNonSuspendable(Object message) {
actor.internalSendNonSuspendable(message);
}
@Override
public boolean trySend(Message message) {
return actor.trySend(message);
}
@Override
protected void addLifecycleListener(LifecycleListener listener) {
lifecycleListenerProxy.addLifecycleListener(this, listener);
}
@Override
protected void removeLifecycleListener(LifecycleListener listener) {
lifecycleListenerProxy.removeLifecycleListener(this, listener);
}
@Override
protected void removeObserverListeners(ActorRef actor) {
lifecycleListenerProxy.removeLifecycleListeners(this, actor);
}
@Override
protected void throwIn(RuntimeException e) {
internalSendNonSuspendable(new RemoteActorThrowInAdminMessage(e));
}
@Override
public void interrupt() {
internalSendNonSuspendable(new RemoteActorInterruptAdminMessage());
}
protected static abstract class RemoteActorAdminMessage implements java.io.Serializable {
}
static class RemoteActorRegisterListenerAdminMessage extends RemoteActorAdminMessage {
private final ActorLifecycleListener listener;
@Override
public String toString() {
return "RemoteActorListenerAdminMessage{" + "listener=" + listener + '}';
}
public RemoteActorRegisterListenerAdminMessage(ActorLifecycleListener listener) {
this.listener = listener;
}
public ActorLifecycleListener getListener() {
return listener;
}
}
static class RemoteActorUnregisterListenerAdminMessage extends RemoteActorAdminMessage {
private final ActorRefImpl observer;
private final LifecycleListener listener;
public RemoteActorUnregisterListenerAdminMessage(ActorRefImpl observer) {
this.observer = observer;
this.listener = null;
}
public RemoteActorUnregisterListenerAdminMessage(LifecycleListener listener) {
this.listener = listener;
this.observer = null;
}
public ActorRefImpl getObserver() {
return observer;
}
public LifecycleListener getListener() {
return listener;
}
}
private static class RemoteActorInterruptAdminMessage extends RemoteActorAdminMessage {
}
private static class RemoteActorThrowInAdminMessage extends RemoteActorAdminMessage {
private RuntimeException e;
public RemoteActorThrowInAdminMessage(RuntimeException e) {
this.e = e;
}
public RuntimeException getException() {
return e;
}
}
}