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


C# Agent.Send方法代码示例

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


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

示例1: 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

示例2: Main

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

            string serverAddress = args[0];

            try
            {
                Agent clientAgent = new Agent();

                // read lines of text from standard input
                // and post each one for transmission

                string inputLine = null;
                while((inputLine = Console.ReadLine()) != null)
                {
                    Parameters param = new Parameters();

                    // the "content" field name is arbitrary,
                    // but needs to be recognized at the server side

                    param.SetString("content", inputLine);

                    OutgoingMessage message =
                        clientAgent.Send(serverAddress,
                            "printer", "print", param);

                    message.WaitForTransmission();
                    message.Close();
                }

                clientAgent.Close();
            }
            catch(Exception ex)
            {
                Console.WriteLine(
                   "error: {0}", ex.Message);
            }
        }
开发者ID:morambro,项目名称:TrainProject,代码行数:44,代码来源:ClientSynchronous.cs

示例3: 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

示例4: 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

示例5: Main

        static void Main(string[] args)
        {
            if(args.Length != 3)
            {
                Console.WriteLine(
                    "expecting three parameters: " +
                    "name server address and two integers");
                return;
            }

            String nameServerAddress = args[0];

            int a;
            int b;
            try
            {
                a = int.Parse(args[1]);
                b = int.Parse(args[2]);
            }
            catch(Exception)
            {
                Console.WriteLine("cannot parse the parameters");
                return;
            }

            try
            {
                Agent clientAgent = new Agent();

                // obtain the address of calculator server

                Parameters resolveParams = new Parameters();
                resolveParams.SetString("object", "calculator");

                OutgoingMessage nsQuery =
                    clientAgent.Send(nameServerAddress,
                            "names", "resolve", resolveParams);

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

                Parameters resolveReply = nsQuery.Reply;
                String calculatorAddress =
                    resolveReply.GetString("location");

                nsQuery.Close();

                // send message to the calculator object
                Parameters param = new Parameters();
                param.SetInteger("a", a);
                param.SetInteger("b", b);

                OutgoingMessage message =
                    clientAgent.Send(calculatorAddress,
                        "calculator", "calculate", param);

                message.WaitForCompletion();
                OutgoingMessage.MessageState state =
                    message.State;
                if(state == OutgoingMessage.MessageState.REPLIED)
                {
                    Parameters reply = message.Reply;

                    int sum = reply.GetInteger("sum");
                    int difference = reply.GetInteger("difference");
                    int product = reply.GetInteger("product");
                    int ratio = 0;

                    Parameters.Entry ratioEntry =
                        reply.Find("ratio");
                    bool ratioDefined = ratioEntry != null;
                    if(ratioDefined)
                    {
                        ratio = ratioEntry.GetInteger();
                    }

                    Console.WriteLine("sum        = {0}", sum);
                    Console.WriteLine("difference = {0}",
                        difference);
                    Console.WriteLine("product    = {0}", product);

                    Console.Write("ratio      = ");
                    if(ratioDefined)
                    {
                        Console.WriteLine(ratio);
                    }
                    else
                    {
                        Console.WriteLine("<undefined>");
                    }
                }
                else if(state ==
                    OutgoingMessage.MessageState.REJECTED)
                {

//.........这里部分代码省略.........
开发者ID:morambro,项目名称:TrainProject,代码行数:101,代码来源:ClientNS.cs

示例6: 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


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