Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAA-1654: Add CoAP transport to Kaa #1361

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions client/client-multi/client-java-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
<version>2.0.0-M2</version>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>element-connector</artifactId>
<version>2.0.0-M2</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public class TransportProtocolIdConstants {
private static final int TCP_TRANSPORT_PROTOCOL_VERSION = 1;
public static final TransportProtocolId TCP_TRANSPORT_ID = new TransportProtocolId(
TCP_TRANSPORT_PROTOCOL_ID, TCP_TRANSPORT_PROTOCOL_VERSION);
private static final int COAP_TRANSPORT_PROTOCOL_ID = 0x0b5d1174;
private static final int COAP_TRANSPORT_PROTOCOL_VERSION = 1;
public static final TransportProtocolId COAP_TRANSPORT_ID = new TransportProtocolId(
COAP_TRANSPORT_PROTOCOL_ID, COAP_TRANSPORT_PROTOCOL_VERSION);




private TransportProtocolIdConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.kaaproject.kaa.client.channel.impl.channels;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs Apache license header. Both Jenkins and Travis are currently failing because of missing Apache license headers.


import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.Utils;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.kaaproject.kaa.client.channel.ChannelDirection;
import org.kaaproject.kaa.client.channel.IpTransportInfo;
import org.kaaproject.kaa.client.channel.KaaDataChannel;
import org.kaaproject.kaa.client.channel.KaaDataDemultiplexer;
import org.kaaproject.kaa.client.channel.KaaDataMultiplexer;
import org.kaaproject.kaa.client.channel.ServerType;
import org.kaaproject.kaa.client.channel.TransportConnectionInfo;
import org.kaaproject.kaa.client.channel.TransportProtocolId;
import org.kaaproject.kaa.client.channel.TransportProtocolIdConstants;

import org.kaaproject.kaa.client.channel.connectivity.ConnectivityChecker;
import org.kaaproject.kaa.common.TransportType;
import org.kaaproject.kaa.common.endpoint.security.MessageEncoderDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class CoapChannel implements KaaDataChannel {

public static final Logger LOG = LoggerFactory // NOSONAR
.getLogger(CoapChannel.class);

private static final Map<TransportType, ChannelDirection> SUPPORTED_TYPES = new HashMap<TransportType, ChannelDirection>();

static {
SUPPORTED_TYPES.put(TransportType.PROFILE, ChannelDirection.BIDIRECTIONAL);
SUPPORTED_TYPES.put(TransportType.CONFIGURATION, ChannelDirection.BIDIRECTIONAL);
SUPPORTED_TYPES.put(TransportType.NOTIFICATION, ChannelDirection.BIDIRECTIONAL);
}

private static final String CHANNEL_ID = "default_coap_channel";
private KaaDataDemultiplexer demultiplexer;
private KaaDataMultiplexer multiplexer;
private IpTransportInfo currentServer;

@Override
public void sync(TransportType type) {
sync(Collections.singleton(type));
}

@Override
public void sync(Set<TransportType> types) {

if (multiplexer == null) {
LOG.warn("Can't sync. Channel {} multiplexer is not set", getId());
return;
}
if (demultiplexer == null) {
LOG.warn("Can't sync. Channel {} demultiplexer is not set", getId());
return;
}

Map<TransportType, ChannelDirection> typeMap = new HashMap<>(getSupportedTransportTypes().size());
for (TransportType type : types) {
LOG.info("Processing sync {} for channel [{}]", type, getId());
ChannelDirection direction = getSupportedTransportTypes().get(type);
if (direction != null) {
typeMap.put(type, direction);
} else {
LOG.error("Unsupported type {} for channel [{}]", type, getId());
}
for (Map.Entry<TransportType, ChannelDirection> typeIt : getSupportedTransportTypes().entrySet()) {
if (!typeIt.getKey().equals(type)) {
typeMap.put(typeIt.getKey(), ChannelDirection.DOWN);
}
}
}

}

@Override
public void syncAll() {
}

@Override
public void syncAck(TransportType type) {
LOG.info("Adding sync acknowledgement for type {} as a regular sync for channel [{}]", type, getId());
syncAck(Collections.singleton(type));
}

@Override
public void syncAck(Set<TransportType> type) {
}

@Override
public String getId() {
return CHANNEL_ID;
}

@Override
public TransportProtocolId getTransportProtocolId() {
return TransportProtocolIdConstants.COAP_TRANSPORT_ID;
}

@Override
public ServerType getServerType() {
return ServerType.OPERATIONS;
}

@Override
public void setDemultiplexer(KaaDataDemultiplexer demultiplexer) {
if (demultiplexer != null) {
this.demultiplexer = demultiplexer;
}
}

@Override
public void setMultiplexer(KaaDataMultiplexer multiplexer) {
if (multiplexer != null) {
this.multiplexer = multiplexer;
}
}

@Override
public void setServer(TransportConnectionInfo server) {

LOG.info("Setting server [{}] for channel [{}]", server, getId());
if (server == null) {
LOG.warn("Server is null for Channel [{}].", getId());
return;
}

IpTransportInfo oldServer = currentServer;
this.currentServer = new IpTransportInfo(server);

}

@Override
public TransportConnectionInfo getServer() {
return currentServer;

}

@Override
public void setConnectivityChecker(ConnectivityChecker checker) {
}

@Override
public Map<TransportType, ChannelDirection> getSupportedTransportTypes() {
return SUPPORTED_TYPES;
}

@Override
public void shutdown() {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

/**
*
Copy link

@odovhai odovhai Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need some more java-docs with explanation. It would be better if all comments inside this method are moved to the java-doc .

* @param myUri uri of source
* @param method CoAP method like POST,GET,...
* @param payload Message Payload
* @param msgType Confirmable or Non confirmable
* @return server response
*/
public CoapResponse sendData(String myUri, String method, String payload, String msgType) {
LOG.info("this is SendData function!");
URI uri = null;

if (myUri != null) {
// input URI from command line arguments
try {
uri = new URI(myUri);
} catch (URISyntaxException exception) {
LOG.info("Invalid URI: " + exception.getMessage());
}

CoapClient client = new CoapClient(uri);
LOG.info(" new coap client is registerd");

CoapResponse response;

if (msgType == "NON") {
client.useNONs();
} else {
client.useCONs();
}

if (method == "post") {
response = client.post(payload, MediaTypeRegistry.TEXT_PLAIN);
} else {
response = client.get();
}

if (response != null) {

LOG.info(String.valueOf(response.getCode()));
LOG.info(String.valueOf(response.getOptions()));
LOG.info(response.getResponseText());

LOG.info("\nADVANCED\n");
// access advanced API with access to more details through .advanced()
LOG.info(Utils.prettyPrint(response));

} else {
LOG.info("No response received.");
}

return response;
} else {
// display help
LOG.info("Californium Client");
String simpleName = CoapChannel.class.getSimpleName();
LOG.info("Usage: " + simpleName + " URI");
LOG.info("URI: The CoAP URI of the remote resource to GET");
return null;
}
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1439,5 +1439,10 @@ Copyright 2014-2016 CyberVision, Inc.
<id>repository.kaaproject.snapshots</id>
<url>http://repository.kaaproject.org/repository/snapshots/</url>
</repository>
<repository>
<id>repo.eclipse.org</id>
<name>Californium Repository</name>
<url>https://repo.eclipse.org/content/repositories/californium/</url>
</repository>
</repositories>
</project>
14 changes: 14 additions & 0 deletions server/node/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@
<artifactId>config</artifactId>
<classifier>tcp</classifier>
</dependency>

<dependency>
<groupId>org.kaaproject.kaa.server.transports.coap</groupId>
<artifactId>transport</artifactId>
<version>0.11.0-SNAPSHOT</version>
<classifier>coap</classifier>
</dependency>
<dependency>
<groupId>org.kaaproject.kaa.server.transports.coap</groupId>
<artifactId>config</artifactId>
<version>0.11.0-SNAPSHOT</version>
<classifier>coap</classifier>
</dependency>

<dependency>
<groupId>org.kaaproject.kaa.server.common</groupId>
<artifactId>log-shared</artifactId>
Expand Down
Loading