Divided integration and benchmark scripts
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
@GrabResolver(name='moquette-bintray', root='http://dl.bintray.com/andsel/maven/')
|
||||||
|
@Grab(group='org.dna.mqtt', module='moquette-broker', version='0.6')
|
||||||
|
|
||||||
|
import org.eclipse.moquette.server.Server
|
||||||
|
|
||||||
|
println "Starting broker in embedded mode"
|
||||||
|
Server server = new Server()
|
||||||
|
//Properties props = [port: 1883, host: "0.0.0.0", 'password_file': '../broker/config/password_file.conf'] as Properties
|
||||||
|
Properties props = new Properties()
|
||||||
|
props.setProperty('port', '1883')
|
||||||
|
props.setProperty('host', "0.0.0.0")
|
||||||
|
props.setProperty('password_file', '../broker/config/password_file.conf')
|
||||||
|
println "starting proprs $props"
|
||||||
|
server.startServer(props)
|
||||||
|
println "Stopping broker.."
|
||||||
|
server.stopServer()
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import org.eclipse.paho.client.mqttv3.MqttClient
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttMessage
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttException
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence
|
||||||
|
|
||||||
|
String tmpDir = System.getProperty("java.io.tmpdir")
|
||||||
|
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir)
|
||||||
|
|
||||||
|
MqttClient client = new MqttClient("tcp://localhost:1883", "SampleClient", dataStore)
|
||||||
|
client.connect()
|
||||||
|
println "connected"
|
||||||
|
(1..10).each {
|
||||||
|
//MqttMessage message = new MqttMessage("Hello world!! $it".toString().bytes)
|
||||||
|
String s = "Hello World!! $it"
|
||||||
|
MqttMessage message = new MqttMessage(s.bytes)
|
||||||
|
message.qos = 2
|
||||||
|
message.retained = true
|
||||||
|
print "publishing.."
|
||||||
|
client.getTopic("log").publish(message)
|
||||||
|
println "published"
|
||||||
|
}
|
||||||
|
|
||||||
|
client.disconnect()
|
||||||
|
println "disconnected"
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
@Grab(group='org.fusesource.hawtdb', module='hawtdb', version='1.6')
|
||||||
|
|
||||||
|
import org.fusesource.hawtbuf.codec.StringCodec
|
||||||
|
import org.fusesource.hawtdb.api.IndexFactory
|
||||||
|
import org.fusesource.hawtdb.api.HashIndexFactory
|
||||||
|
import org.fusesource.hawtdb.api.BTreeIndexFactory
|
||||||
|
import org.fusesource.hawtdb.api.PageFile
|
||||||
|
import org.fusesource.hawtdb.api.PageFileFactory
|
||||||
|
import org.fusesource.hawtdb.api.MultiIndexFactory
|
||||||
|
import org.fusesource.hawtdb.api.SortedIndex
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
File tmpFile = new File(System.getProperty("user.home") + File.separator + "test_hawtdb.dat")
|
||||||
|
//tmpFile.createNewFile()
|
||||||
|
|
||||||
|
def writeSomeData(tmpFile) {
|
||||||
|
def factory = new PageFileFactory()
|
||||||
|
factory.file = tmpFile
|
||||||
|
factory.open()
|
||||||
|
def pageFile = factory.getPageFile()
|
||||||
|
MultiIndexFactory multiIndexFactory = new MultiIndexFactory(pageFile);
|
||||||
|
|
||||||
|
BTreeIndexFactory<String, String> indexFactory = new BTreeIndexFactory<String, String>()
|
||||||
|
indexFactory.setKeyCodec(StringCodec.INSTANCE)
|
||||||
|
|
||||||
|
BTreeIndexFactory<String, String> indexFactory2 = new BTreeIndexFactory<String, String>()
|
||||||
|
|
||||||
|
|
||||||
|
SortedIndex<String, String> writeIndex = multiIndexFactory.openOrCreate("subscriptions", indexFactory)
|
||||||
|
SortedIndex<String, String> retainedIndex = multiIndexFactory.openOrCreate("retained", indexFactory2)
|
||||||
|
println "Hawt opened, try to read"
|
||||||
|
println "Size: ${writeIndex.size()}"
|
||||||
|
|
||||||
|
println "write something"
|
||||||
|
writeIndex.put("titolo", "capucetto rosso")
|
||||||
|
retainedIndex.put("autore", "leggenda popolare")
|
||||||
|
pageFile.flush()
|
||||||
|
|
||||||
|
println "check after write content: ${writeIndex.get("titolo")}, size ${writeIndex.size()}"
|
||||||
|
|
||||||
|
//pageFile.close()
|
||||||
|
factory.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def readAllFileContents(tmpFile) {
|
||||||
|
def factory = new PageFileFactory()
|
||||||
|
factory.file = tmpFile
|
||||||
|
factory.open()
|
||||||
|
def pageFile = factory.getPageFile()
|
||||||
|
|
||||||
|
MultiIndexFactory multiIndexFactory = new MultiIndexFactory(pageFile);
|
||||||
|
|
||||||
|
IndexFactory<String, String> indexFactory = new BTreeIndexFactory<String, String>()
|
||||||
|
BTreeIndexFactory<String, String> indexFactory2 = new BTreeIndexFactory<String, String>()
|
||||||
|
indexFactory.setKeyCodec(StringCodec.INSTANCE)
|
||||||
|
|
||||||
|
SortedIndex<String, String> readingIndex = multiIndexFactory.openOrCreate("subscriptions", indexFactory)
|
||||||
|
SortedIndex<String, String> retainedIndex = multiIndexFactory.openOrCreate("retained", indexFactory2)
|
||||||
|
|
||||||
|
println "read something, size ${readingIndex.size()}"
|
||||||
|
for (def entry : readingIndex) {
|
||||||
|
println "read k, v -> ${entry.key}, ${entry.value} "
|
||||||
|
}
|
||||||
|
|
||||||
|
for (def entry : retainedIndex) {
|
||||||
|
println "read k, v -> ${entry.key}, ${entry.value} "
|
||||||
|
}
|
||||||
|
|
||||||
|
factory.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
writeSomeData(tmpFile)
|
||||||
|
readAllFileContents(tmpFile)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
@Grab(group='org.fusesource.hawtdb', module='hawtdb', version='1.6')
|
||||||
|
|
||||||
|
import org.fusesource.hawtbuf.codec.StringCodec
|
||||||
|
import org.fusesource.hawtdb.api.BTreeIndexFactory
|
||||||
|
import org.fusesource.hawtdb.api.PageFile
|
||||||
|
import org.fusesource.hawtdb.api.PageFileFactory
|
||||||
|
import org.fusesource.hawtdb.api.SortedIndex
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
File tmpFile = new File(System.getProperty("user.home") + File.separator + "test_hawtdb.dat")
|
||||||
|
//tmpFile.createNewFile()
|
||||||
|
|
||||||
|
def writeSomeData(tmpFile) {
|
||||||
|
def factory = new PageFileFactory()
|
||||||
|
factory.file = tmpFile
|
||||||
|
factory.open()
|
||||||
|
def pageFile = factory.getPageFile()
|
||||||
|
|
||||||
|
BTreeIndexFactory<String, String> indexFactory = new BTreeIndexFactory<String, String>()
|
||||||
|
indexFactory.setKeyCodec(StringCodec.INSTANCE)
|
||||||
|
|
||||||
|
SortedIndex<String, String> writeIndex = indexFactory.openOrCreate(pageFile)
|
||||||
|
println "Hawt opened, try to read"
|
||||||
|
println "Size: ${writeIndex.size()}"
|
||||||
|
|
||||||
|
println "write something"
|
||||||
|
writeIndex.put("titolo", "capucetto rosso")
|
||||||
|
pageFile.flush()
|
||||||
|
|
||||||
|
println "check after write content: ${writeIndex.get("titolo")}, size ${writeIndex.size()}"
|
||||||
|
|
||||||
|
//pageFile.close()
|
||||||
|
factory.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def readAllFileContents(tmpFile) {
|
||||||
|
def factory = new PageFileFactory()
|
||||||
|
factory.file = tmpFile
|
||||||
|
factory.open()
|
||||||
|
def pageFile = factory.getPageFile()
|
||||||
|
|
||||||
|
BTreeIndexFactory<String, String> indexFactory = new BTreeIndexFactory<String, String>()
|
||||||
|
indexFactory.setKeyCodec(StringCodec.INSTANCE)
|
||||||
|
|
||||||
|
SortedIndex<String, String> readingIndex = indexFactory.openOrCreate(pageFile)
|
||||||
|
|
||||||
|
println "read something, size ${readingIndex.size()}"
|
||||||
|
for (def entry : readingIndex) {
|
||||||
|
println "read k, v -> ${entry.key}, ${entry.value} "
|
||||||
|
}
|
||||||
|
|
||||||
|
factory.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
writeSomeData(tmpFile)
|
||||||
|
readAllFileContents(tmpFile)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
@Grab(group='org.fusesource.hawtdb', module='hawtdb', version='1.6')
|
||||||
|
|
||||||
|
import org.fusesource.hawtbuf.codec.StringCodec
|
||||||
|
import org.fusesource.hawtdb.api.BTreeIndexFactory
|
||||||
|
import org.fusesource.hawtdb.api.PageFile
|
||||||
|
import org.fusesource.hawtdb.api.PageFileFactory
|
||||||
|
import org.fusesource.hawtdb.api.SortedIndex
|
||||||
|
import org.fusesource.hawtbuf.Buffer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
File tmpFile = new File(System.getProperty("user.home") + File.separator + "test_hawtdb.dat")
|
||||||
|
//tmpFile.createNewFile()
|
||||||
|
|
||||||
|
def writeSomeData(tmpFile) {
|
||||||
|
def factory = new PageFileFactory()
|
||||||
|
factory.file = tmpFile
|
||||||
|
factory.open()
|
||||||
|
def pageFile = factory.getPageFile()
|
||||||
|
int pageIdx = pageFile.alloc()
|
||||||
|
pageFile.write(pageIdx, new Buffer("Test".getBytes()))
|
||||||
|
println "writed at page index ${pageIdx}"
|
||||||
|
pageFile.flush()
|
||||||
|
pageFile.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def readTest(tmpFile) {
|
||||||
|
def factory = new PageFileFactory()
|
||||||
|
factory.file = tmpFile
|
||||||
|
factory.open()
|
||||||
|
def pageFile = factory.getPageFile()
|
||||||
|
println "page size is: " + pageFile.pageSize
|
||||||
|
Buffer b = new Buffer(512)
|
||||||
|
pageFile.read(0, b)
|
||||||
|
println "content try read " + b
|
||||||
|
|
||||||
|
pageFile.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
writeSomeData(tmpFile)
|
||||||
|
readTest(tmpFile)
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||||
|
<title>Moquette performances</title>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- 1. Add these JavaScript inclusions in the head of your page -->
|
||||||
|
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
||||||
|
<script type="text/javascript" src="http://code.highcharts.com/highcharts.js"></script>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- 2. Add the JavaScript to initialize the chart on document ready -->
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(document).ready(function() {
|
||||||
|
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
chart: {
|
||||||
|
renderTo: 'container',
|
||||||
|
type: 'line'
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
text: 'Moquette ping-pong Qos0'
|
||||||
|
},
|
||||||
|
xAxis: {
|
||||||
|
categories: [],
|
||||||
|
tickInterval: 100
|
||||||
|
},
|
||||||
|
yAxis: {
|
||||||
|
title: {
|
||||||
|
text: 'num msgs'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
series: []
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
Load the data from the CSV file. This is the contents of the file:
|
||||||
|
|
||||||
|
Apples,Pears,Oranges,Bananas,Plums
|
||||||
|
John,8,4,6,5
|
||||||
|
Jane,3,4,2,3
|
||||||
|
Joe,86,76,79,77
|
||||||
|
Janet,3,16,13,15
|
||||||
|
|
||||||
|
*/
|
||||||
|
//$.get('file:///home/andrea/workspace/moquette/tools_scripts/perf_1000.csv', function(data) {
|
||||||
|
$.get('perf_tmp.csv', function(data) {
|
||||||
|
// Split the lines
|
||||||
|
var lines = data.split('\n');
|
||||||
|
var series = {
|
||||||
|
name: 'Bira',
|
||||||
|
//data: [1, 2, 4, 5, 6]
|
||||||
|
data: [/*[100,1], [200, 2], [300, 4], [400, 5], [500, 6]*/]
|
||||||
|
};
|
||||||
|
$.each(lines, function(lineNo, line) {
|
||||||
|
var items = line.split(',');
|
||||||
|
if (lineNo > 0) {
|
||||||
|
var tick = parseFloat(items[0]);
|
||||||
|
var value = parseFloat(items[1]);
|
||||||
|
if (!isNaN(tick)) {
|
||||||
|
series.data.push([tick, value]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// header line containes categories
|
||||||
|
/*if (lineNo == 0) {
|
||||||
|
$.each(items, function(itemNo, item) {
|
||||||
|
if (itemNo > 0) options.xAxis.categories.push(item);
|
||||||
|
});
|
||||||
|
}*/
|
||||||
|
|
||||||
|
// the rest of the lines contain data with their name in the first position
|
||||||
|
/*else {
|
||||||
|
$.each(items, function(itemNo, item) {
|
||||||
|
if (itemNo == 0) {
|
||||||
|
series.name = item;
|
||||||
|
} else {
|
||||||
|
series.data.push(parseFloat(item));
|
||||||
|
});
|
||||||
|
|
||||||
|
//options.series.push(series);
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
});
|
||||||
|
options.series.push(series);
|
||||||
|
var chart = new Highcharts.Chart(options);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<!-- 3. Add the container -->
|
||||||
|
<div id="container" style="width: 800px; height: 400px; margin: 0 auto"></div>
|
||||||
|
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
@Grab(group='org.fusesource.mqtt-client', module='mqtt-client', version='1.5')
|
||||||
|
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
|
||||||
|
import org.fusesource.mqtt.client.BlockingConnection
|
||||||
|
import org.fusesource.mqtt.client.MQTT
|
||||||
|
import org.fusesource.mqtt.client.Message
|
||||||
|
import org.fusesource.mqtt.client.QoS
|
||||||
|
import org.fusesource.mqtt.client.Topic
|
||||||
|
|
||||||
|
class Reciever {
|
||||||
|
MQTT mqtt = new MQTT()
|
||||||
|
BlockingConnection connection
|
||||||
|
|
||||||
|
public void connectAndSubscribe(boolean cleanSession) {
|
||||||
|
mqtt.setCleanSession(cleanSession)
|
||||||
|
mqtt.setHost("localhost", 1883)
|
||||||
|
|
||||||
|
mqtt.setClientId("reciever")
|
||||||
|
connection = mqtt.blockingConnection()
|
||||||
|
connection.connect()
|
||||||
|
Topic[] topics = [new Topic("/topic", QoS.AT_LEAST_ONCE)]
|
||||||
|
byte[] qoses = connection.subscribe(topics)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reciever r = new Reciever()
|
||||||
|
r.connectAndSubscribe(false)
|
||||||
|
Message message
|
||||||
|
3.times { i ->
|
||||||
|
message = r.connection.receive()
|
||||||
|
println "Topic: ${message.topic}, payload: ${new String(message.payload)}"
|
||||||
|
message.ack()
|
||||||
|
}
|
||||||
|
r.connection.kill()
|
||||||
|
//r.connection.disconnect()
|
||||||
|
|
||||||
|
Thread.sleep(3000)
|
||||||
|
|
||||||
|
r.connectAndSubscribe(false)
|
||||||
|
3.times { i ->
|
||||||
|
message = r.connection.receive()
|
||||||
|
println "Topic: ${message.topic}, payload: ${new String(message.payload)}"
|
||||||
|
message.ack()
|
||||||
|
}
|
||||||
|
r.connection.disconnect()
|
||||||
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
//@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-releases/')
|
||||||
|
@Grab(group='org.fusesource.mqtt-client', module='mqtt-client', version='1.5')
|
||||||
|
|
||||||
|
import java.net.URISyntaxException
|
||||||
|
|
||||||
|
import org.fusesource.mqtt.client.BlockingConnection
|
||||||
|
import org.fusesource.mqtt.client.MQTT
|
||||||
|
import org.fusesource.mqtt.client.QoS
|
||||||
|
|
||||||
|
|
||||||
|
MQTT mqtt = new MQTT()
|
||||||
|
//mqtt.setHost("test.mosquitto.org", 1883);
|
||||||
|
mqtt.setHost("localhost", 1883)
|
||||||
|
|
||||||
|
BlockingConnection connection = mqtt.blockingConnection()
|
||||||
|
connection.connect()
|
||||||
|
|
||||||
|
//for (int i = 0; i < 1000; i++) {
|
||||||
|
(1..1000).each{ i ->
|
||||||
|
// LOG.info("Publishing");
|
||||||
|
String payload = "Hello world MQTT!!${i}"
|
||||||
|
connection.publish("/topic", payload.getBytes(), QoS.AT_LEAST_ONCE, false)
|
||||||
|
println payload
|
||||||
|
Thread.sleep 1000
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.disconnect()
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
@Grab(group='org.fusesource.mqtt-client', module='mqtt-client', version='1.5')
|
||||||
|
|
||||||
|
import org.fusesource.mqtt.client.BlockingConnection
|
||||||
|
import org.fusesource.mqtt.client.MQTT
|
||||||
|
import org.fusesource.mqtt.client.Message
|
||||||
|
import org.fusesource.mqtt.client.QoS
|
||||||
|
import org.fusesource.mqtt.client.Topic
|
||||||
|
|
||||||
|
String host = args[0]
|
||||||
|
MQTT mqtt = new MQTT()
|
||||||
|
mqtt.setHost(host, 1883)
|
||||||
|
mqtt.setCleanSession(true)
|
||||||
|
mqtt.setClientId("SubscriberClient")
|
||||||
|
|
||||||
|
BlockingConnection subscriber = mqtt.blockingConnection()
|
||||||
|
subscriber.connect()
|
||||||
|
println "Connected to ${host}"
|
||||||
|
|
||||||
|
Topic[] topics = [new Topic("/topic", QoS.AT_MOST_ONCE)]
|
||||||
|
byte[] qoses = subscriber.subscribe(topics)
|
||||||
|
println "Subscribed to ${topics}"
|
||||||
|
|
||||||
|
//start a publisher
|
||||||
|
MQTT mqtt2 = new MQTT()
|
||||||
|
mqtt2.setHost(host, 1883)
|
||||||
|
BlockingConnection publisher = mqtt2.blockingConnection()
|
||||||
|
publisher.connect()
|
||||||
|
publisher.publish("/topic", 'Hello world!!'.bytes, QoS.AT_MOST_ONCE, false)
|
||||||
|
|
||||||
|
//check subscriber received
|
||||||
|
Message message = subscriber.receive()
|
||||||
|
assert "/topic" == message.topic
|
||||||
|
assert "Hello world!!" == new String(message.payload)
|
||||||
|
message.ack()
|
||||||
|
|
||||||
|
println "shutdown subscriber"
|
||||||
|
subscriber.disconnect()
|
||||||
|
|
||||||
|
println "shutdown publisher"
|
||||||
|
publisher.disconnect()
|
||||||
|
|
||||||
|
println "Kill your broker and start it again, then trivialPublish.groovy"
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
@Grab(group='org.fusesource.mqtt-client', module='mqtt-client', version='1.5')
|
||||||
|
|
||||||
|
import org.fusesource.mqtt.client.BlockingConnection
|
||||||
|
import org.fusesource.mqtt.client.MQTT
|
||||||
|
import org.fusesource.mqtt.client.Message
|
||||||
|
import org.fusesource.mqtt.client.QoS
|
||||||
|
import org.fusesource.mqtt.client.Topic
|
||||||
|
|
||||||
|
//test case moquette 0.7 update for 2015-1-26
|
||||||
|
//client eclipse.paht.javaclient version :1.0.1
|
||||||
|
String host = args.size() > 0 ? args[0] : 'localhost'
|
||||||
|
|
||||||
|
MQTT mqttPub = new MQTT()
|
||||||
|
mqttPub.setHost(host, 1883)
|
||||||
|
mqttPub.setCleanSession(false)
|
||||||
|
mqttPub.setClientId("PublisherClient")
|
||||||
|
|
||||||
|
MQTT mqttSub = new MQTT()
|
||||||
|
mqttSub.setHost(host, 1883)
|
||||||
|
mqttSub.setCleanSession(false)
|
||||||
|
mqttSub.setClientId("FlapSubscriberClient")
|
||||||
|
|
||||||
|
BlockingConnection publisher = mqttPub.blockingConnection()
|
||||||
|
publisher.connect()
|
||||||
|
println "Publisher connected to ${host}"
|
||||||
|
|
||||||
|
BlockingConnection subscriber = mqttSub.blockingConnection()
|
||||||
|
subscriber.connect()
|
||||||
|
println "${subscriber} connected to ${host}"
|
||||||
|
Topic[] topics = [new Topic("/topic", QoS.AT_MOST_ONCE)]
|
||||||
|
byte[] qoses = subscriber.subscribe(topics)
|
||||||
|
println "${subscriber} Subscribed to ${topics}"
|
||||||
|
|
||||||
|
subscriber.disconnect()
|
||||||
|
println "${subscriber} disconnects immediately"
|
||||||
|
|
||||||
|
println "${publisher} publishes QoS 2 messages on /topic"
|
||||||
|
publisher.publish("/topic", 'Hello world!!'.bytes, QoS.EXACTLY_ONCE, false)
|
||||||
|
|
||||||
|
|
||||||
|
subscriber = mqttSub.blockingConnection()
|
||||||
|
subscriber.connect()
|
||||||
|
println "${subscriber} reconnects to ${host}"
|
||||||
|
|
||||||
|
Message message = subscriber.receive()
|
||||||
|
assert "/topic" == message.topic
|
||||||
|
String msgBody = new String(message.payload)
|
||||||
|
assert "Hello world!!" == msgBody
|
||||||
|
message.ack()
|
||||||
|
println "${subscriber} received on topic ${message.topic} the message \n\t ${msgBody}"
|
||||||
|
subscriber.disconnect()
|
||||||
|
println "${subscriber} disconnects again"
|
||||||
|
|
||||||
|
subscriber = mqttSub.blockingConnection()
|
||||||
|
subscriber.connect()
|
||||||
|
println "${subscriber} reconnects again to ${host}"
|
||||||
|
|
||||||
|
println "Wait receive something..."
|
||||||
|
message = subscriber.receive()
|
||||||
|
msgBody = new String(message.payload)
|
||||||
|
assert "Hello world!!" == msgBody
|
||||||
|
message.ack()
|
||||||
|
println "${subscriber} received again on topic ${message.topic} the message \n\t ${msgBody}"
|
||||||
|
|
||||||
|
//aclient (clear session false)
|
||||||
|
//bclient (clear session false)
|
||||||
|
//
|
||||||
|
//a -> connection Server
|
||||||
|
//b -> connection Server
|
||||||
|
//b -> subscriptions (qos =0)
|
||||||
|
//b -> distconnection or lostconnection
|
||||||
|
//
|
||||||
|
//a ->publish ("abc/topic","test1",qos=2,false);
|
||||||
|
//
|
||||||
|
//...
|
||||||
|
//a ->publish ("abc/topic","test10",qos=2,false);
|
||||||
|
//
|
||||||
|
//b -> connection Server
|
||||||
|
//received message test1
|
||||||
|
//...
|
||||||
|
//received message test10
|
||||||
|
//
|
||||||
|
//b-> distconnection Server or loast connection
|
||||||
|
//b->connnection Server
|
||||||
|
//received message test1
|
||||||
|
//...
|
||||||
|
//received message test10
|
||||||
|
//
|
||||||
|
//why repeat received message
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-releases/')
|
||||||
|
@Grab(group='org.eclipse.paho', module='org.eclipse.paho.client.mqttv3', version='1.0.1', ext='jar')
|
||||||
|
|
||||||
|
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttClient
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttMessage
|
||||||
|
import org.eclipse.paho.client.mqttv3.MqttException
|
||||||
|
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence
|
||||||
|
|
||||||
|
String host = args[0]
|
||||||
|
|
||||||
|
String tmpDir = System.getProperty("java.io.tmpdir")
|
||||||
|
MqttDefaultFilePersistence dataStore1 = new MqttDefaultFilePersistence(tmpDir + "/client1")
|
||||||
|
MqttClient client1 = new MqttClient("tcp://${host}:1883", "ConnectedClient", dataStore1)
|
||||||
|
client1.connect()
|
||||||
|
println "Client1 connected"
|
||||||
|
sleep(3000)
|
||||||
|
|
||||||
|
//now connect a second client but with the same ID
|
||||||
|
MqttDefaultFilePersistence dataStore2 = new MqttDefaultFilePersistence(tmpDir + "/client2")
|
||||||
|
MqttClient client2 = new MqttClient("tcp://${host}:1883", "ConnectedClient", dataStore2)
|
||||||
|
client2.connect()
|
||||||
|
println "Client2 connected"
|
||||||
|
try {
|
||||||
|
client1.subscribe("topic", 2)
|
||||||
|
println "BAD! Client1 subscribed to topic but its session has to be dropped out by client2"
|
||||||
|
} catch (Exception ex) {
|
||||||
|
println "Ok, client1 has been dropped out"
|
||||||
|
}
|
||||||
|
sleep(3000)
|
||||||
|
|
||||||
|
//now connect a second client but with the same ID
|
||||||
|
MqttDefaultFilePersistence dataStorePublisher = new MqttDefaultFilePersistence(tmpDir + "/publisher")
|
||||||
|
MqttClient publisher = new MqttClient("tcp://${host}:1883", "Publisher", dataStorePublisher)
|
||||||
|
publisher.connect()
|
||||||
|
println "publisher connected"
|
||||||
|
client2.subscribe("topic", 2)
|
||||||
|
println "Client2 subscribed to topic"
|
||||||
|
sleep(3000)
|
||||||
|
|
||||||
|
print "Publisher is going to publish.."
|
||||||
|
publisher.publish('topic', 'Hello world!!'.bytes, 2, false)
|
||||||
|
println "Ok mate, done!"
|
||||||
|
|
||||||
|
try {
|
||||||
|
client1.disconnect()
|
||||||
|
println "BAD! Client1 has been able to call disconnect, but it has to be already dropped out"
|
||||||
|
} catch (Exception ex) {
|
||||||
|
println "Ok, client1 can't disconnect because it was already disconnected"
|
||||||
|
}
|
||||||
|
|
||||||
|
client2.disconnect()
|
||||||
|
println "Client2 disconnected"
|
||||||
|
|
||||||
|
publisher.disconnect()
|
||||||
|
println "publisher disconnected"
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
@Grab(group='org.eclipse.jetty.websocket', module='websocket-client', version='9.2.0.M1', ext='jar')
|
||||||
|
@Grab(group='org.slf4j', module='slf4j-api', version='1.7.7')
|
||||||
|
@Grab(group='org.slf4j', module='slf4j-simple', version='1.7.7')
|
||||||
|
|
||||||
|
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.eclipse.jetty.websocket.client.WebSocketClient;
|
||||||
|
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
|
||||||
|
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("onClose - Connection closed: %d - %s%n", statusCode, reason);
|
||||||
|
this.session = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketConnect
|
||||||
|
public void onConnect(Session session) throws IOException {
|
||||||
|
System.out.println("onConnect - Got connect");
|
||||||
|
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
|
||||||
|
|
||||||
|
byte[] rawMsg = [ 0x10, //message type
|
||||||
|
0x11, //remaining length 17 bytes
|
||||||
|
0x00, 0x06, //MQIspd length
|
||||||
|
'M' as char, 'Q' as char, 'I' as char, 's' as char,'d' as char, 'p' as char,
|
||||||
|
0x03, //protocol version
|
||||||
|
0x32, //flags
|
||||||
|
0x00, 0x0A,// keepAlive Timer, 10 seconds
|
||||||
|
0x00, 0x03, 0x41, 0x42, 0x43 // A, B, C as client ID
|
||||||
|
] as byte[]
|
||||||
|
ByteBuffer msg = ByteBuffer.wrap(rawMsg);
|
||||||
|
|
||||||
|
//DBG
|
||||||
|
println "onConnect - send bytes " + rawMsg.encodeHex().toString()
|
||||||
|
//DBG
|
||||||
|
|
||||||
|
// try {
|
||||||
|
session.getRemote().sendBytes(msg);
|
||||||
|
println("onConnect - Sent connect bytes");
|
||||||
|
this.connectSentLatch.countDown();
|
||||||
|
//session.close(StatusCode.NORMAL, "I'm done");
|
||||||
|
println("onConnect - After connect");
|
||||||
|
|
||||||
|
println "send disconnect"
|
||||||
|
disconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean awaitConnected(int duration, TimeUnit unit) throws InterruptedException {
|
||||||
|
return this.connectSentLatch.await(duration, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a disconnect message to the server
|
||||||
|
* */
|
||||||
|
public void disconnect() {
|
||||||
|
byte[] rawMsg = [0xE0, 0x00] as byte[]
|
||||||
|
println "disconnect - send bytes " + rawMsg.encodeHex().toString()
|
||||||
|
ByteBuffer msg = ByteBuffer.wrap(rawMsg);
|
||||||
|
this.session.getRemote().sendBytes(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onMessage(Session session, byte[] buf, int offset, int length) {
|
||||||
|
System.out.printf("onMessage - Got msg: %s%n", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketError
|
||||||
|
public void onError(Session session, Throwable cause) {
|
||||||
|
System.out.printf("onError - Got exception: %s%n", cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
String destUri = "ws://localhost:8080/mqtt"
|
||||||
|
//String destUri = "ws://test.mosquitto.org:8080/"
|
||||||
|
WebSocketClient client = new WebSocketClient()
|
||||||
|
MQTTWebSocket socket = new MQTTWebSocket();
|
||||||
|
client.start();
|
||||||
|
URI echoUri = new URI(destUri);
|
||||||
|
ClientUpgradeRequest request = new ClientUpgradeRequest();
|
||||||
|
client.connect(socket, echoUri, request);
|
||||||
|
println "main - Connecting to : ${echoUri}"
|
||||||
|
boolean connected = socket.awaitConnected(4, TimeUnit.SECONDS);
|
||||||
|
println "main - After wait for awaitConnected was ${connected}"
|
||||||
|
|
||||||
|
//println "send disconnect"
|
||||||
|
//socket.disconnect()
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<!--script type="text/javascript" src="paho.javascript-1.0.0/mqttws31.js"></script-->
|
||||||
|
<script type="application/javascript" src="http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.javascript.git/plain/src/mqttws31.js"></script>
|
||||||
|
<!--script type="text/javascript" src="file:///home/andrea/workspace/moquette_github/mqttws31.js"></script-->
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
// Create a client instance
|
||||||
|
var client;
|
||||||
|
function doConnect() {
|
||||||
|
console.log("connect invoked");
|
||||||
|
client = new Paho.MQTT.Client("127.0.0.1", Number(8080), "", "clientId");
|
||||||
|
//client = new Paho.MQTT.Client("test.mosquitto.org", Number(8080), "", "clientId");
|
||||||
|
//client = new Paho.MQTT.Client("ws://whistler1.hursley.ibm.com", Number(1883), "ClientId");
|
||||||
|
|
||||||
|
client.startTrace();
|
||||||
|
// set callback handlers
|
||||||
|
client.onConnectionLost = onConnectionLost;
|
||||||
|
client.onMessageArrived = onMessageArrived;
|
||||||
|
|
||||||
|
// connect the client
|
||||||
|
client.connect({onSuccess:onConnect});
|
||||||
|
console.log("connect finished");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// called when the client connects
|
||||||
|
function onConnect() {
|
||||||
|
// Once a connection has been made, make a subscription and send a message.
|
||||||
|
console.log("onConnect");
|
||||||
|
client.subscribe("/World");
|
||||||
|
message = new Paho.MQTT.Message("Hello");
|
||||||
|
message.destinationName = "/World";
|
||||||
|
client.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// called when the client loses its connection
|
||||||
|
function onConnectionLost(responseObject) {
|
||||||
|
if (responseObject.errorCode !== 0) {
|
||||||
|
console.log("onConnectionLost:"+responseObject.errorMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// called when a message arrives
|
||||||
|
function onMessageArrived(message) {
|
||||||
|
console.log("onMessageArrived:"+message.payloadString);
|
||||||
|
}
|
||||||
|
|
||||||
|
function doSubscribe() {
|
||||||
|
client.subscribe("/World");
|
||||||
|
}
|
||||||
|
|
||||||
|
function doDisconnect() {
|
||||||
|
client.disconnect();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Example Web Messaging web page.</h1>
|
||||||
|
<form id="example">
|
||||||
|
<fieldset>
|
||||||
|
<legend id="Connect" > Connect </legend>
|
||||||
|
Make a connection to the server, and set up a call back used if a
|
||||||
|
message arrives for this client.
|
||||||
|
<br>
|
||||||
|
<input type="button" value="Connect" onClick="doConnect(this.form)" name="Connect"/>
|
||||||
|
<input type="checkbox" name="connected" disabled="disabled"/>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<fieldset>
|
||||||
|
<legend id="Subscribe" > Subscribe </legend>
|
||||||
|
Make a subscription to topic "/World".
|
||||||
|
<br> <input type="button" value="Subscribe" onClick="doSubscribe(this.form)"/>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<fieldset>
|
||||||
|
<legend id="Send" > Send </legend>
|
||||||
|
Create a Message object containing the word "Hello" and then publish it at
|
||||||
|
the server.
|
||||||
|
<br>
|
||||||
|
<input type="button" value="Send" onClick="doSend(this.form)"/>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<fieldset>
|
||||||
|
<legend id="Receive" > Receive </legend>
|
||||||
|
A copy of the published Message is received in the callback we created earlier.
|
||||||
|
<textarea name="receiveMsg" rows="1" cols="40" disabled="disabled"></textarea>
|
||||||
|
</fieldset>
|
||||||
|
|
||||||
|
<fieldset>
|
||||||
|
<legend id="Disconnect" > Disconnect </legend>
|
||||||
|
Now disconnect this client from the server.
|
||||||
|
<br> <input type="button" value="Disconnect" onClick="doDisconnect()"/>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user