forked from jvermillard/m3da-server
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- just TCP for now and no security - test and examples are missing
- Loading branch information
1 parent
c93e8ef
commit d4e3d47
Showing
4 changed files
with
185 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2013 Sierra Wireless. | ||
All rights reserved. This program and the accompanying materials | ||
are made available under the terms of the Eclipse Public License v1.0 | ||
which accompanies this distribution, and is available at | ||
http://www.eclipse.org/legal/epl-v10.html | ||
Contributors: | ||
Sierra Wireless - initial API and implementation | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>m3da</groupId> | ||
<artifactId>m3da-parent</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>m3da</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>M3DA client</name> | ||
<artifactId>m3da-client</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>m3da</groupId> | ||
<artifactId>m3da-codec</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<!-- TEST DEPENDENCIES --> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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,30 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2013 Sierra Wireless. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Sierra Wireless - initial API and implementation | ||
******************************************************************************/ | ||
package m3da.client; | ||
|
||
import java.io.IOException; | ||
|
||
import m3da.codec.DecoderException; | ||
import m3da.codec.dto.M3daEnvelope; | ||
|
||
/** | ||
* | ||
* M3DA client for sending and receiving {@link M3daEnvelope} from a server. | ||
* | ||
*/ | ||
public interface M3daClient { | ||
|
||
public void connect() throws IOException; | ||
|
||
public M3daEnvelope sendEnvelope(M3daEnvelope envelope) throws IOException, DecoderException; | ||
|
||
public void close() throws IOException; | ||
} |
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,107 @@ | ||
package m3da.client; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import m3da.codec.DecoderException; | ||
import m3da.codec.DecoderOutput; | ||
import m3da.codec.EnvelopeDecoder; | ||
import m3da.codec.EnvelopeEncoder; | ||
import m3da.codec.M3daCodecService; | ||
import m3da.codec.dto.M3daEnvelope; | ||
import m3da.codec.impl.M3daCodecServiceImpl; | ||
|
||
/** | ||
* M3DA TCP client. Use it for connecting to a M3DA server. | ||
* <p> | ||
* Usage : | ||
* | ||
* <pre> | ||
* M3daTcpClient client = new M3daTcpClient("m2m.eclipse.org", 44900); | ||
* client.connect(); | ||
* M3daEnvelope env = new M3daEnvelope(); | ||
* .. | ||
* | ||
* M3daEnvelope receivedEnv = client.send(env); | ||
* client.close(); | ||
* </pre> | ||
*/ | ||
public class M3daTcpClient implements M3daClient { | ||
|
||
private final String host; | ||
|
||
private final int port; | ||
|
||
private final EnvelopeEncoder encoder; | ||
|
||
private final EnvelopeDecoder decoder; | ||
|
||
private final byte[] readBuffer = new byte[65536]; | ||
|
||
private Socket socket; | ||
|
||
/** | ||
* Create a TCP M3DA client. | ||
* | ||
* @param host the server hostname or IP address | ||
* @param port the TCP port to use (should be 44900) | ||
*/ | ||
public M3daTcpClient(String host, int port) { | ||
this.host = host; | ||
this.port = port; | ||
|
||
M3daCodecService codec = new M3daCodecServiceImpl(); | ||
encoder = codec.createEnvelopeEncoder(); | ||
decoder = codec.createEnvelopeDecoder(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void connect() throws IOException { | ||
socket = new Socket(host, port); | ||
socket.setSoTimeout(2000); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public M3daEnvelope sendEnvelope(M3daEnvelope envelope) throws IOException, DecoderException { | ||
ByteBuffer buffer = encoder.encode(envelope); | ||
socket.getOutputStream().write(buffer.array()); | ||
|
||
final List<M3daEnvelope> list = new ArrayList<M3daEnvelope>(); | ||
final DecoderOutput<M3daEnvelope> output = new DecoderOutput<M3daEnvelope>() { | ||
@Override | ||
public void decoded(final M3daEnvelope pdu) { | ||
list.add(pdu); | ||
} | ||
}; | ||
|
||
do { | ||
// read some bytes | ||
final int bytes = socket.getInputStream().read(readBuffer); | ||
if (bytes <= 0) { | ||
throw new RuntimeException("connection closed"); | ||
} | ||
decoder.decodeAndAccumulate(ByteBuffer.wrap(readBuffer, 0, bytes), output); | ||
} while (list.size() == 0); | ||
decoder.finishDecode(); | ||
|
||
return list.get(0); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void close() throws IOException { | ||
socket.close(); | ||
} | ||
|
||
} |
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