def test_serve(): channel = Channel(uri=URI, exchange=EXCHANGE) service = ServiceProvider(channel) topic = "MyService" service.delegate(topic, my_service, Struct, Int64Value) subscription = Subscription(channel) sent = Message(content="body".encode('latin')) channel.publish(sent, topic=subscription.name) recv = channel.consume(timeout=1.0) assert recv.subscription_id == subscription.id # Trying to serve a message from another subscription should fail assert service.should_serve(recv) is False with pytest.raises(RuntimeError): service.serve(recv) sent.topic = topic channel.publish(message=sent) recv = channel.consume(timeout=1.0) assert service.should_serve(recv) is True service.serve(recv) channel.close()
def awarenessOn(channel): message = Message() condition = Struct() condition["enabled"] = True message.pack(condition) message.topic = "RobotGateway.0.SetAwareness" channel.publish(message)
def test_create_reply_empty_request(): request = Message() request.topic = "topic" reply = request.create_reply() assert reply.topic == request.reply_to assert reply.correlation_id == request.correlation_id assert reply.content_type == request.content_type
def commandNavigateTo(posX, posY, channel): goal = Position() goal.x = posX goal.y = posY goal.z = 0 goalMessage = Message() goalMessage.pack(goal) goalMessage.topic = "RobotGateway.0.NavigateTo" channel.publish(goalMessage)
def makeMessage(linear, angular): message = Message() robotConfig = RobotConfig() robotConfig.speed.linear = linear robotConfig.speed.angular = angular message.pack(robotConfig) message.topic = "RobotGateway.0.SetConfig" # Public message channel.publish(message)
def makeMessage(linear, angular): # Create message with command for # linear and angular velocities message = Message() robotConfig = RobotConfig() robotConfig.speed.linear = linear robotConfig.speed.angular = angular message.pack(robotConfig) message.topic = "RobotGateway.0.SetConfig" # Public message channel.publish(message)
def test_injection(): tracer = Tracer() with tracer.span(name="span_name") as span: message = Message() message.inject_tracing(span) assert message.metadata['x-b3-sampled'] == '1' assert message.metadata['x-b3-traceid'] == str( tracer.tracer.span_context.trace_id) assert message.metadata['x-b3-flags'] == '0' assert message.metadata['x-b3-spanid'] == str(span.span_id) assert message.metadata['x-b3-parentspanid'] == '0000000000000000'
def awarenessOff(channel): ''' message = Message() condition = Struct() condition["enabled"] = False message.pack(condition) message.topic = "RobotGateway.0.SetAwareness" channel.publish(message) ''' message = Message() message.topic = "RobotGateway.0.PauseAwareness" channel.publish(message)
def request_serve_consume(number): struct = Struct() struct.fields["value"].number_value = number message = Message(struct) message.reply_to = subscription message.pack(struct) channel.publish(topic="MyService", message=message) request = channel.consume(timeout=1.0) assert request.body == message.body service.serve(request) assert request.unpack(Struct) == struct return channel.consume(timeout=1.0)
def makeTurn(linear, angular, channel): # Create message with command for # # linear and angular velocities message = Message() robotConfig = RobotConfig() robotConfig.speed.linear = linear robotConfig.speed.angular = angular message.pack(robotConfig) message.topic = "RobotGateway.0.SetConfig" # Public message channel.publish(message) t_a = datetime.now() t_b = t_a t_diff = relativedelta(t_b, t_a) while t_diff.seconds < 4: t_b = datetime.now() t_diff = relativedelta(t_b, t_a) robotConfig.speed.linear = 0 robotConfig.speed.angular = 0 message.pack(robotConfig) message.topic = "RobotGateway.0.SetConfig" # Public message channel.publish(message) t_a = datetime.now() t_b = t_a t_diff = relativedelta(t_b, t_a) while t_diff.seconds < 2: t_b = datetime.now() t_diff = relativedelta(t_b, t_a)
def commandMoveTo(posX, posY, channel): goal = Pose() goal.position.x = posX goal.position.y = posY goal.position.z = 0 goal.orientation.yaw = math.atan2(goal.position.y, goal.position.x) goal.orientation.pitch = 0 goal.orientation.roll = 0 goalMessage = Message() goalMessage.pack(goal) goalMessage.topic = "RobotGateway.0.MoveTo" channel.publish(goalMessage)
def run(self): while True: msg = self.channel.consume() self.log.debug("topic='{}' metadata={}", msg.topic, msg.metadata) if msg.metadata["destination_kind"] != "queue" or \ msg.metadata["source_name"] != "is": continue event = msg.topic.split('.')[-1] topic = msg.metadata["routing_key"] queue = msg.metadata["destination_name"] if event == "created": self.consumers.info[topic].consumers.append(queue) elif event == "deleted": self.consumers.info[topic].consumers.remove(queue) if len(self.consumers.info[topic].consumers) == 0: del self.consumers.info[topic] self.log.info("event='{}' topic='{}' queue='{}'", event, topic, queue) self.channel.publish( Message(content=self.consumers), topic="BrokerEvents.Consumers", )
def request(self, content, topic, timeout_ms, metadata=None): if not self.can_request(): raise Exception( "Can't request more than {}. Use 'RequestManager.can_request' " "method to check if you can do requests.") tracer = Tracer( exporter=self._zipkin_exporter) if self._do_tracing else None span = tracer.start_span(name='request') if self._do_tracing else None msg = Message(content=content) msg.topic = topic msg.reply_to = self._subscription msg.timeout = timeout_ms / 1000.0 self._log.debug("[Sending] metadata={}, cid={}", metadata, msg.correlation_id) if self._do_tracing: for key, value in (metadata or {}).items(): span.add_attribute(key, value) tracer.end_span() msg.inject_tracing(span) self._publish(msg, metadata) if len(self._requests) >= self._max_requests: self._can_request = False
def test_constructor(): body = "body".encode('latin') reply_to = "reply_to" ctype = ContentType.JSON msg = Message(content=body, reply_to=reply_to, content_type=ctype) assert msg.body == body assert msg.reply_to == reply_to assert msg.content_type == ctype
def test_body(size): channel = Channel(uri=URI, exchange=EXCHANGE) subscription = Subscription(channel) subscription.subscribe("MyTopic.Sub.Sub") sent = Message() sent.reply_to = subscription sent.topic = "MyTopic.Sub.Sub" sent.body = bytes(bytearray(range(256)) * int(size)) channel.publish(message=sent) received = channel.consume(timeout=1.0) assert repr(sent.body) == repr(received.body) assert sent.body == received.body channel.close()
def test_extraction(): message = Message() message.metadata['x-b3-sampled'] = '1' message.metadata['x-b3-traceid'] = 'f047c6f208eb36ab' message.metadata['x-b3-flags'] = '0' message.metadata['x-b3-spanid'] = 'ef81a2f9c261473d' message.metadata['x-b3-parentspanid'] = '0000000000000000' context = message.extract_tracing() assert context.trace_id == 'f047c6f208eb36ab' assert context.span_id == 'ef81a2f9c261473d'
def test_propagation(): topic = "span_test" channel = Channel(uri=URI, exchange=EXCHANGE) subscription = Subscription(channel) subscription.subscribe(topic) tracer = Tracer() with tracer.span(name="span_name") as span: span_id = span.span_id message = Message() message.body = "body".encode('latin1') message.inject_tracing(span) channel.publish(topic=topic, message=message) message2 = channel.consume(timeout=1.0) span_context = message2.extract_tracing() assert span_context.span_id == span_id assert span_context.trace_id == tracer.tracer.span_context.trace_id
def test_type_safety(property, valid_types, invalid_types): message = Message() for value in valid_types: print("valid value=", value) setattr(message, property, value) assert getattr(message, property) == value for value in invalid_types: print("invalid value=", value) with pytest.raises(TypeError): setattr(message, property, value)
def test_empty_topic(): channel = Channel(uri=URI, exchange=EXCHANGE) message = Message(content="body".encode('latin')) with pytest.raises(RuntimeError): channel.publish(message) with pytest.raises(RuntimeError): channel.publish(message, topic="") subscription = Subscription(channel) channel.publish(message, topic=subscription.name) recv = channel.consume(timeout=1.0) assert recv.body == message.body message.topic = subscription.name channel.publish(message) recv = channel.consume(timeout=1.0) assert recv.body == message.body channel.close()
def test_channel(): channel = Channel(uri=URI, exchange=EXCHANGE) subscription = Subscription(channel) subscription.subscribe("MyTopic.Sub.Sub") struct = Struct() struct.fields["value"].number_value = 666.0 sent = Message(struct) sent.reply_to = subscription sent.created_at = int(1000 * now()) / 1000.0 sent.timeout = 1.0 sent.topic = "MyTopic.Sub.Sub" channel.publish(message=sent) received = channel.consume(timeout=1.0) assert sent.reply_to == received.reply_to assert sent.subscription_id == received.subscription_id assert sent.content_type == received.content_type assert sent.body == received.body assert sent.status == received.status assert sent.topic == received.topic assert sent.correlation_id == received.correlation_id assert sent.timeout == received.timeout assert sent.metadata == received.metadata assert sent.created_at == received.created_at assert str(sent) == str(received) struct2 = received.unpack(Struct) assert str(struct) == str(struct2) assert struct == struct2 channel.close()
def run(self, id, broker_uri): service_name = "CameraGateway.{}".format(id) channel = Channel(broker_uri) server = ServiceProvider(channel) logging = LogInterceptor() server.add_interceptor(logging) server.delegate(topic=service_name + ".GetConfig", request_type=FieldSelector, reply_type=CameraConfig, function=self.get_config) server.delegate(topic=service_name + ".SetConfig", request_type=CameraConfig, reply_type=Empty, function=self.set_config) self.driver.start_capture() self.logger.info("Listening for requests") while True: image = self.driver.grab_image() channel.publish(Message(content=image), topic=service_name + ".Frame") pose = self.driver.get_pose() frameTransList = FrameTransformations() frameTransList.tfs.extend([pose]) channel.publish(Message(content=frameTransList), topic=service_name + ".FrameTransformations") try: message = channel.consume(timeout=0) if server.should_serve(message): server.serve(message) except socket.timeout: pass
def test_multi_subscription(): channel = Channel(uri=URI, exchange=EXCHANGE) message = Message() subscription1 = Subscription(channel) subscription2 = Subscription(channel) channel.publish(message, topic=subscription1.name) recv = channel.consume(timeout=1.0) assert recv.subscription_id == subscription1.name channel.publish(message, topic=subscription2.name) recv = channel.consume(timeout=1.0) assert recv.subscription_id == subscription2.name channel.close()
def test_create_reply(): request = Message() request.topic = "topic" request.reply_to = "reply_to" request.content_type = ContentType.JSON reply = request.create_reply() assert reply.topic == request.reply_to assert reply.correlation_id == request.correlation_id assert reply.content_type == request.content_type
def consume_ready(self, timeout=1.0): received_msgs = [] # wait for new message try: stated_at = now() while True: _timeout = max(0.0, stated_at + timeout - now()) msg = self._channel.consume(timeout=_timeout) if msg.status.ok() and msg.has_correlation_id(): cid = msg.correlation_id if cid in self._requests: received_msgs.append( (msg, self._requests[cid]["metadata"])) del self._requests[cid] except socket.timeout: pass # check for timeouted requests for cid in self._requests.keys(): timeouted_msg = self._requests[cid]["msg"] if timeouted_msg.deadline_exceeded(): msg = Message() msg.body = timeouted_msg.body msg.topic = timeouted_msg.topic msg.reply_to = self._subscription msg.timeout = timeouted_msg.timeout metadata = self._requests[cid]["metadata"] del self._requests[cid] self._log.debug("[Retring] metadata={}, cid={}", metadata, msg.correlation_id) self._publish(msg, metadata) if not self._can_request and len(self._requests) <= self._min_requests: self._can_request = True return received_msgs
port=ops.zipkin_port, transport=BackgroundThreadTransport(max_batch_size=20), ) subscription.subscribe('Skeletons.Localization') sks_hm = SkeletonsHeatmap(ops) period = ops.period_ms / 1000.0 deadline = now() while True: deadline += period msgs = [] while True: try: msgs.append(channel.consume_until(deadline=deadline)) except: break span_context = msgs[-1].extract_tracing() if len(msgs) > 0 else None tracer = Tracer(exporter=exporter, span_context=span_context) with tracer.span(name='Render') as span: sks_list = list(map(lambda x: x.unpack(ObjectAnnotations), msgs)) sks_hm.update_heatmap(sks_list) im_pb = sks_hm.get_pb_image() msg = Message() msg.topic = service_name msg.pack(im_pb) msg.inject_tracing(span) channel.publish(msg)
def main(): service_name = "CameraGateway" log = Logger(service_name) options = load_options() camera = CameraGateway(fps=options["fps"]) publish_channel = Channel(options['broker_uri']) rpc_channel = Channel(options['broker_uri']) server = ServiceProvider(rpc_channel) logging = LogInterceptor() server.add_interceptor(logging) server.delegate(topic=service_name + ".*.GetConfig", request_type=FieldSelector, reply_type=CameraConfig, function=camera.get_config) server.delegate(topic=service_name + ".*.SetConfig", request_type=CameraConfig, reply_type=Empty, function=camera.set_config) exporter = create_exporter(service_name=service_name, uri=options["zipkin_uri"]) while True: # iterate through videos listed for video in options['videos']: # id of the first sequence of videos person_id = video['person_id'] gesture_id = video['gesture_id'] # getting the path of the 4 videos video_files = { cam_id: os.path.join( options['folder'], 'p{:03d}g{:02d}c{:02d}.mp4'.format(person_id, gesture_id, cam_id)) for cam_id in options["cameras_id"] } for iteration in range(video['iterations']): info = { "person": person_id, "gesture": gesture_id, "iteration": iteration } log.info('{}', str(info).replace("'", '"')) # object that let get images from multiples videos files video_loader = FramesLoader(video_files) # iterate through all samples on video while True: time_initial = time.time() # listen server for messages about change try: message = rpc_channel.consume(timeout=0) if server.should_serve(message): server.serve(message) except socket.timeout: pass frame_id, frames = video_loader.read() for cam in sorted(frames.keys()): tracer = Tracer(exporter) span = tracer.start_span(name='frame') pb_image = to_pb_image(frames[cam]) msg = Message(content=pb_image) msg.inject_tracing(span) topic = 'CameraGateway.{}.Frame'.format(cam) publish_channel.publish(msg, topic=topic) tracer.end_span() took_ms = (time.time() - time_initial) * 1000 dt = (1 / camera.fps) - (took_ms / 1000) if dt > 0: time.sleep(dt) info = { "sample": frame_id, "took_ms": took_ms, "wait_ms": dt * 1000 } log.info('{}', str(info).replace("'", '"')) if frame_id >= (video_loader.num_samples - 1): video_loader.release() del video_loader gc.collect() break if options['loop'] is False: break
'--uri', '-u', type=str, required=False, help='broker_uri', default='amqp://localhost:5672') args = parser.parse_args() device = args.device fps = args.fps broker_uri = args.uri cap = VideoCapture(device) if cap.isOpened(): log.info('Connected to device: /dev/video{}'.format(device)) else: log.error('Coudn\'t find device: /dev/video{}'.format(device)) exit() log.info('Camera FPS set to {}'.format(fps)) cap.set(CAP_PROP_FPS, fps) channel = StreamChannel(broker_uri) log.info('Connected to broker {}', broker_uri) log.info('Starting to capture') while True: ret, frame = cap.read() msg = Message() msg.topic = 'CameraGateway.{}.Frame'.format(args.id) msg.pack(get_pb_image(frame)) channel.publish(msg)
sonar_topic = "RobotGateway.{}.SonarScan".format(op_config.robot_parameters.id) pose_topic = "RobotGateway.{}.Pose".format(op_config.robot_parameters.id) # instania channel passando endereco do broker # estabelece conexao com o broker channel = Channel(op_config.broker_uri) ATSPlog.info("event=ChannelInitDone") # Inscreve nos topicos que deseja receber mensagem subscription = Subscription(channel) subscription.subscribe(pose_topic) subscription.subscribe(sonar_topic) ATSPlog.info("SubscriptionsDone") # Envia mensagem de getConfig get_req = Message(reply_to=subscription) # Salva correlation_id do request cor_id = get_req.correlation_id # print("cor_id: ", cor_id) # Broadcast message to anyone interested (subscribed) channel.publish(message=get_req, topic=service_name + ".GetConfig") # Envia msg de setConfig config = RobotConfig() config.speed.linear = 0.5 config.speed.angular = 0.0 set_req = Message(content=config, reply_to=subscription) # Broadcast message to anyone interested (subscribed) channel.publish(topic=service_name + ".SetConfig", message=set_req) print("Andou!")
from is_wire.core import Channel, Subscription, Message from is_msgs.robot_pb2 import RobotConfig import os from sys import argv uri = os.environ[ "BROKER_URI"] if "BROKER_URI" in os.environ else "amqp://10.10.2.20:30000" channel = Channel(uri) subscription = Subscription(channel) config = RobotConfig() config.speed.linear = float(argv[1] or 0.0) config.speed.angular = float(argv[2] if len(argv) is 3 else 0.0) channel.publish(message=Message(content=config, reply_to=subscription), topic="RobotGateway.0.SetConfig") reply = channel.consume(timeout=1.0) print reply.status channel.publish(message=Message(reply_to=subscription), topic="RobotGateway.0.GetConfig") reply = channel.consume(timeout=1.0) print reply.unpack(RobotConfig)
state = State.MAKE_REQUESTS frame_fetcher = FrameVideoFetcher(video_files=pending_videos, base_folder=options.folder) while True: if state == State.MAKE_REQUESTS: state = State.RECV_REPLIES if len(requests) < MIN_REQUESTS: while len(requests) <= MAX_REQUESTS: base_name, frame_id, frame = frame_fetcher.next() if frame is None: if len(requests) == 0: state = State.EXIT break pb_image = make_pb_image(frame) msg = Message(content=pb_image, reply_to=subscription) msg.timeout = DEADLINE_SEC channel.publish(msg, topic='SkeletonsDetector.Detect') requests[msg.correlation_id] = { 'content': pb_image, 'base_name': base_name, 'frame_id': frame_id, 'requested_at': time.time() } continue elif state == State.RECV_REPLIES: try: msg = channel.consume(timeout=1.0) if msg.status.ok():