public void ObserveTest13()
        {
            CoapConfig clientConfig = new CoapConfig()
            {
                NotificationReregistrationBackoff = 0
            };

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer) });

            clientEndpoint.Start();

            CreateServer();

            CoapClient coapClient = new CoapClient
            {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse = null;
            int      count        = 1;

            _resource.UpdateContent($"First string {count - 1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            AutoResetEvent reregistered = new AutoResetEvent(false);

            relation.Request.Reregistering += (a, b) => {
                reregistered.Set();
                b.RefreshRequest.IsCancelled = true;
            };


            byte[] tokenBytes = relation.Request.Token;
            bool   dataSent   = false;
            int    requestNo  = 0;
            int    observerNo = 1;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(0, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);

                            switch (requestNo)
                            {
                            case 0:
                                Assert.IsFalse(item.Request.HasOption(OptionType.ETag));
                                break;

                            case 1:
                            case 2:
                                Assert.AreEqual(4, item.Request.ETags.ToArray().Length);
                                break;
                            }

                            requestNo += 1;
                            Debug.WriteLine($"Request: {item.Request}");
                        }
                        else if (item.Response != null)
                        {
                            Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                            Assert.AreEqual(observerNo, item.Response.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                            Debug.WriteLine($"Response: {item.Response} {item.Response.Observe}");
                        }
                        else
                        {
                            Assert.Fail();
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    Assert.IsTrue(trigger.WaitOne(1000));
                    Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                    Assert.AreEqual(observerNo, lastResponse.Observe);
                    dataSent     = false;
                    lastResponse = null;

                    if (count == 4)
                    {
                        relation.UpdateETags(new byte[][] { new byte[] { 1 }, new byte[] { 3 }, new byte[] { 5 }, new byte[] { 7 } });
                        count += 1;
                    }
                    else if (count == 8)
                    {
                        Assert.IsTrue(reregistered.WaitOne(20 * 1000));
                        count += 1;
                    }

                    if (count == 9)
                    {
                        count += 1;
                        _resource.UpdateContent($"New string {count}");
                        observerNo += 1;
                        count      += 1;
                    }
                    else if (count < 12)
                    {
                        _resource.UpdateContent($"New string {count}");
                        count      += 1;
                        observerNo += 1;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            Assert.AreEqual(12, count);
        }
        public void ObserveTest1()
        {
            CoapConfig clientConfig = new CoapConfig();

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer) });

            clientEndpoint.Start();

            CreateServer();

            CoapClient coapClient = new CoapClient {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse = null;
            int      count        = 1;

            _resource.UpdateContent($"First string {count-1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            byte[] tokenBytes = relation.Request.Token;
            bool   dataSent   = false;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(0, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);
                        }
                        else if (item.Response != null)
                        {
                            Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                            Assert.AreEqual(count, item.Response.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                        }
                        else
                        {
                            Assert.Fail();
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    Assert.IsTrue(trigger.WaitOne(1000));
                    Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                    Assert.AreEqual(count, lastResponse.Observe);
                    dataSent     = false;
                    lastResponse = null;

                    if (count < 5)
                    {
                        _resource.UpdateContent($"New string {count}");
                        count += 1;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            Assert.AreEqual(5, count);
        }
        public void ObserveTest3()
        {
            CoapConfig clientConfig = new CoapConfig()
            {
                MaxRetransmit = 0
            };

            LogManager.Level = LogLevel.Debug;

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer), typeof(TokenLayer), typeof(ReliabilityLayer) });

            clientEndpoint.Start();

            CreateServer();
            _config.NonTimeout = 100;  // Change this value up - at 10 it cleans up the NON before the RST has a chance to get back.

            CoapClient coapClient = new CoapClient {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse = null;
            int      count        = 1;

            _resource.UpdateContent($"First string {count - 1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            int  emptyCount = 0;
            bool dataSent   = false;

            byte[] tokenBytes = relation.Request.Token;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            Debug.WriteLine($"Request: {item.Request}");
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(0, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);
                        }
                        else if (item.Response != null)
                        {
                            Debug.WriteLine($"Response: {item.Response}");
                            Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                            Assert.AreEqual(count, item.Response.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                        }
                        else
                        {
                            Debug.WriteLine($"RST: {item.EmptyMessage}");
                            emptyCount += 1;
                            dataSent    = true;
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    dataSent = false;
                    if (count >= 3)
                    {
                        Assert.IsNull(lastResponse);
                    }
                    else
                    {
                        Assert.IsTrue(trigger.WaitOne(10 * 1000));
                        Assert.IsNotNull(lastResponse);
                        Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                        Assert.AreEqual(count, lastResponse.Observe);
                        lastResponse = null;
                    }

                    if (count < 5)
                    {
                        _resource.UpdateContent($"New string {count}");
                        count += 1;
                        if (count == 3)
                        {
                            relation.ReactiveCancel();
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else if (count == 5)
                {
                    break;
                }
                else if (emptyCount != 0)
                {
                    _resource.UpdateContent($"New string {count}");
                    count += 1;
                }
            }

            Assert.AreEqual(1, emptyCount);
            Assert.AreEqual(5, count);
        }
        public void ObserveTest2()
        {
            CoapConfig clientConfig = new CoapConfig()
            {
                NotificationReregistrationBackoff = 0
            };

            Pump = new MockMessagePump();

            IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer) });

            clientEndpoint.Start();

            CreateServer();

            CoapClient coapClient = new CoapClient {
                EndPoint = clientEndpoint,
                Uri      = new Uri($"coap://{MockMessagePump.ServerAddress}{_resource.Uri}"),
                Timeout  = 0
            };

            Response lastResponse    = null;
            int      count           = 1;
            int      clientObserveNo = 0;

            _resource.UpdateContent($"First string {count - 1}");

            AutoResetEvent      trigger  = new AutoResetEvent(false);
            CoapObserveRelation relation = coapClient.ObserveAsync(
                (r) => {
                lastResponse = r;
                trigger.Set();
            });

            AutoResetEvent reregistered = new AutoResetEvent(false);

            relation.Request.Reregistering += (a, b) => {
                reregistered.Set();
            };

            bool dataSent = false;

            byte[] tokenBytes = relation.Request.Token;

            while (true)
            {
                Thread.Sleep(1);
                if (Pump.Queue.Count > 0)
                {
                    MockQueueItem item = Pump.Queue.Peek();
                    switch (item.ItemType)
                    {
                    case MockQueueItem.QueueType.NetworkSend:
                        if (item.Request != null)
                        {
                            if (tokenBytes == null)
                            {
                                tokenBytes = relation.Request.Token;
                            }

                            Assert.IsTrue(item.Request.HasOption(OptionType.Observe));
                            Assert.AreEqual(clientObserveNo, item.Request.Observe);
                            CollectionAssert.AreEqual(tokenBytes, item.Request.Token);
                            clientObserveNo += 1;
                        }
                        else if (item.Response != null)
                        {
                            if (count < 4)
                            {
                                Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
                                Assert.AreEqual(count, item.Response.Observe);
                            }
                            else
                            {
                                // Assert.IsFalse(item.Response.HasOption(OptionType.Observe));
                            }

                            CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
                            dataSent = true;
                        }
                        else
                        {
                            Assert.Fail();
                        }

                        break;
                    }

                    Pump.Pump();
                }
                else if (dataSent)
                {
                    dataSent = false;
                    if (count > 4)
                    {
                        Assert.IsNull(lastResponse);
                    }
                    else
                    {
                        Assert.IsTrue(trigger.WaitOne(1000));
                        Assert.IsNotNull(lastResponse);
                        if (count == 4)
                        {
                            Assert.IsFalse(lastResponse.HasOption(OptionType.Observe));
                        }
                        else
                        {
                            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
                            Assert.AreEqual(count, lastResponse.Observe);
                        }

                        lastResponse = null;
                    }

                    if (count < 5)
                    {
                        if (count == 3)
                        {
                            relation.ProactiveCancel();
                            _resource.UpdateContent($"New string {count}");
                        }
                        else
                        {
                            _resource.UpdateContent($"New string {count}");
                        }
                        count += 1;
                    }
                    else
                    {
                        break;
                    }
                }
                else if (count == 5)
                {
                    break;
                }
            }

            Assert.AreEqual(2, clientObserveNo);
            Assert.AreEqual(5, count);

            //  Total # of seconds is MaxAge = 1 + backoff = 0 + random(2, 15)
            Assert.IsFalse(reregistered.WaitOne(17 * 1000));
        }
예제 #5
0
        private void CommonTestCode(Method method, bool longRequest, bool longResponse, int expectedClient, int expectedServer)
        {
            string requestText = null;

            byte[] observeToken = new byte[] { 0xa, 0xa, 0xa };
            int    observeNum   = 0;

            string[] currentResourceContent = new string[2];

            Type[]          layerTypes = new Type[] { typeof(BlockwiseLayer) };
            MockMessagePump pump       = new MockMessagePump(layerTypes, ClientConfig, ServerConfig);

            Request r = new Request(method);

            if (method == Method.POST)
            {
                requestText = longRequest ? LongPostRequest : ShortPostRequest;
                currentResourceContent[0] = longResponse ? LongPostResponse : ShortPostReponse;
                r.PayloadString           = requestText;
            }
            else
            {
                currentResourceContent[0] = longResponse ? LongGetResponse : ShortGetResponse;
                if (Observe)
                {
                    r.Observe = 1;
                    r.Token   = observeToken;
                }
            }
            pump.SendRequest(r);

            if (Parallel)
            {
                Request r2 = new Request(method)
                {
                    UriPath = "/resource2"
                };
                currentResourceContent[1] = longResponse ? LongGetResponse.Replace("get ", "getA") : ShortGetResponse + "P";
                pump.SendRequest(r2);
            }

            int      clientCount     = 0;
            int      serverCount     = 0;
            int      success         = 0;
            Exchange observeExchange = null;

            while (pump.Pump())
            {
                MockQueueItem item = pump.Queue.Peek();

                switch (item.ItemType)
                {
                //  Check conditions of the request when ready to transmit it on the wire
                case MockQueueItem.QueueType.ClientSendRequestNetwork:
                    if (Observe)
                    {
                        if (item.Request.HasOption(OptionType.Observe))
                        {
                            Assert.IsFalse(item.Request.HasOption(OptionType.Block2));
                        }
                        else
                        {
                            Assert.IsTrue(item.Request.Block2.NUM > 0);
                        }
                    }

                    clientCount += 1;
                    break;

                //  Check conditions of the response when ready to transmit it on the wire
                case MockQueueItem.QueueType.ServerSendResponseNetwork:
                    if (Observe)
                    {
                        if (item.Response.HasOption(OptionType.Observe))
                        {
                            Assert.AreEqual(0, item.Response.Block2.NUM);
                            CollectionAssert.AreEqual(observeToken, item.Response.Token);
                        }
                        else
                        {
                            Assert.IsTrue(item.Response.Block2.NUM > 0);
                            CollectionAssert.AreNotEqual(observeToken, item.Response.Token);
                        }
                    }
                    serverCount += 1;
                    break;

                // Server Resource is going to respond
                case MockQueueItem.QueueType.ServerSendRequest:
                    pump.Queue.Dequeue();
                    if (method == Method.POST)
                    {
                        Assert.AreEqual(requestText, item.Request.PayloadString);
                    }
                    else
                    {
                        Assert.AreEqual(0, item.Request.PayloadSize);
                    }

                    Response s = new Response(StatusCode.Content);
                    s.PayloadString = currentResourceContent[0];

                    if (Parallel && item.Request.UriPath == "/resource2")
                    {
                        s.PayloadString = currentResourceContent[1];
                    }

                    if (Observe && item.Request.HasOption(OptionType.Observe))
                    {
                        s.Observe       = 3;
                        observeExchange = item.Exchange;
                        s.Type          = MessageType.NON;
                    }

                    item.Exchange.EndPoint.SendResponse(item.Exchange, s);
                    break;

                case MockQueueItem.QueueType.ClientSendResponse:
                    pump.Queue.Dequeue();

                    if (Parallel && item.Exchange.Request.UriPath == "/resource2")
                    {
                        Assert.AreEqual(currentResourceContent[1], item.Response.PayloadString);
                        currentResourceContent[1] = currentResourceContent[0].Replace("get ", "get9");
                    }
                    else
                    {
                        Assert.AreEqual(currentResourceContent[0], item.Response.PayloadString);
                        currentResourceContent[0] = currentResourceContent[0].Replace("get ", "get3");
                    }

                    success += 1;

                    //  For observe, send a second observe out
                    if (Observe && observeNum == 0)
                    {
                        observeNum += 1;


                        s = new Response(StatusCode.Content)
                        {
                            PayloadString = currentResourceContent[0],
                            Observe       = 5,
                            Type          = MessageType.NON
                        };

                        List <MockStack> stacks = pump.ServerStacks[MockMessagePump.ServerAddress];
                        stacks[0].MyEndPoint.SendResponse(observeExchange, s);
                    }

                    break;
                }
            }

            if (Parallel || Observe)
            {
                Assert.AreEqual(2, success);
            }
            else
            {
                Assert.AreEqual(1, success);
            }

            Assert.AreEqual(expectedClient, clientCount);
            Assert.AreEqual(expectedServer, serverCount);
        }
예제 #6
0
        public void BlockwiseTest7()
        {
            string response2 = LongGetResponse.Replace("get ", "get2");

            string responseText;

            Request r = new Request(Method.GET)
            {
                Destination = MockMessagePump.MulticastAddress
            };

            responseText = LongGetResponse;

            MockMessagePump pump = new MockMessagePump(new Type[] { typeof(BlockwiseLayer) }, ClientConfig, ServerConfig);

            int clientCount = 0;
            int serverCount = 0;
            int success     = 0;
            int sent        = 0;

            pump.SendRequest(r);
            while (pump.Pump())
            {
                MockQueueItem item = pump.Queue.Peek();

                switch (item.ItemType)
                {
                case MockQueueItem.QueueType.ClientSendRequestNetwork:
                    clientCount += 1;
                    break;

                case MockQueueItem.QueueType.ServerSendResponseNetwork:
                    serverCount += 1;
                    break;

                case MockQueueItem.QueueType.ServerSendRequest:
                    pump.Queue.Dequeue();
                    Assert.AreEqual(0, item.Request.PayloadSize);

                    Response s = new Response(StatusCode.Content);
                    s.PayloadString = (sent == 0) ? responseText : response2;
                    sent           += 1;
                    item.Exchange.EndPoint.SendResponse(item.Exchange, s);
                    break;

                case MockQueueItem.QueueType.ClientSendResponse:
                    pump.Queue.Dequeue();

                    if (item.Response.PayloadString == responseText)
                    {
                        Assert.IsTrue((success & 1) == 0);
                        success |= 1;
                    }
                    else if (item.Response.PayloadString == response2)
                    {
                        Assert.IsTrue((success & 2) == 0);
                        success |= 2;
                    }
                    else
                    {
                        Assert.Fail();
                    }

                    break;
                }
            }

            Assert.AreEqual(3, success);
            Assert.AreEqual(21, clientCount);
            Assert.AreEqual(22, serverCount);
        }