def test_data_is_not_bytes(self): client = Client() client.connect_local(port=7050) push = client.push() with pytest.raises(TypeError): push(u'msg')
def test_client_addresses_property(self): client = Client() addresses = (('10.0.0.1', 1567), ('10.0.0.2', 1568), ('10.0.0.3', 1569)) for ip, port in addresses: client.connect(ip, port) assert client.addresses == addresses
def test_connection_already_exist(self): client = Client() client.connect_local(port=1024) with pytest.raises(ValueError): client.connect_local(port=1024) client.disconnect_local(port=1024) client.connect_local(port=1024)
def get_listener(*channels: Channel): # Connects the client to as many servers as desired client = Client() # connect to all ports ports = sum([channel.ports for channel in channels], []) for port in ports: client.connect_local(port=port) # Assigns an iterable to wait for incoming messages with the topics return get_mapped_stream(client, *channels)
def test_there_was_no_connection_to_disconnect(self): client = Client() client.connect_local(port=1024) with pytest.raises(ValueError): client.disconnect_local(port=1025) client.disconnect_local(port=1024) with pytest.raises(ValueError): client.disconnect_local(port=1024)
def test_subscribed_topics_are_not_bytes(self): client = Client() client.connect_local(port=7099) client.sub(topics=[b'topic1']) with pytest.raises(TypeError): client.sub(topics=[u'topic1']) with pytest.raises(TypeError): client.sub(topics=[b'topic1', u'topic2'])
def test_port_on_range_cascaded(self): Client()\ .connect_local(port=1024)\ .disconnect_local(port=1024)\ .connect_local(port=7000)\ .disconnect_local(port=7000)\ .connect_local(port=65535)\ .disconnect_local(port=65535)
def test_disconnect_all_cascaded(self): Client()\ .connect_local(port=1024)\ .connect_local(port=1025)\ .connect_local(port=1026)\ .connect_local(port=1027)\ .disconnect_all()\ .connect_local(port=1024)\ .connect_local(port=1025)\ .connect_local(port=1026)\ .connect_local(port=1027)
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 listen_for_pub_with_empty_topic(): client = Client() client.connect(ip='127.0.0.1', port=7895) return client.sub()
def request_gens(): client = Client() client.connect_local(port=7892) return client.request()
from aura.managers import StorageManager as db from aura.managers import SemanticManager as graph from zeroless import Client, Server from aura.managers import helpers import json zmq_broker = Client() zmq_broker.connect_local(port=helpers.ports["broker"]) push_to_broker = zmq_broker.push() zmq_task = Client() zmq_task.connect_local(port=helpers.ports["task_manager"]) push_to_task = zmq_task.push() def verify(collection, obj_id): return db.get(collection, obj_id) != None def create(collection, obj): if not verify(collection, obj["@id"]): graph.parse(json.dumps(obj)) db.store(collection, obj) else: print("Already have " + str(obj["@id"]) + " in database.") def notify_unknown_object(obj_type, obj_id): obj = {} obj["id"] = obj_id obj["type"] = obj_type
from zeroless import Client # Connects the client to as many servers as desired client = Client() client.connect_local(port=12345) # Initiate a request client # And assigns a callable and an iterable # To both transmit and wait for incoming messages request, listen_for_reply = client.request() for id, msg in [(b"1", b"Msg1"), (b"2", b"Msg2"), (b"3", b"Msg3")]: request(id, msg) response = next(listen_for_reply) print(response)
import logging from zeroless import (Client, log) # Setup console logging consoleHandler = logging.StreamHandler() log.setLevel(logging.DEBUG) log.addHandler(consoleHandler) # Connects the client to as many servers as desired client = Client() client.connect_local(port=12345) # Initiate a push client # And assigns a callable to push messages push = client.push() for msg in [b"Msg1", b"Msg2", b"Msg3"]: push(msg)
def __build_watcher(self): client = Client() client.connect_local(port=self.PORT) return client
def test_pair_client_cannot_connect_more_than_once(self): client = Client() client.connect_local(port=7200) client.connect_local(port=7201) with pytest.raises(RuntimeError): client.pair() client = Client() client.connect_local(port=7200) client.pair() with pytest.raises(RuntimeError): client.connect_local(port=7201)
def listen_for_pub(): client = Client() client.connect_local(port=7893) return client.sub()
import logging from zeroless import (Client, log) # Setup console logging consoleHandler = logging.StreamHandler() log.setLevel(logging.DEBUG) log.addHandler(consoleHandler) # Connects the client to as many servers as desired client = Client() client.connect_local(port=12345) # Initiate a subscriber client # Assigns an iterable to wait for incoming messages with the topic 'sh' listen_for_pub = client.sub(topics=[b'sh']) for topic, msg in listen_for_pub: print(topic, ' - ', msg)
def listen_for_pub_with_embedded_topic(): client = Client() client.connect(ip='127.0.0.1', port=7894) return client.sub(topics=[b'sh'])
def listen_for_pub_with_embedded_topic(): client = Client() client.connect_local(port=7894) return client.sub(topics=[b'sh'])
from zeroless import Client # Connects the client to a single server client = Client() client.connect_local(port=12345) # Initiate a pair client # And assigns a callable to transmit messages pair, _ = client.pair() for id, msg in [(b"1", b"Msg1"), (b"2", b"Msg2"), (b"3", b"Msg3")]: pair(id, msg)
def listen_for_pub_with_empty_topic(): client = Client() client.connect_local(port=7896) return client.sub()
def test_connection_after_pattern_was_established(self): client = Client() listen_for_push = client.pull() client.connect_local(port=1024) with pytest.raises(ValueError): client.connect_local(port=1024) client.disconnect_local(port=1024) with pytest.raises(ValueError): client.disconnect_local(port=1024)
#!/usr/bin/env python3 from os import environ import paho.mqtt.client as mqtt from aura.managers import helpers from zeroless import Client, Server mqtt_broker_ip = environ['MQTT_BROKER_IP'] 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
from zeroless import (Client, Server) import json from aura.managers import SemanticManager as graph from aura.managers import StorageManager as db from aura.managers import helpers zmq_device = Client() zmq_device.connect_local(port=helpers.ports['device_manager']) push_to_device = zmq_device.push() def create_condition(condition): print("create_condition") # TaskManager -> StorageManager db.store('conditions', condition) def update_condition(): # TaskManager -> StorageManager print("update_condition") def remove_condition(): # TaskManager -> StorageManager print("remove_condition") def send_command(device, command): # TaskManager -> DeviceManager print("send_command")
def push(): client = Client() client.connect_local(port=7891) return client.push()
def test_port_on_range(self): client = Client() client.connect_local(port=1024) client.disconnect_local(port=1024) client.connect_local(port=7000) client.disconnect_local(port=7000) client.connect_local(port=65535) client.disconnect_local(port=65535)
def test_disconnect_all(self): client = Client() client.connect_local(port=1024) client.connect_local(port=1025) client.connect_local(port=1026) client.connect_local(port=1027) client.disconnect_all() client.connect_local(port=1024) client.connect_local(port=1025) client.connect_local(port=1026) client.connect_local(port=1027)
from zeroless import Client # Connects the client to as many servers as desired client = Client() client.connect_local(port=12345) # Initiate a subscriber client # Assigns an iterable to wait for incoming messages with any topic listen_for_pub = client.sub() for topic, id, msg in listen_for_pub: print(topic, ' - ', id, ' - ', msg)
from aura.managers import StorageManager as db from aura.managers import SemanticManager as graph from zeroless import (Client, Server) from aura.managers import helpers import json zmq_broker = Client() zmq_broker.connect_local(port=helpers.ports['broker']) push_to_broker = zmq_broker.push() zmq_task = Client() zmq_task.connect_local(port=helpers.ports['task_manager']) push_to_task = zmq_task.push() def verify(collection, obj_id): return (db.get(collection, obj_id) != None) def create(collection, obj): if not verify(collection, obj['@id']): graph.parse(json.dumps(obj)) db.store(collection, obj) else: print("Already have " + str(obj['@id']) + " in database.") def notify_unknown_object(obj_type, obj_id): obj = {} obj['id'] = obj_id obj['type'] = obj_type push_to_broker(json.dumps(obj).encode()) def send_command(device, command):
def pair(): client = Client() client.connect_local(port=7890) send, _ = client.pair() return send
import logging from zeroless import (Client, log) # Setup console logging consoleHandler = logging.StreamHandler() log.setLevel(logging.DEBUG) log.addHandler(consoleHandler) # Connects the client to a single server client = Client() client.connect_local(port=12345) # Initiate a pair client # And assigns a callable and an iterable # To both transmit and wait for incoming messages pair, listen_for_pair = client.pair() for msg in [b"Msg1", b"Msg2", b"Msg3"]: pair(msg) response = next(listen_for_pair) print(response)