コード例 #1
0
ファイル: P.cs プロジェクト: Mossharbor/Telegraphy
        static void Main(string[] args)
        {
            // This sample show how a developer can use the microsoft queuing managment service (msmq)
            // to store messages while offline in order to send them later.
            // We simulate being offline by throwing a WebException
            // We then send the message to the msmq as a store until we are back online (aka fix the default actor to not throw exceptions)
            // Once we are back online we retrieve the items from the durable queue and finish sending them to the default actor.

            // Turn msmq on on windows 10
            //     Open Control Panel.
            //     Click Programs and then, under Programs and Features, click Turn Windows Features on and off.
            //     Expand Microsoft Message Queue(MSMQ) Server, expand Microsoft Message Queue(MSMQ) Server Core, and then select the check boxes for the following Message Queuing features to install. ...
            //     Click OK.

            // send queued items to the
            string durableQueueName = "DurableQueueName";

            Telegraph.Instance.Register(new LocalQueueOperator());
            string       messageStr = "DurableSenderException.";
            DefaultActor da         = new DefaultActor();

            da.OnMessageHandler = delegate(IActorMessage s) { throw new System.Net.WebException(); };

            Telegraph.Instance.Register <Messages.SendMsgToDurableQueue, MsmqDeliveryOperator <string> >(() => new MsmqDeliveryOperator <string>(durableQueueName));
            Telegraph.Instance.Register <string, DefaultActor>(() => da);
            Telegraph.Instance.Register(typeof(System.Net.WebException), SendMessageToDurableQueueForLaterDelivery);

            int i = 0;

            for (; i < 10; ++i)
            {
                string msg = (messageStr + i.ToString());
                Telegraph.Instance.Tell(msg.ToActorMessage());
            }

            Console.WriteLine(string.Format("We sent {0} strings to the default actor", failCount));
            while (failCount != i)
            {
                System.Threading.Thread.Sleep(100);
            }

            Console.WriteLine(string.Format("{0} strings failed", failCount));
            Console.WriteLine(string.Format("We sent {0} strings to the durable queue", failCount));
            Task.WaitAll(durableTasksToWaitOn.ToArray());

            //Change the default actor to now process string messages
            int rerun = 0;

            da.OnMessageHandler = delegate(IActorMessage s) { Console.WriteLine((string)s.Message); ++rerun;  return(true); };
            long receptionOp = Telegraph.Instance.Register(new MsmqReceptionOperator <string>(durableQueueName));

            Telegraph.Instance.Register <string, DefaultActor>(receptionOp, () => da);

            while (rerun <= i)
            {
                System.Threading.Thread.Sleep(100);
            }

            Console.WriteLine(string.Format("After fixing the default actor we sent {0} retrieved strings from the durable queue", failCount));
        }
コード例 #2
0
 public void Apply([NotNull] IThreatActor actor)
 {
     _id         = actor.Id;
     Description = actor.Description;
     _actor      = actor.ActorType;
     actor.CloneProperties(this);
 }
コード例 #3
0
 public ThreatActor([NotNull] IThreatModel model, DefaultActor actor)
 {
     _id         = Guid.NewGuid();
     _model      = model;
     _modelId    = model.Id;
     _actor      = actor;
     Name        = actor.GetEnumLabel();
     Description = actor.GetEnumDescription();
 }
コード例 #4
0
        static void RetryMessage()
        {
            System.Diagnostics.Debug.WriteLine("RetryMessage");
            Telegraph.Instance.Register(new LocalQueueOperator()); // performs a reset.
            string messageStr = "RetryMessage.";

            // this is using the sequential local concurrency type which will create a new Actor for each message.
            long         count = 0;
            DefaultActor da    = new DefaultActor();

            da.OnMessageHandler = delegate(IActorMessage s)
            {
                Interlocked.Increment(ref count);

                if (count == 1)
                {
                    throw new ArgumentException("Testing here");
                }
                else
                {
                    Console.WriteLine("Handled " + (string)s.Message);
                }

                return(true);
            };

            Telegraph.Instance.Register <string, DefaultActor>(() => da);

            Func <Exception, IActor, IActorMessage, IActorInvocation, IActor> errhandler = delegate(Exception ex, IActor actor, IActorMessage msg, IActorInvocation invoker)
            {
                // send the msg back to the queue
                Console.WriteLine("Sending msg back to the queue.");
                Telegraph.Instance.Tell(msg);

                return(null);
            };

            // fallback to this exception since we didnt find a null ref exception
            Telegraph.Instance.Register(typeof(Exception), errhandler);

            for (int i = 0; i < 10; ++i)
            {
                IActorMessage msg = new SimpleMessage <string>(messageStr + i.ToString());

                Telegraph.Instance.Tell(msg);
            }

            Telegraph.Instance.WaitTillEmpty(new TimeSpan(0, 3, 0));
        }
コード例 #5
0
        public IThreatActor AddThreatActor(DefaultActor actor)
        {
            IThreatActor result = null;

            if (actor != DefaultActor.Unknown)
            {
                var threatActor = GetThreatActor(actor);
                if (threatActor == null)
                {
                    result = new ThreatActor(this, actor);
                    Add(result);
                    RegisterEvents(result);
                }
            }

            return(result);
        }
コード例 #6
0
        static void RestartActorAndReprocess()
        {
            System.Diagnostics.Debug.WriteLine("RetryMessage");
            Telegraph.Instance.Register(new LocalQueueOperator()); // performs a reset.
            string messageStr = "RetryMessage.";

            // this is using the sequential local concurrency type which will create a new Actor for each message.
            DefaultActor da = new DefaultActor();

            da.OnMessageHandler = delegate(IActorMessage s)
            {
                throw new ArgumentException("Testing here");
            };

            Telegraph.Instance.Register <string, DefaultActor>(() => da);

            Func <Exception, IActor, IActorMessage, IActorInvocation, IActor> errhandler = delegate(Exception ex, IActor actor, IActorMessage msg, IActorInvocation invoker)
            {
                Console.WriteLine("Creating New Actor");
                DefaultActor da2 = new DefaultActor();
                da2.OnMessageHandler = delegate(IActorMessage s)
                {
                    Console.WriteLine("New Actor:" + (string)s.Message);
                    return(true);
                };

                // send the msg back to the queue
                Console.WriteLine("Sending msg back to the queue.");
                Telegraph.Instance.Tell(msg);

                return(da2);
            };

            // fallback to this exception since we didnt find a null ref exception
            Telegraph.Instance.Register(typeof(Exception), errhandler);

            IActorMessage origMessage = new SimpleMessage <string>(messageStr);

            Telegraph.Instance.Tell(origMessage);

            System.Threading.Thread.Sleep(1000);

            Telegraph.Instance.WaitTillEmpty(new TimeSpan(0, 3, 0));
        }
コード例 #7
0
        public void RegisterMessageToActorByOp()
        {
            Telegraph.Instance.UnRegisterAll();
            string             message = "RegisterMessageToActorByOp";
            bool               called  = false;
            LocalQueueOperator op      = new LocalQueueOperator();
            DefaultActor       da      = new DefaultActor();

            Telegraph.Instance.Register <string, DefaultActor>(op, () => da);

            da.OnMessageHandler = (msg) =>
            {
                called = true;
                Assert.IsTrue(msg.Message.Equals(message));
                return(true);
            };

            Telegraph.Instance.Ask(message.ToActorMessage()).Wait();
            Assert.IsTrue(called);
        }
コード例 #8
0
        public void RegisterValueTypeToActorForOneThreadPerActorTypeSwitchboard()
        {
            Telegraph.Instance.UnRegisterAll();
            long         computeOpID = Telegraph.Instance.Register(new LocalQueueOperator(new OneThreadPerActorTypeSwitchboard()));
            int          message     = 1;
            bool         called      = false;
            DefaultActor da          = new DefaultActor();

            Telegraph.Instance.Register <ValueTypeMessage <int>, DefaultActor>(computeOpID, () => da);

            da.OnMessageHandler = (msg) =>
            {
                called = true;
                Assert.IsFalse(!msg.Message.Equals(message));
                return(true);
            };

            Telegraph.Instance.Ask(message.ToActorMessage()).Wait();
            Assert.IsTrue(called);
        }
コード例 #9
0
        static void LogExceptions()
        {
            System.Diagnostics.Debug.WriteLine("LogExceptions");
            Telegraph.Instance.Register(new LocalQueueOperator()); // performs a reset.
            string messageStr = "LogExceptions.";

            // this is using the sequential local concurrency type which will create a new Actor for each message.
            DefaultActor da = new DefaultActor();

            da.OnMessageHandler = delegate(IActorMessage s) { throw new NullReferenceException(); };

            Telegraph.Instance.Register <byte[], DefaultActor>(() => da);

            Telegraph.Instance.Register(typeof(NullReferenceException), HandleExceptionWithPrintingIt);

            for (int i = 0; i < 10; ++i)
            {
                IActorMessage msg = new ValueTypeMessage <byte>(Encoding.ASCII.GetBytes(messageStr + i.ToString()));

                Telegraph.Instance.Tell(msg);
            }

            Telegraph.Instance.Ask(new ControlMessages.HangUp()).Wait();
        }
コード例 #10
0
 public IThreatActor GetThreatActor(DefaultActor actor)
 {
     return(actor != DefaultActor.Unknown ? _actors?.FirstOrDefault(x => x.ActorType == actor) : null);
 }