]> git.mxchange.org Git - quix0rs-apt-p2p.git/blob - knet.py
only send the ID along in khashmir messages, don't send the host and
[quix0rs-apt-p2p.git] / knet.py
1 #
2 #  knet.py
3 #  UberTracker
4 #
5 #  Created by andrew loewenstern on Sun Jun 13 2004.
6 #  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
7 #
8
9 from khashmir.khashmir import Khashmir
10 from twisted.internet import reactor
11 from whrandom import randrange
12 import sys, os
13
14 class Network:
15     def __init__(self, size=0, startport=5555, localip='127.0.0.1'):
16         self.num = size
17         self.startport = startport
18         self.localip = localip
19
20     def _done(self, val):
21         self.done = 1
22         
23     def setUp(self):
24         self.kfiles()
25         self.l = []
26         for i in range(self.num):
27             self.l.append(Khashmir('', self.startport + i, '/tmp/kh%s.db' % (self.startport + i)))
28         reactor.iterate()
29         reactor.iterate()
30         
31         for i in self.l:
32             i.addContact(self.localip, self.l[randrange(0,self.num)].port)
33             i.addContact(self.localip, self.l[randrange(0,self.num)].port)
34             i.addContact(self.localip, self.l[randrange(0,self.num)].port)
35             reactor.iterate()
36             reactor.iterate()
37             reactor.iterate() 
38             
39         for i in self.l:
40             self.done = 0
41             i.findCloseNodes(self._done)
42             while not self.done:
43                 reactor.iterate()
44         for i in self.l:
45             self.done = 0
46             i.findCloseNodes(self._done)
47             while not self.done:
48                 reactor.iterate()
49
50     def tearDown(self):
51         for i in self.l:
52             i.listenport.stopListening()
53         self.kfiles()
54         
55     def kfiles(self):
56         for i in range(self.startport, self.startport+self.num):
57             try:
58                 os.unlink('/tmp/kh%s.db' % i)
59             except:
60                 pass
61             
62         reactor.iterate()
63     
64 if __name__ == "__main__":
65     n = Network(int(sys.argv[1]), int(sys.argv[2]), sys.argv[3])
66     n.setUp()
67     try:
68         reactor.run()
69     finally:
70         n.tearDown()