forked from linux-msm/qrtr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrtr.py
executable file
·97 lines (79 loc) · 3.33 KB
/
qrtr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python2.7
import ctypes
import collections
from ctypes import CDLL, CFUNCTYPE, POINTER, cast, py_object
from ctypes import c_char_p, c_void_p, c_int, pointer
_qrtr = CDLL("./libqrtr.so")
class qrtr:
Result = collections.namedtuple('Result', ['service', 'instance', 'addr'])
_cbtype = CFUNCTYPE(None, c_void_p, c_int, c_int, c_int, c_int)
def __init__(self, port=0):
self.sock = _qrtr.qrtr_open(port)
if self.sock < 0:
raise RuntimeError("unable to open qrtr socket")
self.service = None
self._qrtr = _qrtr
def __del__(self):
self._qrtr.qrtr_close(self.sock)
def _lookup_list_add(self, ptr, srv, instance, node, port):
res = qrtr.Result(srv, instance, (node, port))
cast(ptr, POINTER(py_object)).contents.value.append(res)
def lookup(self, srv, instance=0, ifilter=0):
results = []
err = _qrtr.qrtr_lookup(self.sock, srv, instance, ifilter,
qrtr._cbtype(self._lookup_list_add), cast(pointer(py_object(results)), c_void_p))
if err:
raise RuntimeError("query failed")
return results
def publish(self, service, version, instance):
err = _qrtr.qrtr_publish(self.sock, service, version, instance)
if err:
raise RuntimeError("publish failed")
self.service = (service, version, instance)
def new_server(self, service, version, instance):
err = _qrtr.qrtr_new_server(self.sock, service, version, instance)
if err:
raise RuntimeError("new_server failed")
self.service = (service, version, instance)
return self.service
def remove_server(self, service):
err = _qrtr.qrtr_remove_server(self.sock, *service)
if err:
raise RuntimeError("remove_server failed")
self.service = None
def new_lookup(self, service, version, instance):
err = _qrtr.qrtr_new_lookup(self.sock, service, version, instance)
if err:
raise RuntimeError("new_lookup failed")
return (service, version, instance)
def remove_lookup(self, lookup):
err = _qrtr.qrtr_remove_lookup(self.sock, *lookup)
if err:
raise RuntimeError("remove_lookup failed")
def send(self, addr, data):
node, port = addr
n = _qrtr.qrtr_sendto(self.sock, node, port, c_char_p(data), len(data))
if n:
raise RuntimeError("sendto failed")
def recv(self, sz=65536):
buf = ctypes.create_string_buffer(sz)
n = _qrtr.qrtr_recv(self.sock, c_char_p(ctypes.addressof(buf)), sz)
if n <= 0:
raise RuntimeError("recv failed")
return buf[0:n]
def recvfrom(self, sz=65536):
node = ctypes.c_int()
port = ctypes.c_int()
buf = ctypes.create_string_buffer(sz)
n = _qrtr.qrtr_recvfrom(self.sock, c_char_p(ctypes.addressof(buf)),
ctypes.byref(node), ctypes.byref(port))
if n <= 0:
raise RuntimeError("recvfrom failed")
return (buf[0:n], (node.value, port.value))
def poll(self, tout=0):
return _qrtr.qrtr_poll(self.sock, tout)
if __name__ == "__main__":
svcs = qrtr().lookup(15) # 15 is the test service
print " service instance addr"
for svc in svcs:
print "% 8d % 8d %s" % (svc.service, svc.instance, svc.addr)