Statistics
| Branch: | Revision:

## mobicen / varie / util / oldNode.py @ 9110387b

 1 ```from prettytable import PrettyTable ``` ```import os ``` ```import EventScheduler, MyUtil ``` ```import code ``` ```class Node(object): ``` ``` def __init__(self, name, outFolder): ``` ``` self.name = name ``` ``` self.RT={} #RoutingTable {destName:{'distance':float, 'NHs':[lista dei next-hop equivalenti]}} ``` ``` self.RT[name] = {'distance': 0.0, 'NHs': [name]} ``` ``` self.neighs={} #key: neigh_name, linkCost: attributes of neigh eg{'weight':float} ``` ``` self.bufferedDV={} ``` ``` def set_Neighbours(self,neighs): ``` ``` self.neighs.update(neighs) ``` ``` def getLinkCost(self, v): ``` ``` if (v in self.neighs): ``` ``` try: ``` ``` return float(self.neighs[v]['weight']) ``` ``` except KeyError: ``` ``` return 1.0 ``` ``` return 1000.0 ``` ```class LegacyNode(Node): ``` ``` def printNode(self): ``` ``` print "LegacyNode: "+self.name ``` ``` t = PrettyTable(['DEST', 'NHs']) ``` ``` for d in self.RT: ``` ``` t.add_row([str(d), self.RT[d]['NHs']]) ``` ``` print t ``` ``` def sendDV(self): ``` ``` toSend = {} #prepare standard DV (plus dummy-propagation of buffered info) ``` ``` ``` ``` for dest in self.RT: #Plain DV protocol ``` ``` dist = self.RT[dest]['distance'] ``` ``` if (dest in self.bufferedDV and len(self.bufferedDV[dest]) > 1): #dummy propagation ``` ``` CentrInfo = self.bufferedDV[dest][1] #a tuple (creator,rdist,splitcontr,load) ``` ``` toSend[dest] = (dist, CentrInfo) ``` ``` else: ``` ``` toSend[dest] = (dist,) ``` ``` return toSend ``` ``` def processRecInfo(self, time, senderName, DV): ``` ``` self.bufferedDV = DV #store last received DV in RX_buffer ``` ``` lc = self.getLinkCost(senderName) ``` ``` for dest in DV: ``` ``` adv_dist = DV[dest][0] ``` ``` #Bellman-Ford (multi NHs) ``` ``` if dest not in self.RT: ``` ``` self.RT[dest] = {'distance': adv_dist + lc, 'NHs': [senderName]} ``` ``` else: ``` ``` cdist = self.RT[dest]['distance'] ``` ``` if (adv_dist + lc <= cdist): ``` ``` if (adv_dist + lc < cdist): ``` ``` self.RT[dest]['distance'] = adv_dist + lc ``` ``` self.RT[dest]['NHs'] = [senderName] ``` ``` elif (adv_dist + lc == cdist): ``` ``` if (senderName not in self.RT[dest]['NHs']): ``` ``` self.RT[dest]['NHs'].append(senderName) ``` ```class PopNode(Node): ``` ``` def __init__(self, name, outFolder): ``` ``` Node.__init__(self, name, outFolder) ``` ``` self.DT = {name: {'contribs': {}, 'Load': 0.0}} #DestinationTable {destName: {'contribs':{neigh: contrib}, 'Load':load of dest}} ``` ``` ``` ``` ``` ``` def printNode(self): ``` ``` print "PopNode: "+self.name+ " - Load: "+str(self.computeCentrality()) ``` ``` t = PrettyTable(['DEST', 'NHs','CONTRs','Load']) ``` ``` for d in self.RT: ``` ``` contr_string = "" ``` ``` try: ``` ``` contributors = self.DT[d]['contribs'] ``` ``` contr_string = str(contributors) ``` ``` except: ``` ``` contr_string="[//]" ``` ``` pass ``` ``` t.add_row([str(d), self.RT[d]['NHs'], contr_string, str(self.DT[d]['Load'])]) ``` ``` print t ``` ``` #send DV to all neighbours and schedule next sending ``` ``` def sendDV(self): ``` ``` #update centrality ``` ``` myCentr = self.computeCentrality() ``` ``` toSend = {} #prepare DV as dict {dest_name:(distance,CentrInfo=(NHs,contribute,Load))} ``` ``` ``` ``` for dest in self.RT: ``` ``` dist = self.RT[dest]['distance'] ``` ``` NHs = self.RT[dest]['NHs'] ``` ``` Load = -1.0 ``` ``` try: ``` ``` Load = self.DT[dest]['Load'] ``` ``` except: ``` ``` pass ``` ``` mycontr = 1.0 #my contributes for any dest is at least 1, counting for me ``` ``` try: ``` ``` contributors = self.DT[dest]['contribs'] ``` ``` mycontr = 1.0 + sum(self.DT[dest]['contribs'].values()) ``` ``` except KeyError: ``` ``` # if I know dest but I do not have any contributes for it ``` ``` pass ``` ``` #Split contributes before sending them ``` ``` splitcontr = mycontr / len(NHs) ``` ``` # CentrInfo = (creator, creator_dist, splitcontr, load) ``` ``` toSend[dest] = (dist, (self.name, dist, splitcontr, Load)) ``` ``` return toSend ``` ``` def processRecInfo(self, time, senderName, DV): ``` ``` ``` ``` myCentr = self.computeCentrality() ``` ``` lc = self.getLinkCost(senderName) ``` ``` for dest in DV: ``` ``` '''if (dest==self.name): ``` ``` continue''' ``` ``` adv_dist = DV[dest][0] ``` ``` #Bellman-Ford (multi NHs) ``` ``` if dest not in self.RT: ``` ``` self.RT[dest] = {'distance': adv_dist + lc, 'NHs': [senderName]} ``` ``` self.DT[dest] = {'contribs': {}, 'Load': 0.0} ``` ``` else: ``` ``` cdist = self.RT[dest]['distance'] ``` ``` if (adv_dist + lc <= cdist): ``` ``` if (adv_dist + lc < cdist): ``` ``` self.RT[dest]['distance'] = adv_dist + lc ``` ``` self.RT[dest]['NHs'] = [senderName] ``` ``` elif (adv_dist + lc == cdist): ``` ``` if (senderName not in self.RT[dest]['NHs']): ``` ``` self.RT[dest]['NHs'].append(senderName) ``` ``` # END Bellman-Ford, now RT is up-to-date ``` ``` ``` ``` # Guerrieri + contributions intercepting ``` ``` for dest in DV: ``` ``` try: ``` ``` CentrInfo = DV[dest][1] ``` ``` except IndexError as e: ``` ``` #No CentrInfo ``` ``` pass ``` ``` else: ``` ``` creator = CentrInfo[0] ``` ``` rdist = CentrInfo[1] ``` ``` splitcontr = CentrInfo[2] ``` ``` Load = CentrInfo[3] ``` ``` cdist = self.RT[dest]['distance'] ``` ``` try: ``` ``` rcost = self.RT[creator]['distance'] ``` ``` except: ``` ``` rcost = 1000000.0 ``` ``` ''' ``` ``` ### TUTOR OF LEGACY NEIGH ### ``` ``` tutor_addend = 0 ``` ``` if (senderName != creator): ``` ``` #il mio vicino e' legacy! genero un contributo da ``` ``` #parte di senderName se sono suo NH per raggiungere dest ``` ``` if (adv_dist >= lc + self.RT[dest]['distance'] and rcost < 1000000.0): ``` ``` tutor_addend += (rcost * (rcost + 1) / 2) #si assume metrica hop ``` ``` #tutor_addend += 1 #recupero ``` ``` ''' ``` ``` #Intercetto se la mia cdist < rdist e, in particolare, ``` ``` # rdist >= cdist + rcost, perche' vuol dire che io giaccio ``` ``` # su un cammino minimo tra remote e dest ``` ``` # NB: i contributi pero' li taggo per vicino che me li consegna!!! ``` ``` # cammini minimi equivalenti possono infatti convergere in me e, ``` ``` # pur provenendo dallo stesso remoteNode, arrivarmi gia' splittati. ``` ``` # Pertanto devo tenermi i contributi separati per vicino che me li manda ``` ``` if (cdist < rdist and (rdist >= cdist + rcost)): ``` ``` #self.updateContributors(senderName, dest, splitcontr + tutor_addend) #con tutoring vicini Legacy ``` ``` self.updateContributors(senderName, dest, splitcontr) #senza tutoring vicini Legacy ``` ``` else: ``` ``` self.removeContr(senderName, dest) ``` ``` # La condizione di aggiornamento del Load puo' essere piu' blanda. ``` ``` # Se il remoteNode e' piu' vicino di me (punto e basta) allora mi fido. ``` ``` # Poiche' ogni link costa almeno 1 posso mettere < e non <= ``` ``` if (rdist < cdist): ``` ``` self.DT[dest]['Load'] = Load ``` ``` ``` ``` def computeCentrality(self): ``` ``` tot=0.0 ``` ``` for d in self.DT: ``` ``` if (d != self.name): ``` ``` part = sum(self.DT[d]['contribs'].values()) ``` ``` tot += part ``` ``` self.DT[self.name]['Load'] = float(tot) ``` ``` return float(tot) ``` ``` def updateContributors(self, neighName, dest, contr): ``` ``` self.DT[dest]['contribs'][neighName] = contr ``` ``` def removeContr(self, neighName, dest): ``` ``` if neighName in self.DT[dest]['contribs']: ``` ` self.DT[dest]['contribs'].pop(neighName)`