├── README.md ├── ncoap-core ├── pom.xml └── src │ ├── main │ ├── java │ │ └── de │ │ │ └── uzl │ │ │ └── itm │ │ │ └── ncoap │ │ │ ├── application │ │ │ ├── AbstractCoapApplication.java │ │ │ ├── CoapChannelPipelineFactory.java │ │ │ ├── client │ │ │ │ ├── ClientCallback.java │ │ │ │ ├── ClientChannelPipelineFactory.java │ │ │ │ └── CoapClient.java │ │ │ ├── endpoint │ │ │ │ ├── CoapEndpoint.java │ │ │ │ └── CoapEndpointChannelPipelineFactory.java │ │ │ ├── linkformat │ │ │ │ ├── LinkParam.java │ │ │ │ ├── LinkValue.java │ │ │ │ └── LinkValueList.java │ │ │ └── server │ │ │ │ ├── CoapServer.java │ │ │ │ ├── CoapServerChannelPipelineFactory.java │ │ │ │ └── resource │ │ │ │ ├── NotObservableWebresource.java │ │ │ │ ├── ObservableWebresource.java │ │ │ │ ├── Webresource.java │ │ │ │ ├── WellKnownCoreResource.java │ │ │ │ └── WrappedResourceStatus.java │ │ │ ├── communication │ │ │ ├── AbstractCoapChannelHandler.java │ │ │ ├── blockwise │ │ │ │ ├── BlockSize.java │ │ │ │ ├── client │ │ │ │ │ ├── ClientBlock1Handler.java │ │ │ │ │ └── ClientBlock2Handler.java │ │ │ │ └── server │ │ │ │ │ ├── ServerBlock1Handler.java │ │ │ │ │ └── ServerBlock2Handler.java │ │ │ ├── codec │ │ │ │ ├── CoapMessageDecoder.java │ │ │ │ ├── CoapMessageEncoder.java │ │ │ │ ├── HeaderDecodingException.java │ │ │ │ └── OptionCodecException.java │ │ │ ├── dispatching │ │ │ │ ├── Token.java │ │ │ │ ├── client │ │ │ │ │ ├── ResponseDispatcher.java │ │ │ │ │ └── TokenFactory.java │ │ │ │ └── server │ │ │ │ │ ├── NotFoundHandler.java │ │ │ │ │ ├── RequestConsumer.java │ │ │ │ │ └── RequestDispatcher.java │ │ │ ├── events │ │ │ │ ├── AbstractMessageExchangeEvent.java │ │ │ │ ├── AbstractMessageTransferEvent.java │ │ │ │ ├── EmptyAckReceivedEvent.java │ │ │ │ ├── MessageIDAssignedEvent.java │ │ │ │ ├── MessageIDReleasedEvent.java │ │ │ │ ├── MessageRetransmittedEvent.java │ │ │ │ ├── MiscellaneousErrorEvent.java │ │ │ │ ├── NoMessageIDAvailableEvent.java │ │ │ │ ├── ResetReceivedEvent.java │ │ │ │ ├── TransmissionTimeoutEvent.java │ │ │ │ ├── client │ │ │ │ │ ├── BlockwiseResponseTransferFailedEvent.java │ │ │ │ │ ├── ContinueResponseReceivedEvent.java │ │ │ │ │ ├── RemoteServerSocketChangedEvent.java │ │ │ │ │ ├── ResponseBlockReceivedEvent.java │ │ │ │ │ └── TokenReleasedEvent.java │ │ │ │ └── server │ │ │ │ │ ├── ObserverAcceptedEvent.java │ │ │ │ │ └── RemoteClientSocketChangedEvent.java │ │ │ ├── identification │ │ │ │ ├── AbstractIdentificationHandler.java │ │ │ │ ├── ClientIdentificationHandler.java │ │ │ │ ├── EndpointID.java │ │ │ │ ├── EndpointIDFactory.java │ │ │ │ └── ServerIdentificationHandler.java │ │ │ ├── observing │ │ │ │ ├── ClientObservationHandler.java │ │ │ │ ├── ResourceStatusAge.java │ │ │ │ └── ServerObservationHandler.java │ │ │ └── reliability │ │ │ │ ├── inbound │ │ │ │ ├── ClientInboundReliabilityHandler.java │ │ │ │ └── ServerInboundReliabilityHandler.java │ │ │ │ └── outbound │ │ │ │ ├── AbstractOutboundReliabilityHandler.java │ │ │ │ ├── ClientOutboundReliabilityHandler.java │ │ │ │ ├── MessageIDFactory.java │ │ │ │ └── ServerOutboundReliabilityHandler.java │ │ │ └── message │ │ │ ├── CoapMessage.java │ │ │ ├── CoapRequest.java │ │ │ ├── CoapResponse.java │ │ │ ├── MessageCode.java │ │ │ ├── MessageType.java │ │ │ └── options │ │ │ ├── ContentFormat.java │ │ │ ├── EmptyOptionValue.java │ │ │ ├── OpaqueOptionValue.java │ │ │ ├── Option.java │ │ │ ├── OptionValue.java │ │ │ ├── StringOptionValue.java │ │ │ └── UintOptionValue.java │ └── resources │ │ └── stylesheet.css │ └── test │ └── java │ └── de │ └── uzl │ └── itm │ └── ncoap │ ├── AbstractCoapTest.java │ ├── communication │ ├── AbstractCoapCommunicationTest.java │ ├── ClientReceivesSeparateResponseTest.java │ ├── ClientReceivesUpdateNotifications.java │ ├── ClientSendsCONRequestThatTimesOut.java │ ├── ClientSendsNONRequest.java │ ├── ClientSendsNONRequestThatTimesOut.java │ ├── ClientSendsPingToAnotherClient.java │ ├── ClientSendsPingToServer.java │ ├── ClientSendsTheSameCONRequestTwice.java │ ├── ClientSendsTheSameNONRequestTwice.java │ ├── ServerSendsPiggyBackedResponseTest.java │ ├── ServerSendsSeparateResponseTest.java │ ├── TestParallelRequests.java │ ├── TestParallelRequestsEndpointToEndpoint.java │ ├── blockwise │ │ ├── ClientSendsMultiplePostRequestsWithBlock1AndBlock2.java │ │ ├── ClientSendsPostRequestWithBlock1.java │ │ ├── ClientSendsRequestWithBlock1AndBlock2ToETHZ.java │ │ └── ClientSendsRequestWithBlock1ToETHZ.java │ ├── codec │ │ ├── AllowDefaultOptionValues.java │ │ ├── DecodeEncodedMessageTest.java │ │ ├── MessageDecodingWithInvalidMessages.java │ │ ├── MessageDecodingWithValidMessages.java │ │ ├── MessageEncoding.java │ │ ├── OptionEncoding.java │ │ └── tools │ │ │ ├── CoapTestDecoder.java │ │ │ └── CoapTestEncoder.java │ ├── identification │ │ └── EndpointIDRepeatedInResponseTest.java │ └── observe │ │ ├── ObservationStopsDueToErrorResponse.java │ │ ├── ObservationTerminationTest.java │ │ ├── ResourceStatusChangeDuringRetransmission.java │ │ └── client │ │ └── ResourceStatusAgeTest.java │ ├── endpoints │ ├── DummyEndpoint.java │ ├── client │ │ └── TestCallback.java │ └── server │ │ ├── NotObservableTestWebresource.java │ │ ├── NotObservableTestWebresourceForPost.java │ │ └── ObservableTestWebresource.java │ ├── linkformat │ ├── LinkFormatTest.java │ └── WellKnownCoreResourceTests.java │ └── message │ ├── CoapMessageEqualityTest.java │ ├── CoapRequestCreationTest.java │ └── CoapRequestUriOptionsTest.java ├── ncoap-simple-client ├── README.md ├── log4j.xml ├── pom.xml └── src │ └── main │ ├── java │ └── de │ │ └── uzl │ │ └── itm │ │ └── ncoap │ │ └── examples │ │ └── client │ │ ├── SimpleCoapClient.java │ │ ├── callback │ │ ├── SimpleCallback.java │ │ └── SimpleObservationCallback.java │ │ └── config │ │ ├── ClientCmdLineArgumentsWrapper.java │ │ └── LoggingConfiguration.java │ └── resources │ └── log4j.default.xml ├── ncoap-simple-server ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── de │ │ └── uzl │ │ └── itm │ │ └── ncoap │ │ └── examples │ │ └── server │ │ ├── LoggingConfiguration.java │ │ ├── SimpleCoapServer.java │ │ ├── SimpleNotObservableWebresource.java │ │ └── SimpleObservableTimeService.java │ └── resources │ └── log4j.default.xml └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | ## A JAVA implementation of the CoAP - Java使用netty封装CoAP通信 2 | 3 | This implementation of the Constrained Application Protocol bases on the asynchronous and event-driven network 4 | application framework [Netty](http://netty.io) (thats where the 'n' in nCoAP comes from). The nCoAP framework 5 | currently covers 6 | 7 | * the raw protocol ([RFC 7252](https://tools.ietf.org/html/rfc7252)), 8 | * the observation of CoAP resources ([RFC 7641](https://tools.ietf.org/html/rfc7641)), 9 | * the blockwise transfer ([draft 19](https://tools.ietf.org/html/draft-ietf-core-block-19)), 10 | * the identification of endpoints with changing IPs 11 | ([draft 01](https://tools.ietf.org/html/draft-kleine-core-coap-endpoint-id-01)) , and 12 | * the CoRE Link Format ([RFC 6690](https://tools.ietf.org/html/rfc6690)). 13 | 14 | but without DTLS (i.e. support for the coaps scheme). 15 | 16 | provide simple CoAP applications for both, client and server. There intention is to highlight, how easy it is to 17 | write such applications using ncoap. 18 | 19 | 实例中有client、server可进行测试调用 -------------------------------------------------------------------------------- /ncoap-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | de.uzl.itm 9 | ncoap-complete 10 | 1.8.3-SNAPSHOT 11 | 12 | 13 | ncoap-core 14 | nCoAP Core 15 | 16 | Implementation of the Constrained Application Protocol (RFC 7252), the extensions to observe CoAP resources 17 | (RFC 7641), transfer messages blockwise (draft 19), identify CoAP endpoints despite changing IP addresses 18 | (draft 01), and the CoRE Link Format (RFC 6690). 19 | 20 | jar 21 | 22 | 23 | 24 | repository.jboss.org 25 | https://repository.jboss.org/nexus/content/repositories/releases 26 | 27 | 28 | 29 | 30 | 31 | org.slf4j 32 | slf4j-api 33 | 1.6.6 34 | 35 | 36 | io.netty 37 | netty 38 | 3.9.0.Final 39 | 40 | 41 | com.google.guava 42 | guava 43 | 16.0.1 44 | 45 | 46 | org.slf4j 47 | slf4j-log4j12 48 | 1.6.6 49 | test 50 | 51 | 52 | junit 53 | junit 54 | 4.11 55 | test 56 | 57 | 58 | joda-time 59 | joda-time 60 | 2.1 61 | test 62 | 63 | 64 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/application/CoapChannelPipelineFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.application; 26 | 27 | import de.uzl.itm.ncoap.communication.codec.CoapMessageDecoder; 28 | import de.uzl.itm.ncoap.communication.codec.CoapMessageEncoder; 29 | import org.jboss.netty.channel.ChannelHandler; 30 | import org.jboss.netty.channel.ChannelPipeline; 31 | import org.jboss.netty.channel.ChannelPipelineFactory; 32 | import org.jboss.netty.channel.Channels; 33 | import org.jboss.netty.handler.execution.ExecutionHandler; 34 | import org.slf4j.Logger; 35 | import org.slf4j.LoggerFactory; 36 | 37 | import java.util.*; 38 | import java.util.concurrent.ScheduledExecutorService; 39 | 40 | /** 41 | * Abstract base class for pipeline factories for clients, servers and peers. 42 | * 43 | * @author Oliver Kleine 44 | */ 45 | public abstract class CoapChannelPipelineFactory implements ChannelPipelineFactory { 46 | 47 | private static Logger LOG = LoggerFactory.getLogger(CoapChannelPipelineFactory.class.getName()); 48 | 49 | private Set channelHandlers; 50 | 51 | 52 | protected CoapChannelPipelineFactory(ScheduledExecutorService executor) { 53 | this.channelHandlers = new LinkedHashSet<>(); 54 | 55 | addChannelHandler(new ExecutionHandler(executor)); 56 | addChannelHandler(new CoapMessageEncoder()); 57 | addChannelHandler(new CoapMessageDecoder()); 58 | } 59 | 60 | 61 | protected void addChannelHandler(ChannelHandler channelHandler) { 62 | this.channelHandlers.add(channelHandler); 63 | } 64 | 65 | public Set getChannelHandlers () { 66 | return this.channelHandlers; 67 | } 68 | 69 | @Override 70 | public ChannelPipeline getPipeline() throws Exception { 71 | ChannelPipeline pipeline = Channels.pipeline(); 72 | 73 | for(ChannelHandler handler : this.channelHandlers) { 74 | String handlerName = handler.getClass().getSimpleName(); 75 | pipeline.addLast(handler.getClass().getSimpleName(), handler); 76 | LOG.debug("Added Handler to Pipeline: {}.", handlerName); 77 | } 78 | 79 | return pipeline; 80 | } 81 | 82 | // /** 83 | // * Returns the {@link org.jboss.netty.channel.ChannelHandler} instance which is part of each pipeline created 84 | // * using this factory (or null if no such handler exists). See static constants for the 85 | // * available names. 86 | // * 87 | // * @param name the name of the {@link org.jboss.netty.channel.ChannelHandler instance to be returned} 88 | // * 89 | // * @return the {@link org.jboss.netty.channel.ChannelHandler} instance which is part of each pipeline created 90 | // * using this factory (or null if no such handler exists). 91 | // */ 92 | // public ChannelHandler getChannelHandler(String name) { 93 | // return handler.get(name); 94 | // } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/application/client/ClientChannelPipelineFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.application.client; 26 | 27 | import de.uzl.itm.ncoap.application.CoapChannelPipelineFactory; 28 | import de.uzl.itm.ncoap.communication.blockwise.client.ClientBlock1Handler; 29 | import de.uzl.itm.ncoap.communication.blockwise.client.ClientBlock2Handler; 30 | import de.uzl.itm.ncoap.communication.dispatching.client.ResponseDispatcher; 31 | import de.uzl.itm.ncoap.communication.dispatching.client.TokenFactory; 32 | import de.uzl.itm.ncoap.communication.identification.ClientIdentificationHandler; 33 | import de.uzl.itm.ncoap.communication.observing.ClientObservationHandler; 34 | import de.uzl.itm.ncoap.communication.reliability.inbound.ClientInboundReliabilityHandler; 35 | import de.uzl.itm.ncoap.communication.reliability.outbound.ClientOutboundReliabilityHandler; 36 | import de.uzl.itm.ncoap.communication.reliability.outbound.MessageIDFactory; 37 | import org.jboss.netty.channel.ChannelPipeline; 38 | import org.jboss.netty.channel.socket.DatagramChannel; 39 | 40 | import java.util.concurrent.ScheduledExecutorService; 41 | 42 | 43 | /** 44 | * Factory to provide the {@link ChannelPipeline} for a newly created {@link DatagramChannel} for a 45 | * {@link CoapClient}. 46 | * 47 | * @author Oliver Kleine 48 | */ 49 | public class ClientChannelPipelineFactory extends CoapChannelPipelineFactory { 50 | 51 | 52 | /** 53 | * Creates a new instance of {@link ClientChannelPipelineFactory}. 54 | * 55 | * @param executor The {@link ScheduledExecutorService} to provide the thread(s) for I/O operations 56 | */ 57 | public ClientChannelPipelineFactory(ScheduledExecutorService executor) { 58 | 59 | super(executor); 60 | addChannelHandler(new ClientIdentificationHandler(executor)); 61 | addChannelHandler(new ClientOutboundReliabilityHandler(executor, new MessageIDFactory(executor))); 62 | addChannelHandler(new ClientInboundReliabilityHandler(executor)); 63 | addChannelHandler(new ClientBlock2Handler(executor)); 64 | addChannelHandler(new ClientBlock1Handler(executor)); 65 | addChannelHandler(new ClientObservationHandler(executor)); 66 | addChannelHandler(new ResponseDispatcher(executor, new TokenFactory())); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/application/server/CoapServerChannelPipelineFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.application.server; 26 | 27 | import de.uzl.itm.ncoap.application.CoapChannelPipelineFactory; 28 | import de.uzl.itm.ncoap.communication.blockwise.BlockSize; 29 | import de.uzl.itm.ncoap.communication.blockwise.server.ServerBlock1Handler; 30 | import de.uzl.itm.ncoap.communication.blockwise.server.ServerBlock2Handler; 31 | import de.uzl.itm.ncoap.communication.dispatching.server.NotFoundHandler; 32 | import de.uzl.itm.ncoap.communication.dispatching.server.RequestDispatcher; 33 | import de.uzl.itm.ncoap.communication.identification.ServerIdentificationHandler; 34 | import de.uzl.itm.ncoap.communication.observing.ServerObservationHandler; 35 | import de.uzl.itm.ncoap.communication.reliability.inbound.ServerInboundReliabilityHandler; 36 | import de.uzl.itm.ncoap.communication.reliability.outbound.MessageIDFactory; 37 | import de.uzl.itm.ncoap.communication.reliability.outbound.ServerOutboundReliabilityHandler; 38 | import org.jboss.netty.channel.ChannelPipeline; 39 | import org.jboss.netty.channel.socket.DatagramChannel; 40 | 41 | import java.util.concurrent.ScheduledExecutorService; 42 | 43 | /** 44 | * Factory to provide the {@link ChannelPipeline} for newly created {@link DatagramChannel}s. 45 | * 46 | * @author Oliver Kleine 47 | */ 48 | public class CoapServerChannelPipelineFactory extends CoapChannelPipelineFactory { 49 | 50 | /** 51 | * Creates a new instance of {@link CoapServerChannelPipelineFactory}. 52 | * 53 | * @param executor The {@link ScheduledExecutorService} to provide the thread(s) for I/O operations 54 | * @param notFoundHandler the {@link de.uzl.itm.ncoap.communication.dispatching.server.NotFoundHandler} 55 | * to handle inbound {@link de.uzl.itm.ncoap.message.CoapRequest}s targeting 56 | * unknown {@link de.uzl.itm.ncoap.application.server.resource.Webresource}s. 57 | */ 58 | public CoapServerChannelPipelineFactory(ScheduledExecutorService executor, NotFoundHandler notFoundHandler, 59 | BlockSize maxBlock1Size, BlockSize maxBlock2Size) { 60 | 61 | super(executor); 62 | addChannelHandler(new ServerIdentificationHandler(executor)); 63 | addChannelHandler(new ServerOutboundReliabilityHandler(executor, new MessageIDFactory(executor))); 64 | addChannelHandler(new ServerInboundReliabilityHandler(executor)); 65 | addChannelHandler(new ServerBlock1Handler(executor, maxBlock1Size)); 66 | addChannelHandler(new ServerBlock2Handler(executor, maxBlock2Size)); 67 | addChannelHandler(new ServerObservationHandler(executor)); 68 | addChannelHandler(new RequestDispatcher(notFoundHandler, executor)); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/application/server/resource/WrappedResourceStatus.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.application.server.resource; 26 | 27 | /** 28 | * Instances of {@link WrappedResourceStatus} wrap a particular 29 | * representation of the actual status with meta data. 30 | * 31 | * @author Oliver Kleine 32 | */ 33 | public class WrappedResourceStatus { 34 | 35 | private byte[] content; 36 | private long contentFormat; 37 | private byte[] etag; 38 | private long maxAge; 39 | 40 | /** 41 | * Creates a new instance of {@link WrappedResourceStatus} 42 | * @param content the serialized resource status 43 | * @param contentFormat the number representing the serialization format 44 | * @param etag the ETAG value of the actual status 45 | * @param maxAge the number of seconds this status is allowed to be cached 46 | */ 47 | public WrappedResourceStatus(byte[] content, long contentFormat, byte[] etag, long maxAge) { 48 | this.content = content; 49 | this.contentFormat = contentFormat; 50 | this.etag = etag; 51 | this.maxAge = maxAge; 52 | } 53 | 54 | /** 55 | * Returns the serialized resource status, i.e. a particular representation 56 | * @return the serialized resource status, i.e. a particular representation 57 | */ 58 | public byte[] getContent() { 59 | return content; 60 | } 61 | 62 | /** 63 | * Returns the number referring to the format of the serialized resource status returned by {@link #getContent()}. 64 | * 65 | * @return the number referring to the format of the serialized resource status 66 | */ 67 | public long getContentFormat() { 68 | return contentFormat; 69 | } 70 | 71 | /** 72 | * Returns the ETAG value of the serialized resource status returned by {@link #getContent()}. 73 | * @return the ETAG value of the serialized resource status 74 | */ 75 | public byte[] getEtag() { 76 | return etag; 77 | } 78 | 79 | /** 80 | * Returns the number of seconds a cache is allowed to cache this status 81 | * @return the number of seconds a cache is allowed to cache this status 82 | */ 83 | public long getMaxAge() { 84 | return maxAge; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/blockwise/BlockSize.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.blockwise; 26 | 27 | import de.uzl.itm.ncoap.message.options.UintOptionValue; 28 | import jdk.nashorn.internal.ir.Block; 29 | 30 | /** 31 | * Created by olli on 09.02.16. 32 | */ 33 | public enum BlockSize { 34 | UNBOUND(-1, 65536), 35 | SIZE_16(0, 16), 36 | SIZE_32(1, 32), 37 | SIZE_64(2, 64), 38 | SIZE_128(3, 128), 39 | SIZE_256(4, 256), 40 | SIZE_512(5, 512), 41 | SIZE_1024(6, 1024); 42 | 43 | private int encodedSize; 44 | private int decodedSize; 45 | 46 | public static final int UNDEFINED = -1; 47 | private static final int SZX_MIN = 0; 48 | private static final int SZX_MAX = 6; 49 | 50 | BlockSize(int encodedSize, int decodedSize) { 51 | this.encodedSize = encodedSize; 52 | this.decodedSize = decodedSize; 53 | } 54 | 55 | public int getSzx() { 56 | return this.encodedSize; 57 | } 58 | 59 | public int getSize() { 60 | return this.decodedSize; 61 | } 62 | 63 | public static int getSize(long szx) { 64 | return getBlockSize(szx).getSize(); 65 | } 66 | 67 | public static boolean isValid(long szx) { 68 | return !(szx < SZX_MIN) && !(szx > SZX_MAX); 69 | } 70 | 71 | /** 72 | * Returns the SZX value representing the smaller block size. 73 | * 74 | * @param szx1 the first SZX 75 | * @param szx2 the second SZX 76 | * 77 | * @return the SZX value representing the smaller block size. 78 | */ 79 | public static long min(long szx1, long szx2) throws IllegalArgumentException { 80 | if (szx1 < UNDEFINED || szx1 > SZX_MAX || szx2 < UNDEFINED || szx2 > SZX_MAX) { 81 | throw new IllegalArgumentException("SZX value out of allowed range."); 82 | } else if (szx1 == BlockSize.UNDEFINED) { 83 | return szx2; 84 | } else if (szx2 == BlockSize.UNDEFINED) { 85 | return szx1; 86 | } else { 87 | return Math.min(szx1, szx2); 88 | } 89 | } 90 | 91 | public static BlockSize getBlockSize(long szx) throws IllegalArgumentException{ 92 | if (szx == BlockSize.UNDEFINED) { 93 | return BlockSize.UNBOUND; 94 | } else if (szx == 0) { 95 | return SIZE_16; 96 | } else if (szx == 1) { 97 | return SIZE_32; 98 | } else if (szx == 2) { 99 | return SIZE_64; 100 | } else if (szx == 3) { 101 | return SIZE_128; 102 | } else if (szx == 4) { 103 | return SIZE_256; 104 | } else if (szx == 5) { 105 | return SIZE_512; 106 | } else if (szx == 6) { 107 | return SIZE_1024; 108 | } else { 109 | throw new IllegalArgumentException("Unsupported SZX value (Block Option): " + szx); 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/codec/HeaderDecodingException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.codec; 26 | 27 | import java.net.InetSocketAddress; 28 | 29 | /** 30 | * An {@link HeaderDecodingException} indicates that the header, i.e. the first 4 bytes of an inbound serialized 31 | * {@link de.uzl.itm.ncoap.message.CoapMessage} are malformed. This exception is thrown during the decoding process and causes an RST message 32 | * to be sent to the inbound message origin. 33 | * 34 | * @author Oliver Kleine 35 | */ 36 | public class HeaderDecodingException extends Exception{ 37 | 38 | private int messageID; 39 | private InetSocketAddress remoteSocket; 40 | 41 | /** 42 | * Creates a new instance of {@link HeaderDecodingException}. 43 | * 44 | * @param messageID the message ID of the message that caused 45 | * @param remoteSocket the malformed message origin 46 | */ 47 | public HeaderDecodingException(int messageID, InetSocketAddress remoteSocket, String message) { 48 | super(message); 49 | this.messageID = messageID; 50 | this.remoteSocket = remoteSocket; 51 | } 52 | 53 | /** 54 | * Returns the message ID of the inbound malformed message 55 | * 56 | * @return the message ID of the inbound malformed message 57 | */ 58 | public int getMessageID() { 59 | return messageID; 60 | } 61 | 62 | /** 63 | * Returns the malformed inbound messages origin CoAP endpoints 64 | * 65 | * @return the malformed inbound messages origin CoAP endpoints 66 | */ 67 | public InetSocketAddress getremoteSocket() { 68 | return remoteSocket; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/dispatching/server/RequestConsumer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.dispatching.server; 26 | 27 | import com.google.common.util.concurrent.SettableFuture; 28 | import de.uzl.itm.ncoap.message.CoapRequest; 29 | import de.uzl.itm.ncoap.message.CoapResponse; 30 | 31 | import java.net.InetSocketAddress; 32 | 33 | /** 34 | * Created by olli on 05.10.15. 35 | */ 36 | public interface RequestConsumer { 37 | 38 | /** 39 | * This method is invoked by the framework on inbound {@link de.uzl.itm.ncoap.message.CoapRequest}s with 40 | * {@link de.uzl.itm.ncoap.message.MessageCode#PUT} if there is no 41 | * {@link de.uzl.itm.ncoap.application.server.resource.Webresource} registered at the path given as 42 | * {@link de.uzl.itm.ncoap.message.CoapRequest#getUriPath()}. 43 | * 44 | * @param responseFuture the {@link com.google.common.util.concurrent.SettableFuture} to be set with a proper 45 | * {@link de.uzl.itm.ncoap.message.CoapResponse} to indicate whether there was a new 46 | * {@link de.uzl.itm.ncoap.application.server.resource.Webresource} created or not. 47 | * 48 | * @param coapRequest the {@link de.uzl.itm.ncoap.message.CoapRequest} to be processed 49 | * 50 | * @param remoteSocket the {@link java.net.InetSocketAddress} of the {@link de.uzl.itm.ncoap.message.CoapRequest}s origin. 51 | */ 52 | public void processCoapRequest(SettableFuture responseFuture, CoapRequest coapRequest, 53 | InetSocketAddress remoteSocket) throws Exception; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/AbstractMessageExchangeEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | 29 | import java.net.InetSocketAddress; 30 | 31 | /** 32 | * Created by olli on 05.09.15. 33 | */ 34 | public abstract class AbstractMessageExchangeEvent{ 35 | 36 | 37 | private final InetSocketAddress remoteSocket; 38 | private final Token token; 39 | 40 | 41 | public AbstractMessageExchangeEvent(InetSocketAddress remoteSocket, Token token) { 42 | this.remoteSocket = remoteSocket; 43 | this.token = token; 44 | } 45 | 46 | /** 47 | * Returns the remote endpoint of the message exchange (i.e. communication) that caused this events 48 | * @return the remote endpoint of the message exchange (i.e. communication) that caused this events 49 | */ 50 | public InetSocketAddress getRemoteSocket() { 51 | return remoteSocket; 52 | } 53 | 54 | /** 55 | * Returns the token of the message exchange (i.e. communication) that caused this events 56 | * @return the token of the message exchange (i.e. communication) that caused this events 57 | */ 58 | public Token getToken() { 59 | return token; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/AbstractMessageTransferEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | 29 | import java.net.InetSocketAddress; 30 | 31 | /** 32 | * Abstract base class for internal events that are caused within an ongoing or aspired message exchange 33 | * 34 | * @author Oliver Kleine 35 | */ 36 | public abstract class AbstractMessageTransferEvent extends AbstractMessageExchangeEvent{ 37 | 38 | private int messageID; 39 | 40 | /** 41 | * Creates a new instance of {@link AbstractMessageTransferEvent} 42 | * @param remoteSocket the remote socket of the transfer that caused this event 43 | * @param messageID the message ID of the transfer that caused this event 44 | * @param token the {@link Token} of the of the transfer that caused this event 45 | */ 46 | public AbstractMessageTransferEvent(InetSocketAddress remoteSocket, int messageID, Token token) { 47 | super(remoteSocket, token); 48 | this.messageID = messageID; 49 | } 50 | 51 | 52 | /** 53 | * Returns the message ID of the message that caused this events 54 | * @return the message ID of the message that caused this events 55 | */ 56 | public int getMessageID() { 57 | return messageID; 58 | } 59 | 60 | // /** 61 | // * Returns true if this events causes the related message exchange to stop and false 62 | // * otherwise 63 | // * @return true if this events causes the related message exchange to stop and false 64 | // * otherwise 65 | // */ 66 | // public abstract boolean stopsMessageExchange(); 67 | } 68 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/EmptyAckReceivedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | /** 26 | * Copyright (c) 2012, Oliver Kleine, Institute of Telematics, University of Luebeck 27 | * All rights reserved 28 | * 29 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 30 | * following conditions are met: 31 | * 32 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 33 | * disclaimer. 34 | * 35 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 36 | * following disclaimer in the documentation and/or other materials provided with the distribution. 37 | * 38 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 39 | * products derived from this software without specific prior written permission. 40 | * 41 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 42 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 44 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 45 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 46 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48 | */ 49 | package de.uzl.itm.ncoap.communication.events; 50 | 51 | import de.uzl.itm.ncoap.communication.dispatching.Token; 52 | import de.uzl.itm.ncoap.communication.reliability.outbound.ServerOutboundReliabilityHandler; 53 | 54 | import java.net.InetSocketAddress; 55 | 56 | /** 57 | * Instances are sent upstream (i.e. to the plugtest) by the {@link ServerOutboundReliabilityHandler} 58 | * when there was an empty acknowledgement received indicating that a recipient received a a confirmable 59 | * message. 60 | * 61 | * @author Oliver Kleine 62 | */ 63 | public class EmptyAckReceivedEvent extends AbstractMessageTransferEvent { 64 | 65 | /** 66 | * @param remoteSocket the remote endpoint that sent the empty ACK 67 | * @param messageID the message ID of the message whose reception was confirmed with this empty ACK 68 | * @param token the token of the confirmed message 69 | */ 70 | public EmptyAckReceivedEvent(InetSocketAddress remoteSocket, int messageID, Token token) { 71 | super(remoteSocket, messageID, token); 72 | } 73 | 74 | // @Override 75 | // public boolean stopsMessageExchange() { 76 | // return false; 77 | // } 78 | 79 | @Override 80 | public String toString() { 81 | return "EMPTY ACK (from " + this.getRemoteSocket() + " for message ID " + this.getMessageID() + " with token " 82 | + this.getToken() + ")"; 83 | } 84 | 85 | public interface Handler { 86 | public void handleEvent(EmptyAckReceivedEvent event); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/MessageIDAssignedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | 29 | import java.net.InetSocketAddress; 30 | 31 | /** 32 | * Instances of {@link MessageIDAssignedEvent} are sent upstream if a message ID was assigned to an outbound 33 | * message. 34 | * 35 | * @author Oliver Kleine 36 | */ 37 | public class MessageIDAssignedEvent extends AbstractMessageTransferEvent { 38 | 39 | /** 40 | * Creates a new instance of {@link MessageIDAssignedEvent} 41 | * 42 | * @param remoteSocket the socket of the recipient of the message that was assigned a message ID 43 | * @param messageID the message ID that was assigned 44 | * @param token the {@link Token} of the message that was assigned a message ID 45 | */ 46 | public MessageIDAssignedEvent(InetSocketAddress remoteSocket, int messageID, Token token) { 47 | super(remoteSocket, messageID, token); 48 | } 49 | 50 | 51 | 52 | @Override 53 | public String toString() { 54 | return "MESSAGE ID " + this.getMessageID() + " ASSIGNED (remote endpoint: " + this.getRemoteSocket() + 55 | ", token: " + this.getToken() + ")"; 56 | } 57 | 58 | public interface Handler { 59 | public void handleEvent(MessageIDAssignedEvent event); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/MessageIDReleasedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | 29 | import java.net.InetSocketAddress; 30 | 31 | /** 32 | * Created by olli on 27.04.16. 33 | */ 34 | public class MessageIDReleasedEvent extends AbstractMessageTransferEvent { 35 | 36 | /** 37 | * Creates a new instance of {@link AbstractMessageTransferEvent} 38 | * 39 | * @param remoteSocket the remote socket of the transfer that caused this event 40 | * @param messageID the message ID of the transfer that caused this event 41 | * @param token the {@link Token} of the of the transfer that caused this event 42 | */ 43 | public MessageIDReleasedEvent(InetSocketAddress remoteSocket, int messageID, Token token) { 44 | super(remoteSocket, messageID, token); 45 | } 46 | 47 | public interface Handler { 48 | void handleEvent(MessageIDReleasedEvent event); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/MessageRetransmittedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import java.net.InetSocketAddress; 28 | 29 | import de.uzl.itm.ncoap.communication.dispatching.Token; 30 | 31 | /** 32 | * Instances are sent upstream by the 33 | * {@link de.uzl.itm.ncoap.communication.reliability.outbound.ClientOutboundReliabilityHandler} whenever there was a 34 | * retransmission of a confirmable {@link de.uzl.itm.ncoap.message.CoapMessage}. 35 | * 36 | * @author Oliver Kleine 37 | */ 38 | public class MessageRetransmittedEvent extends AbstractMessageTransferEvent { 39 | 40 | /** 41 | * Creates a new instance of {@link MessageRetransmittedEvent} 42 | * 43 | * @param remoteSocket the desired recipient of the retransmitted message 44 | * @param messageID the message ID of the retransmitted message 45 | * @param token the {@link Token} of the retransmitted 46 | * message 47 | */ 48 | public MessageRetransmittedEvent(InetSocketAddress remoteSocket, int messageID, Token token) { 49 | super(remoteSocket, messageID, token); 50 | } 51 | 52 | // @Override 53 | // public boolean stopsMessageExchange() { 54 | // return false; 55 | // } 56 | 57 | @Override 58 | public String toString() { 59 | return "MESSAGE RETRANSMITTED (to " + this.getRemoteSocket() + " with message ID " + this.getMessageID() 60 | + " and token " + this.getToken() + ")"; 61 | } 62 | 63 | public interface Handler { 64 | public void handleEvent(MessageRetransmittedEvent event); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/MiscellaneousErrorEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | 29 | import java.net.InetSocketAddress; 30 | 31 | /** 32 | * Instances of {@link MiscellaneousErrorEvent} are sent upstream if some error occurred during message transfer. 33 | * 34 | * @author Oliver Kleine 35 | */ 36 | public class MiscellaneousErrorEvent extends AbstractMessageTransferEvent { 37 | 38 | private final String description; 39 | 40 | /** 41 | * Creates a new instance of {@link MiscellaneousErrorEvent} 42 | * 43 | * @param remoteSocket the remote socket of the transfer that caused this event 44 | * @param messageID the message ID of the message that caused this event 45 | * @param token the {@link Token} of the message that caused this event 46 | * @param description a human readable description of the error that caused this event 47 | */ 48 | public MiscellaneousErrorEvent(InetSocketAddress remoteSocket, int messageID, Token token, String description) { 49 | super(remoteSocket, messageID, token); 50 | this.description = description; 51 | } 52 | 53 | /** 54 | * Returns a human readable description of the error that caused this event 55 | * @return a human readable description of the error that caused this event 56 | */ 57 | public String getDescription() { 58 | return this.description; 59 | } 60 | 61 | // @Override 62 | // public boolean stopsMessageExchange() { 63 | // return true; 64 | // } 65 | 66 | @Override 67 | public String toString() { 68 | return "MISCELLANEOUS MESSAGE EXCHANGE ERROR (remote endpoint: " + this.getRemoteSocket() + 69 | ", message ID: " + this.getMessageID() + ", token: " + this.getToken() + ")"; 70 | } 71 | 72 | public interface Handler { 73 | public void handleEvent(MiscellaneousErrorEvent event); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/NoMessageIDAvailableEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | 29 | import java.net.InetSocketAddress; 30 | 31 | /** 32 | * Created by olli on 11.09.15. 33 | */ 34 | public class NoMessageIDAvailableEvent extends AbstractMessageExchangeEvent { 35 | 36 | public NoMessageIDAvailableEvent(InetSocketAddress remoteSocket, Token token) { 37 | super(remoteSocket, token); 38 | } 39 | 40 | public interface Handler { 41 | public void handleEvent(NoMessageIDAvailableEvent event); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/ResetReceivedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | /** 26 | * Copyright (c) 2012, Oliver Kleine, Institute of Telematics, University of Luebeck 27 | * All rights reserved 28 | * 29 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 30 | * following conditions are met: 31 | * 32 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 33 | * disclaimer. 34 | * 35 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 36 | * following disclaimer in the documentation and/or other materials provided with the distribution. 37 | * 38 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 39 | * products derived from this software without specific prior written permission. 40 | * 41 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 42 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 44 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 45 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 46 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48 | */ 49 | package de.uzl.itm.ncoap.communication.events; 50 | 51 | import de.uzl.itm.ncoap.communication.dispatching.Token; 52 | 53 | import java.net.InetSocketAddress; 54 | 55 | /** 56 | * Internal {@link de.uzl.itm.ncoap.communication.events.AbstractMessageTransferEvent} which is fired whenever an 57 | * a RST message was received. 58 | * 59 | * @author Oliver Kleine 60 | */ 61 | public class ResetReceivedEvent extends AbstractMessageTransferEvent { 62 | 63 | /** 64 | * Creates a new instance of {@link ResetReceivedEvent} 65 | * 66 | * @param remoteSocket the socket address of the remote endpoint that sent the RST 67 | * @param messageID the message ID that was contained in the received RST message 68 | * @param token the {@link Token} of the confirmed message 69 | */ 70 | public ResetReceivedEvent(InetSocketAddress remoteSocket, int messageID, Token token) { 71 | super(remoteSocket, messageID, token); 72 | } 73 | 74 | 75 | 76 | @Override 77 | public String toString() { 78 | return "EMPTY RST (from " + this.getRemoteSocket() + " for message ID " + this.getMessageID() + " with token " 79 | + this.getToken() + ")"; 80 | } 81 | 82 | public interface Handler { 83 | public void handleEvent(ResetReceivedEvent event); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/TransmissionTimeoutEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | 29 | import java.net.InetSocketAddress; 30 | 31 | /** 32 | * Instances of {@link TransmissionTimeoutEvent} are sent upstream if there was no reaction (ACK, RST or response, 33 | * respectively) received from the remote endpoint within 34 | * {@link de.uzl.itm.ncoap.communication.reliability.outbound.MessageIDFactory#EXCHANGE_LIFETIME} even though some 35 | * reaction was expected, e.g. during a confirmable message transfer. 36 | * 37 | * @author Oliver Kleine 38 | */ 39 | public class TransmissionTimeoutEvent extends AbstractMessageTransferEvent { 40 | 41 | /** 42 | * Creates a new instance of {@link TransmissionTimeoutEvent} 43 | * 44 | * @param remoteSocket the remote endpoint that did not confirm the reception of a reliable message 45 | * @param messageID the message ID of the message that caused this event 46 | * @param token the {@link Token} of the message that caused this event 47 | */ 48 | public TransmissionTimeoutEvent(InetSocketAddress remoteSocket, int messageID, Token token) { 49 | super(remoteSocket, messageID, token); 50 | } 51 | 52 | 53 | public interface Handler { 54 | public void handleEvent(TransmissionTimeoutEvent event); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/client/BlockwiseResponseTransferFailedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events.client; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | import de.uzl.itm.ncoap.communication.events.AbstractMessageExchangeEvent; 29 | 30 | import java.net.InetSocketAddress; 31 | 32 | /** 33 | * Created by olli on 12.04.16. 34 | */ 35 | public class BlockwiseResponseTransferFailedEvent extends AbstractMessageExchangeEvent { 36 | 37 | public BlockwiseResponseTransferFailedEvent(InetSocketAddress remoteSocket, Token token) { 38 | super(remoteSocket, token); 39 | } 40 | 41 | public interface Handler { 42 | void handleEvent(BlockwiseResponseTransferFailedEvent event); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/client/ContinueResponseReceivedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events.client; 26 | 27 | import de.uzl.itm.ncoap.communication.blockwise.BlockSize; 28 | import de.uzl.itm.ncoap.communication.dispatching.Token; 29 | import de.uzl.itm.ncoap.communication.events.AbstractMessageExchangeEvent; 30 | 31 | import java.net.InetSocketAddress; 32 | 33 | /** 34 | * Created by olli on 13.04.16. 35 | */ 36 | public class ContinueResponseReceivedEvent extends AbstractMessageExchangeEvent { 37 | 38 | private BlockSize block1Size; 39 | 40 | public ContinueResponseReceivedEvent(InetSocketAddress remoteSocket, Token token, long block1Szx) { 41 | super(remoteSocket, token); 42 | this.block1Size = BlockSize.getBlockSize(block1Szx); 43 | } 44 | 45 | public BlockSize getBlock1Size() { 46 | return this.block1Size; 47 | } 48 | 49 | public interface Handler { 50 | void handleEvent(ContinueResponseReceivedEvent event); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/client/RemoteServerSocketChangedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events.client; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | import de.uzl.itm.ncoap.communication.events.AbstractMessageExchangeEvent; 29 | 30 | 31 | import java.net.InetSocketAddress; 32 | 33 | /** 34 | * Created by olli on 31.08.15. 35 | */ 36 | public class RemoteServerSocketChangedEvent extends AbstractMessageExchangeEvent { 37 | 38 | private final InetSocketAddress previous; 39 | 40 | /** 41 | * Creates a new instance of {@link RemoteServerSocketChangedEvent} 42 | * 43 | * @param remoteSocket the new socket of the remote endpoint 44 | * @param previous the previous socket of the remote endpoint 45 | * @param token the {@link Token} of ongoing conversation 46 | */ 47 | public RemoteServerSocketChangedEvent(InetSocketAddress remoteSocket, InetSocketAddress previous, Token token) { 48 | 49 | super(remoteSocket, token); 50 | this.previous = previous; 51 | 52 | } 53 | 54 | /** 55 | * Returns the previous socket address 56 | * @return the previous socket address 57 | */ 58 | public InetSocketAddress getPreviousRemoteSocket() { 59 | return previous; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "REMOTE SERVER SOCKET CHANGED (old: " + this.getPreviousRemoteSocket() + ", new: " + this.getRemoteSocket() + 65 | ", Token: " + this.getToken() + ")"; 66 | } 67 | 68 | public interface Handler { 69 | public void handleEvent(RemoteServerSocketChangedEvent event); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/client/ResponseBlockReceivedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events.client; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | import de.uzl.itm.ncoap.communication.events.AbstractMessageExchangeEvent; 29 | 30 | import java.net.InetSocketAddress; 31 | 32 | /** 33 | * Created by olli on 01.03.16. 34 | */ 35 | public class ResponseBlockReceivedEvent extends AbstractMessageExchangeEvent { 36 | 37 | private long receivedLength; 38 | private long expectedLength; 39 | 40 | public ResponseBlockReceivedEvent(InetSocketAddress remoteSocket, Token token, long receivedLength, 41 | long expectedLength) { 42 | super(remoteSocket, token); 43 | this.receivedLength = receivedLength; 44 | this.expectedLength = expectedLength; 45 | } 46 | 47 | 48 | public long getReceivedLength() { 49 | return receivedLength; 50 | } 51 | 52 | 53 | public long getExpectedLength() { 54 | return expectedLength; 55 | } 56 | 57 | 58 | public interface Handler { 59 | void handleEvent(ResponseBlockReceivedEvent event); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/client/TokenReleasedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | /** 26 | * Copyright (c) 2012, Oliver Kleine, Institute of Telematics, University of Luebeck 27 | * All rights reserved 28 | * 29 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 30 | * following conditions are met: 31 | * 32 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 33 | * disclaimer. 34 | * 35 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 36 | * following disclaimer in the documentation and/or other materials provided with the distribution. 37 | * 38 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 39 | * products derived from this software without specific prior written permission. 40 | * 41 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 42 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 44 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 45 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 46 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48 | */ 49 | package de.uzl.itm.ncoap.communication.events.client; 50 | 51 | import de.uzl.itm.ncoap.communication.dispatching.Token; 52 | import de.uzl.itm.ncoap.communication.events.AbstractMessageExchangeEvent; 53 | 54 | import java.net.InetSocketAddress; 55 | 56 | /** 57 | * Created by olli on 03.09.15. 58 | */ 59 | public class TokenReleasedEvent extends AbstractMessageExchangeEvent { 60 | 61 | public TokenReleasedEvent(InetSocketAddress remoteSocket, Token token) { 62 | super(remoteSocket, token); 63 | } 64 | 65 | public interface Handler { 66 | public void handleEvent(TokenReleasedEvent event); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/server/ObserverAcceptedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events.server; 26 | 27 | import de.uzl.itm.ncoap.application.server.resource.ObservableWebresource; 28 | import de.uzl.itm.ncoap.communication.blockwise.BlockSize; 29 | import de.uzl.itm.ncoap.communication.dispatching.Token; 30 | import de.uzl.itm.ncoap.communication.events.AbstractMessageExchangeEvent; 31 | import de.uzl.itm.ncoap.message.options.ContentFormat; 32 | 33 | import java.net.InetSocketAddress; 34 | 35 | /** 36 | * Created by olli on 04.09.15. 37 | */ 38 | public class ObserverAcceptedEvent extends AbstractMessageExchangeEvent { 39 | 40 | private final ObservableWebresource webresource; 41 | private final long contentFormat; 42 | private final BlockSize block2Size; 43 | 44 | /** 45 | * Creates a new instance of {@link de.uzl.itm.ncoap.communication.events.server.ObserverAcceptedEvent} 46 | * 47 | * @param remoteSocket the socket of the newly accepted observer 48 | * @param token the {@link Token} to be used for this observation 49 | */ 50 | public ObserverAcceptedEvent(InetSocketAddress remoteSocket, Token token, 51 | ObservableWebresource webresource, long contentFormat, BlockSize block2Size) { 52 | 53 | super(remoteSocket, token); 54 | this.webresource = webresource; 55 | this.contentFormat = contentFormat; 56 | this.block2Size = block2Size; 57 | } 58 | 59 | /** 60 | * Returns the {@link de.uzl.itm.ncoap.application.server.resource.Webresource} instance that is observed 61 | * @return the {@link de.uzl.itm.ncoap.application.server.resource.Webresource} instance that is observed 62 | */ 63 | public ObservableWebresource getWebresource() { 64 | return webresource; 65 | } 66 | 67 | /** 68 | * Returns the number representing the content format to be used for this observation 69 | * 70 | * @see ContentFormat 71 | * 72 | * @return the number representing the content format to be used for this observation 73 | */ 74 | public long getContentFormat() { 75 | return contentFormat; 76 | } 77 | 78 | public BlockSize getBlock2Size() { 79 | return block2Size; 80 | } 81 | 82 | public interface Handler { 83 | public void handleEvent(ObserverAcceptedEvent event); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/events/server/RemoteClientSocketChangedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.events.server; 26 | 27 | import de.uzl.itm.ncoap.communication.dispatching.Token; 28 | import de.uzl.itm.ncoap.communication.events.AbstractMessageExchangeEvent; 29 | 30 | import java.net.InetSocketAddress; 31 | 32 | /** 33 | * Created by olli on 31.08.15. 34 | */ 35 | public class RemoteClientSocketChangedEvent extends AbstractMessageExchangeEvent { 36 | 37 | private final InetSocketAddress previous; 38 | 39 | /** 40 | * Creates a new instance of {@link de.uzl.itm.ncoap.communication.events.server.RemoteClientSocketChangedEvent} 41 | * 42 | * @param remoteSocket the remote endpoint that caused this event 43 | * @param token the {@link Token} of the exchange 44 | * that caused this event 45 | */ 46 | public RemoteClientSocketChangedEvent(InetSocketAddress remoteSocket, InetSocketAddress previous, Token token) { 47 | 48 | super(remoteSocket, token); 49 | this.previous = previous; 50 | 51 | } 52 | 53 | public InetSocketAddress getPreviousRemoteSocket() { 54 | return previous; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "REMOTE CLIENT SOCKET CHANGED (old: " + this.getPreviousRemoteSocket() + ", new: " + this.getRemoteSocket() + 60 | ", Token: " + this.getToken() + ")"; 61 | } 62 | 63 | public interface Handler { 64 | public void handleEvent(RemoteClientSocketChangedEvent event); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/identification/EndpointID.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.identification; 26 | 27 | import com.google.common.primitives.Bytes; 28 | import com.google.common.primitives.Longs; 29 | import com.google.common.primitives.UnsignedLongs; 30 | import de.uzl.itm.ncoap.communication.dispatching.Token; 31 | 32 | import java.util.Arrays; 33 | 34 | /** 35 | * Created by olli on 31.08.15. 36 | */ 37 | public class EndpointID implements Comparable{ 38 | 39 | public static final int MAX_LENGTH = 4; 40 | 41 | private byte[] bytes; 42 | 43 | /** 44 | * Creates a new {@link de.uzl.itm.ncoap.communication.identification.EndpointID} instance. 45 | * 46 | * @param bytes the byte array this {@link de.uzl.itm.ncoap.communication.identification.EndpointID} is 47 | * supposed to consist of 48 | * @throws IllegalArgumentException if the length of the given byte array is larger than 8 49 | */ 50 | public EndpointID(byte[] bytes) { 51 | if (bytes.length > MAX_LENGTH) { 52 | throw new IllegalArgumentException("Maximum endpoint ID length is 4 (but given length was " + 53 | bytes.length + ")"); 54 | } 55 | this.bytes = bytes; 56 | } 57 | 58 | /** 59 | * Returns the byte array this {@link Token} instance wraps 60 | * @return the byte array this {@link Token} instance wraps 61 | */ 62 | public byte[] getBytes() { 63 | return this.bytes; 64 | } 65 | 66 | /** 67 | * Returns a representation of the token in form of a HEX string or "" for tokens of length 0 68 | * @return a representation of the token in form of a HEX string or "" for tokens of length 0 69 | */ 70 | @Override 71 | public String toString() { 72 | String tmp = Token.bytesToHex(getBytes()); 73 | 74 | if (tmp.length() == 0) 75 | return ""; 76 | else 77 | return "0x" + tmp; 78 | } 79 | 80 | @Override 81 | public int hashCode() { 82 | return Arrays.hashCode(bytes); 83 | } 84 | 85 | @Override 86 | public boolean equals(Object object) { 87 | if (object == null || (!(object instanceof EndpointID))) 88 | return false; 89 | 90 | EndpointID other = (EndpointID) object; 91 | return Arrays.equals(this.getBytes(), other.getBytes()); 92 | } 93 | 94 | @Override 95 | public int compareTo(EndpointID other) { 96 | 97 | if (other.equals(this)) 98 | return 0; 99 | 100 | if (this.getBytes().length < other.getBytes().length) 101 | return -1; 102 | 103 | if (this.getBytes().length > other.getBytes().length) 104 | return 1; 105 | 106 | return UnsignedLongs.compare(Longs.fromByteArray(Bytes.concat(this.getBytes(), new byte[8])), 107 | Longs.fromByteArray(Bytes.concat(other.getBytes(), new byte[8]))); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/communication/observing/ResourceStatusAge.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.observing; 26 | 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | 30 | /** 31 | * Instances of {@link ResourceStatusAge} represent the data 32 | * which is necessary to make the age of received update notifications comparable. 33 | * 34 | * @author Oliver Kleine 35 | */ 36 | public class ResourceStatusAge { 37 | 38 | public static final long MODULUS = (long) Math.pow(2, 24); 39 | private static final long THRESHOLD = (long) Math.pow(2, 23); 40 | 41 | private static Logger log = LoggerFactory.getLogger(ResourceStatusAge.class.getName()); 42 | 43 | private long sequenceNo; 44 | private long timestamp; 45 | 46 | /** 47 | * Creates a new instance of {@link ResourceStatusAge} 48 | * @param sequenceNo the sequence number, i.e. {@link de.uzl.itm.ncoap.message.options.Option#OBSERVE} of an update 49 | * notification 50 | * @param timestamp the reception timestamp of an update notification 51 | */ 52 | public ResourceStatusAge(long sequenceNo, long timestamp) { 53 | this.sequenceNo = sequenceNo; 54 | this.timestamp = timestamp; 55 | } 56 | 57 | /** 58 | * Returns true if received is newer than latest or false 59 | * otherwise 60 | * 61 | * @param latest the {@link ResourceStatusAge} of the latest 62 | * update notification received so far 63 | * @param received the {@link ResourceStatusAge} of the newly 64 | * received update notification 65 | * 66 | * @return true if received is newer than latest or false 67 | * otherwise 68 | */ 69 | public static boolean isReceivedStatusNewer(ResourceStatusAge latest, ResourceStatusAge received) { 70 | if (latest.sequenceNo < received.sequenceNo && received.sequenceNo - latest.sequenceNo < THRESHOLD) { 71 | log.debug("Criterion 1 matches: received ({}) is newer than latest ({}).", received, latest); 72 | return true; 73 | } 74 | 75 | if (latest.sequenceNo > received.sequenceNo && latest.sequenceNo - received.sequenceNo > THRESHOLD) { 76 | log.debug("Criterion 2 matches: received ({}) is newer than latest ({}).", received, latest); 77 | return true; 78 | } 79 | 80 | if (received.timestamp > latest.timestamp + 128000L) { 81 | log.debug("Criterion 3 matches: received ({}) is newer than latest ({}).", received, latest); 82 | return true; 83 | } 84 | 85 | log.debug("No criterion matches: received({}) is older than latest ({}).", received, latest); 86 | return false; 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return "STATUS AGE (Sequence No: " + this.sequenceNo + ", Reception Timestamp: " + this.timestamp + ")"; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/message/MessageType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | /** 26 | * Copyright (c) 2012, Oliver Kleine, Institute of Telematics, University of Luebeck 27 | * All rights reserved. 28 | * 29 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 30 | * following conditions are met: 31 | * 32 | * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 33 | * disclaimer. 34 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 35 | * following disclaimer in the documentation and/or other materials provided with the distribution. 36 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 37 | * products derived from this software without specific prior written permission. 38 | * 39 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 40 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 41 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 42 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 43 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 44 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 46 | */ 47 | 48 | package de.uzl.itm.ncoap.message; 49 | 50 | import com.google.common.collect.ImmutableMap; 51 | 52 | import java.util.HashMap; 53 | 54 | 55 | /** 56 | * This class is a container of useful helpers to deal with message types. 57 | * 58 | * @author Oliver Kleine 59 | */ 60 | public abstract class MessageType { 61 | 62 | /** 63 | * Corresponds to Message EventType 0 64 | */ 65 | public static final int CON = 0; 66 | 67 | /** 68 | * Corresponds to Message EventType 1 69 | */ 70 | public static final int NON = 1; 71 | 72 | /** 73 | * Corresponds to Message EventType 2 74 | */ 75 | public static final int ACK = 2; 76 | 77 | /** 78 | * Corresponds to Message EventType 3 79 | */ 80 | public static final int RST = 3; 81 | 82 | private static final HashMap MESSAGE_TYPES = new HashMap<>(); 83 | static { 84 | MESSAGE_TYPES.putAll(ImmutableMap.builder() 85 | .put(CON, "CON (" + CON + ")") 86 | .put(NON, "NON (" + NON + ")") 87 | .put(ACK, "ACK (" + ACK + ")") 88 | .put(RST, "RST (" + RST + ")") 89 | .build() 90 | ); 91 | } 92 | 93 | public static String asString(int messageType) { 94 | String result = MESSAGE_TYPES.get(messageType); 95 | return result == null ? "UNKOWN (" + messageType + ")" : result; 96 | } 97 | 98 | 99 | /** 100 | * Returns true if and only if the given number corresponds to a valid message type. Otherwise it 101 | * returns false. 102 | * 103 | * @param number the number to check whether it corresponds to a valid message type. 104 | * 105 | * @return whether the given number corresponds to a valid message type. 106 | */ 107 | public static boolean isMessageType(int number) { 108 | return MESSAGE_TYPES.containsKey(number); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/message/options/ContentFormat.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.message.options; 26 | 27 | /** 28 | * Helper class to provide constants for several content formats. 29 | * 30 | * @author Oliver Kleine 31 | */ 32 | public abstract class ContentFormat { 33 | 34 | /** 35 | * Corresponds to number -1 36 | */ 37 | public static final long UNDEFINED = -1; 38 | 39 | /** 40 | * Corresponds to number 0 41 | */ 42 | public static final long TEXT_PLAIN_UTF8 = 0; 43 | 44 | /** 45 | * Corresponds to number 40 46 | */ 47 | public static final long APP_LINK_FORMAT = 40; 48 | 49 | /** 50 | * Corresponds to number 41 51 | */ 52 | public static final long APP_XML = 41; 53 | 54 | /** 55 | * Corresponds to number 42 56 | */ 57 | public static final long APP_OCTET_STREAM = 42; 58 | 59 | /** 60 | * Corresponds to number 47 61 | */ 62 | public static final long APP_EXI = 47; 63 | 64 | /** 65 | * Corresponds to number 50 66 | */ 67 | public static final long APP_JSON = 50; 68 | 69 | /** 70 | * Corresponds to number 201 (no standard but defined for very selfish reasons) 71 | */ 72 | public static final long APP_RDF_XML = 201; 73 | 74 | /** 75 | * Corresponds to number 202 (no standard but defined for very selfish reasons) 76 | */ 77 | public static final long APP_TURTLE = 202; 78 | 79 | /** 80 | * Corresponds to number 203 (no standard but defined for very selfish reasons) 81 | */ 82 | public static final long APP_N3 = 203; 83 | 84 | /** 85 | * Corresponds to number 205 (no standard but defined for very selfish reasons) 86 | */ 87 | public static final long APP_SHDT = 205; 88 | } 89 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/message/options/EmptyOptionValue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.message.options; 26 | 27 | import org.slf4j.Logger; 28 | import org.slf4j.LoggerFactory; 29 | 30 | import java.util.Arrays; 31 | 32 | /** 33 | * An empty option achieves it's goal just by being present in a message or not. However, as the internal 34 | * representation of options needs an instance of {@link OptionValue} empty options are represented using 35 | * {@link EmptyOptionValue}. 36 | * 37 | * @author Oliver Kleine 38 | */ 39 | public final class EmptyOptionValue extends OptionValue { 40 | 41 | private static Logger log = LoggerFactory.getLogger(EmptyOptionValue.class.getName()); 42 | 43 | /** 44 | * @param optionNumber the option number of the {@link EmptyOptionValue} to be created 45 | * 46 | * @throws java.lang.IllegalArgumentException if the given option number does not refer to an empty option 47 | */ 48 | public EmptyOptionValue(int optionNumber) throws IllegalArgumentException { 49 | super(optionNumber, new byte[0], false); 50 | 51 | log.debug("Empty Option (#{}) created.", optionNumber); 52 | } 53 | 54 | 55 | /** 56 | * Returns null 57 | * @return null 58 | */ 59 | @Override 60 | public Void getDecodedValue() { 61 | return null; 62 | } 63 | 64 | 65 | /** 66 | * Returns 0 67 | * @return 0 68 | */ 69 | @Override 70 | public int hashCode() { 71 | return 0; 72 | } 73 | 74 | /** 75 | * Checks if a given {@link Object} equals this {@link EmptyOptionValue} instance. A given {@link Object} equals 76 | * this {@link EmptyOptionValue} if and only if the {@link Object} is an instance of {@link EmptyOptionValue}. 77 | * 78 | * @param object the object to check for equality with this instance of {@link EmptyOptionValue} 79 | * 80 | * @return true if the given {@link Object} is an instance of {@link EmptyOptionValue} and 81 | * false otherwise. 82 | */ 83 | @Override 84 | public boolean equals(Object object) { 85 | if (!(object instanceof EmptyOptionValue)) 86 | return false; 87 | 88 | EmptyOptionValue other = (EmptyOptionValue) object; 89 | return Arrays.equals(this.getValue(), other.getValue()); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /ncoap-core/src/main/java/de/uzl/itm/ncoap/message/options/OpaqueOptionValue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.message.options; 26 | 27 | import java.util.Arrays; 28 | 29 | /** 30 | * This class contains all specific functionality for {@link OptionValue} instances of {@link OptionValue.Type#OPAQUE}. 31 | * 32 | * @author Oliver Kleine 33 | */ 34 | public class OpaqueOptionValue extends OptionValue { 35 | 36 | private final static char[] hexArray = "0123456789ABCDEF".toCharArray(); 37 | 38 | public OpaqueOptionValue(int optionNumber, byte[] value) throws IllegalArgumentException { 39 | super(optionNumber, value, false); 40 | } 41 | 42 | /** 43 | * For {@link OpaqueOptionValue}s the returned value is the same as {@link #getValue()}. 44 | * 45 | * @return the byte array containing the actual value of this option 46 | */ 47 | @Override 48 | public byte[] getDecodedValue() { 49 | return this.value; 50 | } 51 | 52 | 53 | @Override 54 | public int hashCode() { 55 | return Arrays.hashCode(getDecodedValue()); 56 | } 57 | 58 | 59 | @Override 60 | public boolean equals(Object object) { 61 | if (!(object instanceof OpaqueOptionValue)) 62 | return false; 63 | 64 | OpaqueOptionValue other = (OpaqueOptionValue) object; 65 | return Arrays.equals(this.getValue(), other.getValue()); 66 | } 67 | 68 | 69 | /** 70 | * Returns a {@link String} representation of this options value. Basically, this is a shortcut for 71 | * {@link #toHexString(byte[])} with {@link #getValue()} as given parameter. 72 | * 73 | * @return a {@link String} representation of this options value 74 | */ 75 | @Override 76 | public String toString() { 77 | return toHexString(this.value); 78 | } 79 | 80 | /** 81 | * Returns a {@link String} representation of this options value, i.e. the string if the 82 | * length of the byte array returned by {@link #getValue()} is 0 or a hex-string representing the bytes contained 83 | * in that array. 84 | * 85 | * @param bytes the byte array to get the hex-string representation of 86 | * 87 | * @return a {@link String} representation of this options value 88 | */ 89 | public static String toHexString(byte[] bytes) { 90 | if (bytes.length == 0) 91 | return ""; 92 | else 93 | return "0x" + bytesToHex(bytes); 94 | // return "0x" + new BigInteger(1, bytes).toString(16).toUpperCase(); 95 | } 96 | 97 | private static String bytesToHex(byte[] bytes) { 98 | char[] hexChars = new char[bytes.length * 2]; 99 | for ( int j = 0; j < bytes.length; j++ ) { 100 | int v = bytes[j] & 0xFF; 101 | hexChars[j * 2] = hexArray[v >>> 4]; 102 | hexChars[j * 2 + 1] = hexArray[v & 0x0F]; 103 | } 104 | return new String(hexChars); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/AbstractCoapTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap; 26 | 27 | import org.apache.log4j.*; 28 | import org.junit.BeforeClass; 29 | 30 | /** 31 | * Abstract class to be extended by all nCoAP tests to get proper logging 32 | * 33 | * @author Oliver Kleine 34 | */ 35 | public abstract class AbstractCoapTest { 36 | 37 | private static boolean isLoggingConfigured = false; 38 | 39 | protected AbstractCoapTest() { 40 | try{ 41 | if (!isLoggingConfigured) { 42 | initializeLogging(); 43 | setupLogging(); 44 | isLoggingConfigured = true; 45 | } 46 | } 47 | catch (Exception e) { 48 | throw new RuntimeException("Could not create test scenario. ", e); 49 | } 50 | } 51 | 52 | 53 | protected static void initializeLogging() { 54 | 55 | if (!isLoggingConfigured) { 56 | Logger.getRootLogger().removeAllAppenders(); 57 | 58 | //asynchronous appender 59 | AsyncAppender asyncAppender = new AsyncAppender(); 60 | asyncAppender.setBufferSize(100000); 61 | 62 | //console-appender 63 | String pattern = "%-23d{yyyy-MM-dd HH:mm:ss,SSS} | %-32.32t | %-35.35c{1} | %-5p | %m%n"; 64 | PatternLayout patternLayout = new PatternLayout(pattern); 65 | asyncAppender.addAppender(new ConsoleAppender(patternLayout)); 66 | 67 | //add asynchronous appender to root-logger 68 | Logger.getRootLogger().addAppender(asyncAppender); 69 | 70 | //Define default log-level 71 | Logger.getRootLogger().setLevel(Level.ERROR); 72 | } 73 | } 74 | 75 | /** 76 | * If the extending test class is supposed to use any other logging level then {@link Level#ERROR} for everything 77 | * then this can be configured by overriding this method 78 | * 79 | * @throws Exception 80 | */ 81 | public abstract void setupLogging() throws Exception; 82 | } 83 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/AbstractCoapCommunicationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication; 26 | 27 | import de.uzl.itm.ncoap.AbstractCoapTest; 28 | 29 | import org.apache.log4j.Level; 30 | import org.apache.log4j.Logger; 31 | import org.junit.AfterClass; 32 | import org.junit.Ignore; 33 | 34 | 35 | /** 36 | * Abstract class to be extended by all tests classes to test communication functionality, i.e. all 37 | * tests using at least one of 38 | * {@link de.uzl.itm.ncoap.application.client.CoapClient}, or 39 | * {@link de.uzl.itm.ncoap.application.server.CoapServer}. 40 | * 41 | * @author Oliver Kleine 42 | */ 43 | @Ignore 44 | public abstract class AbstractCoapCommunicationTest extends AbstractCoapTest{ 45 | 46 | private static Logger log = Logger.getLogger(AbstractCoapCommunicationTest.class.getName()); 47 | 48 | private static boolean areComponentsSetup = false; 49 | private static boolean isTestScenarioCreated = false; 50 | private static boolean areComponentsShutdown = false; 51 | 52 | 53 | /** 54 | * This method is to create all necessary instances of 55 | * {@link de.uzl.itm.ncoap.application.client.CoapClient}, 56 | * {@link de.uzl.itm.ncoap.application.server.CoapServer} and 57 | * {@link de.uzl.itm.ncoap.endpoints.DummyEndpoint}. 58 | * 59 | * Additionally all necessary instances of webservices are supposed to be created and registered at the 60 | * {@link de.uzl.itm.ncoap.application.server.CoapServer} instance(s) 61 | * 62 | * @throws Exception 63 | */ 64 | public abstract void setupComponents() throws Exception; 65 | 66 | /** 67 | * This method is supposed to define all communication to create the scenario the test method do their testing on 68 | */ 69 | public abstract void createTestScenario() throws Exception; 70 | 71 | /** 72 | * This method is to shutdown all existing instances of 73 | * {@link de.uzl.itm.ncoap.application.client.CoapClient}, 74 | * {@link de.uzl.itm.ncoap.application.server.CoapServer}, and 75 | * {@link de.uzl.itm.ncoap.endpoints.DummyEndpoint}. 76 | * 77 | * @throws Exception 78 | */ 79 | public abstract void shutdownComponents() throws Exception; 80 | 81 | 82 | public AbstractCoapCommunicationTest() { 83 | 84 | Logger.getLogger("AbstractCoapCommunicationTest") 85 | .setLevel(Level.INFO); 86 | 87 | try { 88 | if (!areComponentsSetup) { 89 | log.info("START TEST (" + this.getClass().getSimpleName() + ")"); 90 | setupComponents(); 91 | areComponentsSetup = true; 92 | } 93 | 94 | if (!isTestScenarioCreated) { 95 | createTestScenario(); 96 | isTestScenarioCreated = true; 97 | log.info("SCENARIO CREATED (" + this.getClass().getSimpleName() + ")"); 98 | } 99 | 100 | if (!areComponentsShutdown) { 101 | shutdownComponents(); 102 | areComponentsShutdown = true; 103 | log.info("COMPONENTS SHUT DOWN (" + this.getClass().getSimpleName() + ")"); 104 | } 105 | 106 | } 107 | catch (Exception e) { 108 | throw new RuntimeException("Could not create test scenario. ", e); 109 | } 110 | } 111 | 112 | @AfterClass 113 | public static void reset() { 114 | areComponentsSetup = false; 115 | areComponentsShutdown = false; 116 | isTestScenarioCreated = false; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/ClientSendsNONRequestThatTimesOut.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication; 26 | 27 | import de.uzl.itm.ncoap.application.client.CoapClient; 28 | import de.uzl.itm.ncoap.communication.dispatching.client.ResponseDispatcher; 29 | import de.uzl.itm.ncoap.communication.reliability.outbound.ClientOutboundReliabilityHandler; 30 | import de.uzl.itm.ncoap.communication.reliability.outbound.MessageIDFactory; 31 | import de.uzl.itm.ncoap.endpoints.client.TestCallback; 32 | import de.uzl.itm.ncoap.message.*; 33 | import org.apache.log4j.Level; 34 | import org.apache.log4j.Logger; 35 | import org.junit.Ignore; 36 | import org.junit.Test; 37 | 38 | import java.net.InetSocketAddress; 39 | import java.net.URI; 40 | 41 | import static junit.framework.Assert.assertEquals; 42 | 43 | /** 44 | * Tests to verify the server functionality related to piggy-backed responses. 45 | * 46 | * @author Stefan Hueske, Oliver Kleine 47 | */ 48 | public class ClientSendsNONRequestThatTimesOut extends AbstractCoapCommunicationTest { 49 | 50 | private static Logger LOG = Logger.getLogger(ClientSendsNONRequestThatTimesOut.class.getName()); 51 | 52 | private static CoapClient client; 53 | private static TestCallback callback; 54 | private static CoapRequest request; 55 | 56 | 57 | @Override 58 | public void setupLogging() throws Exception { 59 | Logger.getLogger(ClientSendsNONRequestThatTimesOut.class.getName()).setLevel(Level.DEBUG); 60 | Logger.getLogger(ClientOutboundReliabilityHandler.class.getName()).setLevel(Level.DEBUG); 61 | Logger.getLogger(MessageIDFactory.class.getName()).setLevel(Level.DEBUG); 62 | Logger.getLogger(ResponseDispatcher.class.getName()).setLevel(Level.DEBUG); 63 | Logger.getLogger(TestCallback.class.getName()).setLevel(Level.DEBUG); 64 | 65 | } 66 | 67 | @Override 68 | public void setupComponents() throws Exception { 69 | client = new CoapClient(); 70 | callback = new TestCallback(); 71 | URI targetUri = new URI("coap://localhost/"); 72 | request = new CoapRequest(MessageType.NON, MessageCode.GET, targetUri); 73 | } 74 | 75 | @Override 76 | public void shutdownComponents() throws Exception { 77 | client.shutdown(); 78 | } 79 | 80 | @Override 81 | public void createTestScenario() throws Exception { 82 | 83 | // Client 84 | // | 85 | // (1) |--------GET (NON)-----------> there is no server ... 86 | // | 87 | // | 88 | // timeout (after 247 seconds) 89 | 90 | 91 | LOG.warn("*****THIS TEST TAKES MORE THAN 4 MINUTES******"); 92 | 93 | //send request to testServer 94 | client.sendCoapRequest(request, new InetSocketAddress("localhost", 5683), callback); 95 | 96 | //wait some time for response from server 97 | Thread.sleep(250000); 98 | } 99 | 100 | @Test 101 | public void testClientReceivedTimeout() { 102 | assertEquals("Client received wrong number of timeouts.", 1, callback.getTransmissionTimeouts().size()); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/ClientSendsPingToAnotherClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication; 26 | 27 | import de.uzl.itm.ncoap.application.client.CoapClient; 28 | import de.uzl.itm.ncoap.endpoints.client.TestCallback; 29 | import org.apache.log4j.Level; 30 | import org.apache.log4j.Logger; 31 | import org.junit.Test; 32 | 33 | import java.net.InetSocketAddress; 34 | 35 | import static org.junit.Assert.assertFalse; 36 | 37 | /** 38 | * Created by olli on 08.03.14. 39 | */ 40 | public class ClientSendsPingToAnotherClient extends AbstractCoapCommunicationTest{ 41 | 42 | private static CoapClient client1; 43 | private static CoapClient client2; 44 | private static TestCallback callback; 45 | 46 | 47 | @Override 48 | public void setupComponents() throws Exception { 49 | client1 = new CoapClient("CoAP Client #1"); 50 | client2 = new CoapClient("CoAP Client #2"); 51 | callback = new TestCallback(); 52 | } 53 | 54 | 55 | @Override 56 | public void createTestScenario() throws Exception { 57 | InetSocketAddress client2Address = new InetSocketAddress("127.0.0.1", client2.getPort()); 58 | client1.sendCoapPing(client2Address, callback); 59 | Thread.sleep(1000); 60 | } 61 | 62 | 63 | @Override 64 | public void shutdownComponents() throws Exception { 65 | client1.shutdown(); 66 | client2.shutdown(); 67 | } 68 | 69 | 70 | @Override 71 | public void setupLogging() throws Exception { 72 | Logger.getLogger("de.uzl.itm.ncoap.communication.reliability.inbound.ClientInboundReliabilityHandler") 73 | .setLevel(Level.DEBUG); 74 | 75 | Logger.getLogger("de.uzl.itm.ncoap.communication.AbstractCoapChannelHandler") 76 | .setLevel(Level.DEBUG); 77 | 78 | Logger.getRootLogger().setLevel(Level.ERROR); 79 | } 80 | 81 | 82 | @Test 83 | public void testResetReceived() { 84 | assertFalse("No RST (Pong) received.", callback.getEmptyRSTs().isEmpty()); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/ClientSendsPingToServer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication; 26 | 27 | import de.uzl.itm.ncoap.application.client.CoapClient; 28 | import de.uzl.itm.ncoap.application.server.CoapServer; 29 | import de.uzl.itm.ncoap.communication.reliability.inbound.ServerInboundReliabilityHandler; 30 | import de.uzl.itm.ncoap.endpoints.client.TestCallback; 31 | import org.apache.log4j.Level; 32 | import org.apache.log4j.Logger; 33 | import org.junit.Test; 34 | 35 | import java.net.InetSocketAddress; 36 | 37 | import static org.junit.Assert.assertFalse; 38 | 39 | /** 40 | * Created by olli on 08.03.14. 41 | */ 42 | public class ClientSendsPingToServer extends AbstractCoapCommunicationTest{ 43 | 44 | private static CoapClient coapClient; 45 | private static CoapServer coapServer; 46 | private static TestCallback callback; 47 | 48 | 49 | @Override 50 | public void setupComponents() throws Exception { 51 | coapClient = new CoapClient(); 52 | callback = new TestCallback(); 53 | coapServer = new CoapServer(); 54 | } 55 | 56 | 57 | @Override 58 | public void createTestScenario() throws Exception { 59 | InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.1", coapServer.getPort()); 60 | coapClient.sendCoapPing(serverAddress, callback); 61 | 62 | Thread.sleep(1000); 63 | } 64 | 65 | 66 | @Override 67 | public void shutdownComponents() throws Exception { 68 | coapServer.shutdown().get(); 69 | coapClient.shutdown(); 70 | } 71 | 72 | 73 | @Override 74 | public void setupLogging() throws Exception { 75 | Logger.getLogger(ServerInboundReliabilityHandler.class.getName()).setLevel(Level.DEBUG); 76 | Logger.getLogger(AbstractCoapChannelHandler.class.getName()).setLevel(Level.DEBUG); 77 | Logger.getLogger(TestCallback.class.getName()).setLevel(Level.DEBUG); 78 | 79 | Logger.getRootLogger().setLevel(Level.WARN); 80 | } 81 | 82 | 83 | @Test 84 | public void testResetReceived() { 85 | assertFalse("No RST (Pong) received.", callback.getEmptyRSTs().isEmpty()); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/blockwise/ClientSendsRequestWithBlock1AndBlock2ToETHZ.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.blockwise; 26 | 27 | import de.uzl.itm.ncoap.application.client.CoapClient; 28 | import de.uzl.itm.ncoap.communication.AbstractCoapCommunicationTest; 29 | import de.uzl.itm.ncoap.communication.blockwise.client.ClientBlock1Handler; 30 | import de.uzl.itm.ncoap.communication.blockwise.client.ClientBlock2Handler; 31 | import de.uzl.itm.ncoap.endpoints.client.TestCallback; 32 | import de.uzl.itm.ncoap.message.CoapMessage; 33 | import de.uzl.itm.ncoap.message.CoapRequest; 34 | import de.uzl.itm.ncoap.message.MessageCode; 35 | import de.uzl.itm.ncoap.message.MessageType; 36 | import de.uzl.itm.ncoap.message.options.ContentFormat; 37 | import org.apache.log4j.Level; 38 | import org.apache.log4j.Logger; 39 | import org.junit.Test; 40 | 41 | import java.net.InetSocketAddress; 42 | import java.net.URI; 43 | 44 | import static org.junit.Assert.assertEquals; 45 | 46 | 47 | /** 48 | * Created by olli on 07.04.16. 49 | */ 50 | public class ClientSendsRequestWithBlock1AndBlock2ToETHZ extends AbstractCoapCommunicationTest { 51 | 52 | private static CoapClient coapClient; 53 | private static CoapRequest coapRequest; 54 | private static TestCallback clientCallback; 55 | 56 | @Override 57 | public void setupComponents() throws Exception { 58 | coapClient = new CoapClient(); 59 | URI targetURI = new URI("coap://vs0.inf.ethz.ch:5683/large-post"); 60 | coapRequest = new CoapRequest(MessageType.CON, MessageCode.POST, targetURI); 61 | coapRequest.setPreferredBlock1Size(BlockSize.SIZE_16); 62 | coapRequest.setPreferredBlock2Size(BlockSize.SIZE_16); 63 | byte[] payload = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(CoapMessage.CHARSET); 64 | coapRequest.setContent(payload, ContentFormat.TEXT_PLAIN_UTF8); 65 | clientCallback = new TestCallback(); 66 | } 67 | 68 | @Override 69 | public void createTestScenario() throws Exception { 70 | coapClient.sendCoapRequest(coapRequest, new InetSocketAddress("vs0.inf.ethz.ch", 5683), clientCallback); 71 | Thread.sleep(5000); 72 | } 73 | 74 | @Override 75 | public void shutdownComponents() throws Exception { 76 | coapClient.shutdown(); 77 | } 78 | 79 | @Override 80 | public void setupLogging() throws Exception { 81 | Logger.getLogger(ClientBlock1Handler.class.getName()).setLevel(Level.DEBUG); 82 | Logger.getLogger(ClientBlock2Handler.class.getName()).setLevel(Level.DEBUG); 83 | Logger.getLogger(TestCallback.class.getName()).setLevel(Level.DEBUG); 84 | } 85 | 86 | @Test 87 | public void testReceiverReceivedTwoMessages() { 88 | String message = "Client did not receive 1 response"; 89 | assertEquals(message, 1, clientCallback.getCoapResponses().size()); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/blockwise/ClientSendsRequestWithBlock1ToETHZ.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.blockwise; 26 | 27 | import de.uzl.itm.ncoap.application.client.CoapClient; 28 | import de.uzl.itm.ncoap.communication.AbstractCoapCommunicationTest; 29 | import de.uzl.itm.ncoap.communication.blockwise.client.ClientBlock1Handler; 30 | import de.uzl.itm.ncoap.endpoints.client.TestCallback; 31 | import de.uzl.itm.ncoap.message.CoapMessage; 32 | import de.uzl.itm.ncoap.message.CoapRequest; 33 | import de.uzl.itm.ncoap.message.MessageCode; 34 | import de.uzl.itm.ncoap.message.MessageType; 35 | import de.uzl.itm.ncoap.message.options.ContentFormat; 36 | import org.apache.log4j.Level; 37 | import org.apache.log4j.Logger; 38 | import org.junit.Test; 39 | 40 | 41 | import java.net.InetSocketAddress; 42 | import java.net.URI; 43 | 44 | import static org.junit.Assert.assertEquals; 45 | 46 | 47 | /** 48 | * Created by olli on 07.04.16. 49 | */ 50 | public class ClientSendsRequestWithBlock1ToETHZ extends AbstractCoapCommunicationTest { 51 | 52 | private static CoapClient coapClient; 53 | private static CoapRequest coapRequest; 54 | private static TestCallback clientCallback; 55 | 56 | @Override 57 | public void setupComponents() throws Exception { 58 | coapClient = new CoapClient("Coap Client (BLOCK 1)"); 59 | URI targetURI = new URI("coap://vs0.inf.ethz.ch:5683/large-post"); 60 | coapRequest = new CoapRequest(MessageType.CON, MessageCode.POST, targetURI); 61 | coapRequest.setPreferredBlock1Size(BlockSize.SIZE_16); 62 | byte[] payload = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(CoapMessage.CHARSET); 63 | coapRequest.setContent(payload, ContentFormat.TEXT_PLAIN_UTF8); 64 | clientCallback = new TestCallback(); 65 | } 66 | 67 | @Override 68 | public void createTestScenario() throws Exception { 69 | coapClient.sendCoapRequest(coapRequest, new InetSocketAddress("vs0.inf.ethz.ch", 5683), clientCallback); 70 | Thread.sleep(5000); 71 | } 72 | 73 | @Override 74 | public void shutdownComponents() throws Exception { 75 | coapClient.shutdown(); 76 | } 77 | 78 | @Override 79 | public void setupLogging() throws Exception { 80 | Logger.getLogger(ClientBlock1Handler.class.getName()).setLevel(Level.DEBUG); 81 | } 82 | 83 | @Test 84 | public void testReceiverReceivedTwoMessages() { 85 | String message = "Client did not receive 1 response"; 86 | assertEquals(message, 1, clientCallback.getCoapResponses().size()); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/codec/AllowDefaultOptionValues.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.codec; 26 | 27 | import de.uzl.itm.ncoap.AbstractCoapTest; 28 | import de.uzl.itm.ncoap.message.CoapMessage; 29 | import de.uzl.itm.ncoap.message.CoapRequest; 30 | import de.uzl.itm.ncoap.message.MessageCode; 31 | import de.uzl.itm.ncoap.message.MessageType; 32 | import de.uzl.itm.ncoap.message.options.Option; 33 | import de.uzl.itm.ncoap.message.options.OptionValue; 34 | import de.uzl.itm.ncoap.message.options.StringOptionValue; 35 | 36 | import de.uzl.itm.ncoap.message.options.UintOptionValue; 37 | import org.junit.Test; 38 | import static org.junit.Assert.*; 39 | 40 | import java.net.URI; 41 | 42 | /** 43 | * Created by olli on 02.08.16. 44 | */ 45 | 46 | public class AllowDefaultOptionValues extends AbstractCoapTest { 47 | 48 | private static String HOST = "10.20.30.40"; 49 | private static byte[] HOST_ENCODED = HOST.getBytes(CoapMessage.CHARSET); 50 | 51 | @Override 52 | public void setupLogging() throws Exception { 53 | 54 | } 55 | 56 | @Test 57 | public void setIpAddressAsHostOption() throws Exception{ 58 | URI webserviceURI = new URI("coap://" + HOST + ":" + OptionValue.URI_PORT_DEFAULT + "/registry"); 59 | CoapRequest coapRequest = new CoapRequest(MessageType.CON, MessageCode.POST, webserviceURI, false); 60 | coapRequest.addOption(Option.URI_HOST, new StringOptionValue(Option.URI_HOST, HOST_ENCODED, true)); 61 | 62 | assertEquals("Unexpected value for HOST option: ", HOST, coapRequest.getUriHost()); 63 | } 64 | 65 | @Test (expected = IllegalArgumentException.class) 66 | public void setIpAddressAsHostOption2() throws Exception{ 67 | URI webserviceURI = new URI("coap://" + HOST + ":" + OptionValue.URI_PORT_DEFAULT + "/registry"); 68 | CoapRequest coapRequest = new CoapRequest(MessageType.CON, MessageCode.POST, webserviceURI, false); 69 | coapRequest.addOption(Option.URI_HOST, new StringOptionValue(Option.URI_HOST, HOST_ENCODED)); 70 | 71 | // this should not be executed as the previous statement is supposed to throw an exception 72 | assertTrue(false); 73 | } 74 | 75 | @Test 76 | public void setDefaultPortAsPortOption() throws Exception{ 77 | URI webserviceURI = new URI("coap://" + HOST + ":" + OptionValue.URI_PORT_DEFAULT + "/registry"); 78 | CoapRequest coapRequest = new CoapRequest(MessageType.CON, MessageCode.POST, webserviceURI, false); 79 | UintOptionValue value = new UintOptionValue(Option.URI_PORT, OptionValue.ENCODED_URI_PORT_DEFAULT, true); 80 | coapRequest.addOption(Option.URI_PORT, value); 81 | 82 | assertEquals("Unexpected value for PORT option: ", OptionValue.URI_PORT_DEFAULT, coapRequest.getUriPort()); 83 | } 84 | 85 | @Test (expected = IllegalArgumentException.class) 86 | public void setDefaultPortAsPortOption2() throws Exception{ 87 | URI webserviceURI = new URI("coap://" + HOST + ":" + OptionValue.URI_PORT_DEFAULT + "/registry"); 88 | CoapRequest coapRequest = new CoapRequest(MessageType.CON, MessageCode.POST, webserviceURI, false); 89 | coapRequest.addOption(Option.URI_PORT, new UintOptionValue(Option.URI_PORT, 90 | OptionValue.ENCODED_URI_PORT_DEFAULT, false)); 91 | 92 | // this should not be executed as the previous statement is supposed to throw an exception 93 | assertTrue(false); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/codec/DecodeEncodedMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.codec; 26 | 27 | import com.google.common.collect.Lists; 28 | import de.uzl.itm.ncoap.AbstractCoapTest; 29 | import de.uzl.itm.ncoap.communication.dispatching.Token; 30 | import de.uzl.itm.ncoap.communication.codec.tools.CoapTestDecoder; 31 | import de.uzl.itm.ncoap.communication.codec.tools.CoapTestEncoder; 32 | import de.uzl.itm.ncoap.message.MessageCode; 33 | import de.uzl.itm.ncoap.message.options.ContentFormat; 34 | import de.uzl.itm.ncoap.message.CoapMessage; 35 | import de.uzl.itm.ncoap.message.CoapRequest; 36 | import de.uzl.itm.ncoap.message.MessageType; 37 | import org.apache.log4j.Level; 38 | import org.apache.log4j.Logger; 39 | import org.jboss.netty.buffer.ChannelBuffer; 40 | import org.junit.Before; 41 | import org.junit.Test; 42 | import org.junit.runner.RunWith; 43 | import org.junit.runners.Parameterized; 44 | 45 | import java.net.URI; 46 | import java.util.Collection; 47 | 48 | import static org.junit.Assert.assertEquals; 49 | 50 | /** 51 | * Created with IntelliJ IDEA. 52 | * User: olli 53 | * Date: 12.11.13 54 | * Time: 23:53 55 | * To change this template use File | Settings | File Templates. 56 | */ 57 | 58 | @RunWith(Parameterized.class) 59 | public class DecodeEncodedMessageTest extends AbstractCoapTest { 60 | 61 | @Parameterized.Parameters(name = "Test {index}: {0}") 62 | public static Collection data() throws Exception { 63 | 64 | initializeLogging(); 65 | 66 | return Lists.newArrayList( 67 | //[0] TKL is 1, but 0 remaining bytes after header 68 | new Object[]{new CoapRequest(MessageType.CON, MessageCode.GET, 69 | new URI("coap://coap.me:5683/test"))}, 70 | 71 | //[1] TKL is 8, but only 6 remaining bytes after header 72 | new Object[]{new CoapRequest(MessageType.NON, MessageCode.POST, 73 | new URI("coap://coap.me:5683/p1/p2/p3/p4/p5/p6/p7"))}, 74 | 75 | new Object[]{new CoapRequest(MessageType.NON, MessageCode.GET, 76 | new URI("coap://example.org/"), true)} 77 | ); 78 | } 79 | 80 | private CoapMessage coapMessage; 81 | private ChannelBuffer encodedMessage; 82 | 83 | public DecodeEncodedMessageTest(CoapMessage coapMessage) throws Exception { 84 | coapMessage.setMessageID(1234); 85 | coapMessage.setToken(new Token(new byte[]{1,2,3,4})); 86 | 87 | if (coapMessage.getMessageCode() == MessageCode.POST) { 88 | String payload = "Some arbitrary payload"; 89 | coapMessage.setContent(payload.getBytes(CoapMessage.CHARSET), ContentFormat.TEXT_PLAIN_UTF8); 90 | } 91 | 92 | this.coapMessage = coapMessage; 93 | } 94 | 95 | @Override 96 | public void setupLogging() throws Exception { 97 | Logger.getLogger("de.uzl.itm.ncoap.communication.codec").setLevel(Level.DEBUG); 98 | Logger.getRootLogger().setLevel(Level.ERROR); 99 | } 100 | 101 | @Before 102 | public void encodeMessage() throws Exception { 103 | encodedMessage = new CoapTestEncoder().encode(coapMessage); 104 | } 105 | 106 | @Test 107 | public void testDecoding() throws Exception { 108 | CoapMessage decodedMessage = (CoapMessage) new CoapTestDecoder().decode(encodedMessage); 109 | assertEquals(coapMessage, decodedMessage); 110 | } 111 | 112 | 113 | } 114 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/codec/MessageDecodingWithInvalidMessages.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.codec; 26 | 27 | import com.google.common.collect.Lists; 28 | import de.uzl.itm.ncoap.AbstractCoapTest; 29 | import de.uzl.itm.ncoap.communication.codec.tools.CoapTestDecoder; 30 | import org.apache.log4j.Level; 31 | import org.apache.log4j.Logger; 32 | import org.jboss.netty.buffer.ChannelBuffer; 33 | import org.jboss.netty.buffer.ChannelBuffers; 34 | import org.junit.Rule; 35 | import org.junit.Test; 36 | import org.junit.rules.ExpectedException; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Parameterized; 39 | 40 | import java.util.Collection; 41 | 42 | 43 | 44 | /** 45 | * Created with IntelliJ IDEA. 46 | * User: olli 47 | * Date: 09.11.13 48 | * Time: 16:28 49 | * To change this template use File | Settings | File Templates. 50 | */ 51 | @RunWith(Parameterized.class) 52 | public class MessageDecodingWithInvalidMessages extends AbstractCoapTest{ 53 | 54 | @Parameterized.Parameters 55 | public static Collection data() throws Exception { 56 | return Lists.newArrayList( 57 | //[0] TKL is 1, but 0 remaining bytes after header 58 | new Object[]{new byte[]{(byte) (0b01000001 & 0b11111111), 0, 0, 0}, 59 | HeaderDecodingException.class, "TKL value is 1 but only 0 bytes left!"}, 60 | 61 | //[1] TKL is 8, but only 6 remaining bytes after header 62 | new Object[]{new byte[]{(byte) (0b01001000 & 0b11111111), 1, 1, 0, 1, 1, 1, 1, 1, 1}, 63 | HeaderDecodingException.class, "TKL value is 8 but only 6 bytes left!"} 64 | ); 65 | } 66 | 67 | @Rule 68 | public ExpectedException exception = ExpectedException.none(); 69 | 70 | 71 | @Override 72 | public void setupLogging() throws Exception { 73 | Logger.getLogger("de.uzl.itm.ncoap.communication.codec").setLevel(Level.DEBUG); 74 | Logger.getRootLogger().setLevel(Level.ERROR); 75 | } 76 | 77 | private ChannelBuffer encodedMessageBuffer; 78 | 79 | 80 | public MessageDecodingWithInvalidMessages(byte[] encodedMessage, Class expectedExceptionClass, 81 | String expectedMessage) { 82 | this.encodedMessageBuffer = ChannelBuffers.wrappedBuffer(encodedMessage); 83 | exception.expect(expectedExceptionClass); 84 | exception.expectMessage(expectedMessage); 85 | } 86 | 87 | 88 | @Test 89 | public void testDecoding() throws Exception { 90 | new CoapTestDecoder().decode(encodedMessageBuffer); 91 | } 92 | } 93 | 94 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/codec/MessageDecodingWithValidMessages.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.codec; 26 | 27 | import com.google.common.collect.Lists; 28 | import de.uzl.itm.ncoap.AbstractCoapTest; 29 | import de.uzl.itm.ncoap.communication.codec.tools.CoapTestDecoder; 30 | import de.uzl.itm.ncoap.message.CoapMessage; 31 | import org.apache.log4j.Level; 32 | import org.apache.log4j.Logger; 33 | import org.jboss.netty.buffer.ChannelBuffer; 34 | import org.jboss.netty.buffer.ChannelBuffers; 35 | import org.junit.Before; 36 | import org.junit.Test; 37 | import org.junit.runner.RunWith; 38 | import org.junit.runners.Parameterized; 39 | 40 | import java.util.Collection; 41 | 42 | import static org.junit.Assert.assertEquals; 43 | 44 | 45 | /** 46 | * Created with IntelliJ IDEA. 47 | * User: olli 48 | * Date: 09.11.13 49 | * Time: 16:28 50 | * To change this template use File | Settings | File Templates. 51 | */ 52 | @RunWith(Parameterized.class) 53 | public class MessageDecodingWithValidMessages extends AbstractCoapTest { 54 | 55 | @Parameterized.Parameters(name = "Test: {1}") 56 | public static Collection data() throws Exception { 57 | 58 | initializeLogging(); 59 | 60 | return Lists.newArrayList( 61 | new Object[]{new byte[]{(byte) (0x60 & 0xFF), 0, (byte) (0xFF & 0xFF), (byte) (0xFF & 0xFF)}, 62 | CoapMessage.createEmptyAcknowledgement(65535)}, 63 | 64 | new Object[]{new byte[]{(byte) (0x70 & 0xFF), 0, 0, 0}, 65 | CoapMessage.createEmptyReset(0)} 66 | ); 67 | } 68 | 69 | 70 | @Override 71 | public void setupLogging() throws Exception { 72 | Logger.getLogger("de.uzl.itm.ncoap.communication.codec").setLevel(Level.DEBUG); 73 | Logger.getRootLogger().setLevel(Level.ERROR); 74 | } 75 | 76 | 77 | private ChannelBuffer encodedMessageBuffer; 78 | private CoapMessage expected; 79 | private CoapMessage actual; 80 | 81 | 82 | public MessageDecodingWithValidMessages(byte[] encodedMessage, CoapMessage expected) { 83 | this.expected = expected; 84 | this.encodedMessageBuffer = ChannelBuffers.wrappedBuffer(encodedMessage); 85 | } 86 | 87 | 88 | @Before 89 | public void test() throws Exception { 90 | actual = (CoapMessage) new CoapTestDecoder().decode(encodedMessageBuffer); 91 | } 92 | 93 | 94 | @Test 95 | public void testProtocolVersion() { 96 | assertEquals(expected.getProtocolVersion(), actual.getProtocolVersion()); 97 | } 98 | 99 | @Test 100 | public void testMessageTyoe() { 101 | assertEquals(expected.getMessageType(), actual.getMessageType()); 102 | } 103 | 104 | @Test 105 | public void testMessageCode() { 106 | assertEquals(expected.getMessageCode(), actual.getMessageCode()); 107 | } 108 | 109 | @Test 110 | public void testMessageID() { 111 | assertEquals(expected.getMessageID(), actual.getMessageID()); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/codec/tools/CoapTestDecoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.codec.tools; 26 | 27 | import de.uzl.itm.ncoap.communication.codec.CoapMessageDecoder; 28 | import org.jboss.netty.buffer.ChannelBuffer; 29 | 30 | /** 31 | * Created with IntelliJ IDEA. 32 | * User: olli 33 | * Date: 12.11.13 34 | * Time: 23:54 35 | * To change this template use File | Settings | File Templates. 36 | */ 37 | public class CoapTestDecoder extends CoapMessageDecoder{ 38 | 39 | public Object decode(ChannelBuffer buffer) throws Exception { 40 | return super.decode(null, buffer); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/codec/tools/CoapTestEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | /** 26 | * Copyright (c) 2012, Oliver Kleine, Institute of Telematics, University of Luebeck 27 | * All rights reserved 28 | * 29 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 30 | * following conditions are met: 31 | * 32 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 33 | * disclaimer. 34 | * 35 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 36 | * following disclaimer in the documentation and/or other materials provided with the distribution. 37 | * 38 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 39 | * products derived from this software without specific prior written permission. 40 | * 41 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 42 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 44 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 45 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 46 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 47 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48 | */ 49 | package de.uzl.itm.ncoap.communication.codec.tools; 50 | 51 | import de.uzl.itm.ncoap.communication.codec.CoapMessageEncoder; 52 | import de.uzl.itm.ncoap.communication.codec.OptionCodecException; 53 | import de.uzl.itm.ncoap.message.CoapMessage; 54 | import de.uzl.itm.ncoap.message.options.OptionValue; 55 | import org.jboss.netty.buffer.ChannelBuffer; 56 | 57 | /** 58 | * Created with IntelliJ IDEA. 59 | * User: olli 60 | * Date: 09.11.13 61 | * Time: 21:47 62 | * To change this template use File | Settings | File Templates. 63 | */ 64 | public class CoapTestEncoder extends CoapMessageEncoder{ 65 | 66 | public ChannelBuffer encode(CoapMessage coapMessage) throws OptionCodecException { 67 | return super.encode(coapMessage); 68 | } 69 | 70 | public void encodeOption(ChannelBuffer buffer, int optionNumber, OptionValue optionValue, int prevNumber) 71 | throws OptionCodecException { 72 | 73 | super.encodeOption(buffer, optionNumber, optionValue, prevNumber); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/identification/EndpointIDRepeatedInResponseTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.identification; 26 | 27 | import de.uzl.itm.ncoap.application.client.CoapClient; 28 | import de.uzl.itm.ncoap.application.server.CoapServer; 29 | import de.uzl.itm.ncoap.communication.AbstractCoapCommunicationTest; 30 | import de.uzl.itm.ncoap.application.client.ClientCallback; 31 | import de.uzl.itm.ncoap.message.CoapRequest; 32 | import de.uzl.itm.ncoap.message.CoapResponse; 33 | import de.uzl.itm.ncoap.message.MessageCode; 34 | import de.uzl.itm.ncoap.message.MessageType; 35 | import org.apache.log4j.Level; 36 | import org.apache.log4j.Logger; 37 | import org.junit.Test; 38 | 39 | import java.net.InetSocketAddress; 40 | import java.net.URI; 41 | 42 | import static org.junit.Assert.assertArrayEquals; 43 | import static org.junit.Assert.assertEquals; 44 | 45 | /** 46 | * Created by olli on 01.09.15. 47 | */ 48 | public class EndpointIDRepeatedInResponseTest extends AbstractCoapCommunicationTest{ 49 | 50 | private static Logger log = Logger.getLogger(EndpointIDRepeatedInResponseTest.class.getName()); 51 | 52 | private static CoapClient client; 53 | private static CoapServer server; 54 | private static CoapRequest coapRequest; 55 | private static CoapResponse coapResponse; 56 | 57 | @Override 58 | public void setupComponents() throws Exception { 59 | client = new CoapClient(); 60 | server = new CoapServer(); 61 | 62 | URI targetUri = new URI("coap://localhost:" + server.getPort() + "/.well-known/core"); 63 | coapRequest = new CoapRequest(MessageType.CON, MessageCode.GET, targetUri); 64 | coapRequest.setEndpointID1(); 65 | } 66 | 67 | @Override 68 | public void createTestScenario() throws Exception { 69 | client.sendCoapRequest(coapRequest, new InetSocketAddress("localhost", server.getPort()), new ClientCallback() { 70 | @Override 71 | public void processCoapResponse(CoapResponse coapResponse) { 72 | log.info("Request: " + coapRequest); 73 | log.info("Response: " + coapResponse); 74 | EndpointIDRepeatedInResponseTest.coapResponse = coapResponse; 75 | } 76 | }); 77 | 78 | Thread.sleep(1000); 79 | } 80 | 81 | @Override 82 | public void shutdownComponents() throws Exception { 83 | server.shutdown().get(); 84 | client.shutdown(); 85 | } 86 | 87 | @Override 88 | public void setupLogging() throws Exception { 89 | Logger.getLogger(EndpointIDRepeatedInResponseTest.class.getName()) 90 | .setLevel(Level.INFO); 91 | 92 | Logger.getLogger("de.uzl.itm.ncoap.communication.identification") 93 | .setLevel(Level.DEBUG); 94 | 95 | } 96 | 97 | @Test 98 | public void testEndpointIDsMatch() { 99 | assertArrayEquals("IDs do not match", coapRequest.getEndpointID1(), coapResponse.getEndpointID2()); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/communication/observe/ObservationStopsDueToErrorResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.communication.observe; 26 | 27 | import de.uzl.itm.ncoap.application.client.CoapClient; 28 | import de.uzl.itm.ncoap.application.server.CoapServer; 29 | import de.uzl.itm.ncoap.communication.AbstractCoapCommunicationTest; 30 | import de.uzl.itm.ncoap.endpoints.client.TestCallback; 31 | import de.uzl.itm.ncoap.endpoints.server.ObservableTestWebresource; 32 | import de.uzl.itm.ncoap.message.CoapRequest; 33 | import de.uzl.itm.ncoap.message.MessageCode; 34 | import de.uzl.itm.ncoap.message.MessageType; 35 | import org.apache.log4j.Level; 36 | import org.apache.log4j.Logger; 37 | import org.junit.Test; 38 | 39 | import java.net.InetSocketAddress; 40 | import java.net.URI; 41 | 42 | 43 | /** 44 | * Created by olli on 23.09.14. 45 | */ 46 | public class ObservationStopsDueToErrorResponse extends AbstractCoapCommunicationTest { 47 | 48 | private static CoapClient client; 49 | private static TestCallback clientCallback; 50 | 51 | private static CoapServer server; 52 | private static InetSocketAddress serverSocket; 53 | 54 | private static ObservableTestWebresource service; 55 | 56 | private static URI serviceUri; 57 | 58 | @Override 59 | public void setupComponents() throws Exception { 60 | client = new CoapClient(); 61 | clientCallback = new TestCallback(); 62 | server = new CoapServer(); 63 | service = new ObservableTestWebresource("/test", 1, 0, server.getExecutor()); 64 | server.registerWebresource(service); 65 | 66 | serverSocket = new InetSocketAddress("localhost", server.getPort()); 67 | serviceUri = new URI("coap", null, "localhost", -1, "/test", null, null); 68 | } 69 | 70 | @Override 71 | public void createTestScenario() throws Exception { 72 | CoapRequest coapRequest = new CoapRequest(MessageType.CON, MessageCode.GET, serviceUri); 73 | coapRequest.setObserve(0); 74 | coapRequest.setAccept(111); 75 | client.sendCoapRequest(coapRequest, serverSocket, clientCallback); 76 | Thread.sleep(2000); 77 | service.setResourceStatus(2, 0); 78 | Thread.sleep(2000); 79 | } 80 | 81 | @Override 82 | public void shutdownComponents() throws Exception { 83 | server.shutdown().get(); 84 | client.shutdown(); 85 | 86 | } 87 | 88 | @Override 89 | public void setupLogging() throws Exception { 90 | Logger.getLogger("de.uzl.itm.ncoap.communication.observing") 91 | .setLevel(Level.DEBUG); 92 | 93 | Logger.getRootLogger().setLevel(Level.ERROR); 94 | } 95 | 96 | @Test 97 | public void test() { 98 | // TODO 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/endpoints/server/NotObservableTestWebresourceForPost.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.endpoints.server; 26 | 27 | import com.google.common.primitives.Ints; 28 | import com.google.common.util.concurrent.SettableFuture; 29 | import de.uzl.itm.ncoap.application.server.resource.NotObservableWebresource; 30 | import de.uzl.itm.ncoap.application.server.resource.WrappedResourceStatus; 31 | import de.uzl.itm.ncoap.message.CoapMessage; 32 | import de.uzl.itm.ncoap.message.CoapRequest; 33 | import de.uzl.itm.ncoap.message.CoapResponse; 34 | import de.uzl.itm.ncoap.message.MessageCode; 35 | import de.uzl.itm.ncoap.message.options.ContentFormat; 36 | import de.uzl.itm.ncoap.message.options.OptionValue; 37 | import org.slf4j.Logger; 38 | import org.slf4j.LoggerFactory; 39 | 40 | import java.net.InetSocketAddress; 41 | import java.util.concurrent.ScheduledExecutorService; 42 | 43 | /** 44 | * Created by olli on 11.04.16. 45 | */ 46 | public class NotObservableTestWebresourceForPost extends NotObservableWebresource{ 47 | 48 | private static Logger LOG = LoggerFactory.getLogger(NotObservableTestWebresourceForPost.class.getName()); 49 | 50 | private byte[] weakEtag; 51 | 52 | public NotObservableTestWebresourceForPost(String servicePath, String initialStatus, long lifetimeSeconds, 53 | ScheduledExecutorService executor) { 54 | super(servicePath, initialStatus, lifetimeSeconds, executor); 55 | } 56 | 57 | @Override 58 | public byte[] getEtag(long contentFormat) { 59 | return this.weakEtag; 60 | } 61 | 62 | @Override 63 | public void updateEtag(String resourceStatus) { 64 | this.weakEtag = Ints.toByteArray(resourceStatus.hashCode()); 65 | } 66 | 67 | @Override 68 | public void shutdown() { 69 | // nothing to do... 70 | } 71 | 72 | @Override 73 | public void processCoapRequest(SettableFuture responseFuture, CoapRequest coapRequest, 74 | InetSocketAddress remoteSocket) throws Exception { 75 | 76 | int messageType = coapRequest.getMessageType(); 77 | 78 | if (coapRequest.getMessageCode() == MessageCode.POST) { 79 | this.setResourceStatus(coapRequest.getContent().toString(CoapMessage.CHARSET), 0); 80 | WrappedResourceStatus status = getWrappedResourceStatus(ContentFormat.TEXT_PLAIN_UTF8); 81 | 82 | CoapResponse coapResponse = new CoapResponse(messageType, MessageCode.CHANGED_204); 83 | coapResponse.setContent(status.getContent(), status.getContentFormat()); 84 | coapResponse.setEtag(status.getEtag()); 85 | 86 | responseFuture.set(coapResponse); 87 | } else { 88 | CoapResponse coapResponse = new CoapResponse(messageType, MessageCode.METHOD_NOT_ALLOWED_405); 89 | coapResponse.setContent("POST ONLY".getBytes(CoapMessage.CHARSET), ContentFormat.TEXT_PLAIN_UTF8); 90 | 91 | responseFuture.set(coapResponse); 92 | } 93 | } 94 | 95 | @Override 96 | public byte[] getSerializedResourceStatus(long contentFormat) { 97 | return getResourceStatus().getBytes(CoapMessage.CHARSET); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /ncoap-core/src/test/java/de/uzl/itm/ncoap/message/CoapRequestCreationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.message; 26 | 27 | import com.google.common.collect.Lists; 28 | import de.uzl.itm.ncoap.AbstractCoapTest; 29 | import org.apache.log4j.Level; 30 | import org.apache.log4j.Logger; 31 | import org.junit.After; 32 | import org.junit.Test; 33 | import org.junit.runner.RunWith; 34 | import org.junit.runners.Parameterized; 35 | 36 | import java.net.InetAddress; 37 | import java.net.URI; 38 | import java.util.Collection; 39 | 40 | import static org.junit.Assert.assertEquals; 41 | 42 | /** 43 | * Created with IntelliJ IDEA. 44 | * User: olli 45 | * Date: 10.11.13 46 | * Time: 23:34 47 | * To change this template use File | Settings | File Templates. 48 | */ 49 | @RunWith(Parameterized.class) 50 | public class CoapRequestCreationTest extends AbstractCoapTest{ 51 | 52 | @Parameterized.Parameters(name = "[{index}] Message Type: {0}, Message Code: {1}") 53 | public static Collection data() throws Exception { 54 | return Lists.newArrayList( 55 | new Object[]{MessageType.CON, 56 | MessageCode.GET, 57 | new URI("coap", null, "www.example.org", -1, "/path/to/service", null, null), 58 | null, 59 | 12345, 60 | new byte[]{1, 2, 3, 4, 5, 6, 7, 8}}, 61 | 62 | new Object[]{MessageType.CON, 63 | MessageCode.GET, 64 | new URI("coap", null, "www.example.org", -1, "/path/to/service", null, null), 65 | InetAddress.getByName("2001:1:2:3:4:5:6:7"), 66 | 65535, 67 | new byte[0]} 68 | ); 69 | } 70 | 71 | private Logger log = Logger.getLogger(this.getClass().getName()); 72 | 73 | private final int messageType; 74 | private final int messageCode; 75 | private final URI targetUri; 76 | private final InetAddress proxyAddress; 77 | private final int messageID; 78 | private final byte[] token; 79 | private CoapRequest coapRequest; 80 | 81 | public CoapRequestCreationTest(int messageType, int messageCode, URI targetUri, 82 | InetAddress proxyAddress, int messageID, byte[] token) throws Exception { 83 | 84 | this.messageType = messageType; 85 | this.messageCode = messageCode; 86 | this.targetUri = targetUri; 87 | this.proxyAddress = proxyAddress; 88 | this.messageID = messageID; 89 | this.token = token; 90 | 91 | log.debug("Create CoAP Request: (Type) " + messageType + ", (Code) " + messageCode); 92 | log.debug("Create CoAP Request: (Type) " + messageType + ", (Code) " + messageCode); 93 | this.coapRequest = new CoapRequest(messageType, messageCode, targetUri); 94 | 95 | 96 | } 97 | 98 | @Test 99 | public void testMessageTypeName() { 100 | assertEquals("Message types do not match, ", coapRequest.getMessageType(), messageType); 101 | } 102 | 103 | @Test 104 | public void testMessageType() { 105 | assertEquals("Message type numbers do not match, ", coapRequest.getMessageType(), messageType); 106 | } 107 | 108 | @After 109 | public void justWaitSomeTimeToCompleteLogging() throws InterruptedException { 110 | Thread.sleep(100); 111 | } 112 | 113 | @Override 114 | public void setupLogging() throws Exception { 115 | Logger.getLogger("de.uniluebeck.itm.ncoap.message").setLevel(Level.DEBUG); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /ncoap-simple-client/README.md: -------------------------------------------------------------------------------- 1 | ## A simple Client with nCoAP 2 | 3 | This MAVEN module contains a simple example application, to showcase how to use the protocol implementation for 4 | clients. The client is configured using command line parameters. For a complete list of supported parameters use 5 | 6 | `java -jar ncoap-simple-client-1.8.3-SNAPSHOT.one-jar.jar --help` 7 | 8 | on the command line after compiling this Maven module or program parameter `--help` in your IDE. 9 | 10 | ### Example 1 11 | 12 | To send a single request to `coap://example.org:5683/test` one can start the client using the following 13 | program parameters (command line or IDE) parameters: 14 | 15 | `--host example.org --port 5683 --path /test --non --duration 20` 16 | 17 | This will cause a non-confirmable CoAP request sent to the resource. Afterwards the client awaits either a single 18 | response or 20 seconds to pass (whatever happens first). Then the application is shut down. 19 | 20 | ### Example 2 21 | 22 | To start the observation of `coap://example.org:5683/obs` one can start the client using the following 23 | program parameters: 24 | 25 | `--host example.org --port 5683 --path /obs --observing --maxUpdates 5 --duration 90` 26 | 27 | This will cause a confirmable CoAP request with the observing option set to be sent to the resource and either await 5 28 | update notifications or 90 seconds to pass (whatever happens first). If one of this shutdown criteria is satisfied, 29 | the application is shut down after another delay of 10 seconds. 30 | 31 | The 10 seconds are to (possibly) enable a graceful abortion of the running observation (depends on the update interval 32 | of the observed resource). 33 | 34 | **Note:** The parameter `--duration` has a default value of 60, i.e. the client shuts down at the latest after 60 35 | seconds (and another 10 seconds as mentioned above) if no other value was explicitly set. 36 | -------------------------------------------------------------------------------- /ncoap-simple-client/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /ncoap-simple-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | 8 | 9 | de.uzl.itm 10 | ncoap-complete 11 | 1.8.3-SNAPSHOT 12 | 13 | 14 | ncoap-simple-client 15 | jar 16 | 17 | Simple nCoAP-Client 18 | 19 | Example module on how to build a simple CoAP client application using nCoAP. 20 | 21 | 22 | 23 | 24 | 25 | de.uzl.itm 26 | ncoap-core 27 | ${project.parent.version} 28 | 29 | 30 | 31 | args4j 32 | args4j 33 | 2.0.26 34 | 35 | 36 | 37 | org.slf4j 38 | slf4j-log4j12 39 | 1.6.6 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | com.jolira 48 | onejar-maven-plugin 49 | 1.4.4 50 | 51 | 52 | 53 | 54 | de.uzl.itm.ncoap.examples.client.SimpleCoapClient 55 | 56 | 57 | 0.97 58 | 59 | true 60 | 61 | onejar 62 | 63 | 64 | 65 | 66 | one-jar 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /ncoap-simple-client/src/main/java/de/uzl/itm/ncoap/examples/client/callback/SimpleCallback.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.examples.client.callback; 26 | 27 | import de.uzl.itm.ncoap.application.client.ClientCallback; 28 | import de.uzl.itm.ncoap.message.CoapResponse; 29 | import org.slf4j.Logger; 30 | import org.slf4j.LoggerFactory; 31 | 32 | import java.util.concurrent.atomic.AtomicBoolean; 33 | import java.util.concurrent.atomic.AtomicInteger; 34 | 35 | /** 36 | * This is a very simple implementation of {@link ClientCallback} which does virtually nothing but log internal events. 37 | * 38 | * @author Oliver Kleine 39 | */ 40 | public class SimpleCallback extends ClientCallback { 41 | 42 | private Logger log = LoggerFactory.getLogger(this.getClass().getName()); 43 | 44 | private AtomicBoolean responseReceived; 45 | private AtomicInteger transmissionCounter; 46 | private AtomicBoolean timedOut; 47 | 48 | 49 | 50 | public SimpleCallback() { 51 | this.responseReceived = new AtomicBoolean(false); 52 | this.transmissionCounter = new AtomicInteger(0); 53 | this.timedOut = new AtomicBoolean(false); 54 | } 55 | 56 | /** 57 | * Increases the reponse counter by 1, i.e. {@link #getResponseCount()} will return a higher value after 58 | * invocation of this method. 59 | * 60 | * @param coapResponse the response message 61 | */ 62 | @Override 63 | public void processCoapResponse(CoapResponse coapResponse) { 64 | responseReceived.set(true); 65 | log.info("Received: {}", coapResponse); 66 | } 67 | 68 | /** 69 | * Returns the number of responses received 70 | * @return the number of responses received 71 | */ 72 | public int getResponseCount() { 73 | return this.responseReceived.get() ? 1 : 0; 74 | } 75 | 76 | 77 | @Override 78 | public void processRetransmission() { 79 | int value = transmissionCounter.incrementAndGet(); 80 | log.info("Retransmission #{}", value); 81 | } 82 | 83 | 84 | @Override 85 | public void processTransmissionTimeout() { 86 | log.info("Transmission timed out..."); 87 | timedOut.set(true); 88 | } 89 | 90 | @Override 91 | public void processResponseBlockReceived(long receivedLength, long expectedLength) { 92 | log.info("Received {}/{} bytes.", receivedLength, expectedLength); 93 | } 94 | 95 | public boolean isTimedOut() { 96 | return timedOut.get(); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /ncoap-simple-client/src/main/java/de/uzl/itm/ncoap/examples/client/callback/SimpleObservationCallback.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.examples.client.callback; 26 | 27 | import de.uzl.itm.ncoap.examples.client.SimpleCoapClient; 28 | import de.uzl.itm.ncoap.message.CoapMessage; 29 | import de.uzl.itm.ncoap.message.CoapResponse; 30 | import org.slf4j.Logger; 31 | import org.slf4j.LoggerFactory; 32 | 33 | import java.lang.Override; 34 | import java.util.concurrent.atomic.AtomicInteger; 35 | 36 | /** 37 | * An instance of {@link SimpleObservationCallback} is used to observe a remote resource with the 38 | * {@link SimpleCoapClient}. It awaits a pre-defined number of update notifications (see constructor parameter) until 39 | * it cancels the observation. 40 | * 41 | * @author Oliver Kleine 42 | */ 43 | public class SimpleObservationCallback extends SimpleCallback { 44 | 45 | private Logger log = LoggerFactory.getLogger(this.getClass().getName()); 46 | 47 | private AtomicInteger responseCounter; 48 | private int expectedNumberOfUpdateNotifications; 49 | 50 | /** 51 | * Creates a new instance of {@link SimpleObservationCallback} 52 | * 53 | * @param expectedNumberOfUpdateNotifications the number of update notifications to be received until the 54 | * observation is automatically stopped 55 | */ 56 | public SimpleObservationCallback(int expectedNumberOfUpdateNotifications) { 57 | this.responseCounter = new AtomicInteger(0); 58 | this.expectedNumberOfUpdateNotifications = expectedNumberOfUpdateNotifications; 59 | } 60 | 61 | /** 62 | * Increases the reponse counter by 1, i.e. {@link #getResponseCount()} will return a higher value after 63 | * invocation of this method. 64 | * 65 | * @param coapResponse the response message 66 | */ 67 | @Override 68 | public void processCoapResponse(CoapResponse coapResponse) { 69 | int value = responseCounter.incrementAndGet(); 70 | log.info("Received #{}: {}", value, coapResponse); 71 | log.info("Full Payload:\n{}", coapResponse.getContent().toString(CoapMessage.CHARSET)); 72 | } 73 | 74 | 75 | @Override 76 | public int getResponseCount() { 77 | return responseCounter.intValue(); 78 | } 79 | 80 | @Override 81 | public boolean continueObservation() { 82 | boolean result = getResponseCount() < expectedNumberOfUpdateNotifications; 83 | 84 | log.info("Received {}/{} responses (continue observation: {})", 85 | new Object[]{this.getResponseCount(), this.expectedNumberOfUpdateNotifications, result}); 86 | 87 | return result; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /ncoap-simple-client/src/main/java/de/uzl/itm/ncoap/examples/client/config/LoggingConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.examples.client.config; 26 | 27 | import org.apache.log4j.xml.DOMConfigurator; 28 | 29 | import java.io.File; 30 | import java.net.URL; 31 | 32 | /** 33 | * Helper class to configure the applications logging 34 | * 35 | * @author Oliver Kleine 36 | */ 37 | public abstract class LoggingConfiguration { 38 | 39 | /** 40 | * Configure the logging with the XML-File (log4j style) located at the given path. If no 41 | * proper XML file was found at the given path this method invoke 42 | * {@link #configureDefaultLogging()}. 43 | * 44 | * @param path the (absolute or relative) path to the XML-config file. 45 | * 46 | * @throws Exception if something went terribly wrong 47 | */ 48 | public static void configureLogging(String path) throws Exception{ 49 | 50 | if (configureLogging(new File(path))) 51 | return; 52 | 53 | configureDefaultLogging(); 54 | 55 | } 56 | 57 | 58 | /** 59 | * Activates a default logging scheme, i.e. {@link org.apache.log4j.Level#INFO} for all 60 | * loggers and only console output. 61 | * 62 | * @throws Exception if something went terribly wrong 63 | */ 64 | public static void configureDefaultLogging() throws Exception{ 65 | System.out.println("Use default logging configuration, i.e. INFO level...\n"); 66 | URL url = LoggingConfiguration.class.getClassLoader().getResource("log4j.default.xml"); 67 | System.out.println("Use config file " + url); 68 | DOMConfigurator.configure(url); 69 | } 70 | 71 | 72 | private static boolean configureLogging(File configFile) throws Exception{ 73 | System.out.println("Looking for file \"log4j.xml\" at path: " + configFile.getAbsolutePath()); 74 | 75 | if (!configFile.exists()) { 76 | System.out.println("File \"log4j.xml\" not found...\n"); 77 | return false; 78 | } 79 | 80 | System.out.println("File \"log4j.xml\" found...\n"); 81 | DOMConfigurator.configure(configFile.toURI().toURL()); 82 | return true; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /ncoap-simple-client/src/main/resources/log4j.default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /ncoap-simple-server/README.md: -------------------------------------------------------------------------------- 1 | ## A simple Server with nCoAP 2 | 3 | This MAVEN module contains a simple server hosting the two Web Services: 4 | 5 | * `/simple` (not observable) 6 | * `/utc-time` (observable, updates every 5 seconds) 7 | 8 | both of which support content formats `text/plain` (no. 0) and `application/xml` (no. 40). To start the server simply 9 | type 10 | 11 | `java -jar ncoap-simple-server-1.8.3-SNAPSHOT.one-jar.jar` 12 | 13 | after running the Maven target "package", i.e. `mvn package`, in a terminal or your IDE. -------------------------------------------------------------------------------- /ncoap-simple-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | 8 | 9 | de.uzl.itm 10 | ncoap-complete 11 | 1.8.3-SNAPSHOT 12 | 13 | 14 | ncoap-simple-server 15 | Simple nCoAP-Server 16 | 17 | Example module on how to build a simple CoAP server application hosting observable and not observable 18 | webservices using nCoAP. 19 | 20 | 21 | jar 22 | 23 | 24 | 25 | 26 | de.uzl.itm 27 | ncoap-core 28 | ${project.parent.version} 29 | 30 | 31 | 32 | org.slf4j 33 | slf4j-log4j12 34 | 1.6.6 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | com.jolira 43 | onejar-maven-plugin 44 | 1.4.4 45 | 46 | 47 | 48 | 49 | de.uzl.itm.ncoap.examples.server.SimpleCoapServer 50 | 51 | 52 | 0.97 53 | 54 | true 55 | 56 | onejar 57 | 58 | 59 | one-jar 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /ncoap-simple-server/src/main/java/de/uzl/itm/ncoap/examples/server/LoggingConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.examples.server; 26 | 27 | import org.apache.log4j.xml.DOMConfigurator; 28 | 29 | import java.io.File; 30 | import java.net.URL; 31 | 32 | /** 33 | * Helper class to configure the applications logging 34 | * 35 | * @author Oliver Kleine 36 | */ 37 | public abstract class LoggingConfiguration { 38 | 39 | /** 40 | * Configure the logging with the XML-File (log4j style) located at the given path. If no 41 | * proper XML file was found at the given path this method invoke 42 | * {@link #configureDefaultLogging()}. 43 | * 44 | * @param path the (absolute or relative) path to the XML-config file. 45 | * 46 | * @throws Exception if something went terribly wrong 47 | */ 48 | public static void configureLogging(String path) throws Exception{ 49 | 50 | if (configureLogging(new File(path))) 51 | return; 52 | 53 | configureDefaultLogging(); 54 | 55 | } 56 | 57 | 58 | /** 59 | * Activates a default logging scheme, i.e. {@link org.apache.log4j.Level#INFO} for all 60 | * loggers and only console output. 61 | * 62 | * @throws Exception if something went terribly wrong 63 | */ 64 | public static void configureDefaultLogging() throws Exception{ 65 | System.out.println("Use default logging configuration, i.e. INFO level...\n"); 66 | URL url = LoggingConfiguration.class.getClassLoader().getResource("log4j.default.xml"); 67 | System.out.println("Use config file " + url); 68 | DOMConfigurator.configure(url); 69 | } 70 | 71 | 72 | private static boolean configureLogging(File configFile) throws Exception{ 73 | System.out.println("Looking for file \"log4j.xml\" at path: " + configFile.getAbsolutePath()); 74 | 75 | if (!configFile.exists()) { 76 | System.out.println("File \"log4j.xml\" not found...\n"); 77 | return false; 78 | } 79 | 80 | System.out.println("File \"log4j.xml\" found...\n"); 81 | DOMConfigurator.configure(configFile.toURI().toURL()); 82 | return true; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /ncoap-simple-server/src/main/java/de/uzl/itm/ncoap/examples/server/SimpleCoapServer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016, Oliver Kleine, Institute of Telematics, University of Luebeck 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 6 | * following conditions are met: 7 | * 8 | * - Redistributions of source messageCode must retain the above copyright notice, this list of conditions and the following 9 | * disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 12 | * following disclaimer in the documentation and/or other materials provided with the distribution. 13 | * 14 | * - Neither the name of the University of Luebeck nor the names of its contributors may be used to endorse or promote 15 | * products derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | */ 25 | package de.uzl.itm.ncoap.examples.server; 26 | 27 | 28 | import de.uzl.itm.ncoap.application.server.CoapServer; 29 | import de.uzl.itm.ncoap.communication.blockwise.BlockSize; 30 | import de.uzl.itm.ncoap.message.options.OptionValue; 31 | 32 | import java.util.concurrent.ScheduledExecutorService; 33 | 34 | /** 35 | * This is a simple application to showcase how to use nCoAP for servers 36 | * 37 | * @author Oliver Kleine 38 | */ 39 | public class SimpleCoapServer extends CoapServer { 40 | 41 | private void registerSimpleNotObservableWebresource() { 42 | // create initial status (one string per paragraph) 43 | String[] status = new String[10]; 44 | for (int i = 0; i < status.length; i++) { 45 | status[i] = "This is paragraph #" + (i + 1); 46 | } 47 | 48 | // register resource at server 49 | this.registerWebresource(new SimpleNotObservableWebresource( 50 | "/simple", status, OptionValue.MAX_AGE_MAX, this.getExecutor() 51 | )); 52 | } 53 | 54 | private void registerSimpleObservableTimeResource() { 55 | // register resource at server 56 | this.registerWebresource(new SimpleObservableTimeService("/utc-time", 5, this.getExecutor())); 57 | } 58 | 59 | /** 60 | * Creates a new instance of {@link SimpleCoapServer} 61 | * @param block1Size the preferred, i.e. max. {@link BlockSize} for requests 62 | * @param block2Size the preferred, i.e. max {@link BlockSize} for responses 63 | */ 64 | public SimpleCoapServer(BlockSize block1Size, BlockSize block2Size) { 65 | super(block1Size, block2Size); 66 | } 67 | 68 | /** 69 | * Starts a {@link CoapServer} instance with two {@link de.uzl.itm.ncoap.application.server.resource.Webresource} 70 | * instances where one is observable and the other one is not. 71 | * 72 | * @param args no arguments needed (may be empty) 73 | * @throws Exception if some unexpected error occurred 74 | */ 75 | public static void main(String[] args) throws Exception { 76 | // configure logging 77 | LoggingConfiguration.configureDefaultLogging(); 78 | 79 | // create server and register resources 80 | SimpleCoapServer server = new SimpleCoapServer(BlockSize.SIZE_64, BlockSize.SIZE_64); 81 | server.registerSimpleNotObservableWebresource(); 82 | server.registerSimpleObservableTimeResource(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /ncoap-simple-server/src/main/resources/log4j.default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | UTF-8 6 | 7 | 8 | 4.0.0 9 | de.uzl.itm 10 | 11 | ncoap-complete 12 | pom 13 | 1.8.3-SNAPSHOT 14 | 15 | nCoAP Complete 16 | 17 | This is the wrapper module to include the protocol implementation (ncoap-core) and simple examples 18 | on how to build clients (ncoap-simple-client) and servers (ncoap-simple-server) 19 | 20 | https://github.com/okleine/nCoAP 21 | 22 | 23 | ncoap-core 24 | ncoap-simple-client 25 | ncoap-simple-server 26 | 27 | 28 | 29 | 30 | itm-maven-repository-releases 31 | ITM Maven Releases Repository 32 | https://maven.itm.uni-luebeck.de/repositories/releases 33 | 34 | 35 | itm-maven-repository-snapshots 36 | ITM Maven Snapshots Repository 37 | https://maven.itm.uni-luebeck.de/repository/snapshots/ 38 | 39 | 40 | kleine-maven-site 41 | scpexe://itm01.itm.uni-luebeck.de/www/itm-media/people/kleine/maven/${project.artifactId}/${project.version} 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.apache.maven.plugins 49 | maven-javadoc-plugin 50 | 2.9.1 51 | 52 | -Xdoclint:none 53 | 54 | http://netty.io/3.9/api/ 55 | http://docs.guava-libraries.googlecode.com/git-history/v16.0.1/javadoc/ 56 | 57 | src/main/resources/stylesheet.css 58 | 59 | 60 | 61 | 62 | javadoc 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | org.apache.maven.plugins 74 | maven-compiler-plugin 75 | 2.3.2 76 | 77 | 1.7 78 | 1.7 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-source-plugin 84 | 2.2 85 | 86 | 87 | 88 | jar 89 | 90 | 91 | 92 | 93 | 94 | 95 | com.mycila.maven-license-plugin 96 | maven-license-plugin 97 | 1.9.0 98 | 99 | true 100 |
${basedir}/license.txt
101 | 102 | **/*.java 103 | 104 | true 105 |
106 | 107 | 108 | compile 109 | 110 | format 111 | 112 | 113 | 114 |
115 |
116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 |
125 |
126 | --------------------------------------------------------------------------------