Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        private CoapObserveRelation ObserveAsync(Request request, Action <Response> notify, Action <FailReason> error)
        {
            IEndPoint endpoint = GetEffectiveEndpoint(request);
            ObserveNotificationOrderer orderer  = new ObserveNotificationOrderer(_config);
            CoapObserveRelation        relation = new CoapObserveRelation(request, endpoint);

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

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

            Prepare(request, endpoint).Send();
            return(relation);
        }
Ejemplo n.º 4
0
        public void TestSynchronousCall()
        {
            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);
        }
Ejemplo n.º 5
0
        private CoapObserveRelation ObserveAsync(Request request, Action<Response> notify, Action<FailReason> error)
        {
            IEndPoint endpoint = GetEffectiveEndpoint(request);
            ObserveNotificationOrderer orderer = new ObserveNotificationOrderer(_config);
            CoapObserveRelation relation = new CoapObserveRelation(request, endpoint);

            request.Respond += (o, e) =>
            {
                Response resp = e.Response;
                lock (orderer)
                {
                    if (orderer.IsNew(resp))
                    {
                        relation.Current = resp;
                        Deliver(notify, e);
                    }
                    else
                    {
                        if (log.IsDebugEnabled)
                            log.Debug("Dropping old notification: " + resp);
                    }
                }
            };
            Action<FailReason> fail = r =>
            {
                relation.Canceled = true;
                Fail(error, r);
            };
            request.Reject += (o, e) => fail(FailReason.Rejected);
            request.Timeout += (o, e) => fail(FailReason.TimedOut);

            Prepare(request, endpoint).Send();
            return relation;
        }