public async Task ActorAutoFacActionSimpleTest() { IActorManager manager = new ActorManager(); var builder = new ContainerBuilder(); builder.RegisterType <StringCache>().As <ICache>(); builder.RegisterInstance(manager).As <IActorManager>(); IContainer container = builder.Build(); using (container.BeginLifetimeScope()) { manager.Register <ICache>(_context, (c, k, m) => { return(container.Resolve <ICache>(new TypedParameter(typeof(ActorKey), k))); }); ActorKey key = new ActorKey("cache/test"); ICache cache = await manager.CreateProxyAsync <ICache>(_context, key); (await cache.GetCount()).Should().Be(1); await manager.DeactivateAsync <ICache>(_context, key); (await cache.GetCount()).Should().Be(2); } await manager.DeactivateAllAsync(_context); }
/// <summary> /// Get actor, if already exist, will return or create new one /// </summary> /// <typeparam name="T">actor type</typeparam> /// <param name="actorKey">actor key</param> /// <returns>actor instance</returns> public T GetActor <T>(ActorKey actorKey) where T : IActor { actorKey.VerifyNotNull(nameof(actorKey)); Type actoryType = typeof(T); lock (_lock) { if (_actorCollection.TryGetValue(actoryType, actorKey, out ActorInstance actorInstance)) { _logger.LogInformation($"{nameof(GetActor)}: found instance, actorKey={actorKey}, actorInstance.ActorKey={actorInstance.ActorKey}"); return((T)actorInstance.GetInstance <T>()); } // Create actor object IActorBase actorBase = _registry.Create <T>(actorKey, this); (actorKey == actorBase.ActorKey).VerifyAssert(x => x, $"{nameof(GetActor)}: CREATED-Error Actor key !=, actorKey={actorKey}, actorBase.ActorKey={actorBase.ActorKey}"); _logger.LogTrace($"{nameof(GetActor)}: CREATED actorBase.ActorKey={actorBase.ActorKey}"); // Create proxy T actorInterface = ActorProxy <T> .Create(actorBase, this); actorInstance = new ActorInstance(typeof(T), actorKey, actorBase, actorInterface); // Add to actor collection _actorCollection.Set(actorInstance); // Create proxy for interface return(actorInstance.GetInstance <T>()); } }
public async Task ActorSingleChainTest() { var builder = new ContainerBuilder(); builder.RegisterType <ActorNode>().As <IActorNode>(); builder.RegisterType <ActorSum>().As <IActorSum>(); ILifetimeScope container = builder.Build(); IActorManager manager = new ActorManagerBuilder() .Register <IActorNode>((c, k, m) => container.Resolve <IActorNode>(new TypedParameter(typeof(ActorKey), k), new TypedParameter(typeof(IActorManager), m))) .Register <IActorSum>((c, k, m) => container.Resolve <IActorSum>(new TypedParameter(typeof(ActorKey), k), new TypedParameter(typeof(IActorManager), m))) .Build(); using (container) { ActorKey key = new ActorKey("node/test"); IActorNode node = await manager.CreateProxyAsync <IActorNode>(_context, key); int sum = 0; for (int i = 0; i < 10; i++) { await node.Add(_context, i); sum += i; } IActorSum sumActor = await manager.CreateProxyAsync <IActorSum>(_context, new ActorKey(sumActorName)); (await sumActor.GetSum()).Should().Be(sum); } await manager.DeactivateAllAsync(_context); }
public async Task ActorMethodTest() { int count = 0; ActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache(y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = await manager.CreateProxy <ICache>(key1); count.Should().Be(1); const string firstText = "first"; bool test = await cache1.IsCached(firstText); test.Should().BeFalse(); await cache1.Add(firstText); test = await cache1.IsCached(firstText); test.Should().BeTrue(); (await manager.Deactivate <ICache>(key1)).Should().BeTrue();; count.Should().Be(0); await manager.DeactivateAll(); }
public void ActorActivated(INetNode node, ActorKey actorKey) { if (_logger.IsEnabled(LogEventLevel.Information)) { _logger.Information("actor:{@actorKey} is activated on node:{@node}", actorKey, node); } }
public async Task ActorAutoFacSimpleTest() { var builder = new ContainerBuilder(); builder.RegisterType <StringCache>().As <ICache>(); ILifetimeScope container = builder.Build(); IActorManager manager = new ActorConfigurationBuilder() .Register <ICache>((c, k, m) => container.Resolve <ICache>(new TypedParameter(typeof(ActorKey), k), new TypedParameter(typeof(IActorManager), m))) .Build() .ToActorManager(); using (container.BeginLifetimeScope()) { ActorKey key = new ActorKey("cache/test"); ICache cache = await manager.CreateProxyAsync <ICache>(_context, key); (await cache.GetCount()).Should().Be(1); await manager.DeactivateAsync <ICache>(_context, key); (await cache.GetCount()).Should().Be(2); } await manager.DeactivateAllAsync(_context); }
public IdentityActor(ActorKey actorKey, IActorManager actorManager, IIdentityStore identityRepository) : base(actorKey, actorManager) { Verify.IsNotNull(nameof(identityRepository), identityRepository); _identityRepository = identityRepository; }
public Task DisposeActor(ActorKey actorKey) { TActor dummy; _actors.TryRemove(actorKey, out dummy); return(TaskExt.Done); }
/// <summary> /// Get actor, if already exist, will return or create new one /// </summary> /// <typeparam name="T">actor type</typeparam> /// <param name="actorKey">actor key</param> /// <returns>actor instance</returns> public T GetActor <T>(ActorKey actorKey) where T : IActor { actorKey.VerifyNotNull(nameof(actorKey)); Type actoryType = typeof(T); lock (_lock) { if (_actorCollection.TryGetValue(actoryType, actorKey, out ActorInstance actorInstance)) { return((T)actorInstance.GetInstance <T>()); } // Create actor object IActorBase actorBase = _registry.Create <T>(actorKey, this); // Create proxy T actorInterface = ActorProxy <T> .Create(actorBase, this); actorInstance = new ActorInstance(typeof(T), actorKey, actorBase, actorInterface); // Add to actor collection _actorCollection.Set(actorInstance); // Create proxy for interface return(actorInstance.GetInstance <T>()); } }
public void ActorPermissionDeleted() { var sut = new PermissionableService(_client); var actor = new ActorKey { Key = Guid.NewGuid().ToString(), Name = $"{GetMethodName()}_Actor" }; var node = new NodeKey { Key = Guid.NewGuid().ToString(), Name = $"{GetMethodName()}_Node" }; var node2 = new NodeKey { Key = Guid.NewGuid().ToString(), Name = $"{GetMethodName()}_Node2" }; var thing = new ThingKey { Key = Guid.NewGuid().ToString(), Name = $"{GetMethodName()}_Thing" }; sut.MakeMember(actor, node); sut.MakeMember(node, node2); sut.MakeMember(node2, node); sut.GrantPermission(node2, thing, $"{GetMethodName()}_Action"); sut.RemoveMember(node, node2); Assert.False(sut.CanItAccess(actor, thing, $"{GetMethodName()}_Action")); }
public Task <IActorProxy> GetActor(ActorKey actorId) { var servicePartitionClient = new ServicePartitionClient <NetCommunicationClient>(_communicationClientFactory, _serviceFabricServiceUri, new ServicePartitionKey(actorId.Id.GetHashCode()), targetReplicaSelector: TargetReplicaSelector.PrimaryReplica, listenerName: InstanceName, retrySettings: new OperationRetrySettings()); return(Task.FromResult <IActorProxy>( new DelegatingActorProxy( new ActorRef(new IPEndPoint(IPAddress.None, 0), actorId), async(context, msg) => //request - reply { msg.AttachHeader(new ActorTargetHeader(actorId)); return await servicePartitionClient.InvokeWithRetryAsync( client => { msg.AttachHeader(new OperationHeader(Guid.NewGuid(), OperationType.Request, context.ActivityId)); return client.Channel.SendRequestAsync(context.Message); }); }, (context, msg) => //oneway { msg.AttachHeader(new ActorTargetHeader(actorId)); servicePartitionClient.InvokeWithRetry( client => { msg.AttachHeader(new OperationHeader(Guid.NewGuid(), OperationType.OneWay, context.ActivityId)); client.Channel.Send(context.Message); }); }, Observable.Never <IActorProxy>()))); }
public async Task ActorMultipleTest() { int count = 0; using IActorHost actorHost = new ActorHost(_loggerFactory) .Register <ICache>(() => new StringCache(y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = actorHost.GetActor <ICache>(key1); count.Should().Be(1); ActorKey key2 = new ActorKey("Cache/Test2"); ICache cache2 = actorHost.GetActor <ICache>(key2); count.Should().Be(2); (await actorHost.Deactivate <ICache>(key1)).Should().BeTrue(); count.Should().Be(1); (await actorHost.Deactivate <ICache>(key2)).Should().BeTrue(); count.Should().Be(0); await actorHost.DeactivateAll(); }
public async Task ActorMethodTest() { int count = 0; using IActorHost actorHost = new ActorHost(_loggerFactory) .Register <ICache>(() => new StringCache(y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = actorHost.GetActor <ICache>(key1); count.Should().Be(1); const string firstText = "first"; bool test = await cache1.IsCached(firstText); test.Should().BeFalse(); await cache1.Add(firstText); test = await cache1.IsCached(firstText); test.Should().BeTrue(); (await actorHost.Deactivate <ICache>(key1)).Should().BeTrue();; count.Should().Be(0); await actorHost.DeactivateAll(); }
public async Task ActorSingleChainTest() { var builder = new ContainerBuilder(); builder.RegisterType <ActorNode>().As <IActorNode>(); builder.RegisterType <ActorSum>().As <IActorSum>(); ILifetimeScope container = builder.Build(); IActorManager manager = new ActorConfigurationBuilder() .Register <IActorNode>(_ => container.Resolve <IActorNode>()) .Register <IActorSum>(_ => container.Resolve <IActorSum>()) .Build() .ToActorManager(); using (container) { ActorKey key = new ActorKey("node/test"); IActorNode node = await manager.CreateProxy <IActorNode>(key); int sum = 0; for (int i = 0; i < 10; i++) { await node.Add(_context, i); sum += i; } IActorSum sumActor = await manager.CreateProxy <IActorSum>(new ActorKey(sumActorName)); (await sumActor.GetSum()).Should().Be(sum); } await manager.DeactivateAll(); }
/// <summary> /// Create actor from either lambda or activator /// </summary> /// <typeparam name="T">actor interface</typeparam> /// <param name="context">context</param> /// <param name="actorKey">actor key</param> /// <param name="actorHost">actor manager</param> /// <returns>instance of actor implementation</returns> public IActorBase Create <T>(ActorKey actorKey, ActorHost actorHost) where T : IActor { actorKey.VerifyNotNull(nameof(actorKey)); actorHost.VerifyNotNull(nameof(actorHost)); Type actorType = typeof(T) .VerifyAssert(x => x.IsInterface, $"{typeof(T)} must be an interface"); ActorRegistration typeRegistration = GetTypeRegistration(actorType); IActor actorObject = typeRegistration.CreateImplementation(); // Set actor key and manager ActorBase?actorBase = actorObject as ActorBase; if (actorBase == null) { string failureMsg = $"Created actor type {actorObject.GetType()} does not derive from ActorBase"; _logger.LogError(failureMsg); throw new InvalidOperationException(failureMsg); } actorBase.ActorKey = actorKey; actorBase.ActorHost = actorHost; actorBase.ActorType = actorType; return((IActorBase)actorObject); }
public async Task GivenAutofacOnlyRegistration_WithResolveOptional_ShouldPass() { var builder = new ContainerBuilder(); builder.RegisterType <StringCache>().As <ICache>(); ILifetimeScope container = builder.Build(); IWorkContext context = new WorkContextBuilder() .Set(new ServiceProviderProxy(x => container.Resolve <ICache>(), x => container.ResolveOptional <ICache>())) .Build(); IActorManager?manager = new ActorConfigurationBuilder() .Set(context) .Build() .ToActorManager(); using (container.BeginLifetimeScope()) { ActorKey key = new ActorKey("cache/test"); ICache cache = await manager.CreateProxy <ICache>(key); (await cache.GetCount()).Should().Be(1); await manager.Deactivate <ICache>(key); (await cache.GetCount()).Should().Be(2); } await manager.DeactivateAll(); }
public async Task ActorMultipleTest() { int count = 0; ActorManager manager = new ActorManager(); manager.Register <ICache>(_context, (c, x, m) => new StringCache(x, m, y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = await manager.CreateProxyAsync <ICache>(_context, key1); count.Should().Be(1); ActorKey key2 = new ActorKey("Cache/Test2"); ICache cache2 = await manager.CreateProxyAsync <ICache>(_context, key2); count.Should().Be(2); await manager.DeactivateAsync <ICache>(_context, key1); count.Should().Be(1); await manager.DeactivateAsync <ICache>(_context, key2); count.Should().Be(0); await manager.DeactivateAllAsync(_context); }
public async Task GivenAutofac_ConstructedFromBuilder_ShouldPass() { var builder = new ContainerBuilder(); builder.RegisterType <StringCache>().As <ICache>(); ILifetimeScope container = builder.Build(); IActorManager?manager = new ActorConfigurationBuilder() .Register(_ => container.Resolve <ICache>()) .Build() .ToActorManager(); using (container.BeginLifetimeScope()) { ActorKey key = new ActorKey("cache/test"); ICache cache = await manager.CreateProxy <ICache>(key); (await cache.GetCount()).Should().Be(1); await manager.Deactivate <ICache>(key); (await cache.GetCount()).Should().Be(2); } await manager.DeactivateAll(); }
public void ActorTimerCallback(IWorkContext context, ActorKey actorKey, string message = null) { Verify.IsNotNull(nameof(context), context); Verify.IsNotNull(nameof(actorKey), actorKey); ActorTimerCallback(_machineName, _processName, context.Cv, context.Tag, actorKey.ToString(), message); }
public async Task GivenActor_WhenDeactivated_ShouldPass() { ActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache()); ActorKey actorKey = new ActorKey("Cache/Test1"); ICache cache = await manager.CreateProxy <ICache>(actorKey); int count = await cache.GetCount(); count.Should().Be(1); await cache.TestAndDeactivate(_context); count = await cache.GetCount(); count.Should().Be(2); await cache.TestAndDeactivate(_context); count = await cache.GetCount(); count.Should().Be(4); }
public async Task ActorMultipleTest() { int count = 0; ActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache(y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = await manager.CreateProxy <ICache>(key1); count.Should().Be(1); ActorKey key2 = new ActorKey("Cache/Test2"); ICache cache2 = await manager.CreateProxy <ICache>(key2); count.Should().Be(2); (await manager.Deactivate <ICache>(key1)).Should().BeTrue(); count.Should().Be(1); (await manager.Deactivate <ICache>(key2)).Should().BeTrue(); count.Should().Be(0); await manager.DeactivateAll(); }
public RoleActor(ActorKey actorKey, IActorManager actorManager, IRoleRepository roleRepository) : base(actorKey, actorManager) { Verify.IsNotNull(nameof(roleRepository), roleRepository); _roleRepository = roleRepository; }
public void ReceivedMessageForUnknownActor(ActorKey actorKey, IOperationContext operation) { if (_logger.IsEnabled(LogEventLevel.Warning)) { _logger.Warning("Received message:{@message} for unknown actor:{@actorKey} from NetChannel:{@channel}", operation.Message, actorKey, operation.ReplyChannel); } }
public void Initialize() { foreach (TAG_CARDTYPE tag_cardtype in this.ACTOR_CARD_TYPES) { IEnumerator enumerator = Enum.GetValues(typeof(TAG_PREMIUM)).GetEnumerator(); try { while (enumerator.MoveNext()) { TAG_PREMIUM current = (TAG_PREMIUM)((int)enumerator.Current); string heroSkinOrHandActor = ActorNames.GetHeroSkinOrHandActor(tag_cardtype, current); ActorKey callbackData = this.MakeActorKey(tag_cardtype, current); AssetLoader.Get().LoadActor(heroSkinOrHandActor, new AssetLoader.GameObjectCallback(this.OnActorLoaded), callbackData, false); } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable == null) { } disposable.Dispose(); } } }
public async Task GivenAutofac_WhenProxyCreated_ShouldPass() { IActorManager manager = new ActorManager(); var builder = new ContainerBuilder(); builder.RegisterType <StringCache>().As <ICache>(); builder.RegisterInstance(manager).As <IActorManager>(); IContainer container = builder.Build(); using (container.BeginLifetimeScope()) { manager.Register <ICache>(_ => container.Resolve <ICache>()); ActorKey key = new ActorKey("cache/test"); ICache cache = await manager.CreateProxy <ICache>(key); (await cache.GetCount()).Should().Be(1); await manager.Deactivate <ICache>(key); (await cache.GetCount()).Should().Be(2); } await manager.DeactivateAll(); }
public bool IsInitializing() { foreach (TAG_CARDTYPE tag_cardtype in this.ACTOR_CARD_TYPES) { IEnumerator enumerator = Enum.GetValues(typeof(TAG_PREMIUM)).GetEnumerator(); try { while (enumerator.MoveNext()) { TAG_PREMIUM current = (TAG_PREMIUM)((int)enumerator.Current); ActorKey key = this.MakeActorKey(tag_cardtype, current); if (!this.m_actorMap.ContainsKey(key)) { return(true); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable == null) { } disposable.Dispose(); } } return(false); }
public async Task ActorProxyMultiTaskTest() { const int taskCount = 10; ActorManager manager = new ActorManager(); manager.Register <ICache>(_context, (context, k, m) => new StringCache(k, m)); var tasks = new List <Task>(); ActorKey key1 = new ActorKey("Cache/Test1"); CancellationTokenSource tokenSource = new CancellationTokenSource(); for (int i = 0; i < taskCount; i++) { Task t = Task.Run(() => TestAccess(manager, key1, tokenSource.Token)); tasks.Add(t); } await Task.Delay(TimeSpan.FromSeconds(10)); tokenSource.Cancel(); Task.WaitAll(tasks.ToArray()); (await manager.DeactivateAsync <ICache>(_context, key1)).Should().BeTrue(); }
public void Initialize() { foreach (TAG_CARDTYPE tag_cardtype in this.ACTOR_CARD_TYPES) { IEnumerator enumerator = Enum.GetValues(typeof(TAG_PREMIUM)).GetEnumerator(); try { while (enumerator.MoveNext()) { TAG_PREMIUM current = (TAG_PREMIUM)((int)enumerator.Current); ActorKey key = this.MakeActorKey(tag_cardtype, current, true); this.m_actorMap.Add(key, new List <Actor>()); ActorKey key2 = this.MakeActorKey(tag_cardtype, current, false); this.m_actorMap.Add(key2, new List <Actor>()); } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable == null) { } disposable.Dispose(); } } if (this.PRELOAD_ACTORS != null) { this.Preload(TAG_CARDTYPE.MINION, TAG_PREMIUM.NORMAL, true, 1); this.Preload(TAG_CARDTYPE.SPELL, TAG_PREMIUM.NORMAL, true, 1); this.Preload(TAG_CARDTYPE.WEAPON, TAG_PREMIUM.NORMAL, true, 1); } }
public void ActorDeactivate(IWorkContext context, ActorKey actorKey, Type actorType, string message = null) { Verify.IsNotNull(nameof(context), context); Verify.IsNotNull(nameof(actorKey), actorKey); Verify.IsNotNull(nameof(actorType), actorType); ActorDeactivate(_machineName, _processName, context.Cv, context.Tag, actorKey.ToString(), actorType.FullName, message); }
public async Task <ActorProxy <TPrimaryNetContract> > GetActor <TPrimaryNetContract>(string actorName) { IActorCoordinator coordinator = GetCoordinator <TPrimaryNetContract>(); ActorKey actorKey = await coordinator.GetOrCreate(actorName); return(await GetActor <TPrimaryNetContract>(actorKey)); }
public void Init(uint serviceKey, NetContractDescription description, IMessageFactory messageFactory, INetPeer channel, ActorKey? actor = null) { Actor = actor; _description = description; _messageFactory = messageFactory; Channel = channel; }
public ActorTargetHeader(ActorKey actorkey) { ActorKey = actorkey; }
public void ReceivedMessageForUnknownActor(ActorKey actorKey, IOperationContext operation) { if (_logger.IsEnabled(LogEventLevel.Warning)) _logger.Warning("Received message:{@message} for unknown actor:{@actorKey} from NetChannel:{@channel}", operation.Message, actorKey, operation.ReplyChannel); }
public void ActorActivated(INetNode node, ActorKey actorKey) { if (_logger.IsEnabled(LogEventLevel.Information)) _logger.Information("actor:{@actorKey} is activated on node:{@node}", actorKey, node); }
public void Init(NetContractDescription description, IMessageFactory messageFactory, IOperationExecutor target, ActorKey? actor = null) { _description = description; _messageFactory = messageFactory; _target = target; Actor = actor; }
public void FailedToSendReply(ActorKey actorKey, Message message, Exception exception) { _logger.Warning("Actor:{@actorKey} whilst processing message:{@message} has failed to send reply because of exception:{exception}", actorKey, message, exception); }
public void ReceivedMessageForActor(ActorKey actorKey, Message message) { _logger.Warning("Actor:{@actorKey} is processing message:{@message}", actorKey, message); }