Example #1
0
        static void Main(string[] args)
        {
            if(args.Length != 1)
            {
                Console.WriteLine(
                    "expecting one parameter: " +
                    "publisher destination");
                return;
            }

            string publisherAddress = args[0];

            try
            {
                ValuePublisher randomValue =
                    new ValuePublisher();

                Agent publisherAgent = new Agent();

                string resolvedAddress =
                    publisherAgent.AddListener(publisherAddress);

                Console.WriteLine(
                    "The publisher is listening on {0}",
                    resolvedAddress);

                publisherAgent.RegisterValuePublisher(
                    "random_number", randomValue);

                // publish random values forever
                Parameters content = new Parameters();
                Random generator = new Random();
                while(true)
                {
                    int random = generator.Next(0, 100);
                    content.SetInteger("value", random);

                    Console.WriteLine("publishing value {0}",
                        random);

                    randomValue.Publish(content);

                    Thread.Sleep(1000);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(
                    "error: {0}", ex.Message);
            }
        }
        public void testSubscribeUnsubscribe()
        {
            Agent publisherAgent = new Agent();

            string publisherAddress =
                publisherAgent.AddListener(localAddress);

            ValuePublisher value = new ValuePublisher();
            publisherAgent.RegisterValuePublisher("my_value", value);

            // no subscribers yet
            Assert.AreEqual(0, value.NumberOfSubscribers);
            Assert.AreEqual(0, value.Subscribers.Count);

            // set up the subscriber side
            bool gotUpdate = false;

            Agent subscriberAgent = new Agent();
            subscriberAgent.RegisterObject("my_update_handler",
                delegate(object sender, IncomingMessageArgs args)
                {
                    Assert.AreEqual("subscription_update",
                        args.Message.MessageName);
                    gotUpdate = true;
                });

            // subscribe
            Parameters parameters = new Parameters();
            parameters.SetString("destination_object", "my_update_handler");
            OutgoingMessage subscribeMsg = subscriberAgent.Send(
                publisherAddress, "my_value", "subscribe", parameters);

            subscribeMsg.WaitForCompletion();

            // there should be one subscriber, as seen at the publisher side

            Assert.AreEqual(1, value.NumberOfSubscribers);
            Assert.AreEqual(1, value.Subscribers.Count);
            Assert.AreEqual("my_update_handler",
                value.Subscribers[0].DestinationObject);

            // publish some value
            Parameters dummy = new Parameters();
            value.Publish(dummy);

            // check if the listener got it
            pause();
            Assert.IsTrue(gotUpdate);

            // unsubscribe
            OutgoingMessage unsubscribeMsg = subscriberAgent.Send(
                publisherAddress, "my_value", "unsubscribe", null);

            unsubscribeMsg.WaitForCompletion();

            // there should be no subscribers
            Assert.AreEqual(0, value.NumberOfSubscribers);
            Assert.AreEqual(0, value.Subscribers.Count);

            // check that the updates do not arrive any longer
            gotUpdate = false;
            value.Publish(dummy);

            pause();

            Assert.IsFalse(gotUpdate);

            value.Close();
            subscriberAgent.Close();
            publisherAgent.Close();
        }
        public void testUnknownCommands()
        {
            // set up the publisher side
            Agent publisherAgent = new Agent();

            string publisherAddress =
                publisherAgent.AddListener(localAddress);

            bool gotUnknown = false;

            ValuePublisher value = new ValuePublisher(
                delegate(object sender, IncomingMessageArgs args)
                {
                    Assert.AreEqual("unknown", args.Message.MessageName);
                    gotUnknown = true;
                    args.Message.Reply(null);
                });
            publisherAgent.RegisterValuePublisher("my_value", value);

            // set up the subscriber side
            Agent subscriberAgent = new Agent();

            // send unknown command
            OutgoingMessage unknownMsg = subscriberAgent.Send(
                publisherAddress, "my_value", "unknown", null);

            unknownMsg.WaitForCompletion();

            Assert.IsTrue(gotUnknown);

            subscriberAgent.Close();
            publisherAgent.Close();
        }