Exemplo n.º 1
0
        public void Behavior(IActor actor, MemoryStream stream)
        {
            CheckArg.Actor(actor);
            // divide object in chunk
            int          chunkSize   = 2048;
            int          memSize     = stream.Capacity;
            int          pos         = 0;
            int          chunknumber = 0;
            List <Chunk> chunkList   = new List <Chunk>();

            stream.Seek(0, SeekOrigin.Begin);
            while (pos < memSize)
            {
                Chunk chk           = new Chunk();
                int   currChunkSize = Math.Min(chunkSize, memSize - pos + 1);
                chk.chunkPart = chunknumber++;
                chk.data      = new byte[currChunkSize];
                stream.Read(chk.data, 0, currChunkSize);
                pos += currChunkSize;
                chunkList.Add(chk);
            }
            chunkList.OrderBy(t => t.chunkPart).Last().last = true;

            foreach (var item in chunkList)
            {
                actor.SendMessage(item);
            }
        }
Exemplo n.º 2
0
        private void GetEntries(IActor actor)
        {
            CheckArg.Actor(actor);
            var entries = fUri2Actor.Keys.AsEnumerable <string>();

            actor.SendMessage(entries);
        }
Exemplo n.º 3
0
        public static IFuture <string> Retrieve(this IActor actor)
        {
            CheckArg.Actor(actor);
            IFuture <string> future = new Future <string>();

            actor.SendMessage(future);
            return(future);
        }
Exemplo n.º 4
0
        public static string RetrieveSync(this IActor actor)
        {
            CheckArg.Actor(actor);
            IFuture <string> future = new Future <string>();

            actor.SendMessage(future);
            return(future.Result());
        }
Exemplo n.º 5
0
        public void Launch(IActor asker, IndexRouter router)
        {
            CheckArg.Actor(router);
            var response = new Response(router, asker, this);

            Become(new Behavior <Response>(r => DoProcessQuery(r)));
            SendMessage(response);
        }
Exemplo n.º 6
0
 public DemoClient(BrokerActor <int> aBroker)
 {
     CheckArg.Actor(aBroker);
     foreach (var item in Enumerable.Range(1, 10))
     {
         // aBroker.SendMessage(String.Format("Start Job {0}",item));
         aBroker.SendMessage(item);
     }
 }
Exemplo n.º 7
0
        public void CheckArgReturnTest()
        {
            using (var stream = new MemoryStream())
            {
                CheckArg.Stream(stream);
            }

            try
            {
                CheckArg.Stream(null);
                Assert.Fail();
            }
            catch (ActorException)
            {
            }

            CheckArg.Address("My address");
            try
            {
                CheckArg.Address(string.Empty);
                Assert.Fail();
            }
            catch (ActorException)
            {
            }

            CheckArg.Behaviors(new Actor.Base.Behaviors());
            try
            {
                CheckArg.Behaviors(null);
                Assert.Fail();
            }
            catch (ActorException)
            {
            }

            CheckArg.Behavior(new Actor.Base.Behavior <string>());
            try
            {
                CheckArg.Behavior(null);
                Assert.Fail();
            }
            catch (ActorException)
            {
            }

            CheckArg.Actor(new Actor.Base.BaseActor());
            try
            {
                CheckArg.Actor(null);
                Assert.Fail();
            }
            catch (ActorException)
            {
            }
        }
Exemplo n.º 8
0
        public static async Task <string> Stat(IActor sender)
        {
            CheckArg.Actor(sender);

            GetInstance().SendMessage((Action <IActor>)GetInstance().DoStat, sender);

            var task = await HostDirectoryActor.GetInstance()
                       .Receive(ans => { return((ans is IActor) && (sender.Equals(((IActor)ans)))); });

            return(task as string);
        }
Exemplo n.º 9
0
        public static ShardDirectoryActor AttachShardDirectoryActor(ActorServer actorServer)
        {
            CheckArg.Actor(actorServer);
            IActor shardFound = DirectoryActor.GetDirectory().GetActorByName("KnownShards");

            if (shardFound != null)
            {
                return((ShardDirectoryActor)shardFound);
            }
            return(new ShardDirectoryActor(actorServer));
        }
Exemplo n.º 10
0
        public static async Task <string> Stat(IActor sender)
        {
            CheckArg.Actor(sender);

            GetInstance().SendMessage((Action <IActor>)GetInstance().DoStat, sender);

            var task = await GetInstance()
                       .ReceiveAsync(ans => ans is IActor actor && (sender.Equals(actor))).ConfigureAwait(false);

            return(task as string);
        }
Exemplo n.º 11
0
 public ShardDirectoryActor(ActorServer actorServer)
     : base()
 {
     CheckArg.Actor(actorServer);
     _shardList = new Dictionary <string, string>
     {
         { "LocalHost", actorServer.FullHost }
     };
     DirectoryActor.GetDirectory().Register(this, "KnownShards");
     HostDirectoryActor.Register(this);
     Become(new Behavior <ShardRequest>(
                t => t is ShardRequest,
                DoProcessShardRequest));
 }
Exemplo n.º 12
0
        public IEnumerable <Field> Launch(IndexRouter router)
        {
            CheckArg.Actor(router);
            ConcurrentQueue <Field> bc = new ConcurrentQueue <Field>();
            var spin  = new SpinWait();
            var asker = new BaseActor(new Behavior <string, IEnumerable <Field> >
                                      (
                                          (s, fs) =>
            {
                foreach (var item in fs)
                {
                    bc.Enqueue(item);
                }
                ReceivedMsg++;
                if (ReceivedMsg == 1)
                {
                    Interlocked.Increment(ref streamStart);
                }
                if (ReceivedMsg >= TotalMsg)
                {
                    Interlocked.Increment(ref streamEnd);
                }
            }
                                      ));
            var response = new Response(router, asker, this);

            Become(new Behavior <Response>(r => DoProcessQuery(r)));
            SendMessage(response);
            while (Interlocked.CompareExchange(ref streamStart, streamStart, 0) == 0)
            {
                spin.SpinOnce();
            }

            do
            {
                while (bc.TryDequeue(out Field field))
                {
                    yield return(field);
                }
                ;
                spin.SpinOnce();
            }while (Interlocked.CompareExchange(ref streamEnd, streamEnd, 0) == 0);
        }
Exemplo n.º 13
0
 public static void Unregister(IActor anActor)
 {
     CheckArg.Actor(anActor);
     GetInstance().SendMessage((Action <IActor>)GetInstance().DoUnregister, anActor);
 }
Exemplo n.º 14
0
 public EchoActor(IActor dest, T aT)
 {
     CheckArg.Actor(dest);
     Become(new ConsoleBehavior <string>());
     dest.SendMessage(this, aT);
 }
Exemplo n.º 15
0
 public void Get(TKey key, IActor sender)
 {
     CheckArg.Actor(sender);
     sender.SendMessage(fKV[key]);
 }
Exemplo n.º 16
0
        public static IEnumerable <IBehavior> BuildFromAttributes(IActor linkedActor)
        {
            CheckArg.Actor(linkedActor);
            var bhvs = new List <IBehavior>();

            // Launch reflexion
            MemberInfo[] memberInfo = linkedActor.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var mi in memberInfo)
            {
#if NETCOREAPP1_1
                ActionBehaviorAttribute deco = mi.GetCustomAttribute <ActionBehaviorAttribute>();
#else
                ActionBehaviorAttribute deco = (ActionBehaviorAttribute)Attribute.GetCustomAttribute(mi, typeof(ActionBehaviorAttribute));
#endif
                if (deco != null)
                {
                    var parameters = ((MethodInfo)mi).GetParameters();
                    switch (parameters.Length)
                    {
                    case 0:
                    {
                        IBehavior bhv = new ActionBehavior(
                            (a) => a.GetType() == mi.GetType(),
                            (a) => a()
                            );
                        bhvs.Add(bhv);
                        break;
                    }

                    case 1:
                    {
                        var             genericArg  = parameters[0].ParameterType;
                        var             genericType = typeof(ActionBehavior <>).MakeGenericType(new Type[] { genericArg });
                        ConstructorInfo ci          = genericType.GetConstructor(Type.EmptyTypes);
                        object          o           = ci.Invoke(new object[] { });
                        bhvs.Add(o as IBehavior);
                        break;
                    }

                    case 2:
                    {
                        IBehavior bhv = new ActionBehavior <object, object>(
                            (a, o1, o2) => a.GetType() == mi.GetType(),
                            (a, o1, o2) => a(o1, o2)
                            );
                        bhvs.Add(bhv);
                        break;
                    }

                    case 3:
                    {
                        Behavior bhv = new Behavior(
                            s =>
                            {
                                return(s.GetType().Name == typeof(MessageParam <, ,>).Name);
                            },
                            s =>
                            {
                                var ts   = s.GetType();
                                var arg1 = ts.GetProperty("Item1").GetValue(s);
                                var arg2 = ts.GetProperty("Item2").GetValue(s);
                                var arg3 = ts.GetProperty("Item3").GetValue(s);
                                ((MethodInfo)mi).Invoke(linkedActor, new[] { arg1, arg2, arg3 });
                            });
                        bhvs.Add(bhv);
                        break;
                    }

                    default:
                    {
                        throw new ActorException(MessageTooMuchArgumentsOnDecoratedActor);
                    }
                    }
                }
            }
            return(bhvs);
        }
Exemplo n.º 17
0
 public static void CompleteInitialize(RemoteSenderActor anActor)
 {
     CheckArg.Actor(anActor);
     anActor.fSerializeService = ActorServer.GetInstance().SerializeService;
     anActor.Become(new Behavior <object>(anActor.DoRouting));
 }
Exemplo n.º 18
0
 internal void DoRegister(IActor anActor)
 {
     CheckArg.Actor(anActor);
     fUri2Actor[anActor.Tag.Key()] = anActor;
 }
Exemplo n.º 19
0
 public static void SendMessage <T1, T2, T3, T4>(this IActor anActor, T1 t1, T2 t2, T3 t3, T4 t4)
 {
     CheckArg.Actor(anActor);
     anActor.SendMessage(new MessageParam <T1, T2, T3, T4>(t1, t2, t3, t4));
 }
Exemplo n.º 20
0
 protected void DoIt(IActor anActor)
 {
     CheckArg.Actor(anActor);
     anActor.SendMessage(this);
 }
Exemplo n.º 21
0
 internal void DoUnregister(IActor anActor)
 {
     CheckArg.Actor(anActor);
     fUri2Actor.Remove(anActor.Tag.Key());
 }
Exemplo n.º 22
0
        public static IEnumerable <IBehavior> BuildFromAttributes(IActor linkedActor)
        {
            CheckArg.Actor(linkedActor);
            var bhvs = new List <IBehavior>();

            // Launch reflexion
            MemberInfo[] memberInfo = linkedActor.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var mi in memberInfo)
            {
#if NETCOREAPP1_1
                BehaviorAttribute deco = mi.GetCustomAttribute <BehaviorAttribute>();
#else
                BehaviorAttribute deco = (BehaviorAttribute)Attribute.GetCustomAttribute(mi, typeof(BehaviorAttribute));
#endif
                if (deco != null)
                {
                    var parameters = ((MethodInfo)mi).GetParameters();
                    switch (parameters.Length)
                    {
                    case 0:
                    {
                        throw new ActorException(MessageNullMessageOnDecoratedActor);
                    }

                    case 1:
                    {
                        Behavior bhv = new Behavior(
                            s => parameters[0].ParameterType.IsAssignableFrom(s.GetType()),
                            s => ((MethodInfo)mi).Invoke(linkedActor, new[] { s }));
                        bhvs.Add(bhv);
                        break;
                    }

                    case 2:
                    {
                        Behavior bhv = new Behavior(
                            s =>
                            {
                                return(s.GetType().Name == typeof(MessageParam <,>).Name);
                            },
                            s =>
                            {
                                var ts   = s.GetType();
                                var arg1 = ts.GetProperty("Item1").GetValue(s);
                                var arg2 = ts.GetProperty("Item2").GetValue(s);
                                ((MethodInfo)mi).Invoke(linkedActor, new[] { arg1, arg2 });
                            });
                        bhvs.Add(bhv);
                        break;
                    }

                    case 3:
                    {
                        Behavior bhv = new Behavior(
                            s =>
                            {
                                return(s.GetType().Name == typeof(MessageParam <, ,>).Name);
                            },
                            s =>
                            {
                                var ts   = s.GetType();
                                var arg1 = ts.GetProperty("Item1").GetValue(s);
                                var arg2 = ts.GetProperty("Item2").GetValue(s);
                                var arg3 = ts.GetProperty("Item3").GetValue(s);
                                ((MethodInfo)mi).Invoke(linkedActor, new[] { arg1, arg2, arg3 });
                            });
                        bhvs.Add(bhv);
                        break;
                    }

                    default:
                    {
                        throw new ActorException(MessageTooMuchArgumentsOnDecoratedActor);
                    }
                    }
                }
            }
            return(bhvs);
        }
Exemplo n.º 23
0
 internal void DoStat(IActor sender)
 {
     CheckArg.Actor(sender);
     sender.SendMessage("Host entries " + fUri2Actor.Count.ToString(CultureInfo.InvariantCulture));
 }
Exemplo n.º 24
0
 public static void Store(this IActor actor, string aData)
 {
     CheckArg.Actor(actor);
     actor.SendMessage(aData);
 }
Exemplo n.º 25
0
 public static void SendMessage <T1, T2>(this IActor anActor, T1 t1, T2 t2)
 {
     CheckArg.Actor(anActor);
     anActor.SendMessage(new MessageParam <T1, T2>(t1, t2));
 }