-
Notifications
You must be signed in to change notification settings - Fork 475
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
elysoly
wants to merge
10
commits into
kaaproject:master
Choose a base branch
from
elysoly:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09f3fc0
CoAP client channel and its protocl ID constants were added.
elysoly 5ee7a3e
Californium CoAP repository was added.
elysoly 100484c
coap transport was added to kaa transports.
elysoly 1924084
coap related jar files was added to pom depenceies.
elysoly cd6bbaf
Some changes, based on code style, applied.
elysoly 575568f
Add some logs in each method to inform it's handling.
elysoly ef3fb84
Add Version number constant.
elysoly 09c2bcd
Relative path changed into absolute one.
elysoly 1fe8a12
Relative path changed into absolute one.
elysoly 64214d4
Californium version changed to 2.0.0-M2.
elysoly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
...-java-core/src/main/java/org/kaaproject/kaa/client/channel/impl/channels/CoapChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
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() { | ||
} | ||
|
||
/** | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.