dont give up on localnodes if there is no network

This commit is contained in:
j 2014-05-27 16:48:50 +02:00
parent 7430d1b483
commit 74d5b21df0

View file

@ -8,6 +8,7 @@ import socket
import struct import struct
import thread import thread
from threading import Thread from threading import Thread
import time
from utils import valid, get_public_ipv6, get_local_ipv4, get_interface from utils import valid, get_public_ipv6, get_local_ipv4, get_interface
from settings import preferences, server, USER_ID, sk from settings import preferences, server, USER_ID, sk
@ -63,15 +64,21 @@ class LocalNodesBase(Thread):
pass pass
def receive(self): def receive(self):
s = self.get_socket()
s.bind(('', self._PORT))
while self._active: while self._active:
data, addr = s.recvfrom(1024) try:
while data[-1] == '\0': s = self.get_socket()
data = data[:-1] # Strip trailing \0's s.bind(('', self._PORT))
data = self.verify(data) while self._active:
if data: data, addr = s.recvfrom(1024)
self.update_node(data) while data[-1] == '\0':
data = data[:-1] # Strip trailing \0's
data = self.verify(data)
if data:
self.update_node(data)
except:
logger.debug('receive failed. restart later', exc_info=1)
time.sleep(10)
def verify(self, data): def verify(self, data):
try: try:
@ -138,7 +145,10 @@ class LocalNodes4(LocalNodesBase):
sockaddr = (self._BROADCAST, self._PORT) sockaddr = (self._BROADCAST, self._PORT)
s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self._TTL) s.setsockopt (socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self._TTL)
s.sendto(packet + '\0', sockaddr) try:
s.sendto(packet + '\0', sockaddr)
except:
logger.debug('LocalNodes4.send failed', exc_info=1)
s.close() s.close()
def get_socket(self): def get_socket(self):
@ -165,7 +175,10 @@ class LocalNodes6(LocalNodesBase):
(family, socktype, proto, canonname, sockaddr) = addr (family, socktype, proto, canonname, sockaddr) = addr
s = socket.socket(family, socktype, proto) s = socket.socket(family, socktype, proto)
s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl) s.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl)
s.sendto(packet + '\0', sockaddr) try:
s.sendto(packet + '\0', sockaddr)
except:
logger.debug('LocalNodes6.send failed', exc_info=1)
s.close() s.close()
def get_socket(self): def get_socket(self):