lokinet/contrib/py/ffi-example/lokinet.py

52 lines
1.1 KiB
Python
Raw Normal View History

2018-05-27 19:13:25 +00:00
#!/usr/bin/env python3
from ctypes import *
import signal
import time
import threading
2018-07-27 04:25:34 +00:00
import os
2018-05-27 19:13:25 +00:00
2018-07-27 04:25:34 +00:00
lib_file = os.path.join(os.path.realpath('.'), 'liblokinet.so')
2018-05-28 12:23:07 +00:00
2018-07-27 04:25:34 +00:00
class LokiNET(threading.Thread):
2018-05-27 19:13:25 +00:00
lib = None
ctx = None
2018-05-28 12:23:07 +00:00
2018-07-27 04:25:34 +00:00
def load(self, lib, conf):
self.lib = CDLL(lib)
self.lib.llarp_ensure_config(conf)
self.ctx = self.lib.llarp_main_init(conf)
return self.ctx != 0
2018-05-27 19:13:25 +00:00
def signal(self, sig):
2018-05-28 12:23:07 +00:00
if self.ctx and self.lib:
2018-05-27 19:13:25 +00:00
self.lib.llarp_main_signal(self.ctx, int(sig))
2018-05-28 12:23:07 +00:00
2018-05-27 19:13:25 +00:00
def run(self):
code = self.lib.llarp_main_run(self.ctx)
2018-05-28 12:23:07 +00:00
print("llarp_main_run exited with status {}".format(code))
2018-07-27 04:25:34 +00:00
def close(self):
if self.lib and self.ctx:
self.lib.llarp_main_free(self.ctx)
2018-05-27 19:13:25 +00:00
def main():
2018-07-27 04:25:34 +00:00
loki = LokiNET()
if loki.load(lib_file, b'daemon.ini'):
loki.start()
2018-05-27 19:13:25 +00:00
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
llarp.signal(signal.SIGINT)
finally:
2018-07-27 04:25:34 +00:00
loki.close()
2018-05-27 19:13:25 +00:00
return
2018-05-28 12:23:07 +00:00
2018-05-27 19:13:25 +00:00
if __name__ == '__main__':
main()