Ejemplo n.º 1
0
 def publish(self, topic, value):
     print("publishing topic: {}, data: {}".format(topic, value))
     message_id = str(uuid.uuid4())
     message_sent_at_timestamp = datetime.now().strftime('%Y-%m-%dT%H::%M::%S.%f')
     host_ip = hip.get_host_ip()
     # topic:data:message_id:message_sent_at_timestamp
     published_data = topic + "#" + str(value) + "#" + message_id + "#" + message_sent_at_timestamp + '#' + host_ip
     self.socket.send_string(published_data)
import zmq
import zk_clientservice as kzcl
import constants as const
import multiprocessing as mp
import time
import os
from pathlib import Path
'''
 args python3 {direct or broker} {zookeeper_ip:port} topic1 topic2
 1. Get current active broker_ip:port from zookeeper
k 2. Retrieve publishers for the topics of the interest
 3. Watch for the active broker node in zookeeper
'''
# e.g args "python3 subscriber_app.py direct 127.0.0.1:2181 topic1 topic2"
# capture subscriber IP for use in logger_function
subscriber_ip = hip.get_host_ip()
publishers = []
# The process to run the subscribers
process_list = []
# Extract the strategy to discover and disseminate the messages
strategy = ""
if len(sys.argv) > 1:
    strategy = sys.argv[1]

if strategy != "direct" and strategy != "broker":
    print("Please submit valid strategy (direct || broker)")
    sys.exit()

# Get zookeeper ip and port, passed in arg[2] as ip:port e.g. 127.0.0.1:2181
zookeeper_ip_port = ""
if len(sys.argv) > 2:
 def test_create_emphemeral_node(self):
     node_path = '/my/znode/n_'
     create_node_path = self.kzclient.create(node_path, hip.get_host_ip(), makepath=True, ephemeral=True,
                                             sequence=True)
     self.assertTrue(len(create_node_path) == len(node_path) + 10)
# Register publisher ip and port to the lamebroker
kzclient = kzcl.ZkClientService()
publisher_port = ""
if strategy == "direct":
    # get the publisher port
    if len(sys.argv) > 3:
        publisher_port = sys.argv[3]

    # Add additional topics if provided for the direct strategy
    if len(sys.argv) > 4:
        for arg in sys.argv[4:]:
            publish_topics.append(arg)

    # Register the publisher to the zookeeper
    publisher_ip_port = hip.get_host_ip() + ":" + publisher_port
    print("Connecting to zookeeper at ip:port=> {}".format(zookeeper_ip_port))
    register_publisher_data_to_zookeeper = publisher_ip_port + '#'

    counter = 1
    for topic in publish_topics:
        if counter < len(publish_topics):
            register_publisher_data_to_zookeeper = register_publisher_data_to_zookeeper + topic + ','
        else:
            register_publisher_data_to_zookeeper = register_publisher_data_to_zookeeper + topic
        counter = counter + 1
    print("Registering publisher to the broker: {}".format(
        register_publisher_data_to_zookeeper))
    kzclient.create_node(
        const.PUBLISHERS_ROOT_PATH + const.PUBLISHERS_NODE_PREFIX,
        register_publisher_data_to_zookeeper, True, True)