当前位置: 首页>>代码示例>>C#>>正文


C# Agent.AddListener方法代码示例

本文整理汇总了C#中Agent.AddListener方法的典型用法代码示例。如果您正苦于以下问题:C# Agent.AddListener方法的具体用法?C# Agent.AddListener怎么用?C# Agent.AddListener使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Agent的用法示例。


在下文中一共展示了Agent.AddListener方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            if(args.Length != 1)
            {
                System.Console.WriteLine(
                    "expecting one parameter: server destination");
                return;
            }

            string serverAddress = args[0];

            try
            {
                Agent serverAgent = new Agent();

                String resolvedAddress =
                    serverAgent.AddListener(serverAddress);

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

                serverAgent.RegisterObject("calculator", calculator);

                // block
                while(true)
                {
                    Thread.Sleep(10000);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("error: {0}", ex.Message);
            }
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:35,代码来源:Server.cs

示例2: Main

        static void Main(string[] args)
        {
            if(args.Length != 1)
            {
                System.Console.WriteLine(
                    "expecting one parameter: name server address");
                return;
            }

            string nameServerAddress = args[0];

            try
            {
                Agent serverAgent = new Agent();

                // prepare the server and bind its address
                // to the name server

                String resolvedAddress =
                    serverAgent.AddListener("tcp://*:*");

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

                Parameters bindParams = new Parameters();
                bindParams.SetString("object", "calculator");
                bindParams.SetString("location", resolvedAddress);

                OutgoingMessage nsBind =
                    serverAgent.Send(nameServerAddress,
                            "names", "bind", bindParams);

                nsBind.WaitForCompletion();
                if (nsBind.State !=
                    OutgoingMessage.MessageState.REPLIED) {
                    Console.WriteLine("error: {0}" +
                            nsBind.ExceptionMsg);
                    return;
                }

                nsBind.Close();

                Console.WriteLine("Address bound by name server.");

                serverAgent.RegisterObject("calculator", calculator);

                // block
                while(true)
                {
                    Thread.Sleep(10000);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("error: {0}", ex.Message);
            }
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:58,代码来源:ServerNS.cs

示例3: setUp

        public void setUp()
        {
            threadsCount = Process.GetCurrentProcess().Threads.Count;

            serverAgent = new Agent();
            serverAddress = serverAgent.AddListener("tcp://*:*");

            clientAgent = new Agent();
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:9,代码来源:AgentTest.cs

示例4: Main

        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);
            }
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:51,代码来源:Publisher.cs

示例5: Main

        static void Main(string[] args)
        {
            if(args.Length != 1 && args.Length != 3)
            {
                Console.WriteLine(
                    "need 1 or 3 parameters:\n" +
                    "   - server address\n" +
                    "   - incoming high water mark\n" +
                    "   - incoming low water mark\n" +
                    "If only server address is given," +
                    " the limits will have default values");
                return;
            }

            string serverAddress = args[0];

            Parameters options = new Parameters();
            if(args.Length == 3)
            {
                int incomingHighWaterMark;
                int incomingLowWaterMark;

                try
                {
                    incomingHighWaterMark = int.Parse(args[1]);
                    incomingLowWaterMark = int.Parse(args[2]);
                }
                catch(FormatException)
                {
                    Console.WriteLine("invalid arguments");
                    return;
                }

                options.SetInteger("incoming_high_water_mark",
                    incomingHighWaterMark);
                options.SetInteger("incoming_low_water_mark",
                    incomingLowWaterMark);
            }

            try
            {
                Agent serverAgent = new Agent(options);

                string resolvedAddress =
                    serverAgent.AddListener(serverAddress);

                Console.WriteLine(
                    "The server is listening on " +
                    resolvedAddress);

                serverAgent.RegisterObject(
                    "object", Consumer);

                // block
                while(true)
                {
                    Thread.Sleep(10000);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(
                    "error: {0}", ex.Message);
            }
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:65,代码来源:Server.cs

示例6: testSubscribeUnsubscribe

        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();
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:71,代码来源:SubscriptionTest.cs

示例7: testUnknownCommands

        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();
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:33,代码来源:SubscriptionTest.cs

示例8: testRawBinary

        public void testRawBinary()
        {
            bool gotMessage = false;

            Encoding encoding = Encoding.UTF8;
            byte[] messageContent =
                encoding.GetBytes("Hello this is raw binary message");
            byte[] replyContent =
                encoding.GetBytes("Hi and this is raw binary response !");

            Parameters options = new Parameters();
            options.SetBoolean(OptionNames.DELIVER_AS_RAW_BINARY, true);

            Agent rawServerAgent = new Agent(options);
            string rawServerAddress =
                rawServerAgent.AddListener("tcp://*:*");

            Agent rawClientAgent = new Agent(options);

            rawServerAgent.RegisterObject(objectName,
                delegate(object sender, IncomingMessageArgs args)
                {
                    gotMessage = true;
                    byte[] content = args.Message.RawContent;
                    Assert.AreEqual(messageContent, content);
                    args.Message.Reply(
                        new RawBinaryDataSource(replyContent));
                });

            OutgoingMessage message = rawClientAgent.Send(
                rawServerAddress, objectName, messageName,
                new RawBinaryDataSource(messageContent));

            message.WaitForCompletion();
            Assert.IsTrue(gotMessage);

            Assert.AreEqual(
                OutgoingMessage.MessageState.REPLIED, message.State);

            byte[] reply = message.RawReply;
            Assert.AreEqual(replyContent, reply);

            message.Close();

            rawClientAgent.Close();
            rawServerAgent.Close();
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:47,代码来源:AgentTest.cs

示例9: testLoadBalancing

        public void testLoadBalancing()
        {
            Agent serverAgent1 = new Agent();
            string serverAddress1 = serverAgent1.AddListener("tcp://*:*");

            Agent serverAgent2 = new Agent();
            string serverAddress2 = serverAgent2.AddListener("tcp://*:*");

            string loadBalancedTarget = string.Format(
                "failover:({0}|{1})", serverAddress1, serverAddress2);

            bool gotMessage1 = false;
            bool gotMessage2 = false;

            serverAgent1.RegisterObject(objectName,
                delegate(object sender, IncomingMessageArgs args)
                {
                    gotMessage1 = true;
                    args.Message.Reply(null);
                });

            serverAgent2.RegisterObject(objectName,
                delegate(object sender, IncomingMessageArgs args)
                {
                    gotMessage2 = true;
                    args.Message.Reply(null);
                });

            OutgoingMessage message = clientAgent.Send(
                loadBalancedTarget, objectName, messageName, null);

            // since this is a load-balanced (and failover) target,
            // the message is implicitly waited for completion
            Assert.AreEqual(
                OutgoingMessage.MessageState.REPLIED, message.State);

            // exactly one of two servers got the message
            // (exclusive-or of 1st or 2nd receiver gotMessage)
            Assert.IsTrue(gotMessage1 ^ gotMessage2);

            message.Close();

            serverAgent1.Close();
            serverAgent2.Close();
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:45,代码来源:AgentTest.cs


注:本文中的Agent.AddListener方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。