Added Client ID generator

This commit is contained in:
selva.andre
2012-08-09 23:14:29 +02:00
parent 937a7b7060
commit 5540805570
@@ -1,6 +1,8 @@
package org.dna.mqtt.moquette.client;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@@ -46,8 +48,8 @@ import org.slf4j.LoggerFactory;
* The API to connect to a MQTT server.
*
* Pass to the constructor the host and port to connect, invoke the connect
* message, invoke publish to send notifications messages, invoke subscribe
* with a callback to be notified when something happen on the desired topic.
* message, invoke publish to send notifications messages, invoke subscribe with
* a callback to be notified when something happen on the desired topic.
*
* @author andrea
*/
@@ -62,22 +64,20 @@ public final class Client {
private static final int NUM_SCHEDULER_TIMER_THREAD = 1;
final static int DEFAULT_RETRIES = 3;
private int m_connectRetries = DEFAULT_RETRIES;
private String m_hostname;
private int m_port;
//internal mangement used for conversation with the server
private IoConnector m_connector;
private IoSession m_session;
private CountDownLatch m_connectBarrier;
private CountDownLatch m_subscribeBarrier;
private byte m_returnCode;
//TODO synchronize the access
//Refact the da model should be a list of callback for each topic
private Map<String, IPublishCallback> m_subscribersList = new HashMap<String, IPublishCallback>();
private ScheduledExecutorService m_scheduler;
private ScheduledFuture m_pingerHandler;
private String m_macAddress;
final Runnable pingerDeamon = new Runnable() {
public void run() {
LOG.debug("Pingreq sent");
@@ -116,6 +116,8 @@ public final class Client {
m_connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, Server.DEFAULT_CONNECT_TIMEOUT);
m_scheduler = Executors.newScheduledThreadPool(NUM_SCHEDULER_TIMER_THREAD);
m_macAddress = readMACAddress();
}
public void connect() throws MQTTException {
@@ -127,7 +129,7 @@ public final class Client {
future.awaitUninterruptibly();
m_session = future.getSession();
} catch (RuntimeIoException e) {
LOG.debug("Failed to connect, retry " + retries + " of ("+m_connectRetries+")", e);
LOG.debug("Failed to connect, retry " + retries + " of (" + m_connectRetries + ")", e);
}
if (retries == m_connectRetries) {
@@ -139,7 +141,7 @@ public final class Client {
//send a message over the session
ConnectMessage connMsg = new ConnectMessage();
connMsg.setKeepAlive(KEEPALIVE_SECS);
connMsg.setClientID("FAKE_MSG_ID1"); //TODO create a generator for the ID
connMsg.setClientID(generateClientID());
m_session.write(connMsg);
//suspend until the server respond with CONN_ACK
@@ -187,7 +189,7 @@ public final class Client {
* Publish to the connected server the payload message to the given topic.
* It's admitted to publish a 0 -length payload.
*/
public void publish(String topic, byte[] payload) throws PublishException{
public void publish(String topic, byte[] payload) throws PublishException {
PublishMessage msg = new PublishMessage();
msg.setTopicName(topic);
msg.setPayload(payload);
@@ -214,7 +216,7 @@ public final class Client {
public void subscribe(String topic, IPublishCallback publishCallback) {
LOG.info("subscribe invoked");
SubscribeMessage msg = new SubscribeMessage();
msg.addSubscription(new SubscribeMessage.Couple((byte)AbstractMessage.QOSType.MOST_ONE.ordinal(), topic));
msg.addSubscription(new SubscribeMessage.Couple((byte) AbstractMessage.QOSType.MOST_ONE.ordinal(), topic));
msg.setQos(AbstractMessage.QOSType.LEAST_ONE);
WriteFuture wf = m_session.write(msg);
@@ -256,7 +258,6 @@ public final class Client {
//TODO check the ACK messageID
}
/**
* TODO extract this SPI method in a SPI
*/
@@ -266,18 +267,15 @@ public final class Client {
m_connectBarrier.countDown();
}
protected void subscribeAckCallback() {
LOG.info("subscribeAckCallback invoked");
m_subscribeBarrier.countDown();
}
protected void publishCallback(String topic, byte[] payload) {
m_subscribersList.get(topic).published(topic, payload);
}
/**
* In the current pinger is not ye executed, then cancel it and schedule
* another by KEEPALIVE_SECS
@@ -289,6 +287,26 @@ public final class Client {
m_pingerHandler = m_scheduler.scheduleWithFixedDelay(pingerDeamon, KEEPALIVE_SECS, KEEPALIVE_SECS, TimeUnit.SECONDS);
}
private String readMACAddress() {
try {
NetworkInterface network = NetworkInterface.getNetworkInterfaces().nextElement();
byte[] mac = network.getHardwareAddress();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
return sb.toString();
} catch (Exception ex) {
throw new MQTTException("Can't retrieve host MAC address", ex);
}
}
private String generateClientID() {
double rnd = Math.random();
return "Moquette-client_" + m_macAddress + Math.round(rnd*1000);
}
public void close() {
//stop the pinger