예제 #1
0
파일: dispatcher.py 프로젝트: ogoodman/serf
 def __init__(self, dispatcher, node_id, path):
     Publisher.__init__(self)
     self.dispatcher = dispatcher
     self.node_id = node_id
     self.path = path.strip('/')
     if self.path:
         self.path += '/'
예제 #2
0
 def __init__(self, dispatcher, node_id, path):
     Publisher.__init__(self)
     self.dispatcher = dispatcher
     self.node_id = node_id
     self.path = path.strip('/')
     if self.path:
         self.path += '/'
예제 #3
0
 def __init__(self, sock, address, client_id='browser', transport=None):
     Publisher.__init__(self)
     self.sock = SocketBuffer(sock)
     self.client_address = address
     self.client_ip = address[0]
     self.node_id = 'server'
     self.client_id = client_id
     self.path = ''
     self.close_sent = False
     if transport is None:
         self.transport = weakref.proxy(self)
     else:
         self.transport = transport
     self.binaries = {}
예제 #4
0
 def __init__(self, node_id='-', verbose=False, ssl=None):
     Publisher.__init__(self)
     self.node_id = node_id
     self.path = ''
     self.nodes = {}
     self.loop_in = None
     self.cond = threading.Condition()
     self.todo = []
     self.wait_for_close = []
     self.loop = None
     self.lsock = None
     self.thread = threading.currentThread()
     self.verbose = verbose
     self.ssl = ssl
     self.conn_num = 0
예제 #5
0
    def testPersistence(self):
        store = {}
        storage = Storage(store)

        storage['pub'] = p = Publisher()
        storage['sub'] = s = Subscriber([])

        p.subscribe('snap', s.onEvent, how=PERSISTENT)
        p.notify('snap', 1)

        self.assertEqual(s.events, [21])

        storage['sub'] = s

        # Reboot.
        storage = Storage(store)
        p = storage['pub']

        p.notify('snap', 2)

        s = storage['sub']
        self.assertEqual(s.events, [21, 22])

        del storage['sub']
        del s

        # When delivery fails, subscriber is removed.
        p.notify('snap', 3)
        self.assertEqual(len(p.subscribers('snap')), 0)
예제 #6
0
    def __init__(self, factory):
        """Create an online list with an online entity factory.

        Args:
            factory

        The factory must have a make() method for making new online
        entities.

        The factory must have an init() method which we call passing
        ourself so that the factory can keep a reference to us and
        set up any subscriptions it requires.
        """
        Publisher.__init__(self)
        self.online = weakref.WeakValueDictionary()
        self.factory = factory
        self.factory.init(self)
예제 #7
0
    def __init__(self, factory):
        """Create an online list with an online entity factory.

        Args:
            factory

        The factory must have a make() method for making new online
        entities.

        The factory must have an init() method which we call passing
        ourself so that the factory can keep a reference to us and
        set up any subscriptions it requires.
        """
        Publisher.__init__(self)
        self.online = weakref.WeakValueDictionary()
        self.factory = factory
        self.factory.init(self)
예제 #8
0
    def testUnsubscribeWeakById(self):
        s = Subscriber([])
        p = Publisher()

        sid = p.subscribe('bar', s.on)
        p.unsubscribe('bar', sid)

        p.notify('foo', 'bar')
        self.assertEqual(s.events, [])
예제 #9
0
    def testNonCyclic(self):
        # Publishers are supposed to break cyclic dependencies; we have
        # two-way communication between parent and child without the
        # child holding a reference to the parent.
        p = Parent(Publisher())

        p.child.notify('event', 'foo')
        p.child.notify('event', 'bar')
        self.assertEqual(p.from_child, 'bar')

        wp = weakref.ref(p)
        self.assertTrue(wp() is not None)
        del p
        self.assertTrue(wp() is None)
예제 #10
0
    def testUnsubscribeWeakById(self):
        s = Subscriber([])
        p = Publisher()

        sid = p.subscribe('bar', s.on)
        p.unsubscribe('bar', sid)

        p.notify('foo', 'bar')
        self.assertEqual(s.events, [])
예제 #11
0
    def testLazyInfo(self):
        s = Subscriber([])
        p = Publisher()
        p.subscribe('bar', s.on)

        self.info_calls = 0

        def info():
            self.info_calls += 1
            return {'n': 42}

        # no subscribers to 'foo' so info won't be called.
        p.notify('foo', info)
        self.assertEqual(s.events, [])
        self.assertEqual(self.info_calls, 0)

        p.notify('bar', info)
        self.assertEqual(s.events, [('bar', {'n': 42})])
        self.assertEqual(self.info_calls, 1)
예제 #12
0
    def testLazyInfo(self):
        s = Subscriber([])
        p = Publisher()
        p.subscribe('bar', s.on)

        self.info_calls = 0
        def info():
            self.info_calls += 1
            return {'n': 42}

        # no subscribers to 'foo' so info won't be called.
        p.notify('foo', info)
        self.assertEqual(s.events, [])
        self.assertEqual(self.info_calls, 0)

        p.notify('bar', info)
        self.assertEqual(s.events, [('bar', {'n': 42})])
        self.assertEqual(self.info_calls, 1)
예제 #13
0
    def testCatchAllSubscriptions(self):
        # Persistent.
        store = {}
        storage = Storage(store)

        storage['pub'] = p = Publisher()
        storage['sub'] = s = Subscriber([])

        sid = p.subscribe('*', s.on, args=('hello', ), how=PERSISTENT)

        del s, p

        p = storage['pub']
        p.notify('dusted', 14)

        # FIXME: a tuple is converted to a list here. That's a
        # serializer bug.
        self.assertEqual(storage['sub'].events, [['dusted', 14, 'hello']])

        p.unsubscribe('*', sid, how=PERSISTENT)
        p.notify('dusted', 15)
        self.assertEqual(len(storage['sub'].events), 1)
예제 #14
0
파일: chat.py 프로젝트: ogoodman/serf
 def __init__(self):
     Publisher.__init__(self)
     self.people = weakref.WeakValueDictionary()
     self.history = []
예제 #15
0
    def testArgs(self):
        # Non-persistent.
        events = []
        def cb(*args):
            events.append(args)

        p = Publisher()
        p.subscribe('done', cb, args=('x', 42))
        p.notify('done', True)

        self.assertEqual(events, [('done', True, 'x', 42)])

        # Persistent.
        store = {}
        storage = Storage(store)

        storage['sub'] = s = Subscriber([])
        sid = p.subscribe('dusted', s.on, args=('hello',), how=PERSISTENT)
        storage['pub'] = p

        del s, p

        p = storage['pub']
        p.notify('dusted', 14)

        # FIXME: a tuple is converted to a list here. That's a
        # serializer bug.
        self.assertEqual(storage['sub'].events, [['dusted', 14, 'hello']])

        p.unsubscribe('dusted', sid, how=PERSISTENT)
        p.notify('dusted', 15)
        self.assertEqual(len(storage['sub'].events), 1)
예제 #16
0
 def init(self, online_list):
     """Implements factory interface for OnlineList."""
     Publisher.__init__(self)
     self.online_list = online_list
     self.online_list.subscribe('offline', self._notifyPlayerOffline)
예제 #17
0
 def __init__(self, player_list):
     Publisher.__init__(self)
     self.name = None
     self.player_list = player_list
예제 #18
0
 def init(self, online_list):
     """Implements factory interface for OnlineList."""
     Publisher.__init__(self)
     self.online_list = online_list
     self.online_list.subscribe('offline', self._notifyPlayerOffline)
예제 #19
0
 def __init__(self, player_list):
     Publisher.__init__(self)
     self.name = None
     self.player_list = player_list
예제 #20
0
 def __init__(self, name, age, subs=None):
     Publisher.__init__(self, subs)
     self.name = name
     self.age = age
예제 #21
0
 def __init__(self, port, handler=None):
     Publisher.__init__(self)
     self.port = port
     self.handler = handler
     self.pool = eventlet.GreenPool(10000)
     self.connections = weakref.WeakValueDictionary()
예제 #22
0
    def testAdaptSubscriber(self):
        pub = Publisher()

        events = []

        # Adapt the subscriber (add 10 to each event info).
        sub = Subscriber(events)
        asub = SubAdapter('info', sub, lambda s, e, i: s(e, i + 10))
        pub.addSub(asub)
        del asub

        pub.notify('info', 1)
        del sub
        pub.notify('info', 2)

        # Only adapted event 1 arrives before asub dies with sub.
        self.assertEqual(events, [11])

        # Adapt the subscribing method (add 10 to each event info).
        sub = Subscriber(events)
        asub = SubAdapter('info', sub, lambda s, e, i: s.onEvent(e, i + 10))
        pub.addSub(asub)
        del asub

        pub.notify('info', 1)
        del sub
        pub.notify('info', 2)

        # Only adapted event 1 arrives before asub dies with sub.
        self.assertEqual(events, [11, 31])
예제 #23
0
    def testArgs(self):
        # Non-persistent.
        events = []

        def cb(*args):
            events.append(args)

        p = Publisher()
        p.subscribe('done', cb, args=('x', 42))
        p.notify('done', True)

        self.assertEqual(events, [('done', True, 'x', 42)])

        # Persistent.
        store = {}
        storage = Storage(store)

        storage['sub'] = s = Subscriber([])
        sid = p.subscribe('dusted', s.on, args=('hello', ), how=PERSISTENT)
        storage['pub'] = p

        del s, p

        p = storage['pub']
        p.notify('dusted', 14)

        # FIXME: a tuple is converted to a list here. That's a
        # serializer bug.
        self.assertEqual(storage['sub'].events, [['dusted', 14, 'hello']])

        p.unsubscribe('dusted', sid, how=PERSISTENT)
        p.notify('dusted', 15)
        self.assertEqual(len(storage['sub'].events), 1)
예제 #24
0
파일: table.py 프로젝트: ogoodman/serf
 def __init__(self, primary=None, indices=None, pkey=None, subs=None):
     Publisher.__init__(self, subs)
     self._primary = primary or {}
     self._indices = indices or {}
     self._pkey = pkey or 0
     self._indexers = {}
예제 #25
0
파일: chat.py 프로젝트: ogoodman/serf
 def __init__(self):
     Publisher.__init__(self)
     self.rooms = {}
예제 #26
0
파일: chat.py 프로젝트: ogoodman/serf
 def __init__(self):
     Publisher.__init__(self)
     self.people = weakref.WeakValueDictionary()
     self.history = []
예제 #27
0
    def testAdaptSubscriber(self):
        pub = Publisher()

        events = []

        # Adapt the subscriber (add 10 to each event info).
        sub = Subscriber(events)
        asub = SubAdapter('info', sub, lambda s, e, i: s(e, i+10))
        pub.addSub(asub)
        del asub

        pub.notify('info', 1)
        del sub
        pub.notify('info', 2)

        # Only adapted event 1 arrives before asub dies with sub.
        self.assertEqual(events, [11])

        # Adapt the subscribing method (add 10 to each event info).
        sub = Subscriber(events)
        asub = SubAdapter('info', sub, lambda s, e, i: s.onEvent(e, i+10))
        pub.addSub(asub)
        del asub

        pub.notify('info', 1)
        del sub
        pub.notify('info', 2)

        # Only adapted event 1 arrives before asub dies with sub.
        self.assertEqual(events, [11, 31])
예제 #28
0
파일: chat.py 프로젝트: ogoodman/serf
 def __init__(self):
     Publisher.__init__(self)
     self.rooms = {}
예제 #29
0
파일: model.py 프로젝트: ogoodman/serf
 def __init__(self, name, age, subs=None):
     Publisher.__init__(self, subs)
     self.name = name
     self.age = age
예제 #30
0
파일: table.py 프로젝트: ogoodman/serf
 def __init__(self, primary=None, indices=None, pkey=None, subs=None):
     Publisher.__init__(self, subs)
     self._primary = primary or {}
     self._indices = indices or {}
     self._pkey = pkey or 0
     self._indexers = {}