Skip to content

Commit

Permalink
waku.py: adapting the code to the lates libwaku version (with working…
Browse files Browse the repository at this point in the history
… thread.)
  • Loading branch information
Ivansete-status committed Jul 31, 2023
1 parent af420c6 commit 4ee0a5d
Showing 1 changed file with 52 additions and 39 deletions.
91 changes: 52 additions & 39 deletions examples/python/waku.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from flask import Flask
import ctypes
import argparse

libwaku = object
try:
libwaku = ctypes.CDLL("libwaku.so")
# This python script should be run from the root repo folder
libwaku = ctypes.CDLL("build/libwaku.so")
except Exception as e:
print("Exception: ", e)
print("""
Expand Down Expand Up @@ -37,68 +39,79 @@ def call_waku(func):
e.g.: 364d111d729a6eb6d2e6113e163f017b5ef03a6f94c9b5b7bb1bb36fa5cb07a9""")
parser.add_argument('-r', '--relay', dest='relay', default="true",
help="Enable relay protocol: true|false [=true]")
parser.add_argument('--peer', dest='peer', default="",
help="Multiqualified libp2p address")

args = parser.parse_args()

# The next 'jsonConfig' is the item passed to the 'waku_new'.
jsonConfig = "{ \
# The next 'json_config' is the item passed to the 'waku_new'.
json_config = "{ \
\"host\": \"%s\", \
\"port\": %d, \
\"key\": \"%s\", \
\"relay\": %s \
}" % (args.host,
int(args.port),
args.key,
args.relay)
"true" if args.relay else "false")


print("AAAA %s", jsonConfig)

# Setup nim runtime and GC.
libwaku.NimMain()

CallbackType = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_size_t)
callback_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_size_t)

# Retrieve the current version of the library
libwaku.waku_version(CallbackType(lambda msg, len:
libwaku.waku_version(callback_type(lambda msg, len:
print("Git Version: %s" %
msg.decode('utf-8'))))
# Retrieve the default pubsub topic
libwaku.waku_default_pubsub_topic(CallbackType(
lambda msg, len:
print("Default pubsub topic: %s" % msg.decode('utf-8'))
default_pubsub_topic = ""
libwaku.waku_default_pubsub_topic(callback_type(
lambda msg, len: (
globals().update(default_pubsub_topic = msg.decode('utf-8')),
print("Default pubsub topic: %s" % msg.decode('utf-8')))
))

print("Bind addr: {}:{}".format(args.host, args.port))
print("Waku Relay enabled: {}".format(args.relay))

# Node creation
# libwaku.waku_new.argtypes = [ctypes.POINTER(ConfigNode),
# ctypes.POINTER(ctypes.POINTER(NimStringDesc))]
call_waku( libwaku.waku_new(jsonConfig,
CallbackType(
libwaku.waku_new.argtypes = [ctypes.c_char_p,
callback_type]

libwaku.waku_new(bytes(json_config, 'utf-8'),
callback_type(
#onErrCb
lambda msg, len:
print("Error calling waku_new: %s",
msg.decode('utf-8'))
))
)

# # Set the event callback
# CallbackType = ctypes.CFUNCTYPE(None, ctypes.c_char_p)
# callback = CallbackType(handle_event)
# libwaku.waku_set_event_callback(callback)

# # Subscribe to the default pubsub topic
# if not libwaku.waku_relay_subscribe(defaultPubsubTopic.encode('utf-8'),
# ctypes.byref(resp)):
# print("Error subscribing to default pubsub topic: {}".format(
# resp.contents.data.decode('utf-8')))
# exit(-1)

# # Start the node
# libwaku.waku_start()

# while True:
# # Tick the async dispatcher to consume OS events (timers, sockets, etc.)
# libwaku.waku_poll()
# Start the node
libwaku.waku_start()

# Set the event callback
callback_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p)
callback = callback_type(handle_event)
libwaku.waku_set_event_callback(callback)

# Subscribe to the default pubsub topic
libwaku.waku_relay_subscribe(default_pubsub_topic.encode('utf-8'),
callback_type(
#onErrCb
lambda msg, len:
print("Error calling waku_new: %s",
msg.decode('utf-8'))
))

libwaku.waku_connect(args.peer.encode('utf-8'),
10000,
callback_type(
# onErrCb
lambda msg, len:
print("Error calling waku_new: %s", msg.decode('utf-8'))))

# app = Flask(__name__)
# @app.route("/")
# def hello_world():
# return "Hello, World!"

# Simply avoid the app to
a = input()

0 comments on commit 4ee0a5d

Please sign in to comment.