declare and populate are now separated so we can redeclare the external IP when it...
[re6stnet.git] / db.py
1 #!/usr/bin/env python
2 import sqlite3, xmlrpclib, time
3 import utils
4
5 class PeerManager:
6
7 def __init__(self, dbPath, server, server_port, refresh_time, external_ip, internal_ip, port, proto, db_size):
8 self._refresh_time = refresh_time
9 self._external_ip = external_ip
10 self._internal_ip = internal_ip
11 self._external_port = port
12 self._proto = proto
13 self._db_size = db_size
14 self._proxy = xmlrpclib.ServerProxy('http://%s:%u' % (server, server_port))
15
16 utils.log('Connectiong to peers database', 4)
17 self._db = sqlite3.connect(dbPath, isolation_level=None)
18 utils.log('Preparing peers database', 4)
19 try:
20 self._db.execute("UPDATE peers SET used = 0")
21 except sqlite3.OperationalError, e:
22 if e.args[0] == 'no such table: peers':
23 raise RuntimeError
24
25 self.next_refresh = time.time()
26
27 def refresh(self):
28 utils.log('Refreshing the peers DB', 2)
29 self._declare()
30 self._populate()
31 self.next_refresh = time.time() + self._refresh_time
32
33 def _declare(self):
34 if self._external_ip != None:
35 utils.log('Declaring our connections info', 3)
36 self._proxy.declare((self._internal_ip, self._external_ip, self._external_port, self._proto))
37 else:
38 utils.log('Warning : could not declare the external ip because it is unknown', 4)
39
40 def _populate(self):
41 utils.log('Populating the peers DB', 2)
42 new_peer_list = self._proxy.getPeerList(self._db_size, self._internal_ip)
43 self._db.executemany("INSERT OR IGNORE INTO peers (ip, port, proto, used) VALUES (?,?,?,0)", new_peer_list)
44 if self._external_ip != None:
45 self._db.execute("DELETE FROM peers WHERE ip = ?", (self._external_ip,))
46 utils.log('New peers : %s' % ', '.join(map(str, new_peer_list)), 5)
47
48 def getUnusedPeers(self, nPeers):
49 return self._db.execute("SELECT id, ip, port, proto FROM peers WHERE used = 0 "
50 "ORDER BY RANDOM() LIMIT ?", (nPeers,))
51
52 def usePeer(self, id):
53 utils.log('Updating peers database : using peer ' + str(id), 5)
54 self._db.execute("UPDATE peers SET used = 1 WHERE id = ?", (id,))
55
56 def unusePeer(self, id):
57 utils.log('Updating peers database : unusing peer ' + str(id), 5)
58 self._db.execute("UPDATE peers SET used = 0 WHERE id = ?", (id,))
59
60 def handle_message(self, msg):
61 script_type, arg = msg.split()
62 if script_type == 'client-connect':
63 utils.log('Incomming connection from %s' % (arg,), 3)
64 elif script_type == 'client-disconnect':
65 utils.log('%s has disconnected' % (arg,), 3)
66 elif script_type == 'route-up':
67 if arg != self._external_ip:
68 self._external_ip = arg
69 utils.log('External Ip : ' + arg, 3)
70 self._declare()
71 else:
72 utils.log('Unknow message recieved from the openvpn pipe : ' + msg, 1)