public void TestServer() { // We do not test for block 0 because the client is currently unable to // know if the user attempts to just retrieve block 0 or if he wants to // do early block negotiation with a specific size but actually wants to // retrieve all blocks. Int32[] blockOrder = { 2, 1, 3 }; String[] expectations = { RESPONSE_PAYLOAD.Substring(32 /* until the end */), RESPONSE_PAYLOAD.Substring(16, 16), null // block is out of bounds }; for (Int32 i = 0; i < blockOrder.Length; i++) { Int32 num = blockOrder[i]; Console.WriteLine("Request block number " + num); Int32 szx = BlockOption.EncodeSZX(16); Request request = Request.NewGet(); request.URI = new Uri("coap://localhost:" + _serverPort + "/" + TARGET); request.SetBlock2(szx, false, num); request.Send(); Response response = request.WaitForResponse(1000); Assert.IsNotNull(response); Assert.AreEqual(expectations[i], response.PayloadString); Assert.IsTrue(response.HasOption(OptionType.Block2)); Assert.AreEqual(num, response.Block2.NUM); Assert.AreEqual(szx, response.Block2.SZX); } }
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); }
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); }