public void onSuccess(Void value) { mqtt.tracer.debug("Restoring MQTT connection state"); // Setup a new overflow so that the replay can be sent out before the original overflow list. LinkedList<Request> originalOverflow = overflow; Map<Short, Request> originalRequests = requests; overflow = new LinkedList<Request>(); requests = new ConcurrentHashMap<Short, Request>(); // Restore any active subscriptions. if (!activeSubs.isEmpty()) { ArrayList<Topic> topics = new ArrayList<Topic>(activeSubs.size()); for (Map.Entry<UTF8Buffer, QoS> entry : activeSubs.entrySet()) { topics.add(new Topic(entry.getKey(), entry.getValue())); } send(new SUBSCRIBE().topics(topics.toArray(new Topic[topics.size()])), null); } // Replay any un-acked requests.. for (Map.Entry<Short, Request> entry : originalRequests.entrySet()) { MQTTFrame frame = entry.getValue().frame; frame.dup(true); // set the dup flag as these frames were previously transmitted. send(entry.getValue()); } // Replay the original overflow for (Request request : originalOverflow) { // Stuff in the overflow never got sent out.. so no need to set the dup flag send(request); } }
static private String toString(MQTTFrame frame) { if( frame == null ) return null; try { switch (frame.messageType()) { case PINGREQ.TYPE: return new PINGREQ().decode(frame).toString(); case PINGRESP.TYPE: return new PINGRESP().decode(frame).toString(); case CONNECT.TYPE: return new CONNECT().decode(frame).toString(); case DISCONNECT.TYPE: return new DISCONNECT().decode(frame).toString(); case SUBSCRIBE.TYPE: return new SUBSCRIBE().decode(frame).toString(); case UNSUBSCRIBE.TYPE: return new UNSUBSCRIBE().decode(frame).toString(); case PUBLISH.TYPE: return new PUBLISH().decode(frame).toString(); case PUBACK.TYPE: return new PUBACK().decode(frame).toString(); case PUBREC.TYPE: return new PUBREC().decode(frame).toString(); case PUBREL.TYPE: return new PUBREL().decode(frame).toString(); case PUBCOMP.TYPE: return new PUBCOMP().decode(frame).toString(); case CONNACK.TYPE: return new CONNACK().decode(frame).toString(); case SUBACK.TYPE: return new SUBACK().decode(frame).toString(); default: return frame.toString(); } } catch (Throwable e) { LOG.warn(e.getMessage(), e); return frame.toString(); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { try { qos[i] = onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); } }
topics.add(new Topic(entry.getKey(), entry.getValue())); send(new SUBSCRIBE().topics(topics.toArray(new Topic[topics.size()])), null);
public void on_mqtt_subscribe(final SUBSCRIBE sub) { subscribe(Arrays.asList(sub.topics()), new Task() { @Override public void run() { queue.execute(new Task() { @Override public void run() { session_state.strategy.update(new Task() { @Override public void run() { SUBACK suback = new SUBACK(); suback.messageId(sub.messageId()); byte[] granted = new byte[sub.topics().length]; int i = 0; for (Topic topic : sub.topics()) { granted[i] = (byte) topic.qos().ordinal(); i++; } suback.grantedQos(granted); send(suback); } }); } }); } }); }
static private String toString(MQTTFrame frame) { if( frame == null ) return null; try { switch (frame.messageType()) { case PINGREQ.TYPE: return new PINGREQ().decode(frame).toString(); case PINGRESP.TYPE: return new PINGRESP().decode(frame).toString(); case CONNECT.TYPE: return new CONNECT().decode(frame).toString(); case DISCONNECT.TYPE: return new DISCONNECT().decode(frame).toString(); case SUBSCRIBE.TYPE: return new SUBSCRIBE().decode(frame).toString(); case UNSUBSCRIBE.TYPE: return new UNSUBSCRIBE().decode(frame).toString(); case PUBLISH.TYPE: return new PUBLISH().decode(frame).toString(); case PUBACK.TYPE: return new PUBACK().decode(frame).toString(); case PUBREC.TYPE: return new PUBREC().decode(frame).toString(); case PUBREL.TYPE: return new PUBREL().decode(frame).toString(); case PUBCOMP.TYPE: return new PUBCOMP().decode(frame).toString(); case CONNACK.TYPE: return new CONNACK().decode(frame).toString(); case SUBACK.TYPE: return new SUBACK().decode(frame).toString(); default: return frame.toString(); } } catch (Throwable e) { LOG.warn(e.getMessage(), e); return frame.toString(); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { try { qos[i] = onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); } }
topics.add(new Topic(entry.getKey(), entry.getValue())); send(new SUBSCRIBE().topics(topics.toArray(new Topic[topics.size()])), null);
public void subscribe(final Topic[] topics, Callback<byte[]> cb) { if(topics==null) { throw new IllegalArgumentException("topics must not be null"); } queue.assertExecuting(); if( disconnected ) { cb.onFailure(createDisconnectedError()); return; } if( listener == DEFAULT_LISTENER ) { cb.onFailure(createListenerNotSetError()); } else { send(new SUBSCRIBE().topics(topics), new ProxyCallback<byte[]>(cb){ @Override public void onSuccess(byte[] value) { for (Topic topic : topics) { activeSubs.put(topic.name(), topic.qos()); } if(next!=null) { next.onSuccess(value); } } }); } }
static private String toString(MQTTFrame frame) { if( frame == null ) return null; try { switch (frame.messageType()) { case PINGREQ.TYPE: return new PINGREQ().decode(frame).toString(); case PINGRESP.TYPE: return new PINGRESP().decode(frame).toString(); case CONNECT.TYPE: return new CONNECT().decode(frame).toString(); case DISCONNECT.TYPE: return new DISCONNECT().decode(frame).toString(); case SUBSCRIBE.TYPE: return new SUBSCRIBE().decode(frame).toString(); case UNSUBSCRIBE.TYPE: return new UNSUBSCRIBE().decode(frame).toString(); case PUBLISH.TYPE: return new PUBLISH().decode(frame).toString(); case PUBACK.TYPE: return new PUBACK().decode(frame).toString(); case PUBREC.TYPE: return new PUBREC().decode(frame).toString(); case PUBREL.TYPE: return new PUBREL().decode(frame).toString(); case PUBCOMP.TYPE: return new PUBCOMP().decode(frame).toString(); case CONNACK.TYPE: return new CONNACK().decode(frame).toString(); case SUBACK.TYPE: return new SUBACK().decode(frame).toString(); default: return frame.toString(); } } catch (Throwable e) { LOG.warn(e.getMessage(), e); return frame.toString(); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { MQTTProtocolSupport.validate(topics[i].name().toString()); try { qos[i] = findSubscriptionStrategy().onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); throw new MQTTProtocolException("SUBSCRIBE command received with no topic filter"); } }
public void onSuccess(Void value) { mqtt.tracer.debug("Restoring MQTT connection state"); // Setup a new overflow so that the replay can be sent out before the original overflow list. LinkedList<Request> originalOverflow = overflow; Map<Short, Request> originalRequests = requests; overflow = new LinkedList<Request>(); requests = new ConcurrentHashMap<Short, Request>(); // Restore any active subscriptions. if (!activeSubs.isEmpty()) { ArrayList<Topic> topics = new ArrayList<Topic>(activeSubs.size()); for (Map.Entry<UTF8Buffer, QoS> entry : activeSubs.entrySet()) { topics.add(new Topic(entry.getKey(), entry.getValue())); } send(new SUBSCRIBE().topics(topics.toArray(new Topic[topics.size()])), null); } // Replay any un-acked requests.. for (Map.Entry<Short, Request> entry : originalRequests.entrySet()) { MQTTFrame frame = entry.getValue().frame; frame.dup(true); // set the dup flag as these frames were previously transmitted. send(entry.getValue()); } // Replay the original overflow for (Request request : originalOverflow) { // Stuff in the overflow never got sent out.. so no need to set the dup flag send(request); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { MQTTProtocolSupport.validate(topics[i].name().toString()); try { qos[i] = findSubscriptionStrategy().onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); throw new MQTTProtocolException("SUBSCRIBE command received with no topic filter"); } }
public void subscribe(final Topic[] topics, Callback<byte[]> cb) { if(topics==null) { throw new IllegalArgumentException("topics must not be null"); } queue.assertExecuting(); if( disconnected ) { cb.onFailure(createDisconnectedError()); return; } if( listener == DEFAULT_LISTENER ) { cb.onFailure(createListenerNotSetError()); } else { send(new SUBSCRIBE().topics(topics), new ProxyCallback<byte[]>(cb){ @Override public void onSuccess(byte[] value) { for (Topic topic : topics) { activeSubs.put(topic.name(), topic.qos()); } if(next!=null) { next.onSuccess(value); } } }); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { MQTTProtocolSupport.validate(topics[i].name().toString()); try { qos[i] = findSubscriptionStrategy().onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); throw new MQTTProtocolException("SUBSCRIBE command received with no topic filter"); } }