diff --git a/lib/src/actor_handler.dart b/lib/src/actor_handler.dart index 760a142..a49f241 100644 --- a/lib/src/actor_handler.dart +++ b/lib/src/actor_handler.dart @@ -1,8 +1,6 @@ -import 'package:spawn_dart/spawn_dart.dart'; import 'package:spawn_dart/src/protocol/eigr/functions/protocol/actors/protocol.pb.dart'; abstract class ActorHandler { - ActorRef getActorRef(); String getRegisteredName(); ActorInvocationResponse handleInvoke(ActorInvocation invocation); } diff --git a/lib/src/reflect_helper.dart b/lib/src/reflect_helper.dart index fd51362..cd1841f 100644 --- a/lib/src/reflect_helper.dart +++ b/lib/src/reflect_helper.dart @@ -13,33 +13,20 @@ class ReflectHelper { output: SimpleConsoleOutput(), ); - static Object createInstance(Type type, - [Context? context, - Symbol? constructor, - List? arguments, - Map? namedArguments]) { - arguments = []; - constructor = Symbol(''); + static Object createInstance(Type type) { + List? arguments = []; + Map? namedArguments = {}; + Symbol? constructor = Symbol(''); var typeMirror = reflectType(type); if (typeMirror is ClassMirror) { - var constructors = - List.from(typeMirror.declarations.values.where((declare) { - return declare is MethodMirror && declare.isConstructor; - })); - - if (constructors.isEmpty) { - return typeMirror - .newInstance(constructor, arguments, namedArguments!) - .reflectee; - } - - throw ArgumentError( - "Cannot create the instance of the Actor type '$type'."); + return typeMirror + .newInstance(constructor, arguments, namedArguments) + .reflectee; } else { throw ArgumentError( - "Cannot create the instance of the Actor type '$type'."); + "Cannot create the instance of the Actor type '$type'. For now, it is not allowed to define Class Constructors for Actors"); } } diff --git a/lib/src/service.dart b/lib/src/service.dart index 62d786f..c3c3639 100644 --- a/lib/src/service.dart +++ b/lib/src/service.dart @@ -23,12 +23,12 @@ class Service { return version.substring(0, version.indexOf(' ')); }(); - late String system; - late Map actorHandlers; + late String spawnSystem; + late Map spawnActorHandlers; Service(String system, Map actorHandlers) { - system = system; - actorHandlers = actorHandlers; + spawnSystem = system; + spawnActorHandlers = actorHandlers; } Handler get handler { @@ -52,9 +52,10 @@ class Service { Response _handleInvocation(ActorInvocation actorInvocationRequest) { ActorInvocationResponse response; - if (actorInvocationRequest.actor.system == system && - actorHandlers.containsKey(actorInvocationRequest.actor.name)) { - ActorHandler handler = actorHandlers[actorInvocationRequest.actor.name]!; + if (actorInvocationRequest.actor.system == spawnSystem && + spawnActorHandlers.containsKey(actorInvocationRequest.actor.name)) { + ActorHandler handler = + spawnActorHandlers[actorInvocationRequest.actor.name]!; response = handler.handleInvoke(actorInvocationRequest); } else { response = ActorInvocationResponse.create(); diff --git a/lib/src/stateful_named_actor_handler.dart b/lib/src/stateful_named_actor_handler.dart index 28e3583..efec9c1 100644 --- a/lib/src/stateful_named_actor_handler.dart +++ b/lib/src/stateful_named_actor_handler.dart @@ -1,42 +1,64 @@ import 'dart:mirrors'; +import 'package:optional/optional.dart'; import 'package:spawn_dart/spawn_dart.dart'; import 'package:spawn_dart/src/actor_handler.dart'; -import 'package:spawn_dart/src/protocol/eigr/functions/protocol/actors/protocol.pb.dart'; +import 'package:spawn_dart/src/protocol/eigr/functions/protocol/actors/protocol.pb.dart' + as spawn_protocol; +import 'package:spawn_dart/src/reflect_helper.dart'; class StatefulNamedActorHandler implements ActorHandler { - Type? actorEntity; + Type? actorEntityType; DeclarationMirror? _mirror; - StatefulNamedActor? statefulNamedActorAnnotationInstance; + StatefulNamedActor? _statefulNamedActorAnnotationInstance; + List _allDeclaredMethods = []; StatefulNamedActorHandler(Type actorEntity) { - actorEntity = actorEntity; - _mirror = reflectClass(actorEntity); + actorEntityType = actorEntity; + _mirror = reflectClass(actorEntityType!); var statefulActorAnnotationMirror = reflectClass(StatefulNamedActor); final InstanceMirror? statefulActorAnnotationInstanceMirror = _mirror ?.metadata .firstWhere((d) => d.type == statefulActorAnnotationMirror); - statefulNamedActorAnnotationInstance = + _statefulNamedActorAnnotationInstance = (statefulActorAnnotationInstanceMirror?.reflectee as StatefulNamedActor); - } - @override - ActorRef getActorRef() { - // TODO: implement getActorRef - return ActorRef(); + if (_allDeclaredMethods.isEmpty) { + _allDeclaredMethods = + ReflectHelper.getAllMethods(reflectClass(_mirror.runtimeType)); + } } @override - ActorInvocationResponse handleInvoke(ActorInvocation invocation) { - return ActorInvocationResponse.create()..actorName = invocation.actor.name; + spawn_protocol.ActorInvocationResponse handleInvoke( + spawn_protocol.ActorInvocation invocation) { + // TODO: implement invoke + Optional actorInstance = + Optional.of(ReflectHelper.createInstance(actorEntityType!)); + + if (actorInstance.isEmpty) { + throw StateError("Actor not found Or error during instance creation"); + } + + spawn_protocol.ActorInvocationResponse response = + spawn_protocol.ActorInvocationResponse.create() + ..actorName = invocation.actor.name + ..actorSystem = invocation.actor.system + ..updatedContext = spawn_protocol.Context.create(); + return response; } @override String getRegisteredName() { - // TODO: implement getRegisteredName - return ""; + String? name = _statefulNamedActorAnnotationInstance?.name; + + if (name == null || name.isEmpty) { + return MirrorSystem.getName(_mirror!.simpleName); + } + + return name; } } diff --git a/lib/src/stateful_unnamed_actor_handler.dart b/lib/src/stateful_unnamed_actor_handler.dart index 9c57d52..81b5dc0 100644 --- a/lib/src/stateful_unnamed_actor_handler.dart +++ b/lib/src/stateful_unnamed_actor_handler.dart @@ -24,12 +24,6 @@ class StatefulUnNamedActorHandler implements ActorHandler { as StatefulUnNamedActor); } - @override - ActorRef getActorRef() { - // TODO: implement getActorRef - return ActorRef(); - } - @override ActorInvocationResponse handleInvoke(ActorInvocation invocation) { // TODO: implement invoke @@ -38,7 +32,13 @@ class StatefulUnNamedActorHandler implements ActorHandler { @override String getRegisteredName() { - // TODO: implement getRegisteredName - return ""; + String? name = statefulUnNamedActorAnnotationInstance?.name; + MirrorSystem.getName(_mirror!.simpleName); + + if (name == null || name.isEmpty) { + return MirrorSystem.getName(_mirror!.simpleName); + } + + return name; } } diff --git a/lib/src/stateless_named_actor_handler.dart b/lib/src/stateless_named_actor_handler.dart index 4c7e747..f57195e 100644 --- a/lib/src/stateless_named_actor_handler.dart +++ b/lib/src/stateless_named_actor_handler.dart @@ -13,16 +13,11 @@ class StatelessNamedActorHandler implements ActorHandler { _mirror = reflectClass(actorEntity); } - @override - ActorRef getActorRef() { - // TODO: implement getActorRef - return ActorRef(); - } - @override ActorInvocationResponse handleInvoke(ActorInvocation invocation) { // TODO: implement invoke - throw UnimplementedError(); + ActorInvocationResponse response = ActorInvocationResponse.create(); + return response; } @override diff --git a/lib/src/stateless_pooled_actor_handler.dart b/lib/src/stateless_pooled_actor_handler.dart index 6467c23..226c498 100644 --- a/lib/src/stateless_pooled_actor_handler.dart +++ b/lib/src/stateless_pooled_actor_handler.dart @@ -13,12 +13,6 @@ class StatelessPooledActorHandler implements ActorHandler { _mirror = reflectClass(actorEntity); } - @override - ActorRef getActorRef() { - // TODO: implement getActorRef - return ActorRef(); - } - @override ActorInvocationResponse handleInvoke(ActorInvocation invocation) { // TODO: implement invoke diff --git a/lib/src/stateless_unnamed_actor_handler.dart b/lib/src/stateless_unnamed_actor_handler.dart index a1731e1..4c8bde4 100644 --- a/lib/src/stateless_unnamed_actor_handler.dart +++ b/lib/src/stateless_unnamed_actor_handler.dart @@ -13,12 +13,6 @@ class StatelessUnNamedActorHandler implements ActorHandler { _mirror = reflectClass(actorEntity); } - @override - ActorRef getActorRef() { - // TODO: implement getActorRef - return ActorRef(); - } - @override ActorInvocationResponse handleInvoke(ActorInvocation invocation) { // TODO: implement invoke