Task <IEnumerable <Tuple <string, QoS, byte[]> > > IMqttStorageProvider.GetRetained(IEnumerable <KeyValuePair <string, DClark.MQTT.Messages.QoS> > subscriptions) { List <Tuple <string, QoS, byte[]> > result = new List <Tuple <string, QoS, byte[]> >(); foreach (var subscription in subscriptions) { string topicFilter = subscription.Key; Dictionary <string, RetainedNode> possibles = new Dictionary <string, RetainedNode>(); possibles.Add("", retained); RetainedNode node = retained; Boolean includeChildren = false; foreach (string fragment in topicFilter.Split('/')) { Dictionary <string, RetainedNode> next = new Dictionary <string, RetainedNode>(); if (fragment == "#" || fragment == "+") { foreach (var possible in possibles) { foreach (var child in possible.Value.children) { next.Add(possible.Key.Length == 0 ? child.Key : possible.Key + "/" + child.Key, child.Value); } } includeChildren = fragment == "#"; } else { foreach (var possible in possibles) { RetainedNode child; if (possible.Value.children.TryGetValue(fragment, out child)) { next.Add(possible.Key.Length == 0 ? fragment : possible.Key + "/" + fragment, child); } } } possibles = next; } foreach (var possible in possibles) { if (possible.Value.body != null) { result.Add(new Tuple <string, QoS, byte[]>(possible.Key, subscription.Value, possible.Value.body)); } if (includeChildren) { AddRetainedRecursive(possible.Key, possible.Value, subscription.Value, result); } } } return(Util.RunSynchronously <IEnumerable <Tuple <string, QoS, byte[]> > >(() => result)); }
void AddRetainedRecursive(string topic, RetainedNode node, QoS qos, List <Tuple <string, QoS, byte[]> > result) { if (node.children == null) { return; } foreach (var child in node.children) { string childTopic = topic + "/" + child.Key; if (child.Value.body != null) { result.Add(new Tuple <string, QoS, byte[]>(childTopic, qos, child.Value.body)); } AddRetainedRecursive(childTopic, child.Value, qos, result); } }
Task IMqttStorageProvider.PutRetained(string topic, byte[] payload) { RetainedNode node = retained; foreach (string fragment in topic.Split('/')) { RetainedNode next; if (node.children == null) { node.children = new Dictionary <string, RetainedNode>(); node.children.Add(fragment, next = new RetainedNode()); } else { if (!node.children.TryGetValue(fragment, out next)) { node.children.Add(fragment, next = new RetainedNode()); } } node = next; } node.body = payload; return(Util.CompletedTask); }