Exemplo n.º 1
0
 /// <summary>
 /// Get A Etcd key from a actorNode
 /// if null, return top Key, means all actorNodes
 /// </summary>
 /// <param name="actorNodeConfig"></param>
 /// <returns></returns>
 private string GetEtcdKeyFromActorNode(ActorNodeConfig actorNodeConfig)
 {
     if (actorNodeConfig == null)
     {
         return(_etcdTopKey);
     }
     return(string.Format("{0}/{1}", _etcdTopKey, actorNodeConfig.Name));
 }
Exemplo n.º 2
0
 public override void Init(ActorNodeConfig conf)
 {
     base.Init(conf);
     Task.Factory.StartNew(async () =>
     {
         await GoRequest();
     });
 }
Exemplo n.º 3
0
 public static Actor Create(ActorNodeConfig conf)
 {
     var actorType = Type.GetType(conf.ActorClass);
     if (actorType == null)
         throw new Exception(string.Format("Not found Actor Class: {0}", conf.ActorClass));
     var obj = (Actor)Activator.CreateInstance(actorType);
     obj.Init(conf);
     return obj;
 }
Exemplo n.º 4
0
        public virtual void Init(ActorNodeConfig conf)
        {
            IsActive = true;
            Conf     = conf;

            RpcService = NewRpcCaller();
            RpcServer  = new RpcServer(RpcService, "*", conf.RpcPort);
            _discovery = new Discovery(Conf.AppToken, Conf.DiscoveryMode, Conf.DiscoveryParam);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 注册一个Actor节点, 通常在Actor启动时注册, 并定时进行心跳式注册, 告诉其它节点"我还在生"
        /// </summary>
        /// <param name="actorNodeConfig"></param>
        /// <returns></returns>
        public async Task <bool> RegisterActor(ActorNodeConfig actorNodeConfig)
        {
            if (actorNodeConfig == null)
            {
                return(false);
            }
            var etcdKey  = GetEtcdKeyFromActorNode(actorNodeConfig);
            var response = await SetAsync(etcdKey, actorNodeConfig.ToJson());

            return(response != null);
        }
Exemplo n.º 6
0
        public static Actor Create(ActorNodeConfig conf)
        {
            var actorType = Type.GetType(conf.ActorClass);

            if (actorType == null)
            {
                throw new Exception(string.Format("Not found Actor Class: {0}", conf.ActorClass));
            }
            var obj = (Actor)Activator.CreateInstance(actorType);

            obj.Init(conf);
            return(obj);
        }
Exemplo n.º 7
0
 /// <summary>
 /// 递归一个Etcd节点,并解析这棵Etcd数所有的ActorNode节点数据
 /// </summary>
 /// <param name="etcdNode"></param>
 /// <param name="nodes"></param>
 private void RecurseEtcdNode(Node etcdNode, ref Dictionary <string, ActorNodeConfig> nodes)
 {
     if (etcdNode.Dir)
     {
         foreach (var subNode in etcdNode.Nodes)
         {
             RecurseEtcdNode(subNode, ref nodes);
         }
     }
     else
     {
         var actorNodeCfg = ActorNodeConfig.FromJson(etcdNode.Value);
         nodes.Add(actorNodeCfg.Name, actorNodeCfg);
     }
 }
Exemplo n.º 8
0
        public static void Main(string[] args)
        {
            var configA = new ActorNodeConfig();
            configA.AppToken = "AppToken1";
            configA.ActorClass = "Cosmos.Test.Performance.ActorA, Cosmos.Test.Performance";
            configA.Name = "TestActorA";
            configA.DiscoveryParam = "discovery.json";

            var configB = new ActorNodeConfig();
            configB.AppToken = "AppToken1";
            configB.ActorClass = "Cosmos.Test.Performance.ActorB, Cosmos.Test.Performance";
            configB.Name = "TestActorB";
            configB.DiscoveryParam = "discovery.json";

            ActorRunner.Run(configA);
            ActorRunner.Run(configB);

            while (true)
            {
                Thread.Sleep(1000);
            }
        }
Exemplo n.º 9
0
        private ActorRunner(ActorNodeConfig conf)
        {
            Conf = conf;

            State = ActorRunState.Running;

            ActorThread = Task.Run(async () =>
            {
                Actor = ActorFactory.Create(conf);
                while (Actor.IsActive)
                {
                    await Task.Delay(1000);
                    SecondsTick++;
                }
            });

            //while (State == ActorRunState.None)
            //{
            //    // block
            //}

            Runners[conf.Name] = this;
        }
Exemplo n.º 10
0
        public TestActor()
        {
            // A server
            var discoverServers = new string[] {"http://127.0.0.1:4001"};
            var actorConf = new ActorNodeConfig()
            {
                Name = "Actor-Test-A",
                DiscoveryParam = discoverServers,
                ActorClass = "CosmosTest.SampleActor, Cosmos.Test",
            };
            _actorA = ActorRunner.Run(actorConf);
            Assert.AreEqual(_actorA.State, ActorRunState.Running);

            // B Server
            var actorConfB = new ActorNodeConfig()
            {
                Name = "Actor-Test-B",
                DiscoveryParam = discoverServers,
                ActorClass = "CosmosTest.SampleActor, Cosmos.Test",
            };
            _actorB = ActorRunner.Run(actorConfB);
            Assert.AreEqual(_actorB.State, ActorRunState.Running);
        }
Exemplo n.º 11
0
        private ActorRunner(ActorNodeConfig conf)
        {
            Conf = conf;


            State = ActorRunState.Running;

            ActorThread = Task.Run(async() =>
            {
                Actor = ActorFactory.Create(conf);
                while (Actor.IsActive)
                {
                    await Task.Delay(1000);
                    SecondsTick++;
                }
            });

            //while (State == ActorRunState.None)
            //{
            //    // block
            //}

            Runners[conf.Name] = this;
        }
Exemplo n.º 12
0
 void OnActorNodesChanged(ActorNodeConfig[] nodes)
 {
     
 }
Exemplo n.º 13
0
		/// <summary>
		/// TODO: create a actor in new process!
		/// </summary>
		/// <returns>The process actor.</returns>
		/// <param name="config">Config.</param>
		public virtual ActorNodeConfig NewProcessActor(ActorNodeConfig config)
		{
			return default(ActorNodeConfig);
		}
Exemplo n.º 14
0
		/// <summary>
		/// TODO: create a actor in thread!  not in config
		/// </summary>
		/// <returns>The thread actor.</returns>
		/// <param name="config">Config.</param>
		public virtual ActorNodeConfig NewThreadActor(ActorNodeConfig config)
		{
			return default(ActorNodeConfig);
		}
Exemplo n.º 15
0
        /// <summary>
        /// Get A Etcd key from a actorNode
        /// if null, return top Key, means all actorNodes
        /// </summary>
        /// <param name="actorNodeConfig"></param>
        /// <returns></returns>
	    private string GetEtcdKeyFromActorNode(ActorNodeConfig actorNodeConfig)
	    {
            if (actorNodeConfig == null)
                return _etcdTopKey;
	        return string.Format("{0}/{1}", _etcdTopKey, actorNodeConfig.Name);
	    }
Exemplo n.º 16
0
        public static ActorRunner Run(ActorNodeConfig conf)
        {
            ActorRunner runner = new ActorRunner(conf);

            return(runner);
        }
Exemplo n.º 17
0
        /// <summary>
        /// 注册一个Actor节点, 通常在Actor启动时注册, 并定时进行心跳式注册, 告诉其它节点"我还在生"
        /// </summary>
        /// <param name="actorNodeConfig"></param>
        /// <returns></returns>
	    public async Task<bool> RegisterActor(ActorNodeConfig actorNodeConfig)
	    {
	        if (actorNodeConfig == null) return false;
	        var etcdKey = GetEtcdKeyFromActorNode(actorNodeConfig);
	        var response = await SetAsync(etcdKey, actorNodeConfig.ToJson());

	        return response != null;
	    }
Exemplo n.º 18
0
        IEnumerator<object> WaitRunner()
        {
            var actorConf = new ActorNodeConfig
            {
                Name = "Actor-Test-1",
                ActorClass = "CosmosTest.SampleActor, CosmosTest",
            };
            var runner1 = ActorRunner.Run(actorConf);
            Assert.AreEqual(runner1.SecondsTick, 0);

            while (runner1.State != ActorRunState.Running)
                yield return null;

            Assert.AreEqual(runner1.State, ActorRunState.Running);
            var runner2 = ActorRunner.GetActorStateByName("Actor-Test-1");
            Assert.AreEqual(runner2.State, ActorRunState.Running);
            Assert.AreEqual(runner2.ActorName, "Actor-Test-1");
        }
Exemplo n.º 19
0
 public static ActorRunner Run(ActorNodeConfig conf)
 {
     ActorRunner runner = new ActorRunner(conf);
     return runner;
 }
Exemplo n.º 20
0
        public async void RegisterEtcdNode()
        {
            var dis = new EtcdDiscoveryMode("testTopKey", "http://localhost:2379");
            var testNode = new ActorNodeConfig();
            testNode.Name = "TestNodeDir/TestNodeKey1";
            testNode.Host = "TestHost";

            var result = await dis.RegisterActor(testNode);
            Assert.IsTrue(result);
            var nodes = await dis.GetNodes();
            Assert.AreEqual(nodes["TestNodeDir/TestNodeKey1"].Host, "TestHost");
        }
Exemplo n.º 21
0
        public override void Init(ActorNodeConfig conf)
        {
            base.Init(conf);

            _handlerServer = new HandlerServer(GetHandler(), conf.ResponsePort);
        }