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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
package akkamon.domain;
import akka.actor.typed.ActorRef;
import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.AbstractBehavior;
import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
public class SceneTrainerGroup extends AbstractBehavior<SceneTrainerGroup.Command> {
public interface Command { }
public static class TrainerOffline
implements Command, AkkamonNexus.Command {
public ActorRef<Trainer.Command> trainer;
public String sceneId;
public String trainerId;
public ActorRef<AkkamonNexus.Command> replyTo;
public TrainerOffline(ActorRef<Trainer.Command> trainerActor, String sceneId, String trainerId, ActorRef<AkkamonNexus.Command> replyTo) {
this.trainer = trainerActor;
this.sceneId = sceneId;
this.trainerId = trainerId;
this.replyTo = replyTo;
}
}
public static Behavior<Command> create(String TrainerGroupId) {
return Behaviors.setup(context -> new SceneTrainerGroup(context, TrainerGroupId));
}
private final String sceneId;
private final Map<String, ActorRef<Trainer.Command>> trainerIdToActor= new HashMap();
public SceneTrainerGroup(ActorContext<Command> context, String sceneId) {
super(context);
this.sceneId = sceneId;
getContext().getLog().info("SceneTrainerGroup Actor {} started", sceneId);
}
@Override
public Receive<Command> createReceive() {
return newReceiveBuilder()
.onMessage(
AkkamonNexus.RequestTrainerRegistration.class,
this::onTrainerRegistration
)
.onMessage(
AkkamonNexus.RequestTrainerOffline.class,
this::onTrainerOfflineRequest
)
.onMessage(
TrainerOffline.class,
this::onWatchedTrainerOffline
)
.onMessage(
AkkamonNexus.RequestStartMoving.class,
this::onStartMoving
)
.onMessage(
AkkamonNexus.RequestStopMoving.class,
this::onStopMoving
)
.onMessage(
AkkamonNexus.RequestNewTilePos.class,
this::onNewTilePos
)
.onMessage(
AkkamonNexus.RequestHeartBeat.class,
this::onHeartBeat
)
.build();
}
private SceneTrainerGroup onWatchedTrainerOffline(TrainerOffline trainerOfflineMsg) {
trainerOfflineMsg.replyTo.tell(trainerOfflineMsg);
trainerIdToActor.remove(trainerOfflineMsg.trainerId);
return this;
}
private SceneTrainerGroup onTrainerOfflineRequest(AkkamonNexus.RequestTrainerOffline trainerOfflineRequest) {
if (this.sceneId.equals(trainerOfflineRequest.sceneId)) {
ActorRef<Trainer.Command> trainerActor = trainerIdToActor.get(trainerOfflineRequest.trainerId);
if (trainerActor != null) {
trainerActor.tell(trainerOfflineRequest);
trainerOfflineRequest.replyTo.tell(new AkkamonNexus.RespondTrainerOffline(
trainerOfflineRequest.requestId,
trainerOfflineRequest.sceneId,
trainerOfflineRequest.session
));
} else {
getContext()
.getLog()
.warn(
"Ignoring trainerOffline for trainerId {}. There is no actor mapped to it.",
trainerOfflineRequest.trainerId
);
}
} else {
getContext()
.getLog()
.warn(
"Ignoring trainerOffline for {}. This actor is responsible for {}.",
trainerOfflineRequest.sceneId,
this.sceneId);
}
return this;
}
private SceneTrainerGroup onHeartBeat(AkkamonNexus.RequestHeartBeat heartBeatRequest) {
Map<String, ActorRef<Trainer.Command>> trainerIdToActorCopy = new HashMap<>(this.trainerIdToActor);
getContext()
.spawnAnonymous(
HeartBeatQuery.create(
trainerIdToActorCopy,
heartBeatRequest.requestId,
sceneId,
heartBeatRequest.replyTo,
Duration.ofSeconds(3)
)
);
return this;
}
private SceneTrainerGroup onNewTilePos(AkkamonNexus.RequestNewTilePos newTilePosRequest) {
if (this.sceneId.equals(newTilePosRequest.sceneId)) {
ActorRef<Trainer.Command> trainerActor = trainerIdToActor.get(newTilePosRequest.trainerId);
if (trainerActor != null) {
trainerActor.tell(newTilePosRequest);
} else {
getContext()
.getLog()
.warn(
"Ignoring newTilePos for trainerId {}. There is no actor mapped to it.",
newTilePosRequest.trainerId
);
}
} else {
getContext()
.getLog()
.warn(
"Ignoring newTilePos for {}. This actor is responsible for {}.",
newTilePosRequest.sceneId,
this.sceneId);
}
return this;
}
private SceneTrainerGroup onStopMoving(AkkamonNexus.RequestStopMoving stopMovingRequest) {
if (this.sceneId.equals(stopMovingRequest.sceneId)) {
ActorRef<Trainer.Command> trainerActor = trainerIdToActor.get(stopMovingRequest.trainerId);
if (trainerActor != null) {
trainerActor.tell(stopMovingRequest);
} else {
getContext()
.getLog()
.warn(
"Ignoring stopMovingRequest for trainerId {}. There is no actor mapped to it.",
stopMovingRequest.trainerId
);
}
} else {
getContext()
.getLog()
.warn(
"Ignoring stopMovingRequest for {}. This actor is responsible for {}.",
stopMovingRequest.sceneId,
this.sceneId);
}
return this;
}
private SceneTrainerGroup onStartMoving(AkkamonNexus.RequestStartMoving startMovingRequest) {
if (this.sceneId.equals(startMovingRequest.sceneId)) {
ActorRef<Trainer.Command> trainerActor = trainerIdToActor.get(startMovingRequest.trainerId);
if (trainerActor != null) {
trainerActor.tell(startMovingRequest);
} else {
getContext()
.getLog()
.warn(
"Ignoring startMovingRequest for trainerId {}. There is no actor mapped to it.",
startMovingRequest.trainerId
);
}
} else {
getContext()
.getLog()
.warn(
"Ignoring startMovingRequest for {}. This actor is responsible for {}.",
startMovingRequest.sceneId,
this.sceneId);
}
return this;
}
private SceneTrainerGroup onTrainerRegistration(AkkamonNexus.RequestTrainerRegistration registrationRequest) {
if (this.sceneId.equals(registrationRequest.sceneId)) {
ActorRef<Trainer.Command> trainerActor = trainerIdToActor.get(registrationRequest.trainerId);
if (trainerActor != null) {
// TODO add optional already registered?
registrationRequest.replyTo.tell(new AkkamonNexus.TrainerRegistered(
registrationRequest.trainerId,
sceneId,
registrationRequest.session
));
} else {
getContext().getLog().info("Creating trainer actor for {}", registrationRequest.trainerId);
trainerActor =
getContext()
.spawn(Trainer.create(sceneId, registrationRequest.trainerId), "trainer-" + registrationRequest.trainerId);
getContext()
.watchWith(trainerActor, new SceneTrainerGroup.TrainerOffline(trainerActor, sceneId, registrationRequest.trainerId, registrationRequest.replyTo));
trainerIdToActor.put(registrationRequest.trainerId, trainerActor);
registrationRequest.replyTo.tell(new AkkamonNexus.TrainerRegistered(
registrationRequest.trainerId,
sceneId,
registrationRequest.session
));
}
} else {
getContext()
.getLog()
.warn(
"Ignoring TrainerRegistration request for {}. This actor is responsible for {}.",
registrationRequest.sceneId,
this.sceneId);
}
return this;
}
}
|