script to class
This commit is contained in:
parent
c60e56db3d
commit
5b098ceb09
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
interfaceip6adress
|
89
main.py
Normal file → Executable file
89
main.py
Normal file → Executable file
@ -1,44 +1,78 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import socket, os, time, select, urllib, sys
|
||||
|
||||
def announce():
|
||||
class Meshenger:
|
||||
devices = [] #the list of all the nodes this this node has seen
|
||||
serve_port = 13338
|
||||
announce_port = 13337
|
||||
own_ip = "0.0.0.0"
|
||||
|
||||
|
||||
def __init__(self):
|
||||
|
||||
os.system("echo 1 >> /proc/sys/net/ipv6/conf/br-lan/disable_ipv6")
|
||||
#self.own_ip = self.get_ip_adress()
|
||||
self.own_ip = "192.168.2.196"
|
||||
|
||||
while True:
|
||||
|
||||
print 'discovering devices'
|
||||
time.sleep(1)
|
||||
self.discover()
|
||||
|
||||
if len(self.devices) > 0:
|
||||
print 'found', len(self.devices),'device(s) retreiving indices'
|
||||
for device in self.devices:
|
||||
nodepath = ip_to_hash(device)
|
||||
|
||||
self.get_index(device, nodepath)
|
||||
|
||||
self.get_messages(device, nodepath)
|
||||
print 'updating own index'
|
||||
self.build_index()
|
||||
#voeg toe aan eigen index --> build_index()
|
||||
time.sleep(5)
|
||||
|
||||
def announce(self):
|
||||
|
||||
#announces it's existance to other nodes
|
||||
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
sock.sendto(bericht, ("ff02::1", 13337))
|
||||
sock.sendto(bericht, ("ff02::1", self.announce_port))
|
||||
sock.close()
|
||||
|
||||
def index():
|
||||
def index(self):
|
||||
|
||||
a = ''
|
||||
# builds the latest index of all the messages that are on this node
|
||||
|
||||
def discover(known_devices):
|
||||
def discover(self):
|
||||
|
||||
# discovers other nodes by listening to the Meshenger announce port
|
||||
own_ip = get_ip_adress()
|
||||
port = 13337 # where do you expect to get a msg?
|
||||
#own_ip = get_ip_adress()
|
||||
#port = 13337 # where do you expect to get a msg?
|
||||
bufferSize = 1024 # whatever you need
|
||||
|
||||
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||
s.bind(('::', port))
|
||||
s.bind(('::', self.announce_port))
|
||||
s.setblocking(0)
|
||||
|
||||
global devices
|
||||
#global devices
|
||||
|
||||
while True:
|
||||
result = select.select([s],[],[])[0][0].recvfrom(bufferSize)
|
||||
if result not in known_devices and result[1][0] != own_ip:
|
||||
devices.append(result[1][0])
|
||||
if result not in self.devices and result[1][0] != self.own_ip:
|
||||
self.devices.append(result[1][0])
|
||||
return
|
||||
time.sleep(1)
|
||||
|
||||
def serve():
|
||||
def serve(self):
|
||||
|
||||
a = ''
|
||||
# serves both the index and the messages on the node over http
|
||||
# plus manages the client-side web interface
|
||||
|
||||
def build_index():
|
||||
def build_index(self):
|
||||
previous_index = []
|
||||
|
||||
current_index = os.listdir('msg/')
|
||||
@ -50,13 +84,13 @@ def build_index():
|
||||
index.write('\n')
|
||||
current_index = previous_index
|
||||
|
||||
def get_index(ip, path):
|
||||
def get_index(self,ip, path):
|
||||
|
||||
os.system('wget http://['+ip+'%adhoc0]:13338/index -O '+os.path.join(path,'index'))
|
||||
|
||||
# downloads the index from other nodes and then proceeds to downloads messages it doesn't have already
|
||||
|
||||
def get_messages(ip, path):
|
||||
def get_messages(self, ip, path):
|
||||
|
||||
with open(os.path.join(path,'index')) as index:
|
||||
index = index.read().split('\n')
|
||||
@ -81,7 +115,7 @@ def get_messages(ip, path):
|
||||
|
||||
# s.close()
|
||||
|
||||
def ip_to_hash(ip):
|
||||
def ip_to_hash(self, ip):
|
||||
import hashlib
|
||||
hasj = hashlib.md5(ip).hexdigest()
|
||||
nodepath = os.path.join(os.path.abspath('nodes/'), hasj)
|
||||
@ -91,33 +125,20 @@ def ip_to_hash(ip):
|
||||
return nodepath
|
||||
|
||||
|
||||
def clientsite():
|
||||
def clientsite(self):
|
||||
a = ''
|
||||
|
||||
#tools
|
||||
#tools
|
||||
|
||||
def get_ip_adress():
|
||||
def get_ip_adress(self):
|
||||
if not os.path.isfile('interfaceip6adress'):
|
||||
os.system('ifconfig -a adhoc0 | grep inet6 > interfaceip6adress')
|
||||
|
||||
with open('interfaceip6adress', 'r') as a:
|
||||
return a.read().split()[2].split('/')[0]
|
||||
|
||||
while True:
|
||||
devices = [] #the list of all the nodes this this node has seen
|
||||
print 'discovering devices'
|
||||
time.sleep(1)
|
||||
discover(devices)
|
||||
|
||||
if len(devices) > 0:
|
||||
print 'found', len(devices),'device(s) retreiving indices'
|
||||
for device in devices:
|
||||
nodepath = ip_to_hash(device)
|
||||
|
||||
get_index(device, nodepath)
|
||||
|
||||
get_messages(device, nodepath)
|
||||
print 'updating own index'
|
||||
build_index()
|
||||
#voeg toe aan eigen index --> build_index()
|
||||
time.sleep(5)
|
||||
if __name__ == "__main__":
|
||||
print "test"
|
||||
meshenger = Meshenger()
|
||||
|
Loading…
Reference in New Issue
Block a user