def test_server_can_bind_on_random_port(self): server = Server(port=None) assert server.port == None listener = server.pull() assert isinstance(server.port, int)
def get_server( port: int, topics: List[str]) -> Dict[str, Callable[[BaseMessageType], None]]: # And assigns a callable to publish messages with the topics' srv = Server(port=port) mapping = { topic: srv.pub(topic=topic.encode('utf-8'), embed_topic=True) for topic in topics } return { k: (lambda packet: v(packet.json().encode('utf-8'))) for k, v in mapping.items() }
def test_published_topic_is_not_included_into_message(self): pub = Server(port=7150).pub(topic=b'topic1') pub(b'topic1 msg') with pytest.raises(ValueError): pub(b'msg')
def main(): listen_for_push = Server(port=helpers.ports['task_manager']).pull() for msg in listen_for_push: obj = json.loads(msg.decode()) if obj['@type'] == 'Measurement': test_conditions(obj) else: print("i don't know what to do with this message")
class ZmqServer: def start(self, port): self._server = Server(port=port) self.pubs = {} def send(self, obj, topic): if not topic in self.pubs: self.pubs[topic] = self._server.pub(topic=topic, embed_topic=True) self.pubs[topic](obj)
def test_port_under_range(self): client = Client() with pytest.raises(ValueError): client.connect_local(port=1023) with pytest.raises(ValueError): client.disconnect_local(port=1023) with pytest.raises(ValueError): Server(port=1023)
def pub_with_empty_topic(): return Server(port=7896).pub(embed_topic=True)
def pub_with_topic(): return Server(port=7895).pub(topic=b'sh')
def pub_with_embedded_topic(): return Server(port=7894).pub(topic=b'sh', embed_topic=True)
def reply_gens(): return Server(port=7892).reply()
def pub(): return Server(port=7893).pub()
def listen_for_push(): return Server(port=7891).pull()
from zeroless import Server # Binds the pair server to port 12345 # And assigns a callable and an iterable # To both transmit and wait for incoming messages pair, listen_for_pair = Server(port=12345).pair() for id, msg in listen_for_pair: print(id, ' - ', msg) pair(msg)
from flask_restful import Api, Resource, reqparse from lib.accounts import authentication as auth from lib.services import utils as services_util import en_us import utils import json import os import traceback from zeroless import (Server) pub = Server(port=35893).pub() class start(Resource): def post(self, client_id, service_id): request_token = request.headers.get('authorization') auth_status = auth.verify(client_id, request_token) if auth_status != 200: return auth_status service = utils.get_service(client_id, service_id) if service.count() == 0: return en_us.NOT_FOUND command = { "serviceid": service_id,
def start(self, port): self._server = Server(port=port) self.pubs = {}
def test_port_already_used(self): listen_for_push = Server(port=65000).pull() with pytest.raises(ValueError): Server(port=65000).pull()
import logging from time import sleep from zeroless import (Server, log) # Setup console logging consoleHandler = logging.StreamHandler() log.setLevel(logging.DEBUG) log.addHandler(consoleHandler) # Binds the publisher server to port 12345 # And assigns a callable to publish messages with the topic 'sh' pub = Server(port=12345).pub(topic=b'sh', embed_topic=True) # Gives publisher some time to get initial subscriptions sleep(1) for msg in [b"Msg1", b"Msg2", b"Msg3"]: pub(msg)
def test_published_topic_is_not_bytes(self): Server(port=7099).pub(topic=b'topic1') with pytest.raises(TypeError): Server(port=7100).pub(topic=u'topic1')
import logging from zeroless import (Server, log) # Setup console logging consoleHandler = logging.StreamHandler() log.setLevel(logging.DEBUG) log.addHandler(consoleHandler) # Binds the reply server to port 12345 # And assigns a callable and an iterable # To both transmit and wait for incoming messages reply, listen_for_request = Server(port=12345).reply() for msg in listen_for_request: print(msg) reply(msg)
from time import sleep, time from zeroless import (Server, Client) # Binds the publisher server to port 12345 # And assigns a callable to publish messages with the topic 'sh' pub = Server(port=12345).pub(topic=b'topic_name', embed_topic=True) # Gives publisher some time to get initial subscriptions while True: pub(b"%f" % time()) sleep(0.2)
def test_server_port_property(self): port = 1050 server = Server(port=port) assert server.port == port
mqtt_port = int(environ['MQTT_PORT']) zmq_client = Client() zmq_client.connect_local(port=helpers.ports['device_manager']) zmq_push = zmq_client.push() def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) client.subscribe("gateways/test") def on_message(client, userdata, msg): zmq_push(msg.payload) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.connect(mqtt_broker_ip, mqtt_port, 60) #Listen for gateways' MQTT messages client.loop_start() #Listen for DeviceManager ZMQ messages listen_for_push = Server(port=helpers.ports['broker']).pull() for msg in listen_for_push: print(msg.decode()) client.publish("gateways/broker",msg.decode()) client.loop_stop()
from zeroless import Server from time import sleep # Binds the publisher server to port 12345 # And assigns a callable to publish messages with no topic specified pub = Server(port=12345).pub(embed_topic=True) # Gives publisher some time to get initial subscriptions sleep(1) for id, msg in [(b"1", b"Msg1"), (b"2", b"Msg2"), (b"3", b"Msg3")]: pub(id, msg)
def listen_for_pair(): _, listen = Server(port=7890).pair() return listen
import logging from zeroless import (Server, log) # Setup console logging consoleHandler = logging.StreamHandler() log.setLevel(logging.DEBUG) log.addHandler(consoleHandler) # Binds the pull server to port 12345 # And assigns an iterable to wait for incoming messages listen_for_push = Server(port=12345).pull() for msg in listen_for_push: print(msg)
def main(): listen_for_push = Server(port=helpers.ports['device_manager']).pull() for msg in listen_for_push: obj = json.loads(msg.decode()) #Measurement if obj['@type'] == 'Measurement': if verify('devices', obj['dev:wasMeasuredBy']): create('measurements', obj) push_to_task(msg) else: notify_unknown_object('Device', obj['dev:wasMeasuredBy']) #Device elif obj['@type'] == 'Device': if verify('platforms', obj['dev:hasPlatform']): create('devices', obj) else: notify_unknown_object('Platform', obj['dev:hasPlatform']) #Platform elif obj['@type'] == 'Platform': if verify('continuous_sensors', obj['dev:hasSensor']) or\ verify('discrete_sensors', obj['dev:hasSensor']): if verify('continuous_actuators', obj['dev:hasActuator']) or\ verify('discrete_actuators', obj['dev:hasActuator']): create('platforms', obj) else: notify_unknown_object('Actuator', obj['dev:hasActuator']) else: notify_unknown_object('Sensor', obj['dev:hasSensor']) #ContinuousSensor elif obj['@type'] == 'ContinuousSensor': if verify('units', obj['sense:canMeasure']): create('continuous_sensors', obj) else: notify_unknown_object('Unit', obj['sense:canMeasure']) #DiscreteSensor elif obj['@type'] == 'DiscreteSensor': if verify('variables', obj['sense:canMeasure']): create('discrete_sensors', obj) else: notify_unknown_object('Variable', obj['sense:canMeasure']) #ContinuousActuator elif obj['@type'] == 'ContinuousActuator': if verify('variables', obj['actuator:increases']): create('continuous_actuators', obj) else: notify_unknown_object('Variable', obj['actuator:increases']) #DiscreteActuator elif obj['@type'] == 'DiscreteActuator': if verify('variables', obj['actuator:changeState']): create('discrete_actuators', obj) else: notify_unknown_object('Variable', obj['actuator:changeState']) #Unit elif obj['@type'] == 'Unit': if verify('variables', obj['sense:unitOf']): create('units', obj) else: notify_unknown_object('Variable', obj['sense:unitOf']) #Variable elif obj['@type'] == 'Variable': create('variables', obj)