added threads for announce + discover
This commit is contained in:
parent
611321aefb
commit
1b23da4055
55
main.py
55
main.py
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import socket, os, time, select, urllib, sys
|
import socket, os, time, select, urllib, sys, threading
|
||||||
|
|
||||||
class Meshenger:
|
class Meshenger:
|
||||||
devices = [] #the list of all the nodes this this node has seen
|
devices = [] #the list of all the nodes this this node has seen
|
||||||
@ -8,6 +8,7 @@ class Meshenger:
|
|||||||
announce_port = 13337
|
announce_port = 13337
|
||||||
#own_ip = "0.0.0.0"
|
#own_ip = "0.0.0.0"
|
||||||
msg_dir = os.path.relpath('msg/')
|
msg_dir = os.path.relpath('msg/')
|
||||||
|
exitapp = False #to kill all threads on
|
||||||
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -16,11 +17,24 @@ class Meshenger:
|
|||||||
self.own_ip = self.get_ip_adress()
|
self.own_ip = self.get_ip_adress()
|
||||||
#self.own_ip = "192.168.2.196"
|
#self.own_ip = "192.168.2.196"
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
print 'discovering devices'
|
try:
|
||||||
time.sleep(1)
|
#print 'discovering devices'
|
||||||
self.discover()
|
d = threading.Thread(target=self.discover)
|
||||||
|
d.daemon = True
|
||||||
|
d.start()
|
||||||
|
|
||||||
|
a = threading.Thread(target=self.announce)
|
||||||
|
a.daemon = True
|
||||||
|
a.start()
|
||||||
|
|
||||||
|
except (KeyboardInterrupt, SystemExit):
|
||||||
|
print 'exiting discovery thread'
|
||||||
|
d.join()
|
||||||
|
a.join()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
if len(self.devices) > 0:
|
if len(self.devices) > 0:
|
||||||
print 'found', len(self.devices),'device(s) retreiving indices'
|
print 'found', len(self.devices),'device(s) retreiving indices'
|
||||||
@ -33,18 +47,19 @@ class Meshenger:
|
|||||||
print 'updating own index'
|
print 'updating own index'
|
||||||
self.build_index()
|
self.build_index()
|
||||||
|
|
||||||
time.sleep(5)
|
time.sleep(5) #free process or ctrl+c
|
||||||
|
|
||||||
|
|
||||||
def announce(self):
|
def announce(self):
|
||||||
"""
|
"""
|
||||||
Announce the node's existance to other nodes
|
Announce the node's existance to other nodes
|
||||||
"""
|
"""
|
||||||
|
while not self.exitapp:
|
||||||
#announces it's existance to other nodes
|
print 'announcing'
|
||||||
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
sock.sendto(bericht, ("ff02::1", self.announce_port))
|
sock.sendto('bericht', ("ff02::1", self.announce_port))
|
||||||
sock.close()
|
sock.close()
|
||||||
|
time.sleep(5)
|
||||||
|
|
||||||
def discover(self):
|
def discover(self):
|
||||||
"""
|
"""
|
||||||
@ -59,7 +74,8 @@ class Meshenger:
|
|||||||
|
|
||||||
#global devices
|
#global devices
|
||||||
|
|
||||||
while True:
|
while not self.exitapp:
|
||||||
|
print 'discovering'
|
||||||
result = select.select([s],[],[])[0][0].recvfrom(bufferSize)
|
result = select.select([s],[],[])[0][0].recvfrom(bufferSize)
|
||||||
if result[1][0] not in self.devices and result[1][0] != self.own_ip:
|
if result[1][0] not in self.devices and result[1][0] != self.own_ip:
|
||||||
self.devices.append(result[1][0])
|
self.devices.append(result[1][0])
|
||||||
@ -68,6 +84,15 @@ class Meshenger:
|
|||||||
|
|
||||||
def serve(self):
|
def serve(self):
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# t = threading.Thread(target=BorderCheckWebserver, args=(self, ))
|
||||||
|
# t.daemon = True
|
||||||
|
# t.start()
|
||||||
|
# time.sleep(2)
|
||||||
|
# except (KeyboardInterrupt, SystemExit):
|
||||||
|
# t.join()
|
||||||
|
# sys.exit()
|
||||||
|
|
||||||
a = ''
|
a = ''
|
||||||
# serves both the index and the messages on the node over http
|
# serves both the index and the messages on the node over http
|
||||||
# plus manages the client-side web interface
|
# plus manages the client-side web interface
|
||||||
@ -144,4 +169,8 @@ class Meshenger:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print "test"
|
print "test"
|
||||||
meshenger = Meshenger()
|
try:
|
||||||
|
meshenger = Meshenger()
|
||||||
|
except (KeyboardInterrupt, SystemExit):
|
||||||
|
exitapp = True
|
||||||
|
raise
|
||||||
|
Loading…
Reference in New Issue
Block a user