Skip to content

Commit

Permalink
Some progress on reflection side
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Santos committed Oct 18, 2023
1 parent c3977ee commit 154c5b8
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 72 deletions.
2 changes: 0 additions & 2 deletions lib/src/actor_handler.dart
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 8 additions & 21 deletions lib/src/reflect_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,20 @@ class ReflectHelper {
output: SimpleConsoleOutput(),
);

static Object createInstance(Type type,
[Context? context,
Symbol? constructor,
List? arguments,
Map<Symbol, dynamic>? namedArguments]) {
arguments = [];
constructor = Symbol('');
static Object createInstance(Type type) {
List? arguments = [];
Map<Symbol, dynamic>? 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");
}
}

Expand Down
15 changes: 8 additions & 7 deletions lib/src/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class Service {
return version.substring(0, version.indexOf(' '));
}();

late String system;
late Map<String, ActorHandler> actorHandlers;
late String spawnSystem;
late Map<String, ActorHandler> spawnActorHandlers;

Service(String system, Map<String, ActorHandler> actorHandlers) {
system = system;
actorHandlers = actorHandlers;
spawnSystem = system;
spawnActorHandlers = actorHandlers;
}

Handler get handler {
Expand All @@ -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();
Expand Down
52 changes: 37 additions & 15 deletions lib/src/stateful_named_actor_handler.dart
Original file line number Diff line number Diff line change
@@ -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<MethodMirror> _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<Object> 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;
}
}
16 changes: 8 additions & 8 deletions lib/src/stateful_unnamed_actor_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
9 changes: 2 additions & 7 deletions lib/src/stateless_named_actor_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/src/stateless_pooled_actor_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/src/stateless_unnamed_actor_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 154c5b8

Please sign in to comment.