|
| 1 | +from flask import Flask |
| 2 | +import ctypes |
| 3 | +import argparse |
| 4 | + |
| 5 | +libwaku = object |
| 6 | +try: |
| 7 | + # This python script should be run from the root repo folder |
| 8 | + libwaku = ctypes.CDLL("build/libwaku.so") |
| 9 | +except Exception as e: |
| 10 | + print("Exception: ", e) |
| 11 | + print(""" |
| 12 | +The 'libwaku.so' library can be created with the next command from |
| 13 | +the repo's root folder: `make libwaku`. |
| 14 | +
|
| 15 | +And it should build the library in 'build/libwaku.so'. |
| 16 | +
|
| 17 | +Therefore, make sure the LD_LIBRARY_PATH env var points at the location that |
| 18 | +contains the 'libwaku.so' library. |
| 19 | +""") |
| 20 | + exit(-1) |
| 21 | + |
| 22 | +def handle_event(event): |
| 23 | + print("Event received: {}".format(event)) |
| 24 | + |
| 25 | +def call_waku(func): |
| 26 | + ret = func() |
| 27 | + if (ret != 0): |
| 28 | + print("Error in %s. Error code: %d" % (locals().keys(), ret)) |
| 29 | + exit(1) |
| 30 | + |
| 31 | +# Parse params |
| 32 | +parser = argparse.ArgumentParser(description='libwaku integration in Python.') |
| 33 | +parser.add_argument('-d', '--host', dest='host', default='0.0.0.0', |
| 34 | + help='Address this node will listen to. [=0.0.0.0]') |
| 35 | +parser.add_argument('-p', '--port', dest='port', default=60000, required=True, |
| 36 | + help='Port this node will listen to. [=60000]') |
| 37 | +parser.add_argument('-k', '--key', dest='key', default="", required=True, |
| 38 | + help="""P2P node private key as 64 char hex string. |
| 39 | +e.g.: 364d111d729a6eb6d2e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9""") |
| 40 | +parser.add_argument('-r', '--relay', dest='relay', default="true", |
| 41 | + help="Enable relay protocol: true|false [=true]") |
| 42 | +parser.add_argument('--peer', dest='peer', default="", |
| 43 | + help="Multiqualified libp2p address") |
| 44 | + |
| 45 | +args = parser.parse_args() |
| 46 | + |
| 47 | +# The next 'json_config' is the item passed to the 'waku_new'. |
| 48 | +json_config = "{ \ |
| 49 | + \"host\": \"%s\", \ |
| 50 | + \"port\": %d, \ |
| 51 | + \"key\": \"%s\", \ |
| 52 | + \"relay\": %s \ |
| 53 | + }" % (args.host, |
| 54 | + int(args.port), |
| 55 | + args.key, |
| 56 | + "true" if args.relay else "false") |
| 57 | + |
| 58 | +callback_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_size_t) |
| 59 | + |
| 60 | +# Retrieve the current version of the library |
| 61 | +libwaku.waku_version(callback_type(lambda msg, len: |
| 62 | + print("Git Version: %s" % |
| 63 | + msg.decode('utf-8')))) |
| 64 | +# Retrieve the default pubsub topic |
| 65 | +default_pubsub_topic = "" |
| 66 | +libwaku.waku_default_pubsub_topic(callback_type( |
| 67 | + lambda msg, len: ( |
| 68 | + globals().update(default_pubsub_topic = msg.decode('utf-8')), |
| 69 | + print("Default pubsub topic: %s" % msg.decode('utf-8'))) |
| 70 | +)) |
| 71 | + |
| 72 | +print("Bind addr: {}:{}".format(args.host, args.port)) |
| 73 | +print("Waku Relay enabled: {}".format(args.relay)) |
| 74 | + |
| 75 | +# Node creation |
| 76 | +libwaku.waku_new.argtypes = [ctypes.c_char_p, |
| 77 | + callback_type] |
| 78 | + |
| 79 | +libwaku.waku_new(bytes(json_config, 'utf-8'), |
| 80 | + callback_type( |
| 81 | + #onErrCb |
| 82 | + lambda msg, len: |
| 83 | + print("Error calling waku_new: %s", |
| 84 | + msg.decode('utf-8')) |
| 85 | + )) |
| 86 | +# Start the node |
| 87 | +libwaku.waku_start() |
| 88 | + |
| 89 | +# Set the event callback |
| 90 | +callback_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p) |
| 91 | +callback = callback_type(handle_event) |
| 92 | +libwaku.waku_set_event_callback(callback) |
| 93 | + |
| 94 | +# Subscribe to the default pubsub topic |
| 95 | +libwaku.waku_relay_subscribe(default_pubsub_topic.encode('utf-8'), |
| 96 | + callback_type( |
| 97 | + #onErrCb |
| 98 | + lambda msg, len: |
| 99 | + print("Error calling waku_new: %s", |
| 100 | + msg.decode('utf-8')) |
| 101 | + )) |
| 102 | + |
| 103 | +libwaku.waku_connect(args.peer.encode('utf-8'), |
| 104 | + 10000, |
| 105 | + callback_type( |
| 106 | + # onErrCb |
| 107 | + lambda msg, len: |
| 108 | + print("Error calling waku_new: %s", msg.decode('utf-8')))) |
| 109 | + |
| 110 | +# app = Flask(__name__) |
| 111 | +# @app.route("/") |
| 112 | +# def hello_world(): |
| 113 | +# return "Hello, World!" |
| 114 | + |
| 115 | +# Simply avoid the app to |
| 116 | +a = input() |
| 117 | + |
0 commit comments