public static int Disconnect(MQTTid brokerID) { if (brokerID == null) { return(-1); } string connectedSoftwareId = MQTTController.getClientIdInternal(); lock (MQTTController.clientStuffLock) { if (MQTTController.clientsActiveConnections.ContainsKey(connectedSoftwareId)) //Connected to somewhere { mqttClientData onMemory = MQTTController.clientsActiveConnections[connectedSoftwareId].First(cdata => cdata.client.remoteAddress.Equals(brokerID.brokerADDR) && cdata.client.remotePort == brokerID.brokerPort); if (onMemory != null) // Connected to this broker { MQTTController.clientsActiveConnections[connectedSoftwareId].Remove(onMemory); // remove this cleint reference to the broker Dictionary <string, mqttClientData> everyBodyUsisngBroker = MQTTController.getWhoisUsing(brokerID.brokerADDR, brokerID.brokerPort); //List<mqttClientData> everyBodyUsisngBroker= MQTTController.clientsActiveConnections[connectedSoftwareId].FindAll(cdata => cdata.client.remoteAddress.Equals(remoteAddr) && cdata.client.remotePort == remotePort); //Disconect if nobody is using this broker if (everyBodyUsisngBroker == null || everyBodyUsisngBroker.Count == 0) { onMemory.client.disconnectToRemoteAsync(); lock (MQTTController.brokerStuffLock) { string remoteBrokerID = MQTTController.idsToClients.First(x => x.Value.Equals(onMemory.client)).Key; MQTTController.idsToClients.Remove(remoteBrokerID); } } else //somebodyelse is using thes connection //remove all the subscriptions from this client if nobody is using them { foreach (string subscriptionToBroker in onMemory.getsubscribedTopics()) { MQTTController.UnSubscribe(onMemory.client, subscriptionToBroker, 0, everyBodyUsisngBroker); } } } } else //notConnected //Nothing { } } return(0); }
public static bool Connected(MQTTid brokerID) { if (brokerID == null) { throw new ArgumentNullException("brokerID cannot be null"); } mqttClientData onMemory = null; string connectedSoftwareId = MQTTController.getClientIdInternal(); lock (MQTTController.clientStuffLock) { if (MQTTController.clientsActiveConnections.ContainsKey(connectedSoftwareId)) //Connected to somewhere { onMemory = MQTTController.clientsActiveConnections[connectedSoftwareId].First(cdata => cdata.client.remoteAddress.Equals(brokerID.brokerADDR) && cdata.client.remotePort == brokerID.brokerPort); } } if (onMemory == null) //I'm connected to this broker { return(false); } return(onMemory.client.connected()); }
public static int Publish(MQTTid brokerID, string topic, string data, byte QOSLevel, bool retain) { if (brokerID == null) { return(-1); } mqttClientData onMemory = null; string connectedSoftwareId = MQTTController.getClientIdInternal(); lock (MQTTController.clientStuffLock) { if (MQTTController.clientsActiveConnections.ContainsKey(connectedSoftwareId)) //Connected to somewhere { onMemory = MQTTController.clientsActiveConnections[connectedSoftwareId].First(cdata => cdata.client.remoteAddress.Equals(brokerID.brokerADDR) && cdata.client.remotePort == brokerID.brokerPort); } } if (onMemory != null) //I'm connected to this broker { return(onMemory.client.publishOnRemote(topic, data, QOSLevel, retain)); } return(-2); }
public static int UnSubscribe(MQTTid brokerID, string topic) { if (brokerID == null) { return(-1); } string connectedSoftwareId = MQTTController.getClientIdInternal(); Dictionary <string, mqttClientData> everyBodyUsisngBroker = MQTTController.getWhoisUsing(brokerID.brokerADDR, brokerID.brokerPort); lock (MQTTController.clientStuffLock) { if (MQTTController.clientsActiveConnections.ContainsKey(connectedSoftwareId)) //Connected to somewhere { mqttClientData onMemory = MQTTController.clientsActiveConnections[connectedSoftwareId].First(cdata => cdata.client.remoteAddress.Equals(brokerID.brokerADDR) && cdata.client.remotePort == brokerID.brokerPort); if (onMemory != null) { MQTTController.UnSubscribe(onMemory.client, topic, 1, everyBodyUsisngBroker); } } } return(0); }
public static string GetData(MQTTid brokerID, string topic, byte QOSLevel) { if (brokerID == null) { throw new ArgumentNullException("brokerID cannot be null"); } MQTTController.Subscribe(brokerID, topic, QOSLevel); mqttClientData onMemory = null; string connectedSoftwareId = MQTTController.getClientIdInternal(); lock (MQTTController.clientStuffLock) { if (MQTTController.clientsActiveConnections.ContainsKey(connectedSoftwareId)) //Connected to somewhere { onMemory = MQTTController.clientsActiveConnections[connectedSoftwareId].First(cdata => cdata.client.remoteAddress.Equals(brokerID.brokerADDR) && cdata.client.remotePort == brokerID.brokerPort); } } if (onMemory != null) //I'm connected to this broker { return(onMemory.getData(topic)); } return(null); }
void ICorefluxMQTTClient.Init(string remoteAddr, int remotePort, Action <string, MQTTMsgPublishEventArgs> onNewMessage, Action <string, bool, System.Diagnostics.EventLogEntryType> logMessage) { this.myID = MQTTController.Init(remoteAddr, remotePort, onNewMessage, logMessage); }