Skip to content

Commit

Permalink
issue #8 simpe M3DA client:
Browse files Browse the repository at this point in the history
 - just TCP for now and no security
 - test and examples are missing
  • Loading branch information
jvermillard committed Mar 29, 2013
1 parent c93e8ef commit d4e3d47
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
47 changes: 47 additions & 0 deletions client/pom.xml
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>
30 changes: 30 additions & 0 deletions client/src/main/java/m3da/client/M3daClient.java
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;
}
107 changes: 107 additions & 0 deletions client/src/main/java/m3da/client/M3daTcpClient.java
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();
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<modules>
<module>codec</module>
<module>server</module>
<module>client</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit d4e3d47

Please sign in to comment.