public void Receive_from_host()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            using (var sut = new PubnubStandInTransceiver(cre, "hostchannel"))
            {
                var        are    = new AutoResetEvent(false);
                HostOutput result = null;
                sut.ReceivedFromHost += _ =>
                {
                    result = _;
                    are.Set();
                };

                var sender = new Pubnub(cre.PublishingKey, cre.SubscriptionKey);
                var ho     = new HostOutput {
                    CorrelationId = Guid.NewGuid(), Data = "hello".Serialize(), Portname = "portname"
                };
                sender.publish(sut.StandInEndpointAddress, ho.Serialize(), _ => { });

                Assert.IsTrue(are.WaitOne(5000));

                Assert.AreEqual(ho.CorrelationId, result.CorrelationId);
                Assert.AreEqual(ho.Data, result.Data);
                Assert.AreEqual(ho.Portname, result.Portname);
            }
        }
        public void Receive_from_standIn()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            using (var sut = new PubnubHostTransceiver(cre, "hostchannel"))
            {
                var       are    = new AutoResetEvent(false);
                HostInput result = null;
                sut.ReceivedFromStandIn += _ =>
                {
                    result = _;
                    are.Set();
                };

                var standIn = new Pubnub(cre.PublishingKey, cre.SubscriptionKey, cre.SecretKey);
                var hi      = new HostInput {
                    CorrelationId = Guid.NewGuid(), Data = "hello".Serialize(), Portname = "portname", StandInEndpointAddress = "endpoint"
                };
                standIn.publish("hostchannel", hi.Serialize(), _ => { });

                Assert.IsTrue(are.WaitOne(5000));

                Assert.AreEqual(hi.CorrelationId, result.CorrelationId);
                Assert.AreEqual(hi.Data, result.Data);
                Assert.AreEqual(hi.Portname, result.Portname);
                Assert.AreEqual(hi.StandInEndpointAddress, result.StandInEndpointAddress);
            }
        }
Exemplo n.º 3
0
        public void Connect_transceivers_operationhost()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            var configServer = new FlowRuntimeConfiguration()
                               .AddFunc <string, string>("greeting", s => s + "x")
                               .AddStream(".@greeting", "greeting")
                               .AddStream("greeting", ".@greeting");

            using (var server = new FlowRuntime(configServer))
                using (var serverhost = new PubnubOperationHost(server, cre, "thehost"))
                {
                    server.Message += Console.WriteLine;

                    var config = new FlowRuntimeConfiguration()
                                 .AddOperation(new PubnubStandInOperation("op", cre, "thehost"))
                                 .AddStream(".in", "op#greeting")
                                 .AddStream("op#greeting", ".out");
                    using (var fr = new FlowRuntime(config))
                    {
                        fr.Message += Console.WriteLine;

                        fr.Process(".in", "hello");

                        var result = "";
                        Assert.IsTrue(fr.WaitForResult(5000, _ => result = _.Data as string));
                        Assert.AreEqual("hellox", result);
                    }
                }
        }
Exemplo n.º 4
0
        public void Run()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            var configServer = new FlowRuntimeConfiguration()
                               .AddFunc <string, string>("hello", s => "hello, " + s)
                               .AddStream(".@hello", "hello")
                               .AddStream("hello", ".@hello");

            using (var server = new FlowRuntime(configServer))
                using (new PubnubOperationHost(server, cre, "host"))
                {
                    server.Message += Console.WriteLine;

                    var configClient = new FlowRuntimeConfiguration()
                                       .AddOperation(new PubnubStandInOperation("standin", cre, "host"))
                                       .AddStream(".in", "standin#hello")
                                       .AddStream("standin#hello", ".out");
                    using (var client = new FlowRuntime(configClient))
                    {
                        client.Message += Console.WriteLine;

                        client.Process(".in", "peter");

                        var result = "";
                        Assert.IsTrue(client.WaitForResult(5000, _ => result = (string)_.Data));

                        Assert.AreEqual("hello, peter", result);
                    }
                }
        }
Exemplo n.º 5
0
        public void Test_request_response2()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            var are = new AutoResetEvent(false);

            var pn = new Pubnub(cre.PublishingKey, cre.SubscriptionKey, cre.SecretKey);

            pn.subscribe("client", (object _) =>
            {
                Console.WriteLine("client received response");
                are.Set();
            },
                         (object _) =>
            {
                Console.WriteLine("subscribed client");
            });


            pn.subscribe("server", (object _) =>
            {
                Console.WriteLine("server received request");
                pn.publish("client", "myresponse", __ => Console.WriteLine("responded from server"));
            },
                         (object _) => Console.WriteLine("subscribed server"));

            Thread.Sleep(1000); // test fails with a delay here

            pn.publish("server", "myrequest", _ => Console.WriteLine("sent from client"));


            Assert.IsTrue(are.WaitOne(10000));
        }
        public void Send_to_host()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            var host = new Pubnub(cre.PublishingKey, cre.SubscriptionKey);

            try
            {
                var are = new AutoResetEvent(false);
                ReadOnlyCollection <object> result = null;
                host.subscribe("hostchannel", (ReadOnlyCollection <object> _) =>
                {
                    result = _;
                    are.Set();
                });

                using (var sut = new PubnubStandInTransceiver(cre, "hostchannel"))
                {
                    var hi = new HostInput
                    {
                        CorrelationId          = Guid.NewGuid(),
                        Data                   = "hello".Serialize(),
                        Portname               = "portname",
                        StandInEndpointAddress = "endpoint"
                    };
                    sut.SendToHost(hi);

                    Assert.IsTrue(are.WaitOne(5000));

                    var hiReceived = Convert.FromBase64String((string)((JValue)result[0]).Value).Deserialize() as HostInput;
                    Assert.AreEqual(hi.CorrelationId, hiReceived.CorrelationId);
                    Assert.AreEqual(hi.Data, hiReceived.Data);
                    Assert.AreEqual(hi.Portname, hiReceived.Portname);
                    Assert.AreEqual(hi.StandInEndpointAddress, hiReceived.StandInEndpointAddress);
                }
            }
            finally
            {
                host.unsubscribe("hostchannel", _ => {});
            }
        }
Exemplo n.º 7
0
        public void Connect_transceivers()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            using (var host = new PubnubHostTransceiver(cre, "thehost"))
            {
                var config = new FlowRuntimeConfiguration()
                             .AddOperation(new PubnubStandInOperation("op", cre, "thehost"))
                             .AddStream(".in", "op#greeting")
                             .AddStream("op#greeting", ".out");
                using (var fr = new FlowRuntime(config))
                {
                    fr.Message += Console.WriteLine;

                    host.ReceivedFromStandIn += rhi =>
                    {
                        var data = (string)rhi.Data.Deserialize();
                        var ho   = new HostOutput
                        {
                            CorrelationId = rhi.CorrelationId,
                            Data          = (data + "y").Serialize(),
                            Portname      = "greeting"
                        };

                        ThreadPool.QueueUserWorkItem(_ =>
                        {
                            host.SendToStandIn(new Tuple <string, HostOutput>(rhi.StandInEndpointAddress, ho));
                        });
                    };

                    fr.Process(".in", "hello");

                    var result = "";
                    Assert.IsTrue(fr.WaitForResult(5000, _ => result = _.Data as string));
                    Assert.AreEqual("helloy", result);
                }
            }
        }
        public void Send_to_standIn()
        {
            var cre = PubnubCredentials.LoadFrom("pubnub credentials.txt");

            using (var sut = new PubnubHostTransceiver(cre, "hostchannel"))
            {
                var standIn = new Pubnub(cre.PublishingKey, cre.SubscriptionKey, cre.SecretKey);
                try
                {
                    var standInChannel = Guid.NewGuid().ToString();

                    var are = new AutoResetEvent(false);
                    ReadOnlyCollection <object> result = null;
                    standIn.subscribe(standInChannel, (ReadOnlyCollection <object> _) =>
                    {
                        result = _;
                        are.Set();
                    });

                    var ho = new HostOutput {
                        CorrelationId = Guid.NewGuid(), Data = "hello".Serialize(), Portname = "portname"
                    };
                    sut.SendToStandIn(new Tuple <string, HostOutput>(standInChannel, ho));

                    Assert.IsTrue(are.WaitOne(5000));

                    var hoReceived = Convert.FromBase64String((string)((JValue)result[0]).Value).Deserialize() as HostOutput;
                    Assert.AreEqual(ho.CorrelationId, hoReceived.CorrelationId);
                    Assert.AreEqual(ho.Data, hoReceived.Data);
                    Assert.AreEqual(ho.Portname, hoReceived.Portname);
                }
                finally
                {
                    standIn.subscribe("standIn", _ => {});
                }
            }
        }