forked from kanflo/uhej-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuhej_client.py
More file actions
209 lines (179 loc) · 7.55 KB
/
Copy pathuhej_client.py
File metadata and controls
209 lines (179 loc) · 7.55 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
The MIT License (MIT)
Copyright (c) 2017 Johan Kanflo (github.com/kanflo)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import time
import threading
import socket
import binascii
import Queue
from uhej import *
import logging
rx_timeout_s = 1
beacon_timeout_s = 5
query_time_s = 2
logger = logging.getLogger(__name__)
# Array of services we subscribe to, each item being a dictionary with the items:
# "name" : string - name of service
# "type" : int - type of service (uhej.UDP, uhej.TCP, uhej.MCAST)
# "subscribed" : bool - True if we are subscribed to a service
# "timestamp" : integer - Unix timestamp of last beacon
# "addr" : ip address - IP address of server where we found the subscription
# "port" : integer - port of server where we found the subscription
service_list = [] # Array of service dictionaties
def init(callback, node_id, ip, mac, name):
global _callback
_callback = callback
print("uHej client '%s on %s:%d" % (name, MCAST_GRP, MCAST_PORT))
_sock_init()
_queue_init()
_thread_init()
f = hello(node_id, ip, mac, name)
tx_sock.sendto(f, (MCAST_GRP, MCAST_PORT))
def subscribe_udp(service_name):
logger.info("Subscribing to UDP service '%s'" % (service_name))
service_list.append({'type':UDP, 'name':service_name, 'subscribed':False, 'timestamp':0, 'addr':None, 'port':None})
def cancel_udp(service_name):
for service in service_list:
if service["name"] == service_name:
logger.info("Cancelling UDP service '%s'" % (service_name))
service_list.remove(service)
def find_service(name, type):
for service in service_list:
if service["name"] == name and service["type"] == type:
return service
return None
### Private functions below ###
def _check_service_announcement(frame):
global _callback
addr = frame["source"]
services = ["UDP", "TCP", "Multicast"]
for ann_service in frame["services"]:
name = ann_service["service_name"]
type = ann_service["type"]
port = ann_service["port"]
service = find_service(name, type)
if None != service:
if not service["subscribed"]:
logger.info("Found %s service '%s' at %s:%d" % (services[type], name, addr, port))
_callback(name, addr, port, True)
service["subscribed"] = True
service["timestamp"] = time.time()
service["addr"] = addr
service["port"] = port
def _check_beacon(frame):
global _callback
match = False
addr = frame["source"]
for service in service_list:
if service["addr"] == addr:
if service["subscribed"]:
logger.info("Beacon from %s service '%s'" % (addr, service["name"]))
service["timestamp"] = time.time()
match = True
else:
logger.warning("Beacon from %s for unsubscribed service '%s'" % (addr, service["name"]))
if not match:
logger.warning("Unknown beacon from %s" % (addr))
def _janitor():
global _callback
global tx_sock
services = ["UDP", "TCP", "Multicast"]
for service in service_list:
if not service["subscribed"] and time.time() - service["timestamp"] > query_time_s:
logger.info("Querying %s service '%s'" % (services[service["type"]], service["name"]))
f = query(service["type"], service["name"])
tx_sock.sendto(f, (MCAST_GRP, MCAST_PORT))
# TODO, someday when I introduct beacons between a client and the service it subscribes to
# elif service["subscribed"]:
# if time.time() - service["timestamp"] > 2*beacon_timeout_s:
# logger.info("Lost '%s'" % (service["name"]))
# _callback(service["name"], None, None, False)
# service["subscribed"] = False
# elif time.time() - service["timestamp"] > beacon_timeout_s:
# service["timestamp"] = time.time()
# logger.info("Beacon to %s:%d" % (service["addr"], MCAST_PORT))
# f = beacon()
# tx_sock.sendto(f, (service["addr"], MCAST_PORT))
def _comms_thread():
global rx_sock
global q
logger.info("uHej server thread")
while 1:
try:
data, addr = rx_sock.recvfrom(1024)
q.put((addr, data))
except Exception, e:
logger.error("Exception")
def _worker_thread():
global q
logger.info("uHej worker thread")
while 1:
data = addr = None
try:
addr, data = q.get(timeout = rx_timeout_s)
except Queue.Empty:
_janitor()
if data != None and addr != None:
port = addr[1]
addr = addr[0]
frame = bytearray(data)
try:
f = decode_frame(frame)
f["source"] = addr
f["port"] = port
if ANNOUNCE == f["frame_type"]:
logger.info("Announce frame from %s:%d" % (f["source"], f["port"]))
_check_service_announcement(f)
elif HELLO == f["frame_type"]:
logger.info("Hello frame")
elif BEACON == f["frame_type"]:
logger.info("Beacon frame")
_check_beacon(f)
except IllegalFrameException, e:
logger.info("%s:%d Illegal frame '%s'" % (addr, port, frame))
def _start_thread(thread):
thread = threading.Thread(target = thread)
thread.daemon = True
thread.start()
def _thread_init():
_start_thread(_comms_thread)
_start_thread(_worker_thread)
def _queue_init():
global q
q = Queue.Queue()
def _sock_init():
global rx_sock
global tx_sock
ANY = "0.0.0.0"
rx_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
try:
rx_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except AttributeError:
pass
rx_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
rx_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 1)
rx_sock.bind((ANY, MCAST_PORT))
try:
host = socket.gethostbyname(socket.gethostname())
except:
host = socket.gethostbyname(socket.gethostname()+".local")
rx_sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(host))
rx_sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(MCAST_GRP) + socket.inet_aton(ANY))
tx_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
tx_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)