1- package io .eigr .spawn ;
1+ package io .eigr .spawn . api ;
22
33import com .sun .net .httpserver .HttpServer ;
44import io .eigr .functions .protocol .Protocol ;
55import io .eigr .functions .protocol .actors .ActorOuterClass ;
6+ import io .eigr .spawn .api .actors .ActorFactory ;
67import io .eigr .spawn .api .actors .ActorRef ;
7- import io .eigr .spawn .api .annotations .NamedActor ;
8- import io .eigr .spawn .api .annotations .PooledActor ;
9- import io .eigr .spawn .api .annotations .UnNamedActor ;
8+ import io .eigr .spawn .api .actors . annotations .NamedActor ;
9+ import io .eigr .spawn .api .actors . annotations .PooledActor ;
10+ import io .eigr .spawn .api .actors . annotations .UnNamedActor ;
1011import io .eigr .spawn .internal .Entity ;
1112import io .eigr .spawn .internal .client .OkHttpSpawnClient ;
1213import io .eigr .spawn .internal .client .SpawnClient ;
@@ -62,11 +63,6 @@ public String getSystem() {
6263 return system ;
6364 }
6465
65- public void start () throws Exception {
66- startServer ();
67- registerActorSystem ();
68- }
69-
7066 public ActorRef createActorRef (String system , String name ) throws Exception {
7167 return ActorRef .of (this .client , system , name );
7268 }
@@ -75,9 +71,14 @@ public ActorRef createActorRef(String system, String name, String parent) throws
7571 return ActorRef .of (this .client , system , name , parent );
7672 }
7773
74+ public void start () throws Exception {
75+ startServer ();
76+ registerActorSystem ();
77+ }
78+
7879 private void startServer () throws IOException {
7980 HttpServer httpServer = HttpServer .create (new InetSocketAddress ("127.0.0.1" , this .port ), 0 );
80- httpServer .createContext ("/api/v1/actors/actions" , new ActorServiceHandler (this . system , this .entities ));
81+ httpServer .createContext ("/api/v1/actors/actions" , new ActorServiceHandler (this , this .entities ));
8182 //httpServer.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
8283 httpServer .setExecutor (Executors .newCachedThreadPool ());
8384 httpServer .start ();
@@ -154,15 +155,15 @@ private Map<String, ActorOuterClass.Actor> getActors(List<Entity> entities) {
154155 )
155156 .setMetadata (metadata )
156157 .setSettings (settings )
157- .addAllActions (getCommands (actorEntity ))
158- .addAllTimerActions (getTimerCommands (actorEntity ))
158+ .addAllActions (getActions (actorEntity ))
159+ .addAllTimerActions (getTimerActions (actorEntity ))
159160 .setState (ActorOuterClass .ActorState .newBuilder ().build ())
160161 .build ();
161162
162163 }).collect (Collectors .toMap (actor -> actor .getId ().getName (), Function .identity ()));
163164 }
164165
165- private List <ActorOuterClass .Action > getCommands (Entity actorEntity ) {
166+ private List <ActorOuterClass .Action > getActions (Entity actorEntity ) {
166167 return actorEntity .getActions ()
167168 .values ()
168169 .stream ()
@@ -175,7 +176,7 @@ private List<ActorOuterClass.Action> getCommands(Entity actorEntity) {
175176 .collect (Collectors .toList ());
176177 }
177178
178- private List <ActorOuterClass .FixedTimerAction > getTimerCommands (Entity actorEntity ) {
179+ private List <ActorOuterClass .FixedTimerAction > getTimerActions (Entity actorEntity ) {
179180 List <ActorOuterClass .FixedTimerAction > timerActions = actorEntity .getTimerActions ()
180181 .values ()
181182 .stream ()
@@ -195,6 +196,17 @@ private List<ActorOuterClass.FixedTimerAction> getTimerCommands(Entity actorEnti
195196 return timerActions ;
196197 }
197198
199+ @ Override
200+ public String toString () {
201+ return new StringJoiner (", " , Spawn .class .getSimpleName () + "[" , "]" )
202+ .add ("system='" + system + "'" )
203+ .add ("port=" + port )
204+ .add ("proxyHost='" + proxyHost + "'" )
205+ .add ("proxyPort=" + proxyPort )
206+ .add ("host='" + host + "'" )
207+ .toString ();
208+ }
209+
198210 public static final class SpawnSystem {
199211
200212 private SpawnClient client ;
@@ -224,30 +236,60 @@ public SpawnSystem withProxyPort(int port) {
224236 return this ;
225237 }
226238
227- public SpawnSystem withActor (Class <?> actorKlass ) {
239+ public SpawnSystem addActor (Class <?> actorKlass ) {
228240 Optional <Entity > maybeEntity = getEntity (actorKlass );
229241 if (maybeEntity .isPresent ()) {
230242 this .entities .add (maybeEntity .get ());
231243 }
232244 return this ;
233245 }
234246
247+ public SpawnSystem addActorWithArgs (Class <?> actorKlass , Object arg , ActorFactory factory ) {
248+ Optional <Entity > maybeEntity = getEntity (actorKlass , arg , factory );
249+ if (maybeEntity .isPresent ()) {
250+ this .entities .add (maybeEntity .get ());
251+ }
252+ return this ;
253+ }
254+
235255 public Spawn build () {
236256 this .client = new OkHttpSpawnClient (this .system , this .proxyHost , this .proxyPort );
237257 return new Spawn (this );
238258 }
239259
240260 private Optional <Entity > getEntity (Class <?> actorKlass ) {
241261 if (Objects .nonNull (actorKlass .getAnnotation (NamedActor .class ))) {
242- return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (NamedActor .class )));
262+ return Optional .of (Entity .fromAnnotationToEntity (
263+ actorKlass , actorKlass .getAnnotation (NamedActor .class ), null , null ));
243264 }
244265
245266 if (Objects .nonNull (actorKlass .getAnnotation (UnNamedActor .class ))) {
246- return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (UnNamedActor .class )));
267+ return Optional .of (Entity .fromAnnotationToEntity (
268+ actorKlass , actorKlass .getAnnotation (UnNamedActor .class ), null , null ));
247269 }
248270
249271 if (Objects .nonNull (actorKlass .getAnnotation (PooledActor .class ))) {
250- return Optional .of (Entity .fromAnnotationToEntity (actorKlass , actorKlass .getAnnotation (PooledActor .class )));
272+ return Optional .of (Entity .fromAnnotationToEntity (
273+ actorKlass , actorKlass .getAnnotation (PooledActor .class ), null , null ));
274+ }
275+
276+ return Optional .empty ();
277+ }
278+
279+ private Optional <Entity > getEntity (Class <?> actorType , Object arg , ActorFactory factory ) {
280+ if (Objects .nonNull (actorType .getAnnotation (NamedActor .class ))) {
281+ NamedActor annotation = actorType .getAnnotation (NamedActor .class );
282+ return Optional .of (Entity .fromAnnotationToEntity (actorType , annotation , arg , factory ));
283+ }
284+
285+ if (Objects .nonNull (actorType .getAnnotation (UnNamedActor .class ))) {
286+ UnNamedActor annotation = actorType .getAnnotation (UnNamedActor .class );
287+ return Optional .of (Entity .fromAnnotationToEntity (actorType , annotation , arg , factory ));
288+ }
289+
290+ if (Objects .nonNull (actorType .getAnnotation (PooledActor .class ))) {
291+ PooledActor annotation = actorType .getAnnotation (PooledActor .class );
292+ return Optional .of (Entity .fromAnnotationToEntity (actorType , annotation , arg , factory ));
251293 }
252294
253295 return Optional .empty ();
0 commit comments