def main(argv=None): _usage = """Usage: %prog [options] <name>""" parser = optparse.OptionParser(usage=_usage) parser.add_option("--url", action="store", default=None) parser.add_option("--id", action="store", default=None) parser.add_option("--controller", action="store_true") parser.add_option("--calls", action="store", type=int, default=0) parser.add_option("--workers", action="store", type=int, default=None) parser.add_option("--timeout", action="store", type=int, default=2) parser.add_option("--config", action="callback", callback=handle_config_option, nargs=2, type="string") opts, extra = parser.parse_args(args=argv) logging.basicConfig(level=logging.INFO) #make this an option if opts.url: cfg.CONF.transport_url = opts.url transport = messaging.get_transport(cfg.CONF) server_name = opts.id or "%s_%s" % (socket.gethostname(), os.getpid()) server = Server(transport, server_name, opts.controller, opts.workers) def signal_handler(s, f): server.stop() signal.signal(signal.SIGINT, signal_handler) server.start() if opts.controller: time.sleep(0.5) # give server time to initialise target = messaging.Target(exchange="test-exchange", topic="test-topic", fanout=True) stub = messaging.RPCClient(transport, target, timeout=2) stub.cast({}, 'start', controller=server_name, count=opts.calls, timeout=opts.timeout) elif opts.calls: target = messaging.Target(exchange="test-exchange", topic="test-topic") stub = messaging.RPCClient(transport, target, timeout=2) client = Client(stub) stats = client.run("abcdefghijklmnopqrstuvwxyz", count=opts.calls, verbose=True) print stats server.stop() server.wait() if opts.controller: server.collector.report() return 0
def start(self, ctx, controller, count=None, fanout=False, server=None, timeout=2, data="abcdefghijklmnopqrstuvwxyz"): if controller == self.name: return self._controller = controller target = self._target if fanout: target.fanout = True if server: target.server = server if count: self._count = count if data: self._data = data stub = messaging.RPCClient(self._transport, target, timeout=timeout) self._client = Client(stub) self._thread = threading.Thread(target=self.run) self._thread.start()
def setUp(self): super(RpcServerFixture, self).setUp() endpoints = [self.endpoint, self] self.server = messaging.get_rpc_server(self.transport, self.target, endpoints) self._ctrl = messaging.RPCClient(self.transport, self.ctrl_target) self._start()
def get_client(target, version_cap=None, serializer=None): assert TRANSPORT is not None serializer = RequestContextSerializer(serializer) return messaging.RPCClient(TRANSPORT, target, version_cap=version_cap, serializer=serializer)
def __init__(self, transport): """Construct an RPC client for the Engine. :param transport: a messaging transport handle :type transport: Transport """ target = messaging.Target(topic=cfg.CONF.engine.topic) self._client = messaging.RPCClient(transport, target)
def run(self): target = messaging.Target(exchange=self._target.exchange, topic=self._target.topic, server=self._controller) ctrlr = messaging.RPCClient(self._transport, target, timeout=5) ctrlr.cast({}, 'announce', server=self.name) stats = self._client.run(self._data, self._count) ctrlr.cast({}, 'submit', server=self.name, results=stats)
def _get_client(self, next_hop, topic): """Turn the DB information for a cell into a messaging.RPCClient.""" transport = self._get_transport(next_hop) target = messaging.Target(topic=topic, version='1.0') serializer = rpc.RequestContextSerializer(None) return messaging.RPCClient(transport, target, version_cap=self.version_cap, serializer=serializer)
def test_call_fanout(self): transport = _FakeTransport(self.conf) client = messaging.RPCClient(transport, messaging.Target(**self.target)) if self.prepare is not _notset: client = client.prepare(**self.prepare) self.assertRaises(exceptions.InvalidTarget, client.call, {}, 'foo')
def get_client(target=None, version_cap=None, serializer=None): if not CONF.queue.enable: return None assert TRANSPORT is not None queue_target = target or get_target() return messaging.RPCClient(TRANSPORT, target=queue_target, version_cap=version_cap, serializer=serializer)
def _get_client(self, next_hop, topic): """Turn the DB information for a cell into a messaging.RPCClient.""" transport_url = next_hop.db_info['transport_url'] transport = messaging.get_transport(cfg.CONF, transport_url, rpc.TRANSPORT_ALIASES) target = messaging.Target(topic=topic, version='1.0') serializer = rpc.RequestContextSerializer(None) return messaging.RPCClient(transport, target, version_cap=self.version_cap, serializer=serializer)
def __init__(self, transport): """Constructs an RPC client for engine. :param transport: Messaging transport. """ serializer = auth_ctx.RpcContextSerializer( auth_ctx.JsonPayloadSerializer()) self._client = messaging.RPCClient( transport, messaging.Target(topic=cfg.CONF.engine.topic), serializer=serializer)
def __init__(self, transport=None, context=None, topic=None): serializer = RequestContextSerializer(JsonPayloadSerializer()) if transport is None: transport = messaging.get_transport(cfg.CONF, aliases=TRANSPORT_ALIASES) self._context = context if topic is None: topic = '' target = messaging.Target(topic=topic) self._client = messaging.RPCClient(transport, target, serializer=serializer)
def __init__(self, storage_driver, table_info_repo, concurrent_tasks=1000, batch_chunk_size=25, schema_operation_timeout=300): SimpleStorageManager.__init__(self, storage_driver, table_info_repo, concurrent_tasks, batch_chunk_size, schema_operation_timeout) transport = messaging.get_transport(CONF) target = messaging.Target(topic='schema') self._rpc_client = messaging.RPCClient(transport, target)
def __init__(self, transport, name, controller, workers): target = messaging.Target(exchange="test-exchange", topic="test-topic", server=name) endpoints = [Test(), Control(transport, target, name)] if controller: self.collector = Collector() if workers: self.collector.workers = workers endpoints.append(self.collector) else: self.collector = None self._server = messaging.get_rpc_server(transport, target, endpoints) self._ctrl = messaging.RPCClient(transport, target, timeout=2)
def test_call_retry(self): transport = _FakeTransport(self.conf) client = messaging.RPCClient(transport, messaging.Target(), retry=self.ctor) self.mox.StubOutWithMock(transport, '_send') msg = dict(method='foo', args={}) kwargs = dict(wait_for_reply=True, timeout=60, retry=self.expect) transport._send(messaging.Target(), {}, msg, **kwargs) self.mox.ReplayAll() if self.prepare is not _notset: client = client.prepare(retry=self.prepare) client.call({}, 'foo')
def test_version_cap(self): self.config(rpc_response_timeout=None) transport = _FakeTransport(self.conf) target = messaging.Target(version=self.version) client = messaging.RPCClient(transport, target, version_cap=self.cap) if self.success: self.mox.StubOutWithMock(transport, '_send') if self.prepare_version is not _notset: target = target(version=self.prepare_version) msg = dict(method='foo', args={}) if target.version is not None: msg['version'] = target.version kwargs = {'retry': None} if self.call: kwargs['wait_for_reply'] = True kwargs['timeout'] = None transport._send(target, {}, msg, **kwargs) self.mox.ReplayAll() prep_kwargs = {} if self.prepare_cap is not _notset: prep_kwargs['version_cap'] = self.prepare_cap if self.prepare_version is not _notset: prep_kwargs['version'] = self.prepare_version if prep_kwargs: client = client.prepare(**prep_kwargs) method = client.call if self.call else client.cast try: method({}, 'foo') except Exception as ex: self.assertIsInstance(ex, messaging.RPCVersionCapError, ex) self.assertFalse(self.success) else: self.assertTrue(self.success)
def test_cast_call(self): self.config(rpc_response_timeout=None) transport = _FakeTransport(self.conf) client = messaging.RPCClient(transport, messaging.Target()) self.mox.StubOutWithMock(transport, '_send') msg = dict(method='foo', args=self.args) kwargs = {'retry': None} if self.call: kwargs['wait_for_reply'] = True kwargs['timeout'] = None transport._send(messaging.Target(), self.ctxt, msg, **kwargs) self.mox.ReplayAll() method = client.call if self.call else client.cast method(self.ctxt, 'foo', **self.args)
def test_call_serializer(self): self.config(rpc_response_timeout=None) transport = _FakeTransport(self.conf) serializer = msg_serializer.NoOpSerializer() client = messaging.RPCClient(transport, messaging.Target(), serializer=serializer) self.mox.StubOutWithMock(transport, '_send') msg = dict(method='foo', args=dict([(k, 's' + v) for k, v in self.args.items()])) kwargs = dict(wait_for_reply=True, timeout=None) if self.call else {} kwargs['retry'] = None transport._send(messaging.Target(), dict(user='******'), msg, **kwargs).AndReturn(self.retval) self.mox.StubOutWithMock(serializer, 'serialize_entity') self.mox.StubOutWithMock(serializer, 'deserialize_entity') self.mox.StubOutWithMock(serializer, 'serialize_context') for arg in self.args: serializer.serialize_entity(self.ctxt, arg).AndReturn('s' + arg) if self.call: serializer.deserialize_entity(self.ctxt, self.retval).\ AndReturn('d' + self.retval) serializer.serialize_context(self.ctxt).AndReturn(dict(user='******')) self.mox.ReplayAll() method = client.call if self.call else client.cast retval = method(self.ctxt, 'foo', **self.args) if self.retval is not None: self.assertEqual('d' + self.retval, retval)
def test_version_cap(self): self.config(rpc_response_timeout=None) transport = _FakeTransport(self.conf) target = messaging.Target(version=self.version) client = messaging.RPCClient(transport, target, version_cap=self.cap) prep_kwargs = {} if self.prepare_cap is not _notset: prep_kwargs['version_cap'] = self.prepare_cap if self.prepare_version is not _notset: prep_kwargs['version'] = self.prepare_version if prep_kwargs: client = client.prepare(**prep_kwargs) if self.can_send_version is not _notset: can_send = client.can_send_version(version=self.can_send_version) else: can_send = client.can_send_version() self.assertEqual(self.can_send, can_send)
def test_cast_to_target(self): target = messaging.Target(**self.ctor) expect_target = messaging.Target(**self.expect) transport = _FakeTransport(self.conf) client = messaging.RPCClient(transport, target) self.mox.StubOutWithMock(transport, '_send') msg = dict(method='foo', args={}) if 'namespace' in self.expect: msg['namespace'] = self.expect['namespace'] if 'version' in self.expect: msg['version'] = self.expect['version'] transport._send(expect_target, {}, msg, retry=None) self.mox.ReplayAll() if self.prepare: client = client.prepare(**self.prepare) if self.double_prepare: client = client.prepare(**self.prepare) client.cast({}, 'foo')
def __init__(self, transport, target, cast=False, name=None, **kwargs): self.name = name or "functional-tests" self.cast = cast self.client = messaging.RPCClient(transport, target, **kwargs)
datefmt='%a, %d %b %Y %H:%M:%S', filename='rpc_client.log', filemode='w') CONF = cfg.CONF default_config_file = './msg.conf' if len(sys.argv) < 2: CONF(['--config-file', default_config_file]) print 'using config file:', default_config_file transport = messaging.get_transport(cfg.CONF) target = messaging.Target(topic='demoservice') client = messaging.RPCClient(transport, target) if __name__ == '__main__': while True: context = {'variableA': 'HAHA'} words = raw_input("input msg to send: ") # change call/cast, or the arguments in prepare()(below following argument keywords) # exchange, topic, namespace, version, server, fanout, timeout, version_cap print print client.prepare(fanout=False).call(context, 'echo', msg=words, sentime=str( datetime.datetime.now())) print
def __init__(self, target): self.__client = messaging.RPCClient( target=target, transport=messaging.get_transport(cfg.CONF), )
def get_client(target, version_cap=None): assert TRANSPORT is not None return messaging.RPCClient(TRANSPORT, target, version_cap=version_cap)
def __init__(self, transport, topic = 'engine'): self.topic = 'gate.%s' % topic self.target = messaging.Target(topic=self.topic, version='1.0') self._client = messaging.RPCClient(transport, self.target)
def __init__(self): transport = messaging.get_transport(CONF) target = messaging.Target(topic='ironic-agent') self.rpc_client = messaging.RPCClient(transport, target)
def _setup_client(self, transport, topic='testtopic'): return messaging.RPCClient(transport, messaging.Target(topic=topic), serializer=self.serializer)
def __init__(self, transport): self._ctx = {} self._topic = cfg.CONF.monitor.topic self._client = messaging.RPCClient(transport, messaging.Target(topic=self._topic))