Added websocket connector

This commit is contained in:
selva.andre
2014-05-18 12:02:18 +02:00
parent 17548eeff7
commit 6755723e0b
10 changed files with 275 additions and 24 deletions
@@ -0,0 +1,83 @@
package org.dna.mqtt.moquette.server;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author andrea
*/
@WebSocket
public class MQTTWebSocket {
private static final Logger LOG = LoggerFactory.getLogger(MQTTWebSocket.class);
private Session session;
private final CountDownLatch connectSentLatch;
public MQTTWebSocket() {
this.connectSentLatch = new CountDownLatch(1);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.printf("Connection closed: %d - %s%n", statusCode, reason);
this.session = null;
}
@OnWebSocketConnect
public void onConnect(Session session) throws IOException {
System.out.printf("Got connect: %s%n", session);
LOG.info("Got connect: %s%n", session);
this.session = session;
//Trivial MQTT Connect message
// assertEquals(12, out.readByte()); //remaining length
// verifyString("MQIsdp", out);
// assertEquals(0x03, out.readByte()); //protocol version
// assertEquals(0x32, out.readByte()); //flags
// assertEquals(2, out.readByte()); //keepAliveTimer msb
// assertEquals(0, out.readByte()); //keepAliveTimer lsb
ByteBuffer msg = ByteBuffer.wrap(new byte[] { 0x10, //message type
0x0C, //remaining lenght 12 bytes
'M', 'Q','I','s','d','p',
0x03, //protocol version
0x32, //flags
0x02, 0x00// keepAlive Timer, 2 seconds
});
// try {
session.getRemote().sendBytes(msg);
this.connectSentLatch.countDown();
session.close(StatusCode.NORMAL, "I'm done");
// } catch (IOException t) {
// LOG.error(null, t);
// }
}
public boolean awaitConnected(int duration, TimeUnit unit) throws InterruptedException {
return this.connectSentLatch.await(duration, unit);
}
@OnWebSocketMessage
public void onMessage(Session session, byte[] buf, int offset, int length) {
System.out.printf("Got msg: %s%n", buf);
}
@OnWebSocketError
public void onError(Session session, Throwable cause) {
System.out.printf("Got exception: %s%n", cause);
}
}
@@ -64,7 +64,7 @@ public class ServerIntegrationFuseTest {
}
@Test
public void connectWithCredetials() throws Exception {
public void connectWithCredentials() throws Exception {
LOG.info("*** connectWithCredetials ***");
MQTT mqtt = new MQTT();
mqtt.setHost("localhost", 1883);
@@ -0,0 +1,66 @@
package org.dna.mqtt.moquette.server;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.dna.mqtt.commons.Constants;
import org.junit.After;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Integration test to check the function of Moquette with a WebSocket channel.
*
* @author andrea
*/
public class ServerIntegrationWebSocketTest {
private static final Logger LOG = LoggerFactory.getLogger(ServerIntegrationPahoTest.class);
Server m_server;
WebSocketClient client;
protected void startServer() throws IOException {
m_server = new Server();
m_server.startServer();
}
@Before
public void setUp() throws Exception {
startServer();
client = new WebSocketClient();
}
@After
public void tearDown() throws Exception {
client.stop();
m_server.stopServer();
File dbFile = new File(Server.STORAGE_FILE_PATH);
if (dbFile.exists()) {
dbFile.delete();
}
}
@Test
public void checkPlainConnect() throws Exception {
LOG.info("*** checkPlainConnect ***");
String destUri = "ws://localhost:" + Constants.WEBSOCKET_PORT;
MQTTWebSocket socket = new MQTTWebSocket();
client.start();
URI echoUri = new URI(destUri);
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket, echoUri, request);
LOG.info("Connecting to : %s%n", echoUri);
boolean connected = socket.awaitConnected(4, TimeUnit.SECONDS);
assertTrue(connected);
}
}