Pregunta

In the Publish & Subscribe model using autobahn, I would like to limit the number of subscribers for a given @exportSub(...). How do you know the number of subscribers?

(From examples)

class MyTopicService(object):

   def __init__(self, allowedTopicIds):
      self.allowedTopicIds = allowedTopicIds

   @exportSub("", True)
   def subscribe(self, topicUriPrefix, topicUriSuffix):
      ret = False
      print "client wants to subscribe to %s %s. Allowed topic ids:%s" % (topicUriPrefix,     topicUriSuffix, self.allowedTopicIds)
      try:
         if topicUriSuffix in self.allowedTopicIds:
            ret = True
            print "Subscribing client to topic %s %s" % (topicUriPrefix, topicUriSuffix)
         else:
            print "Client not allowed to subscribe to topic %s %s" % (topicUriPrefix, topicUriSuffix)
      except:
         print "illegal topic - skipped subscription"
      finally:
         return ret

class MyServerProtocol(WampServerProtocol):
   def onSessionOpen(self):
      self.registerHandlerForPubSub(MyTopicService(my_keys_1), url_1_foo)
      self.registerHandlerForPubSub(MyTopicService(my_keys_2), url_2_bar)

I could probably do this using my own WampServerFactory, overriding the onClientSubscribed and onClientUnsubscribed methods and using an internal array variable... But I would like to know if there is a cleaner way...

  class MyFactory(WampServerFactory):
     def onClientSubscribed(self, *a, **k):
        WampServerFactory.onClientSubscribed(self, a, k)
        print '=== client subscribed '

     def onClientUnsubscribed(self, *a, **k):
        WampServerFactory.onClientUnsubscribed(self, a, k)
        print '=== client unsubscribed '

Code can be found here.

¿Fue útil?

Solución

Unfortunately, there is no public, supported API currently for that.

I agree that something like myWampFactory.getSubscribers(someTopic) would be useful in certain situations. If you care, please file an issue on GitHub so we can track the feature request.

Of the 2 workarouns you mention, overriding onClientSubscribed seems to lead to a 2nd bookeeping of subscriptions, which I find even more unsatisfying than accessing internals (myFactory.subscriptions).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top