Beispiel #1
0
        private CoapObserveRelation ObserveAsync(Request request, Action <Response> notify, Action <FailReason> error)
        {
            IEndPoint           endpoint = GetEffectiveEndpoint(request);
            CoapObserveRelation relation = new CoapObserveRelation(request, endpoint, _config);

            request.Respond += (o, e) =>
            {
                Response resp = e.Response;
                lock (relation) {
                    if (relation.Orderer.IsNew(resp))
                    {
                        relation.Current = resp;
                        Deliver(notify, e);
                    }
                    else
                    {
                        _Log.Debug(m => m("Dropping old notification: {0}", resp));
                    }
                }
            };
            Action <FailReason> fail = r =>
            {
                relation.Canceled = true;
                Fail(error, r);
            };

            request.Rejected += (o, e) => fail(FailReason.Rejected);
            request.TimedOut += (o, e) => fail(FailReason.TimedOut);

            Prepare(request, endpoint).Send();
            return(relation);
        }
Beispiel #2
0
        private CoapObserveRelation Observe(Request request, Action <Response> notify, Action <FailReason> error)
        {
            CoapObserveRelation relation = ObserveAsync(request, notify, error);
            Response            response = relation.Request.WaitForResponse(Timeout);

            if (response == null || !response.HasOption(OptionType.Observe))
            {
                relation.Canceled = true;
            }

            relation.Current = response;
            return(relation);
        }
        private void TestObserve(Uri uri)
        {
            Console.WriteLine("Test observe relation with a reactive cancelation to " + uri);

            ManualResetEvent mre = new ManualResetEvent(false);

            CoapClient client = new CoapClient(uri);

            client.EndPoint = _clientEndpoint;

            Int32 notificationCounter    = 0;
            CoapObserveRelation relation = null;

            relation = client.Observe(response =>
            {
                notificationCounter++;
                Console.WriteLine("Client received notification " + notificationCounter + ": " + response.PayloadString);

                if (notificationCounter == HOW_MANY_NOTIFICATION_WE_WAIT_FOR)
                {
                    Console.WriteLine("Client forgets observe relation to " + uri);
                    SpinWait.SpinUntil(() => relation != null);
                    relation.ProactiveCancel();
                }
                else if (notificationCounter == HOW_MANY_NOTIFICATION_WE_WAIT_FOR + 1)
                {
                    mre.Set();
                }
            }, reason => Assert.Fail(reason.ToString()));

            // Wait until we have received all the notifications and canceled the relation
            Thread.Sleep(HOW_MANY_NOTIFICATION_WE_WAIT_FOR * OBS_NOTIFICATION_INTERVALL + 3000);

            Boolean success = mre.WaitOne(100);

            Assert.IsTrue(success, "Client has not received all expected responses");

            _serverSurveillant.WaitUntilDeduplicatorShouldBeEmpty();
            _serverSurveillant.AssertMapsEmpty();
            _clientSurveillant.AssertMapsEmpty();
        }
Beispiel #4
0
        public void TestSynchronousCall()
        {
            _notifications = 0;

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            // Check that we get the right content when calling get()
            String resp1 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp1);

            String resp2 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp2);

            // Change the content to "two" and check
            String resp3 = client.Post(CONTENT_2).ResponseText;

            Assert.AreEqual(CONTENT_1, resp3);

            String resp4 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_2, resp4);

            // Observe the resource
            _expected = CONTENT_2;
            CoapObserveRelation obs1 = client.Observe(response =>
            {
                Interlocked.Increment(ref _notifications);
                String payload = response.ResponseText;
                Assert.AreEqual(_expected, payload);
                Assert.IsTrue(response.HasOption(OptionType.Observe));
            }, Fail);

            Assert.IsFalse(obs1.Canceled);

            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();

            Thread.Sleep(100);
            _expected = CONTENT_3;
            String resp5 = client.Post(CONTENT_3).ResponseText;

            Assert.AreEqual(CONTENT_2, resp5);

            // Try a put and receive a METHOD_NOT_ALLOWED
            StatusCode code6 = client.Put(CONTENT_4).StatusCode;

            Assert.AreEqual(StatusCode.MethodNotAllowed, code6);

            // Cancel observe relation of obs1 and check that it does no longer receive notifications
            Thread.Sleep(100);
            _expected = null; // The next notification would now cause a failure
            obs1.ReactiveCancel();
            Thread.Sleep(100);
            _resource.Changed();

            // Make another post
            Thread.Sleep(100);
            String resp7 = client.Post(CONTENT_4).ResponseText;

            Assert.AreEqual(CONTENT_3, resp7);

            // Try to use the builder and add a query
            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET);

            ub.Query = QUERY_UPPER_CASE;

            String resp8 = new CoapClient(ub.Uri).Get().ResponseText;

            Assert.AreEqual(CONTENT_4.ToUpper(), resp8);

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.AreEqual(5, _notifications);
            Assert.IsFalse(_failed);
        }
Beispiel #5
0
        public void TestSynchronousCall()
        {
            int            notifications = 0;
            AutoResetEvent syncEvent     = new AutoResetEvent(false);

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            // Check that we get the right content when calling get()
            string resp1 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp1);

            string resp2 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_1, resp2);

            // Change the content to "two" and check
            string resp3 = client.Post(CONTENT_2).ResponseText;

            Assert.AreEqual(CONTENT_1, resp3);

            string resp4 = client.Get().ResponseText;

            Assert.AreEqual(CONTENT_2, resp4);
            Thread.Sleep(100);

            // Observe the resource
            _expected = CONTENT_2;

            object         blockLock    = new object();
            AutoResetEvent trigger      = new AutoResetEvent(false);
            Response       lastResponse = null;

            CoapObserveRelation obs1 = client.Observe(response => {
                lastResponse = response;
                trigger.Set();
            }, Fail);

            Assert.IsFalse(obs1.Canceled);

            Assert.IsTrue(trigger.WaitOne(100));
            Assert.AreEqual(_expected, lastResponse.ResponseText);
            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));

            for (int i = 0; i < 5; i++)
            {
                _resource.Changed();
                Assert.IsTrue(trigger.WaitOne(100));
                Assert.AreEqual(_expected, lastResponse.ResponseText);
                Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
            }

            _expected = CONTENT_3;
            string resp5 = client.Post(CONTENT_3).ResponseText;

            Assert.AreEqual(CONTENT_2, resp5);

            Assert.IsTrue(trigger.WaitOne(100));
            Assert.AreEqual(_expected, lastResponse.ResponseText);
            Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));

            // Try a put and receive a METHOD_NOT_ALLOWED
            StatusCode code6 = client.Put(CONTENT_4).StatusCode;

            Assert.AreEqual(StatusCode.MethodNotAllowed, code6);
            Assert.IsFalse(trigger.WaitOne(100));

            // Cancel observe relation of obs1 and check that it does no longer receive notifications

            obs1.ReactiveCancel();
            Thread.Sleep(100);

            _resource.Changed();
            Assert.IsFalse(trigger.WaitOne(100));

            // Make another post
            Thread.Sleep(100);
            string resp7 = client.Post(CONTENT_4).ResponseText;

            Assert.AreEqual(CONTENT_3, resp7);

            // Try to use the builder and add a query
            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET);

            ub.Query = QUERY_UPPER_CASE;

            string resp8 = new CoapClient(ub.Uri).Get().ResponseText;

            Assert.AreEqual(CONTENT_4.ToUpper(), resp8);

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.IsFalse(_failed);
        }
Beispiel #6
0
        public void TestAsynchronousCall()
        {
            int notifications = 0;

            Uri        uri    = new Uri("coap://localhost:" + _serverPort + "/" + TARGET);
            CoapClient client = new CoapClient(uri);

            client.Error += (o, e) => Fail(e.Reason);

            // Check that we get the right content when calling get()
            client.GetAsync(response => Assert.AreEqual(CONTENT_1, response.ResponseText));
            Thread.Sleep(100);

            client.GetAsync(response => Assert.AreEqual(CONTENT_1, response.ResponseText));
            Thread.Sleep(100);

            // Change the content to "two" and check
            client.PostAsync(CONTENT_2, response => Assert.AreEqual(CONTENT_1, response.ResponseText));
            Thread.Sleep(100);

            client.GetAsync(response => Assert.AreEqual(CONTENT_2, response.ResponseText));
            Thread.Sleep(100);

            // Observe the resource
            string expected          = CONTENT_2;
            int    actualTest        = 0;
            int    notifyTest        = 0;
            CoapObserveRelation obs1 = client.ObserveAsync(response =>
            {
                Interlocked.Increment(ref _notifications);
                string payload = response.ResponseText;
                Assert.AreEqual(_expected, payload);
                Assert.IsTrue(response.HasOption(OptionType.Observe));
            }
                                                           );

            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();
            Thread.Sleep(100);
            _resource.Changed();

            Thread.Sleep(100);
            expected = CONTENT_3;
            client.PostAsync(CONTENT_3, response => Assert.AreEqual(CONTENT_2, response.ResponseText));
            Thread.Sleep(100);

            // Try a put and receive a MethodNotAllowed
            client.PutAsync(CONTENT_4, response => Assert.AreEqual(StatusCode.MethodNotAllowed, response.StatusCode));

            // Cancel observe relation of obs1 and check that it does no longer receive notifications
            Thread.Sleep(100);
            expected = null; // The next notification would now cause a failure
            obs1.ReactiveCancel();
            Thread.Sleep(100);
            _resource.Changed();

            // Make another post
            Thread.Sleep(100);
            client.PostAsync(CONTENT_4, response => Assert.AreEqual(CONTENT_3, response.ResponseText));
            Thread.Sleep(100);

            UriBuilder ub = new UriBuilder("coap", "localhost", _serverPort, TARGET)
            {
                Query = QUERY_UPPER_CASE
            };

            // Try to use the builder and add a query
            new CoapClient(ub.Uri).GetAsync(response => Assert.AreEqual(CONTENT_4.ToUpper(), response.ResponseText));

            // Check that we indeed received 5 notifications
            // 1 from origin GET request, 3 x from changed(), 1 from post()
            Thread.Sleep(100);
            Assert.AreEqual(0, actualTest);
            Assert.AreEqual(0, notifyTest);
            Assert.IsFalse(_failed);
        }