Changeset 1598

Show
Ignore:
Timestamp:
06/05/09 06:17:49 (10 months ago)
Author:
eriol
Message:

Added tests for p2p module

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • netsukuku/trunk/pyntk/ntk/core/p2p.py

    r1596 r1598  
    2727from ntk.lib.rpc import FakeRmt, RPCDispatcher, CallerInfo 
    2828 
     29class P2PError(Exception): 
     30    '''Generic P2P Error''' 
     31 
    2932class ParticipantNode(object): 
    3033    def __init__(self, 
     
    5255        self.pid = pid 
    5356 
    54     def partecipate(self): 
    55         """self.me is now a participant node""" 
     57    def participate(self): 
     58        """Set self.me to be a participant node.""" 
    5659 
    5760        for l in xrange(self.levels): 
     
    108111 
    109112        mp = self.mapp2p 
    110         hIP = [None]*mp.levels 
     113        hIP = [None] * mp.levels 
    111114        for l in reversed(xrange(mp.levels)): 
    112115            for id in xrange(mp.gsize): 
    113                     for sign in [-1,1]: 
    114                             hid=(IP[l]+id*sign)%mp.gsize 
    115                             if mp.node_get(l, hid).participant: 
    116                                     hIP[l]=hid 
    117                                     break 
    118                     if hIP[l]: 
    119                             break 
     116                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 
    120123            if hIP[l] is None: 
    121124                return None 
     
    124127                # we can stop here 
    125128                break 
     129 
    126130        return hIP 
    127131 
     
    135139 
    136140        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() 
    138142        if not br: 
    139143            return None 
    140144        return self.neigh.id_to_neigh(br.gw) 
    141145 
    142     def partecipate(self): 
     146    def participate(self): 
    143147        """Let's become a participant node""" 
    144148 
    145         self.mapp2p.partecipate() 
     149        self.mapp2p.participate() 
    146150 
    147151        for nr in self.neigh.neigh_list(): 
     
    149153 
    150154    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        ''' 
    151159        continue_to_forward = False 
    152160 
     
    164172        # continue to advertise the new participant 
    165173        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 
    167177 
    168178    def msg_send(self, sender_nip, hip, msg): 
     
    180190        n = self.neigh_get(hip) 
    181191        if n: 
    182             exec("return n.ntkd.p2p.PID_"+str(self.mapp2p.pid)
     192            exec("return n.ntkd.p2p.PID_" + str(self.mapp2p.pid)
    183193                 ".msg_send(sender_nip, hip, msg)") 
    184194        else: 
     
    205215    def peer(self, hIP=None, key=None): 
    206216        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") 
    208218        return self.RmtPeer(self, hIP=hIP, key=key) 
    209219 
     
    226236 
    227237        self.remotable_funcs = [self.pid_getall] 
    228         self.events=Event(['P2P_HOOKED']) 
     238        self.events = Event(['P2P_HOOKED']) 
    229239 
    230240    def listen_hook_ev(self, hook): 
     
    241251    def pid_get(self, pid): 
    242252        if pid not in self.service: 
    243                 return self.pid_add(pid) 
     253            return self.pid_add(pid) 
    244254        else: 
    245                 return self.service[pid] 
     255            return self.service[pid] 
    246256 
    247257    def pid_getall(self): 
    248258        return [(s, self.service[s].mapp2p.map_data_pack()) 
    249                         for s in self.service] 
     259                    for s in self.service] 
    250260 
    251261    def p2p_register(self, p2p): 
     
    291301        for s in self.service: 
    292302            if self.service[s].participant: 
    293                     self.service[s].partecipate() 
     303                self.service[s].participate() 
    294304 
    295305        self.events.send('P2P_HOOKED', ())