meshenger/main.py

241 lines
6.4 KiB
Python
Raw Normal View History

2014-04-04 14:59:46 +02:00
#!/usr/bin/python
2014-04-24 16:23:50 +02:00
import socket, os, time, select, urllib, sys, threading
2014-04-04 13:40:46 +02:00
2014-04-04 14:59:46 +02:00
class Meshenger:
devices = {} #the dictionary of all the nodes this this node has seen
serve_port = "13338"
2014-04-04 14:59:46 +02:00
announce_port = 13337
2014-04-04 16:23:33 +02:00
#own_ip = "0.0.0.0"
msg_dir = os.path.relpath('msg/')
2014-04-04 18:39:17 +02:00
exitapp = False #to kill all threads on
2014-04-24 16:23:50 +02:00
index_last_update = str(time.time())
2014-04-04 14:59:46 +02:00
2014-04-04 16:27:15 +02:00
def __init__(self):
2014-04-04 14:59:46 +02:00
os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-lan/disable_ipv6")
os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-hotspot/disable_ipv6")
2014-04-04 16:27:15 +02:00
self.own_ip = self.get_ip_adress()
2014-04-04 14:59:46 +02:00
2014-04-18 18:55:12 +02:00
if not os.path.exists(self.msg_dir):
os.mkdir(self.msg_dir)
print 'Making message directory'
2014-04-04 18:39:17 +02:00
try:
d = threading.Thread(target=self.discover)
d.daemon = True
d.start()
a = threading.Thread(target=self.announce)
a.daemon = True
a.start()
2014-04-28 22:44:59 +02:00
n = threading.Thread(target=self.nodeserve)
n.daemon = True
n.start()
c = threading.Thread(target=self.clientserve)
c.daemon = True
c.start()
2014-04-21 11:57:16 +02:00
2014-04-24 17:03:42 +02:00
b = threading.Thread(target=self.build_index)
b.daemon = True
b.start()
2014-04-28 22:44:59 +02:00
#os.system("python meshenger_clientserve.py")
2014-04-04 18:39:17 +02:00
except (KeyboardInterrupt, SystemExit):
print 'exiting discovery thread'
d.join()
a.join()
2014-04-24 17:03:42 +02:00
b.join()
2014-04-28 22:44:59 +02:00
n.join()
c.join()
2014-04-04 18:39:17 +02:00
sys.exit()
while True:
print 'Entering main loop'
#
2014-04-04 16:27:15 +02:00
if len(self.devices) > 0:
2014-04-24 17:26:08 +02:00
print 'found', len(self.devices),'device(s)'
2014-04-04 14:59:46 +02:00
for device in self.devices:
2014-04-24 16:23:50 +02:00
nodepath = self.ip_to_hash(device) #make a folder for the node (nodes/'hash'/)
nodeupdatepath = os.path.join(self.ip_to_hash(device), 'lastupdate')
2014-04-28 22:44:59 +02:00
print 'Checking age of foreign node index'
2014-04-24 17:26:08 +02:00
print self.devices[device], 'Foreign announce timestamp'
2014-04-24 16:23:50 +02:00
foreign_node_update = open(nodeupdatepath).read()
2014-04-24 17:26:08 +02:00
print foreign_node_update, 'Locally stored timestamp for device'
2014-04-24 14:31:48 +02:00
2014-04-24 16:37:30 +02:00
2014-04-24 16:23:50 +02:00
if self.devices[device] > foreign_node_update:
print 'Foreign node"s index is newer, proceed to download index'
self.get_index(device, nodepath)
2014-04-24 17:18:08 +02:00
self.node_timestamp(device)
print 'downloading messages'
self.get_messages(device, nodepath)
2014-04-28 22:44:59 +02:00
2014-04-04 18:39:17 +02:00
time.sleep(5) #free process or ctrl+c
2014-04-28 22:44:59 +02:00
2014-04-24 16:23:50 +02:00
def node_timestamp(self, ip):
2014-04-24 16:23:50 +02:00
updatepath = os.path.join(self.ip_to_hash(ip), 'lastupdate')
with open(updatepath, 'wb') as lastupdate:
2014-04-24 16:23:50 +02:00
lastupdate.write(self.devices[ip])
#return updatepath
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def announce(self):
2014-04-04 17:05:46 +02:00
"""
2014-04-24 16:23:50 +02:00
Announce the node's existance to other nodes
"""
print 'Announcing'
2014-04-04 18:39:17 +02:00
while not self.exitapp:
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.sendto(self.index_last_update, ("ff02::1", self.announce_port))
2014-04-04 18:39:17 +02:00
sock.close()
time.sleep(5)
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def discover(self):
2014-04-04 17:05:46 +02:00
"""
2014-04-24 16:23:50 +02:00
Discover other devices by listening to the Meshenger announce port
"""
2014-04-04 13:40:46 +02:00
print 'Discovering'
2014-04-04 16:27:15 +02:00
bufferSize = 1024 # whatever you need
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.bind(('::', self.announce_port))
s.setblocking(0)
2014-04-04 18:39:17 +02:00
while not self.exitapp:
2014-04-04 16:27:15 +02:00
result = select.select([s],[],[])[0][0].recvfrom(bufferSize)
2014-04-24 16:23:50 +02:00
if result[1][0] in self.devices and result[1][0] != self.own_ip:
2014-04-24 17:26:08 +02:00
print 'Known node', result[1][0]
2014-04-24 16:23:50 +02:00
self.devices[result[1][0]] = result[0]
#self.devices.append(result[1][0])
2014-04-28 22:44:59 +02:00
2014-04-24 16:37:30 +02:00
elif result[1][0] not in self.devices and result[1][0] != self.own_ip:
#loop for first time
self.devices[result[1][0]] = result[0]
self.node_timestamp(result[1][0])
2014-04-24 17:26:08 +02:00
print 'New node', result[1][0]
2014-04-24 16:37:30 +02:00
2014-04-04 16:27:15 +02:00
time.sleep(1)
2014-04-04 13:40:46 +02:00
2014-04-28 22:44:59 +02:00
def nodeserve(self):
2014-04-21 11:57:16 +02:00
"""
2014-04-28 22:44:59 +02:00
Initialize the nodeserver
2014-04-24 16:23:50 +02:00
"""
2014-04-28 22:44:59 +02:00
print 'Serving to nodes'
import meshenger_nodeserve
meshenger_nodeserve.main()
def clientserve(self):
"""
Initialize the clientserver
"""
print 'Serving to client'
import meshenger_clientserve
meshenger_clientserve.main()
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def build_index(self):
2014-04-04 17:05:46 +02:00
"""
2014-04-24 16:23:50 +02:00
Make an index file of all the messages present on the node.
Save the time of the last update.
"""
2014-04-24 17:03:42 +02:00
print 'Building own index for the first time\n'
if not os.path.exists('index'):
with open('index','wb') as index:
index.write('')
2014-04-04 16:27:15 +02:00
previous_index = []
2014-04-04 13:40:46 +02:00
2014-04-24 17:03:42 +02:00
while not self.exitapp:
current_index = os.listdir(self.msg_dir)
if current_index != previous_index:
with open('index', 'wb') as index:
for message in os.listdir(self.msg_dir):
index.write(message)
index.write('\n')
self.index_last_update = str(int(time.time()))
2014-04-28 22:44:59 +02:00
2014-04-24 17:03:42 +02:00
print 'Index updated:', current_index
2014-04-24 17:03:42 +02:00
with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig
indexupdate.write(str(int(time.time())))
2014-04-24 17:03:42 +02:00
previous_index = current_index
time.sleep(5)
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def get_index(self,ip, path):
2014-04-04 17:05:46 +02:00
"""
2014-04-24 16:23:50 +02:00
Download the indices from other nodes.
"""
2014-04-04 13:40:46 +02:00
os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/index -O '+os.path.join(path,'index'))
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def get_messages(self, ip, path):
2014-04-04 17:05:46 +02:00
"""
2014-04-24 16:23:50 +02:00
Get new messages from other node based on it's index file
"""
try:
with open(os.path.join(path,'index')) as index:
index = index.read().split('\n')
for message in index:
messagepath = os.path.join(os.path.abspath(self.msg_dir), message)
if not os.path.exists(messagepath):
print 'downloading', message, 'to', messagepath
os.system('wget http://['+ip+'%adhoc0]:'+self.serve_port+'/msg/'+message+' -O '+messagepath)
except:
pass
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def ip_to_hash(self, ip):
2014-04-04 17:05:46 +02:00
"""
2014-04-24 16:23:50 +02:00
Convert a node's ip into a hash and make a directory to store it's files
"""
if not os.path.exists('nodes'):
os.mkdir('nodes')
2014-04-04 16:27:15 +02:00
import hashlib
hasj = hashlib.md5(ip).hexdigest()
nodepath = os.path.join(os.path.abspath('nodes'), hasj)
2014-04-04 16:27:15 +02:00
if not os.path.exists(nodepath):
os.mkdir(nodepath)
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
return nodepath
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def clientsite(self):
a = ''
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
#tools
2014-04-04 13:40:46 +02:00
2014-04-04 16:27:15 +02:00
def get_ip_adress(self):
2014-04-04 17:05:46 +02:00
"""
2014-04-24 16:23:50 +02:00
Hack to adhoc0's inet6 adress
"""
2014-04-04 16:27:15 +02:00
if not os.path.isfile('interfaceip6adress'):
os.system('ifconfig -a adhoc0 | grep inet6 > /root/meshenger/interfaceip6adress')
2014-04-04 16:27:15 +02:00
with open('interfaceip6adress', 'r') as a:
return a.read().split()[2].split('/')[0]
2014-04-04 13:40:46 +02:00
2014-04-04 16:23:33 +02:00
2014-04-04 17:05:46 +02:00
if __name__ == "__main__":
2014-04-24 16:23:50 +02:00
print "test"
2014-04-04 18:39:17 +02:00
try:
meshenger = Meshenger()
except (KeyboardInterrupt, SystemExit):
exitapp = True
raise