Rearchitected the broker, switching from ProtocolProcessor and publisher to MQTTConnection, Session and PostOffice

This commit is contained in:
andsel
2018-11-02 12:33:59 +01:00
parent 0ad6ffdd3c
commit a2266e5bbe
60 changed files with 5225 additions and 1025 deletions
+8 -4
View File
@@ -1,14 +1,16 @@
//@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-snapshots/')
//@Grab(group='org.eclipse.paho', module='org.eclipse.paho.client.mqttv3', version='1.0.1-SNAPSHOT')
//@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-releases/')
//@Grab(group='org.eclipse.paho', module='mqtt-client', version='0.4.0')
@GrabResolver(name='Paho', root='https://repo.eclipse.org/content/repositories/paho-releases/')
@Grab(group='org.eclipse.paho', module='mqtt-client', version='0.4.0')
@Grab(group='org.eclipse.paho', module='org.eclipse.paho.client.mqttv3', version='1.2.0')
import org.eclipse.paho.client.mqttv3.MqttAsyncClient
import org.eclipse.paho.client.mqttv3.MqttMessage
import org.eclipse.paho.client.mqttv3.MqttException
import org.eclipse.paho.client.mqttv3.MqttCallback
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence
//import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
import java.util.concurrent.CountDownLatch
import static groovy.json.JsonOutput.*
import java.nio.ByteBuffer
@@ -71,7 +73,7 @@ def publishQos0(client, long numMessages) {
print "publishing.."
long startTime = System.currentTimeMillis()
(1..numMessages).each {
byte[] bytes = ByteBuffer.allocate(8).putLong(System.currentTimeMillis()).array();
byte[] bytes = ByteBuffer.allocate(8).putLong(System.currentTimeMillis()).array()
client.publish('topic', bytes, 0, false)
}
client.publish('/exit', 'Exit'.bytes, 0, false)
@@ -82,6 +84,7 @@ def publishQos0(client, long numMessages) {
if (args.size() < 2) {
println "Usage benchmarker <host> <numMessages>"
println "groovy benchmarker.groovy localhost 1000 500 test1"
return
}
@@ -89,12 +92,13 @@ String host = args[0]
long numMessages = args[1] as long
String tmpDir = System.getProperty("java.io.tmpdir")
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir)
//MemoryPersistence dataStore = new MemoryPersistence()
int rnd = (Math.random() * 100) as int
MqttAsyncClient client = new MqttAsyncClient("tcp://${host}:1883", "SubscriberClient${rnd}", dataStore)
def callback = new SubscriberCallback()
client.callback = callback
client.connect().waitForCompletion(1000);
client.connect().waitForCompletion(1000)
client.subscribe("topic", 0)
println "subscribed to topic"
client.subscribe("/exit", 0)
@@ -0,0 +1,96 @@
@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.2.0', ext='jar')
import org.eclipse.paho.client.mqttv3.MqttClient
import org.eclipse.paho.client.mqttv3.MqttMessage
import org.eclipse.paho.client.mqttv3.MqttCallback
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
/**
* A client that try all the simple MQTT commands
* */
if (args.size() != 2) {
println "usage: <host> <local client name>"
return
}
class SubscriberCallback implements MqttCallback {
private CountDownLatch m_latch = new CountDownLatch(1)
void waitFinish() {
boolean reachZero = m_latch.await(4, TimeUnit.SECONDS)
if (!reachZero) {
println "Expired the time to receive the publish"
}
}
void renewWaiter() {
m_latch = new CountDownLatch(1)
}
void messageArrived(String topic, MqttMessage message) throws Exception {
println "Received on [$topic] message: [$message]"
m_latch.countDown()
}
void deliveryComplete(IMqttDeliveryToken token) {
println "Delivery complete"
}
void connectionLost(Throwable th) {
println "Connection lost"
m_latch.countDown()
}
}
String host = args[0]
String clientName = args[1]
String tmpDir = System.getProperty("java.io.tmpdir")
MqttDefaultFilePersistence dataStore1 = new MqttDefaultFilePersistence(tmpDir + "/" + clientName)
MqttClient client1 = new MqttClient("tcp://${host}:1883", "ConCli${clientName}", dataStore1)
def callback = new SubscriberCallback()
client1.callback = callback
println "start test"
print "connect..."
client1.connect()
println "OK!"
print "subscribe..."
client1.subscribe("/news", 0)
println "OK!"
/*print "publish qos0..."
client1.publish("/news", "Moquette is going to big refactoring, QoS0".bytes, 0, false)
callback.waitFinish()
println "OK!"
callback.renewWaiter()
print "publish qos1..."
client1.publish("/news", "Moquette is going to big refactoring, QoS1".bytes, 1, false)
callback.waitFinish()
println "OK!"
callback.renewWaiter()*/
print "publish qos2..."
client1.publish("/news", "Moquette is going to big refactoring, QoS2".bytes, 2, false)
callback.waitFinish()
println "OK!"
print "unsubscribe..."
client1.unsubscribe("/news")
println "OK!"
print "disconnect..."
client1.disconnect()
println "OK!"
println "Done"
System.exit(0)
@@ -13,7 +13,7 @@ import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence
* */
if (args.size() != 3) {
println "usage: <host> <num_serial_connctions> <local client name>"
println "usage: <host> <num_serial_connections> <local client name>"
return
}
@@ -31,4 +31,4 @@ println "start loppoing to ${numSerialConnection} conn-discon"
client1.disconnect()
print "."
}
println "Done"
println "Done"
@@ -0,0 +1,19 @@
@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.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"
println "Ok mate, done!"
client1.disconnect()
println "Client1 disconnected"