Beispiel #1
0
class ZookeeperCoordinator(object):
    """Network coordinator that discovers nodes using zookeeper."""

    def __init__(self, mesh, localnode, coordinator, address=None, port=45429):
        self.framework = ZookeeperFramework(coordinator, chroot='/pyact')
        self.localnode = localnode
        self.mesh = mesh
        self.port = port
        self.address = address

    def _publish(self):
        """Publish the local node."""
        self.framework.create().parents_if_needed().as_ephemeral().with_data(
            '%s:%d' % (self.address, self.port)).for_path(
            os.path.join('nodes', self.localnode.id))

    def accept(self, socket, address):
        """Accept an incoming connection.

        Note that this is called in an isolated greenlet, we can
        therefor block.
        """
        return handle_connection(socket, self.mesh)

    def start(self):
        """Start the coordinator.

        This will connect to the ZooKeeper cluster and register our
        local node.  It will also establish connections to other
        parties of the mesh.
        """
        self.server = StreamServer(('0.0.0.0', self.port), self.accept)
        self.server.start()
        self.framework.connect()
Beispiel #2
0
class ZookeeperCoordinator(object):
    """Network coordinator that discovers nodes using zookeeper."""

    def __init__(self, mesh, localnode, coordinator, address=None, port=45429):
        self.framework = ZookeeperFramework(coordinator, chroot='/pyact')
        self.localnode = localnode
        self.mesh = mesh
        self.port = port
        self.address = address

    def _publish(self):
        """Publish the local node."""
        self.framework.create().parents_if_needed().as_ephemeral().with_data(
            '%s:%d' % (self.address, self.port)).for_path(
            os.path.join('nodes', self.localnode.id))

    def accept(self, socket, address):
        """Accept an incoming connection.

        Note that this is called in an isolated greenlet, we can
        therefor block.
        """
        return handle_connection(socket, self.mesh)

    def start(self):
        """Start the coordinator.

        This will connect to the ZooKeeper cluster and register our
        local node.  It will also establish connections to other
        parties of the mesh.
        """
        self.server = StreamServer(('0.0.0.0', self.port), self.accept)
        self.server.start()
        self.framework.connect()
Beispiel #3
0
def bootstrap_node(name, zookeeper):
    """Bootstrap a node."""
    framework = ZookeeperFramework(zookeeper, chroot='/pyact')
    framework.connect()
    dispatcher = ZooKeeperDispatcher(name, framework)
    dispatcher.start()
    return dispatcher
Beispiel #4
0
def consume(framework):
    def callback(messages):
        for message in messages:
            print message
    c = consumer.Consumer(framework, 'example-group')
    c.start()
    c.subscribe('test', 0.200).start(callback)
    while True:
        gevent.sleep(5)


def produce(framework):
    p = producer.Producer(framework, 'test')
    p.start()

    while True:
        p.send(["hello there on the other side"])
        gevent.sleep(2)


logging.basicConfig(level=logging.DEBUG)

framework = ZookeeperFramework('localhost:2181', 10)
framework.connect()

gevent.spawn(consume, framework)
gevent.spawn(produce, framework)

while True:
    gevent.sleep(10)
Beispiel #5
0
 def __init__(self, mesh, localnode, coordinator, address=None, port=45429):
     self.framework = ZookeeperFramework(coordinator, chroot='/pyact')
     self.localnode = localnode
     self.mesh = mesh
     self.port = port
     self.address = address
Beispiel #6
0
 def __init__(self, mesh, localnode, coordinator, address=None, port=45429):
     self.framework = ZookeeperFramework(coordinator, chroot='/pyact')
     self.localnode = localnode
     self.mesh = mesh
     self.port = port
     self.address = address