Changeset 1598
- Timestamp:
- 06/05/09 06:17:49 (1 year ago)
- Files:
-
- netsukuku/trunk/pyntk/ntk/core/p2p.py (modified) (12 diffs)
- netsukuku/trunk/pyntk/test/test_p2p.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
netsukuku/trunk/pyntk/ntk/core/p2p.py
r1596 r1598 27 27 from ntk.lib.rpc import FakeRmt, RPCDispatcher, CallerInfo 28 28 29 class P2PError(Exception): 30 '''Generic P2P Error''' 31 29 32 class ParticipantNode(object): 30 33 def __init__(self, … … 52 55 self.pid = pid 53 56 54 def part ecipate(self):55 """ self.me is now a participant node"""57 def participate(self): 58 """Set self.me to be a participant node.""" 56 59 57 60 for l in xrange(self.levels): … … 108 111 109 112 mp = self.mapp2p 110 hIP = [None] *mp.levels113 hIP = [None] * mp.levels 111 114 for l in reversed(xrange(mp.levels)): 112 115 for id in xrange(mp.gsize): 113 for sign in [-1,1]:114 hid=(IP[l]+id*sign)%mp.gsize115 if mp.node_get(l, hid).participant:116 hIP[l]=hid117 break118 if hIP[l]:119 break116 for sign in [-1, 1]: 117 hid = (IP[l] + id * sign) % mp.gsize 118 if mp.node_get(l, hid).participant: 119 hIP[l] = hid 120 break 121 if hIP[l]: 122 break 120 123 if hIP[l] is None: 121 124 return None … … 124 127 # we can stop here 125 128 break 129 126 130 return hIP 127 131 … … 135 139 136 140 lvl = self.mapp2p.nip_cmp(hip, self.maproute.me) 137 br = self.maproute.node_get(lvl, hip[lvl]).best_route()141 br = self.maproute.node_get(lvl, hip[lvl]).best_route() 138 142 if not br: 139 143 return None 140 144 return self.neigh.id_to_neigh(br.gw) 141 145 142 def part ecipate(self):146 def participate(self): 143 147 """Let's become a participant node""" 144 148 145 self.mapp2p.part ecipate()149 self.mapp2p.participate() 146 150 147 151 for nr in self.neigh.neigh_list(): … … 149 153 150 154 def participant_add(self, pIP): 155 '''Add a participant node to the P2P service 156 157 :param pIP: participant node's Netsukuku IP (nip) 158 ''' 151 159 continue_to_forward = False 152 160 … … 164 172 # continue to advertise the new participant 165 173 for nr in self.neigh.neigh_list(): 166 nr.ntkd.p2p.participant_add(self.pid, pIP) 174 nr.ntkd.p2p.participant_add(self.pid, pIP) # ntkd.p2p is a P2PAll 175 # instance so we must 176 # pass also the P2P pid 167 177 168 178 def msg_send(self, sender_nip, hip, msg): … … 180 190 n = self.neigh_get(hip) 181 191 if n: 182 exec("return n.ntkd.p2p.PID_" +str(self.mapp2p.pid)+192 exec("return n.ntkd.p2p.PID_" + str(self.mapp2p.pid) + 183 193 ".msg_send(sender_nip, hip, msg)") 184 194 else: … … 205 215 def peer(self, hIP=None, key=None): 206 216 if hIP is None and key is None: 207 raise Exception("hIP and key are both None. Specify at least one")217 raise P2PError("hIP and key are both None. Specify at least one") 208 218 return self.RmtPeer(self, hIP=hIP, key=key) 209 219 … … 226 236 227 237 self.remotable_funcs = [self.pid_getall] 228 self.events =Event(['P2P_HOOKED'])238 self.events = Event(['P2P_HOOKED']) 229 239 230 240 def listen_hook_ev(self, hook): … … 241 251 def pid_get(self, pid): 242 252 if pid not in self.service: 243 return self.pid_add(pid)253 return self.pid_add(pid) 244 254 else: 245 return self.service[pid]255 return self.service[pid] 246 256 247 257 def pid_getall(self): 248 258 return [(s, self.service[s].mapp2p.map_data_pack()) 249 for s in self.service]259 for s in self.service] 250 260 251 261 def p2p_register(self, p2p): … … 291 301 for s in self.service: 292 302 if self.service[s].participant: 293 self.service[s].partecipate()303 self.service[s].participate() 294 304 295 305 self.events.send('P2P_HOOKED', ())
