├── 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