예제 #1
0
        private void Sub1Handler(MsgOut m, CancellationToken token)
        {
            Console.WriteLine("Sub1 received: " + m.GetPayloadAsString());
            Debug.Assert(m.GetPayloadAsString() == TestPayload);

            //releases the semaphore
            _sem1.Release();
        }
예제 #2
0
        private async Task Sub2HandlerAsync(MsgOut m, CancellationToken token)
        {
            Console.WriteLine("Sub2 (async) received: " + m.GetPayloadAsString());
            Debug.Assert(m.GetPayloadAsString() == TestPayload);

            //releases the semaphore
            _sem2.Release();
            await Task.CompletedTask;
        }
예제 #3
0
        /// <summary>
        /// The async-way subscriber
        /// </summary>
        /// <returns></returns>
        private async Task SubscriberAsync()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //subscribe to the subject
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //waits the message
                MsgOut m = await sub.NextMessageAsync(CancellationToken.None);

                //verify the expectation
                Console.WriteLine("Async received: " + m.GetPayloadAsString());
                Debug.Assert(m.GetPayloadAsString() == TestPayload);
            }
        }
예제 #4
0
        private void Master()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                //sends a request and waits for the response
                MsgOut m = conn.Request(
                    new MsgIn("The.Target").SetPayload("Mario"),
                    TimeSpan.FromSeconds(1),
                    CancellationToken.None
                    );

                Console.WriteLine("Master received: " + m.GetPayloadAsString());
                Debug.Assert(m.GetPayloadAsString() == this._expectedResponsePayload);
            }
        }
예제 #5
0
        private void SubscriberSync()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //instructs the subscriber to self-destroy after
                //exactly MessageCount messages
                sub.AutoUnsubscribe(MessageCount);

                int count = MessageCount;
                while (sub.IsValid)
                {
                    try
                    {
                        //waits for the next message
                        MsgOut m = sub.NextMessage(CancellationToken.None);
                        Console.WriteLine("Sync received: " + m.GetPayloadAsString());
                        Debug.Assert(m.GetPayloadAsString() == TestPayload);
                    }
                    catch (NATSBadSubscriptionException)
                    {
                        //that should be raised by the subscriber,
                        //because the current thread is stick to NextMessage method
                        //but the subscription is actually being disposed
                        break;
                    }
                    catch
                    {
                        //any other exception should be thrown elsewhere
                        throw;
                    }
                    count--;
                }

                Console.WriteLine("Unsubscribed");
                Debug.Assert(count == 0);
            }
        }
예제 #6
0
        private void Slave()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //waits for a request
                MsgOut m = sub.NextMessage(CancellationToken.None);
                Console.WriteLine("Slave received: " + m.GetPayloadAsString());

                //builds the response up, then publish it back as reply
                this._expectedResponsePayload = "Hello " + m.GetPayloadAsString() + "!";
                conn.Publish(
                    new MsgIn(m.ReplyTo).SetPayload(this._expectedResponsePayload),
                    CancellationToken.None
                    );
            }
        }
예제 #7
0
        private void SubscriberSync()
        {
            ClientOptions     opts = ConnectionUtils.GetDefaultOptions();
            ConnectionFactory cf   = new ConnectionFactory();

            using (IConnection conn = cf.CreateConnection(opts))
            {
                IPassiveSubscription sub = conn.SubscribePassive("The.>");

                //waits for a message
                MsgOut m = sub.NextMessage(CancellationToken.None);
                Console.WriteLine("Sync received: " + m.GetPayloadAsString());
                Debug.Assert(m.GetPayloadAsString() == TestPayload);

                //unsubscribes the subject
                sub.Unsubscribe();
                Task.Delay(1000).Wait();

                //releases the semaphore
                this._sem.Release();
            }
        }