Exemple #1
0
        static void Main(string[] args)
        {
            string address = "amqp://*****:*****@127.0.0.1:5672";

            if (args.Length > 0)
            {
                address = args[0];
            }

            // uncomment the following to write frame traces
            //Trace.TraceLevel = TraceLevel.Frame;
            //Trace.TraceListener = (l, f, a) => Console.WriteLine(DateTime.Now.ToString("[hh:mm:ss.fff]") + " " + string.Format(f, a));

            Uri           addressUri = new Uri(address);
            ContainerHost host       = new ContainerHost(new Uri[] { addressUri }, null, addressUri.UserInfo);

            host.Open();
            Console.WriteLine("Container host is listening on {0}:{1}", addressUri.Host, addressUri.Port);

            string requestProcessor = "request_processor";

            host.RegisterRequestProcessor(requestProcessor, new RequestProcessor());
            Console.WriteLine("Request processor is registered on {0}", requestProcessor);

            Console.WriteLine("Press enter key to exit...");
            Console.ReadLine();

            host.Close();
        }
Exemple #2
0
        public static async Task <Message> ProcessManagementRequestAsync(Message message, ManagementRequestProcessor processor)
        {
            ContainerHost host = Open();

            try
            {
                host.RegisterRequestProcessor("$management", processor);
                Connection connection = await host.ConnectAsync();

                var session = new Session(connection);
                try
                {
                    return(await session.SendControlRequestAsync("$management", message));
                }
                finally
                {
                    await session.CloseAsync();

                    await connection.CloseAsync();
                }
            }
            finally
            {
                host.Close();
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            string address = "amqp://*****:*****@127.0.0.1:5672";
            if (args.Length > 0)
            {
                address = args[0];
            }

            // uncomment the following to write frame traces
            //Trace.TraceLevel = TraceLevel.Frame;
            //Trace.TraceListener = (f, a) => Console.WriteLine(DateTime.Now.ToString("[hh:mm:ss.fff]") + " " + string.Format(f, a));

            Uri addressUri = new Uri(address);
            ContainerHost host = new ContainerHost(new Uri[] { addressUri }, null, addressUri.UserInfo);
            host.Open();
            Console.WriteLine("Container host is listening on {0}:{1}", addressUri.Host, addressUri.Port);

            string requestProcessor = "request_processor";
            host.RegisterRequestProcessor(requestProcessor, new RequestProcessor());
            Console.WriteLine("Request processor is registered on {0}", requestProcessor);

            Console.WriteLine("Press enter key to exist...");
            Console.ReadLine();

            host.Close();
        }
Exemple #4
0
        public static async Task <IDictionary <string, object> > ProcessCbsRequestAsync(string messageId, CbsRequestProcessor processor)
        {
            var           responseProperties = new Dictionary <string, object>();
            ContainerHost host = Open();

            try
            {
                host.RegisterRequestProcessor("$cbs", processor);
                Connection connection = await host.ConnectAsync();

                var session = new Session(connection);
                try
                {
                    Message response = await session.SendCbsRequestAsync(messageId);

                    responseProperties["CorrelationId"] = response.Properties.CorrelationId;
                    responseProperties["status-code"]   = response.ApplicationProperties["status-code"];
                }
                finally
                {
                    await session.CloseAsync();

                    await connection.CloseAsync();
                }
            }
            finally
            {
                host.Close();
            }
            return(responseProperties);
        }
Exemple #5
0
        static void Main(string [] args)
        {
            string address = "amqp://*****:*****@10.67.1.82:5672";

            if (args.Length > 0)
            {
                address = args [0];
            }

            Uri           addressUri = new Uri(address);
            ContainerHost host       = new ContainerHost(new Uri [] { addressUri }, null, addressUri.UserInfo);

            host.Open();
            Console.WriteLine("Raspberry Pi host is listening on {0}:{1}", addressUri.Host, addressUri.Port);

            string requestProcessor = "request_processor";

            host.RegisterRequestProcessor(requestProcessor, new RequestProcessor());
            Console.WriteLine("Request processor is registered on {0}", requestProcessor);

            Console.WriteLine("Press enter key to exit...");
            Console.ReadLine();

            host.Close();
        }
Exemple #6
0
            public override void Run()
            {
                Uri addressUri = new Uri(this.Args.Address);
                X509Certificate2 certificate = TestExtensions.GetCertificate(addressUri.Scheme, addressUri.Host, this.Args.CertValue);
                ContainerHost    host        = new ContainerHost(new Uri[] { addressUri }, certificate, addressUri.UserInfo);

                foreach (var listener in host.Listeners)
                {
                    listener.BufferManager     = this.bufferManager;
                    listener.AMQP.MaxFrameSize = this.Args.MaxFrameSize;
                }

                host.Open();
                Console.WriteLine("Container host is listening on {0}:{1}", addressUri.Host, addressUri.Port);

                host.RegisterRequestProcessor(this.Args.Node, this);
                Console.WriteLine("Message processor is registered on {0}", this.Args.Node);

                this.Wait();

                host.Close();
            }
Exemple #7
0
        public static async Task ReqRepTest(int LoopNum)
        {
            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            const string url          = "amqp://*****:*****@127.0.0.1:5672";
            const string requestUrl   = "request_processor";
            const string receiverName = "client-request-receiver";
            const string sendername   = "client-request-sender";

            using (var cts = new CancellationTokenSource())
                using (var connected = new ManualResetEventSlim())
                    using (var started = new ManualResetEventSlim())
                    {
                        await Task.WhenAll(
                            Task.Run(() =>
                        {
                            var uri  = new Uri(url);
                            var host = new ContainerHost(new[] { uri }, null, uri.UserInfo);
                            host.Open();
                            try
                            {
                                host.RegisterRequestProcessor(requestUrl, new RequestProcessor());
                                Console.WriteLine($"wait begin");
                                cts.Token.WaitHandle.WaitOne();
                            }
                            finally
                            {
                                host.Close();
                            }
                            Console.WriteLine($"svr thread done");
                        })
                            ,
                            Task.Run(async() =>
                        {
                            await Task.Yield();
                            var con = new Connection(new Address(url));
                            try
                            {
                                var session = new Session(con);
                                string replyTo = "client-abc";
                                var recvAttach = new Attach()
                                {
                                    Source = new Source()
                                    {
                                        Address = requestUrl
                                    },
                                    Target = new Target()
                                    {
                                        Address = replyTo
                                    }
                                };
                                var recver = new ReceiverLink(session, receiverName, recvAttach, (link, attach) =>
                                {
                                    Console.WriteLine($"{link},{attach}");
                                });
                                recver.Start(300);
                                var sender = new SenderLink(session, sendername, new Target()
                                {
                                    Address = requestUrl
                                }, (obj, ev) =>
                                {
                                    connected.Set();
                                });
                                connected.Wait();
                                var beginTime = sw.Elapsed;
                                Console.WriteLine($"begin sending({sw.Elapsed})");
                                for (int i = 0; i < LoopNum; i++)
                                {
                                    var msg = new Message($"clientmessage{i}");
                                    msg.Properties = new Properties()
                                    {
                                        MessageId = "command-request",
                                        ReplyTo = replyTo
                                    };
                                    msg.ApplicationProperties = new ApplicationProperties();
                                    msg.ApplicationProperties["offset"] = i;
                                    sender.Send(msg);
                                    var response = recver.Receive();
                                    recver.Accept(response);
                                }
                                var endTime = sw.Elapsed;
                                Console.WriteLine($"elapsed(amqp):({sw.Elapsed}, rps={LoopNum/endTime.Subtract(beginTime).TotalSeconds}");
                            }
                            finally
                            {
                                con.Close();
                            }
                        })
                            .ContinueWith(t =>
                        {
                            Console.WriteLine($"cancelling:{t.Status}");
                            cts.Cancel();
                            if (t.IsCanceled)
                            {
                                Console.WriteLine($"a");
                                throw new AggregateException(t.Exception);
                            }
                            if (t.IsFaulted)
                            {
                                Console.WriteLine($"b");
                                throw new AggregateException(t.Exception);
                            }
                        })
                            ).ConfigureAwait(false); // Task.WhenAll
                    }
        }
Exemple #8
0
        public static async Task TestMany()
        {
            const string url          = "amqp://*****:*****@127.0.0.1:5672";
            const string requestUrl   = "request_processor";
            const string receiverName = "client-request-receiver";
            const string sendername   = "client-request-sender";

            using (var cts = new CancellationTokenSource())
            {
                await Task.WhenAll(
                    Task.Run(() =>
                {
                    var uri  = new Uri(url);
                    var host = new ContainerHost(new[] { uri }, null, uri.UserInfo);
                    Console.WriteLine($"opening host");
                    host.Open();
                    try
                    {
                        host.RegisterRequestProcessor(requestUrl, new RequestProcessor());
                        Console.WriteLine($"wait begin");
                        cts.Token.WaitHandle.WaitOne();
                    }
                    finally
                    {
                        host.Close();
                    }
                    Console.WriteLine($"svr thread done");
                })
                    ,
                    Task.Run(async() =>
                {
                    await Task.Delay(1000).ConfigureAwait(false);
                    var con = new Connection(new Address(url));
                    try
                    {
                        var session = new Session(con);
                        string replyTo = "client-abc";
                        var recvAttach = new Attach()
                        {
                            Source = new Source()
                            {
                                Address = requestUrl
                            },
                            Target = new Target()
                            {
                                Address = replyTo
                            }
                        };
                        var recver = new ReceiverLink(session, receiverName, recvAttach, (link, attach) =>
                        {
                            Console.WriteLine($"{link},{attach}");
                        });
                        recver.Start(300);
                        var sender = new SenderLink(session, sendername, requestUrl);
                        Console.WriteLine($"{sender.IsClosed}");
                        for (int i = 0; i < 100; i++)
                        {
                            var msg = new Message($"clientmessage{i}");
                            msg.Properties = new Properties()
                            {
                                MessageId = "command-request",
                                ReplyTo = replyTo
                            };
                            msg.ApplicationProperties = new ApplicationProperties();
                            msg.ApplicationProperties["offset"] = i;
                            sender.Send(msg);
                            var response = recver.Receive();
                            recver.Accept(response);
                            Console.WriteLine($"{response.Properties}, {response.Body}");
                        }
                    }
                    finally
                    {
                        con.Close();
                    }
                })
                    .ContinueWith(t =>
                {
                    Console.WriteLine($"cancelling:{t.Status}");
                    cts.Cancel();
                    if (t.IsCanceled)
                    {
                        Console.WriteLine($"a");
                        throw new AggregateException(t.Exception);
                    }
                    if (t.IsFaulted)
                    {
                        Console.WriteLine($"b");
                        throw new AggregateException(t.Exception);
                    }
                })
                    ).ConfigureAwait(false); // Task.WhenAll
            }
        }
Exemple #9
0
            public override void Run()
            {
                Uri addressUri = new Uri(this.Args.Address);
                X509Certificate2 certificate = TestExtensions.GetCertificate(addressUri.Scheme, addressUri.Host, this.Args.CertValue);
                ContainerHost host = new ContainerHost(new Uri[] { addressUri }, certificate, addressUri.UserInfo);
                foreach (var listener in host.Listeners)
                {
                    listener.BufferManager = this.bufferManager;
                    listener.AMQP.MaxFrameSize = this.Args.MaxFrameSize;
                }

                host.Open();
                Console.WriteLine("Container host is listening on {0}:{1}", addressUri.Host, addressUri.Port);

                host.RegisterRequestProcessor(this.Args.Node, this);
                Console.WriteLine("Message processor is registered on {0}", this.Args.Node);

                this.Wait();

                host.Close();
            }
Exemple #10
0
 public TestAmqpPeer(string address, string user, string password)
 {
     Address       = new Uri(address);
     containerHost = new ContainerHost(new[] { this.Address }, null, $"{user}:{password}");
     containerHost.RegisterRequestProcessor("TestRequestProcessor", new TestRequestProcessor());
 }