def leave_channel(self, name, handler, callback=None): '''Remove yourself from a channel. @param name: The name of the channel. @param handler: An opaque reference to a channel. @param callback: Called (with name of channel as argument) after the handler has been attached to the channel. ''' data = {'name': name, 'handler': serializer.serialize(self, handler)} if callback: data['callback'] = serializer.serialize(self, callback) self._connection.send_command('LEAVECHANNEL', data)
def join_channel(self, name, handler, writeable=True, callback=None): '''Register a handler with a channel. @param name: The name of the channel. @param handler: An opaque reference to a channel. @param writeable: Whether the handler's owner may write to the channel. @param callback: Called (with reference to channel, and name of channel) after the handler has been attached to the channel. ''' if hasattr(writeable, '__call__'): logging.warn('Deprecated -- the joinChannel API has been revised.') writeable, callback = True, writeable data = {'name': name, 'handler': serializer.serialize(self, handler), 'writeable': writeable} if callback: data['callback'] = serializer.serialize(self, callback) self._connection.send_command('JOINCHANNEL', data)
def unpublish_service(self, name, callback=None): '''Stops publishing a service to Bridge. @param name: The name of the service. @param callback: Called (with name of service as argument) when the service has stopped being published. ''' if name == 'system': logging.error('Invalid service name: %s', name) else: data = {'name': name} if callback: data['callback'] = serializer.serialize(self, callback) self._connection.send_command('LEAVEWORKERPOOL', data)
def publish_service(self, name, handler, callback=None): '''Publish a service to Bridge. @param name: The name of the service. @param handler: Any class with a default constructor, or any instance. @param callback: Called (with name of service as argument) when the service has been published. ''' if name == 'system': logging.error('Invalid service name: %s', name) else: self._store[name] = handler data = {'name': name} if callback: data['callback'] = serializer.serialize(self, callback) self._connection.send_command('JOINWORKERPOOL', data)
def test_serialize(self): dummy = BridgeDummy() test1 = Test1() test2 = Test2() test3 = Test3() test4 = lambda:1 obj = { 'a': { 'b': test1 }, 'c': 5, 'd': True, 'e': 'abc', 'f': [test2, test3], 'g': test2, 'h': test3, 'i': test4 } ser = serializer.serialize(dummy, obj) self.assertTrue([test1, ['a', 'b']] in dummy.stored) self.assertTrue([test2, ['c']] in dummy.stored) self.assertTrue([test3, ['c', 'd', 'e']] in dummy.stored) found = False for x in dummy.stored: if x[1] == ['callback']: print(x[0]) self.assertIsInstance(x[0], serializer.Callback) found = True self.assertTrue(found) expected_ser = { 'a': { 'b': "dummy" }, 'c': 5, 'd': True, 'e': 'abc', 'f': ["dummy", "dummy"], 'g': "dummy", 'h': "dummy", 'i': "dummy" } self.assertEqual(expected_ser, ser) return
def _send(self, args, destination): args = list(args) self._connection.send_command('SEND', { 'args': serializer.serialize(self, args), 'destination': destination, })