Package org.apache.mina.coap

Examples of org.apache.mina.coap.CoapMessage


            CoapDecoder decoder = new CoapDecoder();
            ByteBuffer buff = ByteBuffer.allocateDirect(2048);

            Random r = new Random();
            byte[] url = "nlp".getBytes();
            CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 1234, null,
                    new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, url) }, null);

            for (int j = 0; j < 8; j++) {
                long start = System.currentTimeMillis();
                final int count = 100000;
                for (int i = 0; i < count; i++) {
                    buff.position(0);
                    buff.limit(buff.capacity());
                    int id = r.nextInt(1024);
                    msg.setId(id);
                    int bytes = channel.send(encoder.encode(msg, null), target);

                    if (bytes < 1) {
                        System.err.println("write fail :/ " + bytes);
                    } else {
                        buff.position(0);
                        buff.limit(buff.capacity());
                        SocketAddress addy = channel.receive(buff);
                        buff.flip();
                        CoapMessage response = decoder.decode(buff, null);
                        if (response.getId() != id) {
                            System.err.println("gni?");
                        }
                        // System.err.println("response : " + response);
                    }
                }
View Full Code Here


        @Override
        public void messageReceived(IoSession session, Object message) {

            System.err.println("rcvd : " + message);
            CoapMessage msg = (CoapMessage) message;
            if (msg.getCode() == CoapCode.GET.getCode() || msg.getCode() == CoapCode.POST.getCode()
                    || msg.getCode() == CoapCode.PUT.getCode() || msg.getCode() == CoapCode.DELETE.getCode()) {
                CoapMessage resp = reg.respond(msg, session);
                session.write(resp);
            }
        }
View Full Code Here

            @Override
            public void messageReceived(IoSession session, Object message) {
                System.err.println("rcv : " + message);

                CoapMessage resp = reg.respond((CoapMessage) message, session);
                System.err.println("resp : " + resp);
                session.write(resp);
                count++;
                if (count >= 100_000) {
                    System.err.println("time for 100k msg : " + (System.currentTimeMillis() - start));
View Full Code Here

        String url = urlBuilder.toString();
        LOG.debug("requested URL : {}", url);

        if (url.length() < 1) {
            // 4.00 !
            return new CoapMessage(1, MessageType.ACK, CoapCode.BAD_REQUEST.getCode(), request.getId(),
                    request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT,
                            new byte[] { 0 }) }, "no URL !".getBytes());
        }
        if (".well-known/core".equalsIgnoreCase(url)) {
            // discovery !
            return new CoapMessage(1, MessageType.ACK, CoapCode.CONTENT.getCode(), request.getId(), request.getToken(),
                    new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 40 }) },
                    getDiscovery());
        } else {
            ResourceHandler handler = handlers.get(url);
            if (handler == null) {
                // 4.04 !
                return new CoapMessage(1, MessageType.ACK, CoapCode.NOT_FOUND.getCode(), request.getId(),
                        request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT,
                                new byte[] { 0 }) }, "not found !".getBytes());
            } else {
                CoapResponse response = handler.handle(request, session);
                return new CoapMessage(1, request.getType() == MessageType.CONFIRMABLE ? MessageType.ACK
                        : MessageType.NON_CONFIRMABLE, response.getCode(), request.getId(), request.getToken(),
                        response.getOptions(), response.getContent());
            }
        }
    }
View Full Code Here

        }

        if (input.hasRemaining()) {
            throw new ProtocolDecoderException("trailling " + input.remaining() + " bytes in the UDP datagram");
        }
        return new CoapMessage(version, type, code, id, token, options.toArray(EMPTY_OPTION), payload);
    }
View Full Code Here

public class ResourceRegistryTest {

    @Test
    public void generate_4_04_on_unknown_resources() {
        ResourceRegistry reg = new ResourceRegistry();
        CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null,
                new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, "test".getBytes()) }, new byte[] {});
        CoapMessage resp = reg.respond(msg, null);
        assertEquals(1, resp.getVersion());
        assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());
        assertArrayEquals(new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) },
                resp.getOptions());
        assertArrayEquals("not found !".getBytes(), resp.getPayload());
    }
View Full Code Here

            }
        };
        reg.register(handler);

        // 4.04 on bad path
        CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null,
                new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, "meh".getBytes()) }, new byte[] {});
        CoapMessage resp = reg.respond(msg, null);
        assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());

        // on correct path the 2.02 with the response
        msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null, new CoapOption[] {
                                new CoapOption(CoapOptionType.URI_PATH, "myTest".getBytes()),
                                new CoapOption(CoapOptionType.URI_PATH, "Path".getBytes()) }, new byte[] {});
        resp = reg.respond(msg, null);
        assertEquals(1, resp.getVersion());
        assertEquals(12345, resp.getCode());
        assertArrayEquals(new CoapOption[] {}, resp.getOptions());
        assertArrayEquals("bla bla".getBytes(), resp.getPayload());
    }
View Full Code Here

    private CoapEncoder encoder = new CoapEncoder();

    @Test
    public void no_content_no_option() {
        CoapMessage message = NO_CONTENT_NO_OPTION;
        ByteBuffer encoded = encoder.encode(message, null);
        Assert.assertEquals(NO_CONTENT_NO_OPTION_HEX, ByteBufferDumper.toHex(encoded));

    }
View Full Code Here

    }

    @Test
    public void some_content_no_option() {
        CoapMessage message = SOME_CONTENT_NO_OPTION;
        ByteBuffer encoded = encoder.encode(message, null);

        Assert.assertEquals(SOME_CONTENT_NO_OPTION_HEX, ByteBufferDumper.toHex(encoded));

    }
View Full Code Here

    }

    @Test
    public void payload_and_one_option() {
        CoapMessage message = PAYLOAD_AND_ONE_OPTION;
        ByteBuffer encoded = encoder.encode(message, null);
        Assert.assertEquals(PAYLOAD_AND_ONE_OPTION_HEX, ByteBufferDumper.toHex(encoded));

    }
View Full Code Here

TOP

Related Classes of org.apache.mina.coap.CoapMessage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.