Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Converts a BlockOption with the specified parameters to a byte array and
 /// back and checks that the result is the same as the original.
 /// </summary>
 private void TestCombined(Int32 szx, Boolean m, Int32 num)
 {
     BlockOption block = new BlockOption(OptionType.Block1, num, szx, m);
     BlockOption copy = new BlockOption(OptionType.Block1);
     copy.RawValue = block.RawValue;
     Assert.AreEqual(block.SZX, copy.SZX);
     Assert.AreEqual(block.M, copy.M);
     Assert.AreEqual(block.NUM, copy.NUM);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts a BlockOption with the specified parameters to a byte array and
        /// back and checks that the result is the same as the original.
        /// </summary>
        private void TestCombined(Int32 szx, Boolean m, Int32 num)
        {
            BlockOption block = new BlockOption(OptionType.Block1, num, szx, m);
            BlockOption copy  = new BlockOption(OptionType.Block1);

            copy.RawValue = block.RawValue;
            Assert.AreEqual(block.SZX, copy.SZX);
            Assert.AreEqual(block.M, copy.M);
            Assert.AreEqual(block.NUM, copy.NUM);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Places a new response to this request, e.g. to answer it
        /// </summary>
        /// <param name="response"></param>
        public void Respond(Response response)
        {
            response.Request      = this;
            response.Communicator = this.Communicator;

            response.PeerAddress = this.PeerAddress;

            if (_responseCount == 0 && IsConfirmable)
            {
                response.ID = this.ID;
            }

            // TODO 枚举不能与null比较
            //if (null == response.Type)
            {
                if (_responseCount == 0 && IsConfirmable)
                {
                    // use piggy-backed response
                    response.Type = MessageType.ACK;
                }
                else
                {
                    // use separate response:
                    // Confirmable response to confirmable request,
                    // Non-confirmable response to non-confirmable request
                    response.Type = this.Type;
                }
            }

            if (response.Code != CoAP.Code.Empty)
            {
                response.Token         = this.Token;
                response.RequiresToken = this.RequiresToken;

                // echo block1 option
                BlockOption block1 = (BlockOption)GetFirstOption(OptionType.Block1);
                if (null != block1)
                {
                    // TODO: block1.setM(false); maybe in TransferLayer
                    response.AddOption(block1);
                }
            }

            _responseCount++;
            Response = response;
            // Endpoint will call SendResponse();
        }
Ejemplo n.º 5
0
        protected Request Prepare(Request request, IEndPoint endpoint)
        {
            request.Type = _type;
            request.URI  = _uri;

            if (_blockwise != 0)
            {
                request.SetBlock2(BlockOption.EncodeSZX(_blockwise), false, 0);
            }

            if (endpoint != null)
            {
                request.EndPoint = endpoint;
            }

            return(request);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Helper function that creates a BlockOption with the specified parameters
 /// and serializes them to a byte array.
 /// </summary>
 private Byte[] ToBytes(Int32 szx, Boolean m, Int32 num)
 {
     Byte[] bytes = new BlockOption(OptionType.Block1, num, szx, m).RawValue;
     return bytes;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Helper function that creates a BlockOption with the specified parameters
 /// and serializes them to a byte array.
 /// </summary>
 private Byte[] ToBytes(Int32 szx, Boolean m, Int32 num)
 {
     Byte[] bytes = new BlockOption(OptionType.Block1, num, szx, m).RawValue;
     return(bytes);
 }