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); } }
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); } } }); } }
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); } }); } }); } }); }
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); } }
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++) { 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); } }
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); } }
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"); } }
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"); } }
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"); } }