• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Python
  • License
    GNU Lesser Genera...
  • Created about 11 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python port of Zyre

Pyre

This is a Python port of Zyre 1.0, implementing the same ZRE protocol.

Pyre - an open-source framework for proximity-based peer-to-peer applications

Description

Pyre does local area discovery and clustering. A Pyre node broadcasts UDP beacons, and connects to peers that it finds. This class wraps a Pyre node with a message-based API.

All incoming events are messages delivered via the recv call of a Pyre instance. The first frame defines the type of the message, and following frames provide further values:

ENTER fromnode headers
    a new peer has entered the network
EXIT fromnode
    a peer has left the network
JOIN fromnode groupname
    a peer has joined a specific group
LEAVE fromnode groupname
    a peer has joined a specific group
WHISPER fromnode message
    a peer has sent this node a message
SHOUT fromnode groupname message
    a peer has sent one of our groups a message

In SHOUT and WHISPER the message is a single frame in this version of Pyre. In ENTER, the headers frame contains a packed dictionary, that can be unpacked using json.loads(msg) (see chat client).

To join or leave a group, use the join() and leave() methods. To set a header value, use the set_header() method. To send a message to a single peer, use whisper(). To send a message to a group, use shout().

Installation

For now use Pip:

pip install https://github.com/zeromq/pyre/archive/master.zip

API

import pyre
#  Constructor, creates a new Zyre node. Note that until you start the
#  node it is silent and invisible to other nodes on the network.
node = pyre.Pyre()

#  Set node header; these are provided to other nodes during discovery
#  and come in each ENTER message.
node.set_header(name, value)

#  (TODO: Currently a Pyre node starts immediately) Start node, after setting header values. When you start a node it
#  begins discovery and connection.
node.start()

#  Stop node, this signals to other peers that this node will go away.
#  This is polite; however you can also just destroy the node without
#  stopping it.
node.stop()

#  Join a named group; after joining a group you can send messages to
#  the group and all Zyre nodes in that group will receive them.
node.join(group)

#  Leave a group
node.leave(group)

#  Receive next message from network; the message may be a control
#  message (ENTER, EXIT, JOIN, LEAVE) or data (WHISPER, SHOUT).
#  Returns a list of message frames
msgs = node.recv();

# Send message to single peer, specified as a UUID object (import uuid)
# Destroys message after sending
node.whisper(peer, msg)

# Send message to a named group
# Destroys message after sending
node.shout(group, msg);

#  Send string to single peer specified as a UUID string.
#  String is formatted using printf specifiers.
node.whispers(peer, msg_string)

#  Send message to a named group
#  Destroys message after sending
node.shouts(group, msg_string);
    
#  Return handle to the Zyre node, for polling
node.get_socket()
# use node.get_socket().getsockopt(zmq.FD) to acquire 
# the filedescriptor
# Don't use this for getting Pyre events you can use the 
# node.inbox to get those events

Example Chat Client

try:
    from zyre_pyzmq import Zyre as Pyre
except Exception as e:
    print("using Python native module", e)
    from pyre import Pyre 

from pyre import zhelper 
import zmq 
import uuid
import logging
import sys
import json

def chat_task(ctx, pipe):
    n = Pyre("CHAT")
    n.set_header("CHAT_Header1","example header1")
    n.set_header("CHAT_Header2","example header2")
    n.join("CHAT")
    n.start()

    poller = zmq.Poller()
    poller.register(pipe, zmq.POLLIN)
    print(n.socket())
    poller.register(n.socket(), zmq.POLLIN)
    print(n.socket())
    while(True):
        items = dict(poller.poll())
        print(n.socket(), items)
        if pipe in items and items[pipe] == zmq.POLLIN:
            message = pipe.recv()
            # message to quit
            if message.decode('utf-8') == "$$STOP":
                break
            print("CHAT_TASK: %s" % message)
            n.shouts("CHAT", message.decode('utf-8'))
        else:
        #if n.socket() in items and items[n.socket()] == zmq.POLLIN:
            cmds = n.recv()
            msg_type = cmds.pop(0)
            print("NODE_MSG TYPE: %s" % msg_type)
            print("NODE_MSG PEER: %s" % uuid.UUID(bytes=cmds.pop(0)))
            print("NODE_MSG NAME: %s" % cmds.pop(0))
            if msg_type.decode('utf-8') == "SHOUT":
                print("NODE_MSG GROUP: %s" % cmds.pop(0))
            elif msg_type.decode('utf-8') == "ENTER":
                headers = json.loads(cmds.pop(0).decode('utf-8'))
                print("NODE_MSG HEADERS: %s" % headers)
                for key in headers:
                    print("key = {0}, value = {1}".format(key, headers[key]))
            print("NODE_MSG CONT: %s" % cmds)
    n.stop()


if __name__ == '__main__':
    # Create a StreamHandler for debugging
    logger = logging.getLogger("pyre")
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.StreamHandler())
    logger.propagate = False

    ctx = zmq.Context()
    chat_pipe = zhelper.zthread_fork(ctx, chat_task)
    # input in python 2 is different
    if sys.version_info.major < 3:
        input = raw_input

    while True:
        try:
            msg = input()
            chat_pipe.send(msg.encode('utf_8'))
        except (KeyboardInterrupt, SystemExit):
            break
    chat_pipe.send("$$STOP".encode('utf_8'))
    print("FINISHED")

Look at the ZOCP project for examples of how Pyre can be integrated into different environments and frameworks, i.e.:

Pyre uses the Python Logging module. To change the debug level:

    # Create a StreamHandler for debugging
    logger = logging.getLogger("pyre")
    logger.setLevel(logging.INFO)
    # i.e. logging.DEBUG, logging.WARNING
    logger.addHandler(logging.StreamHandler())
    logger.propagate = False

Requirements

Python only needs PyZMQ. On some older versions of Python it also needs the ipaddress module.

The recommended Python version is 3.3+

Project Organization

Pyre is owned by all its authors and contributors. This is an open source project licensed under the LGPLv3. To contribute to Zyre please read the C4.1 process that we use.

To report an issue, use the PYRE issue tracker at github.com.

For more information on this project's maintenance, see MAINTENANCE.md.

More Repositories

1

libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
C++
8,996
star
2

pyzmq

PyZMQ: Python bindings for zeromq
Python
3,480
star
3

netmq

A 100% native C# implementation of ZeroMQ for .NET
C#
2,822
star
4

jeromq

Pure Java ZeroMQ
Java
2,288
star
5

cppzmq

Header-only C++ binding for libzmq
C++
1,737
star
6

zeromq.js

⚡ Node.js bindings to the ØMQ library
TypeScript
1,371
star
7

zmq.rs

A native implementation of ØMQ in Rust
Rust
1,124
star
8

czmq

High-level C binding for ØMQ
C
1,110
star
9

zyre

Zyre - an open-source framework for proximity-based peer-to-peer applications
C
843
star
10

jzmq

Java binding for ZeroMQ
Java
584
star
11

goczmq

goczmq is a golang wrapper for CZMQ.
Go
552
star
12

php-zmq

ZeroMQ for PHP
C
543
star
13

zeromq4-x

ØMQ 4.x stable release branch - bug fixes only
C++
446
star
14

zmqpp

0mq 'highlevel' C++ bindings
C++
418
star
15

zeromq2-x

ØMQ/2.x distribution
C++
365
star
16

malamute

The ZeroMQ Enterprise Messaging Broker
C
311
star
17

gomq

Pure Go Implementation of a Subset of ZeroMQ
Go
290
star
18

clrzmq

CLR (.NET & Mono) binding for 0MQ
C#
272
star
19

filemq

FileMQ is a publish-subscribe file service based on 0MQ
Java
271
star
20

rbzmq

Ruby binding for 0MQ
C
248
star
21

clrzmq4

ZeroMQ C# namespace (.NET and mono, Windows, Linux and MacOSX, x86 and amd64)
C#
235
star
22

zproto

A protocol framework for ZeroMQ
C
228
star
23

zeromq3-x

ØMQ/3.2 release branch - bug fixes only
C++
228
star
24

JSMQ

Javascript client for ZeroMQ/NetMQ
JavaScript
190
star
25

chumak

Pure Erlang implementation of ZeroMQ Message Transport Protocol.
Erlang
189
star
26

erlzmq2

Erlang binding for 0MQ (v2)
C
165
star
27

libcurve

An encryption and authentication library for ZeroMQ applications
C
161
star
28

zproject

CLASS Project Generator
Shell
142
star
29

lzmq

Lua binding to ZeroMQ
Lua
133
star
30

gitdown

Turn github into your publishing platform
Perl 6
130
star
31

zeromq4-1

ZeroMQ 4.1.x stable release branch - bug fixes only
C++
125
star
32

exzmq

ZeroMQ for Elixir
Elixir
116
star
33

fszmq

An F# binding for the ZeroMQ distributed computing library. For more information, please visit:
F#
112
star
34

majordomo

Majordomo Project
C
111
star
35

cljzmq

Clojure bindings for ØMQ
Clojure
105
star
36

rfc

ZeroMQ RFC project
C
104
star
37

dafka

Dafka is a decentralized distributed streaming platform
C
101
star
38

gyre

Golang port of Zyre
Go
87
star
39

zwssock

ZeroMQ WebSocket library for CZMQ
C
85
star
40

jszmq

Javascript port of zeromq
TypeScript
80
star
41

libzmtp

Minimal ZMTP implementation in C
C
53
star
42

ingescape

Model-based framework for broker-free distributed software environments. Any language, any OS, web, cloud.
C
52
star
43

zbroker

Elastic pipes
C
50
star
44

czmqpp

C++ wrapper for czmq. Aims to be minimal, simple and consistent.
C++
43
star
45

zeromq.org

ZeroMQ Website
HTML
32
star
46

cookbook

ZeroMQ Cookbook
Python
32
star
47

pyczmq

Python CZMQ bindings
Python
31
star
48

zebra

REST/HTTP to XRAP gateway
C++
27
star
49

zmtpdump

ZeroMQ Transport Protocol packet analyzer
Roff
26
star
50

jeromq-jms

JeroMQ JMS
Java
25
star
51

jzmq-api

A Java ØMQ API for abstracting the various implementations of ZeroMQ Message Transport Protocol
Java
24
star
52

zmq-jni

Simple High Performance JNI Wrapper for ØMQ
Java
22
star
53

perlzmq

version agnostic Perl bindings for zeromq
Perl
22
star
54

zmtp

Stuff related to the ZMTP protocol
C
18
star
55

contiki-zmtp

ZMTP for Contiki OS
C
16
star
56

zccp

ZeroMQ Command & Control Protocol
Go
14
star
57

jyre

Java implementation of ZRE protocol
Java
14
star
58

f77_zmq

Fortran binding for ZeroMQ
C
13
star
59

ztools

Tools for ØMQ auto-builds and API site
Perl
12
star
60

zeps

ZeroMQ Enterprise Publish-Subscribe (ZEPS) **DEPRECATED**
C
12
star
61

mruby-zmq

mruby bindings for libzmq (v4)
C
11
star
62

zeromq2-0

Packaging project for ØMQ/2.0 series
C++
9
star
63

zkernel

z kernel
C
9
star
64

nimczmq

Nim ( http://nim-lang.org/ ) bindings for CZMQ
Nim
6
star
65

curvezmq-java

Java
5
star
66

gozyre

Go bindings for zeromq libzyre - an open-source framework for proximity-based peer-to-peer applications
Go
5
star
67

issues

Issue test cases
C
4
star
68

zdiscgo

CZMQ service discovery zactor with support for Go plugins.
C
4
star
69

jdafka

Dafka protocol implementation in Java
Java
4
star
70

zlabs

Labs project for experimenting with CZMQ
Shell
4
star
71

zeromq-buildbot

A buildbot based regression tester for Zeromq
Python
3
star
72

azmq1-0

v1.0 release of azmq
C++
3
star
73

lyre

Lua port of Zyre
Lua
3
star
74

jeromq3-x

Java
3
star
75

jzmq3-x

Java
2
star
76

zmtp-java

Java
2
star
77

libzmq-relicense

This repo contains information regarding re-licensing libzmq
Python
2
star
78

clrzmq2

Old repository path for clrzmq
2
star
79

libzmq-fuzz-corpora

fuzzers corpus files for libzmq are stored in binary format in this repository
1
star
80

zeromq-download-redirect

HTML
1
star