Package de.uniluebeck.itm.ncoap.message

Examples of de.uniluebeck.itm.ncoap.message.CoapResponse


    @Test
    public void testFirstMessage(){
        Iterator<Long> receptionTimes = clientCallback.getCoapResponses().keySet().iterator();

        CoapResponse response = clientCallback.getCoapResponses().get(receptionTimes.next());

        assertEquals("Message type is not ACK", MessageType.Name.ACK, response.getMessageTypeName());

        assertEquals("Content does not match.", "Status #1",
                response.getContent().toString(Charset.forName("UTF-8")));
    }
View Full Code Here


    @Test
    public void testSecondMessage(){
        Iterator<Long> receptionTimes = clientCallback.getCoapResponses().keySet().iterator();
        receptionTimes.next();

        CoapResponse response = clientCallback.getCoapResponses().get(receptionTimes.next());

        assertEquals("Wrong Message Code!", MessageCode.Name.CONTENT_205, response.getMessageCodeName());

        assertEquals("Content does not match.", "Status #2",
                response.getContent().toString(Charset.forName("UTF-8")));
    }
View Full Code Here

    public void testThirdMessage(){
        Iterator<Long> receptionTimes = clientCallback.getCoapResponses().keySet().iterator();
        receptionTimes.next();
        receptionTimes.next();

        CoapResponse response = clientCallback.getCoapResponses().get(receptionTimes.next());

        assertEquals("MessageCode is not 404", MessageCode.Name.NOT_FOUND_404, response.getMessageCodeName());
    }
View Full Code Here

     */
    @Override
    public void processCoapRequest(SettableFuture<CoapResponse> responseFuture, CoapRequest coapRequest,
                                   InetSocketAddress remoteEndpoint) throws Exception{

        CoapResponse coapResponse;

        if(!(coapRequest.getMessageCodeName() == MessageCode.Name.GET)){
            coapResponse = CoapResponse.createErrorResponse(coapRequest.getMessageTypeName(),
                    MessageCode.Name.METHOD_NOT_ALLOWED_405, "GET is the only allowed method!");
        }
View Full Code Here

    private CoapResponse processCoapGetRequest(CoapRequest coapRequest){
        try{
            LinkAttribute filterAttribute = createLinkAttributeFromQuery(coapRequest.getUriQuery());

            CoapResponse coapResponse = new CoapResponse(coapRequest.getMessageTypeName(),
                    MessageCode.Name.CONTENT_205);

            byte[] content = getSerializedResourceStatus(filterAttribute);

            coapResponse.setContent(content, ContentFormat.APP_LINK_FORMAT);
            coapResponse.setEtag(this.etag);

            return coapResponse;
        }

        catch(IllegalArgumentException ex){
View Full Code Here

    }

    @Test
    public void testClientsReceivedCorrectResponses(){
        for (int i = 0; i < NUMBER_OF_PARALLEL_REQUESTS; i++){
            CoapResponse coapResponse = clientCallbacks[i].getCoapResponses().values().iterator().next();

            assertEquals("Response Processor " + (i+1) + " received wrong message content",
                    "This is the status of service " + (i+1),
                    coapResponse.getContent().toString(Charset.forName("UTF-8"))
            );
        }
    }
View Full Code Here

    private boolean expected;


    public ResourceStatusAgeTest(long v1, long v2, long t1, long t2, boolean expected){

        CoapResponse coapResponse1 = new CoapResponse(MessageType.Name.CON, MessageCode.Name.CONTENT_205);
        coapResponse1.setObserve(v1);

        CoapResponse coapResponse2 = new CoapResponse(MessageType.Name.CON, MessageCode.Name.CONTENT_205);
        coapResponse2.setObserve(v2);

        long sequenceNo1 = coapResponse1.getObserve();
        long sequenceNo2 = coapResponse2.getObserve();
        log.info("Observe Option Values: V1 =  " + sequenceNo1 + ", V2 = " + sequenceNo2);
        log.info("Timestamps: T1 = " + t1 + ", T2 = " + t1 + ", (T2 > T1 + 128 sec [" + (t2 > t1 + 128000) + "])");

        this.params1 = new ResourceStatusAge(sequenceNo1, t1);
        this.params2 = new ResourceStatusAge(sequenceNo2, t2);
View Full Code Here

        else
            resourceStatus = getWrappedResourceStatus(coapRequest.getAcceptedContentFormats());

        //create CoAP response
        CoapResponse coapResponse;
        if(resourceStatus == null){
            coapResponse = new CoapResponse(coapRequest.getMessageTypeName(), MessageCode.Name.NOT_ACCEPTABLE_406);
            coapResponse.setContent("None of the accepted content formats is supported!".getBytes(CoapMessage.CHARSET),
                    ContentFormat.TEXT_PLAIN_UTF8);
        }

        else{
            coapResponse = new CoapResponse(coapRequest.getMessageTypeName(), MessageCode.Name.CONTENT_205);
            coapResponse.setContent(resourceStatus.getContent(), resourceStatus.getContentFormat());
            coapResponse.setEtag(resourceStatus.getEtag());
            coapResponse.setMaxAge(resourceStatus.getMaxAge());
        }

        responseFuture.set(coapResponse);
    }
View Full Code Here


    public void setMethodNotAllowedResponse(SettableFuture<CoapResponse> responseFuture, CoapRequest coapRequest)
        throws Exception{

        CoapResponse coapResponse = new CoapResponse(coapRequest.getMessageTypeName(),
                MessageCode.Name.METHOD_NOT_ALLOWED_405);

        coapResponse.setContent("Only method GET is allowed!".getBytes(CoapMessage.CHARSET),
                ContentFormat.TEXT_PLAIN_UTF8);

        responseFuture.set(coapResponse);
    }
View Full Code Here

    }


    private void handleIncomingCoapResponse(ChannelHandlerContext ctx, MessageEvent me) {

        CoapResponse coapResponse = (CoapResponse) me.getMessage();

        InetSocketAddress remoteEndpoint = (InetSocketAddress) me.getRemoteAddress();
        Token token = coapResponse.getToken();

        //Current response is NO update notification or is an error response (which SHOULD implicate the first)
        if(!coapResponse.isUpdateNotification() || MessageCode.isErrorMessage(coapResponse.getMessageCode())){
            if(observations.contains(remoteEndpoint, token)){
                log.info("Stop observation (remote address: {}, token: {}) due to received response: {}",
                        new Object[]{remoteEndpoint, token, coapResponse});

                stopObservation(remoteEndpoint, token);
            }
        }

        else if(coapResponse.isUpdateNotification()){
            //current response is update notification but there is no suitable observation
            if(!observations.contains(remoteEndpoint, token)){
                log.warn("No observation found for update notification (remote endpoint: {}, token: {}).",
                        remoteEndpoint, token);
            }

            //Current response is (non-error) update notification and there is a suitable observation
            else if(coapResponse.isUpdateNotification() && !MessageCode.isErrorMessage(coapResponse.getMessageCode())){
                //Lookup status age of latest update notification
                ResourceStatusAge latestStatusAge = observations.get(remoteEndpoint, token);

                //Get status age from newly received update notification
                long receivedSequenceNo = coapResponse.getObserve();
                ResourceStatusAge receivedStatusAge = new ResourceStatusAge(receivedSequenceNo, System.currentTimeMillis());

                if(ResourceStatusAge.isReceivedStatusNewer(latestStatusAge, receivedStatusAge)){
                    updateStatusAge(remoteEndpoint, token, receivedStatusAge);
                }
View Full Code Here

TOP

Related Classes of de.uniluebeck.itm.ncoap.message.CoapResponse

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.