use of org.fusesource.mqtt.codec.MQTTFrame in project activemq-artemis by apache.
the class MQTTTest method testUniqueMessageIds.
@Test(timeout = 60 * 1000)
public void testUniqueMessageIds() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo");
mqtt.setKeepAlive((short) 2);
mqtt.setCleanSession(true);
final List<PUBLISH> publishList = new ArrayList<>();
mqtt.setTracer(new Tracer() {
@Override
public void onReceive(MQTTFrame frame) {
LOG.info("Client received:\n" + frame);
if (frame.messageType() == PUBLISH.TYPE) {
PUBLISH publish = new PUBLISH();
try {
publish.decode(frame);
} catch (ProtocolException e) {
fail("Error decoding publish " + e.getMessage());
}
publishList.add(publish);
}
}
@Override
public void onSend(MQTTFrame frame) {
LOG.info("Client sent:\n" + frame);
}
});
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
// create overlapping subscriptions with different QoSs
QoS[] qoss = { QoS.AT_MOST_ONCE, QoS.AT_LEAST_ONCE, QoS.EXACTLY_ONCE };
final String TOPIC = "TopicA/";
// publish retained message
connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, true);
String[] subs = { TOPIC, "TopicA/#", "TopicA/+" };
for (int i = 0; i < qoss.length; i++) {
connection.subscribe(new Topic[] { new Topic(subs[i], qoss[i]) });
}
// publish non-retained message
connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
int received = 0;
Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
do {
assertNotNull(msg);
assertEquals(TOPIC, new String(msg.getPayload()));
msg.ack();
int waitCount = 0;
while (publishList.size() <= received && waitCount < 10) {
Thread.sleep(1000);
waitCount++;
}
msg = connection.receive(5000, TimeUnit.MILLISECONDS);
} while (msg != null && received++ < subs.length * 2);
assertEquals("Unexpected number of messages", subs.length * 2, received + 1);
// AT_MOST_ONCE
for (int i = 0; i < publishList.size(); i++) {
for (int j = i + 1; j < publishList.size(); j++) {
final PUBLISH publish1 = publishList.get(i);
final PUBLISH publish2 = publishList.get(j);
boolean qos0 = false;
if (publish1.qos() == QoS.AT_MOST_ONCE) {
qos0 = true;
assertEquals(0, publish1.messageId());
}
if (publish2.qos() == QoS.AT_MOST_ONCE) {
qos0 = true;
assertEquals(0, publish2.messageId());
}
if (!qos0) {
assertNotEquals(publish1.messageId(), publish2.messageId());
}
}
}
connection.unsubscribe(subs);
connection.disconnect();
}
use of org.fusesource.mqtt.codec.MQTTFrame in project fabric8 by jboss-fuse.
the class MqttProtocol method snoopConnectionParameters.
@Override
public void snoopConnectionParameters(final SocketWrapper socket, final Buffer received, final Handler<ConnectionParameters> handler) {
final MqttProtocolDecoder h = new MqttProtocolDecoder(this);
h.errorHandler(new Handler<String>() {
@Override
public void handle(String error) {
LOG.info("STOMP protocol decoding error: " + error);
socket.close();
}
});
h.codecHandler(new Handler<MQTTFrame>() {
@Override
public void handle(MQTTFrame event) {
try {
if (event.messageType() == org.fusesource.mqtt.codec.CONNECT.TYPE) {
CONNECT connect = new CONNECT().decode(event);
ConnectionParameters parameters = new ConnectionParameters();
if (connect.clientId() != null) {
parameters.protocolClientId = connect.clientId().toString();
}
if (connect.userName() != null) {
parameters.protocolUser = connect.userName().toString();
// containing the virtual host info.
if (parameters.protocolUser.contains("/")) {
// Strip off the virtual host part of the username..
String[] parts = parameters.protocolUser.split("/", 2);
parameters.protocolVirtualHost = parts[0];
parameters.protocolUser = parts[1];
// Update the connect frame to strip out the virtual host from the username field...
connect.userName(new UTF8Buffer(parameters.protocolUser));
// re-write the received buffer /w the updated connect frame
Buffer tail = received.getBuffer((int) h.getBytesDecoded(), received.length());
BufferSupport.setLength(received, 0);
append(received, connect.encode());
received.appendBuffer(tail);
}
}
handler.handle(parameters);
} else {
LOG.info("Expected a CONNECT frame");
socket.close();
}
} catch (java.net.ProtocolException e) {
LOG.info("Invalid MQTT frame: " + e, e);
socket.close();
}
}
});
socket.readStream().dataHandler(h);
h.handle(received);
}
use of org.fusesource.mqtt.codec.MQTTFrame in project fabric8 by jboss-fuse.
the class MqttProtocol method append.
static void append(Buffer self, MQTTFrame value) {
MQTTFrame frame = (MQTTFrame) value;
self.appendByte(frame.header());
int remaining = 0;
for (org.fusesource.hawtbuf.Buffer buffer : frame.buffers) {
remaining += buffer.length;
}
do {
byte digit = (byte) (remaining & 0x7F);
remaining >>>= 7;
if (remaining > 0) {
digit |= 0x80;
}
self.appendByte(digit);
} while (remaining > 0);
for (org.fusesource.hawtbuf.Buffer buffer : frame.buffers) {
// TODO: see if we avoid the byte[] conversion.
self.appendBytes(buffer.toByteArray());
}
}
Aggregations