Commit 6a665380 authored by Ulysse Beaugnon's avatar Ulysse Beaugnon

private variables/functions in tunnelmanager are now named as private

parent d19b9e2b
...@@ -3,6 +3,7 @@ Bugs : ...@@ -3,6 +3,7 @@ Bugs :
To be done : To be done :
Do a clean-up in the import Do a clean-up in the import
Remove the parameters to choose the number of clients
To be discuss: To be discuss:
Remove the --no-boot option since we know when no node is avalaible Remove the --no-boot option since we know when no node is avalaible
......
...@@ -9,46 +9,50 @@ free_interface_set = set(('client1', 'client2', 'client3', 'client4', 'client5', ...@@ -9,46 +9,50 @@ free_interface_set = set(('client1', 'client2', 'client3', 'client4', 'client5',
class TunnelManager: class TunnelManager:
def __init__(self, write_pipe, peers_db): def __init__(self, write_pipe, peers_db):
self.write_pipe = write_pipe self._write_pipe = write_pipe
self.peers_db = peers_db self._peers_db = peers_db
self.connection_dict = {} self._connection_dict = {}
# TODO : choose this parameters automaticaly
self._clientCount = 15
self._refreshCount = 3
def refresh(self): def refresh(self):
self.cleanDeads() self._cleanDeads()
self.removeSomeTunnels() self._removeSomeTunnels()
self.makeNewTunnels() self._makeNewTunnels()
def cleanDeads(self): def _cleanDeads(self):
for id in self.connection_dict.keys(): for id in self._connection_dict.keys():
p, iface = self.connection_dict[id] p, iface = self._connection_dict[id]
if p.poll() != None: if p.poll() != None:
utils.log('Connection with %s has failed with return code %s' % (id, p.returncode), 3) utils.log('Connection with %s has failed with return code %s' % (id, p.returncode), 3)
free_interface_set.add(iface) free_interface_set.add(iface)
self.peers_db.unusePeer(id) self._peers_db.unusePeer(id)
del self.connection_dict[id] del self._connection_dict[id]
def removeSomeTunnels(self): def _removeSomeTunnels(self):
for i in range(0, max(0, len(self.connection_dict) - utils.config.client_count + utils.config.refresh_count)): for i in range(0, max(0, len(self._connection_dict) - self._clientCount + self._refresh_count)):
peer_id = random.choice(self.connection_dict.keys()) peer_id = random.choice(self._connection_dict.keys())
kill(peer_id) kill(peer_id)
def kill(self, peer_id): def _kill(self, peer_id):
utils.log('Killing the connection with id ' + str(peer_id), 2) utils.log('Killing the connection with id ' + str(peer_id), 2)
p, iface = self.connection_dict.pop(peer_id) p, iface = self._connection_dict.pop(peer_id)
p.kill() p.kill()
free_interface_set.add(iface) free_interface_set.add(iface)
self.peers_db.unusePeer(peer_id) self._peers_db.unusePeer(peer_id)
def makeNewTunnels(self): def _makeNewTunnels(self):
try: try:
for peer_id, ip, port, proto in self.peers_db.getUnusedPeers(utils.config.client_count - len(self.connection_dict), self.write_pipe): for peer_id, ip, port, proto in self._peers_db.getUnusedPeers(self._client_count - len(self._connection_dict), self._write_pipe):
utils.log('Establishing a connection with id %s (%s:%s)' % (peer_id, ip, port), 2) utils.log('Establishing a connection with id %s (%s:%s)' % (peer_id, ip, port), 2)
iface = free_interface_set.pop() iface = free_interface_set.pop()
self.connection_dict[peer_id] = ( openvpn.client( ip, write_pipe, '--dev', iface, '--proto', proto, '--rport', str(port), self._connection_dict[peer_id] = ( openvpn.client( ip, write_pipe, '--dev', iface, '--proto', proto, '--rport', str(port),
stdout=os.open(os.path.join(utils.config.log, 'vifibnet.client.%s.log' % (peer_id,)), stdout=os.open(os.path.join(utils.config.log, 'vifibnet.client.%s.log' % (peer_id,)),
os.O_WRONLY|os.O_CREAT|os.O_TRUNC) ), os.O_WRONLY|os.O_CREAT|os.O_TRUNC) ),
iface) iface)
self.peers_db.usePeer(peer_id) self._peers_db.usePeer(peer_id)
except KeyError: except KeyError:
utils.log("Can't establish connection with %s : no available interface" % ip, 2) utils.log("Can't establish connection with %s : no available interface" % ip, 2)
except Exception: except Exception:
......
...@@ -51,7 +51,7 @@ def main(): ...@@ -51,7 +51,7 @@ def main():
if ready: if ready:
handle_message(read_pipe.readline()) handle_message(read_pipe.readline())
if time.time() >= next_refresh: if time.time() >= next_refresh:
peers_db.populate(10) peers_db.populate(100)
tunnelManager.refresh() tunnelManager.refresh()
next_refresh = time.time() + utils.config.refresh_time next_refresh = time.time() + utils.config.refresh_time
except KeyboardInterrupt: except KeyboardInterrupt:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment