Esempio n. 1
0
 def AddingInfoToLastLevel(self, requesterListOfLastLevelNodes, lowRange,
                           highRange, lenConns):
     oldList = self.state.lastlevel
     self.state.lastlevel = []
     myconn = self.state.myconn
     self.state.myconn = Conn(myconn.addr, myconn.port, myconn.name,
                              lowRange, highRange)
     self.state.lastlevel.append(self.state.myconn)
     self.state.lowRange = lowRange
     self.state.highRange = highRange
     for peer in requesterListOfLastLevelNodes:
         site = peer.split()
         addr = site[0]
         port = int(site[1])
         name = site[2]
         peerConnection = Conn(addr, port, name, lowRange, highRange)
         if (peerConnection not in self.state.lastlevel):
             self.state.lastlevel.append(peerConnection)
     self.printinfowithranges()
     self.state.conns = [None] * lenConns
     newlist = self.state.lastlevel[:]
     newlist.remove(self.state.myconn)
     for level in range(0, lenConns):
         randIndex = random.randint(0, len(newlist) - 1)
         ClientHandler(self.state, newlist[randIndex],
                       'HelpUpdateThisLevel', (self, level)).startup()
     '''
Esempio n. 2
0
 def joinatnormal(self, parameters, level, stage):
     # static method variable
     # self.joinatnormal_stagep: stage of the operation, 3 total
     # self.joinatnormal_responses: the list of responses from poll
     # self.joinatnormal_joinerconn: joiner's information
     if stage == 0 and self.joinatnormal_stagep == 0:
         # send everyone a poll message
         self.joinatnormal_stagep = 1
         self.joinatnormal_responses = []
         joinerip = parameters[2]
         joinerport = int(parameters[3])
         joinername = parameters[4]
         joinerconn = Conn(joinerip, joinerport, joinername)
         self.joinatnormal_joinerconn = joinerconn
         for peer in self.state.conns[level]:
             ClientHandler(self.state, peer, 'join4',
                           (joinerconn, self, level)).startup()
         #reactor.callLater(5,self.joinatnormal,parameters,level,2)
     elif stage == 1 and self.joinatnormal_stagep == 1:
         # receive responses until timeout or we have everyone's response
         # parameters is tuple level, maxpeer, key range, ip
         self.joinatnormal_responses.append(parameters)
         if len(self.joinatnormal_responses) == len(
                 self.state.conns[level]):
             #reactor.callLater(1,self.joinatnormal,None,level,2)
             self.joinatnormal(None, level, 2)
     elif stage == 2 and self.joinatnormal_stagep == 1:
         # clearly indicate we have timed out and can no more receive connections
         self.joinatnormal_stagep = 0
         joinerconn = self.joinatnormal_joinerconn
         self.joinatnormal_responses.sort()
         # now we have the winner first send joiner a list then forward request
         # peerlist must have your own ip as well, replace winner by joiner
         newpeerlist = self.state.conns[level][:]  # shallow copy the list
         winner = (filter(
             lambda x: (x.addr == self.joinatnormal_responses[0][3].addr and
                        x.port == self.joinatnormal_responses[0][3].port),
             newpeerlist))[0]
         joinerconnNew = Conn(joinerconn.addr, joinerconn.port,
                              joinerconn.name, winner.lowRange,
                              winner.highRange)
         try:
             newpeerlist[newpeerlist.index(winner)] = joinerconnNew
         except ValueError:
             pass
         ClientHandler(self.state, joinerconn, 'join3',
                       (newpeerlist, level)).startup()
         # forward request to the winner at next level
         ClientHandler(self.state, winner, 'join5',
                       (joinerconn, level + 1)).startup()
         # there is a probability of winner being replaced by this new guy
         if winner.addr != self.state.myconn.addr and winner.port != self.state.myconn.port:
             if randrange(0, 100) < 10:
                 try:
                     mylist = self.state.conns[level]
                     #self.state.conns[level].remove(winner)
                     #self.state.conns[level].append(joinerconn)
                     mylist[mylist.index(winner)] = joinerconn
                 except ValueError:
                     pass
Esempio n. 3
0
 def checksplit(self):
     self.state.chkstate()
     maxnumberofpeeratlastlevel = self.maxnumberofpeeratlastlevel
     if len(self.state.lastlevel) == maxnumberofpeeratlastlevel:
         # first split by finding mygroup and copying mygroup
         myconn = self.state.myconn
         newlist = self.state.lastlevel[:]
         newlist.sort(key=lambda x: (x.addr, x.port))
         indexofconn = newlist.index(myconn)
         mygroup = indexofconn * 4 / maxnumberofpeeratlastlevel
         diff = self.state.highRange - self.state.lowRange + 1
         rangeSplit = diff / 4
         oldRange = self.state.lowRange
         self.state.highRange = self.state.lowRange + rangeSplit * (
             mygroup + 1) - 1
         self.state.lowRange = self.state.lowRange + rangeSplit * mygroup
         stdout.write("checksplit: my group " + str(mygroup) + "\n")
         newlastLevel = newlist[mygroup * maxnumberofpeeratlastlevel /
                                4:(mygroup + 1) *
                                maxnumberofpeeratlastlevel / 4]
         self.state.lastlevel = []
         for peer in newlastLevel:
             self.state.lastlevel.append(
                 Conn(peer.addr, peer.port, peer.name, self.state.lowRange,
                      self.state.highRange))
         # then get the last-1 peers and do something
         # list is supposed to be myself and random peers from each group
         list = [None] * 4
         for x in range(0, 4):
             lRange = oldRange + rangeSplit * (x)
             hRange = oldRange + rangeSplit * (x + 1) - 1
             if x == mygroup:
                 myconn = self.state.myconn
                 list[x] = Conn(myconn.addr, myconn.port, myconn.name,
                                lRange, hRange)
             else:
                 connection = newlist[
                     x * maxnumberofpeeratlastlevel / 4 +
                     randrange(0, maxnumberofpeeratlastlevel / 4)]
                 list[x] = Conn(connection.addr, connection.port,
                                connection.name, lRange, hRange)
             #stdout.write("checksplit: group "+str(x)+"\n")
         # last update last-1 given list
         self.updatelast(list)
     self.state.chkstate()
     return
 def joinatbottom(self,parameters):
     # lock
     # send messages to all the peers in the group
     # and then send client a contact list
     # check split
     joinerip=parameters[2]
     joinerport=int(parameters[3])
     joinername=parameters[4]
     joinerconn=Conn(joinerip,joinerport,joinername)
     lastlevel=self.state.lastlevel[:]
     for peer in self.state.lastlevel:
         ClientHandler(self.state,peer,'join2',joinerconn).startup()
         #ClientHandler(self.state,peer,'join2',Conn(joinerip,joinerport,joinername)).startup()
     lastlevel.append(joinerconn)
     ClientHandler(self.state,joinerconn,'join6',(lastlevel)).startup()
Esempio n. 5
0
 def reduceByOneLevelAndShareInfo(self, lastLevelNodesPeerListMessage):
     for message in lastLevelNodesPeerListMessage:
         parameters = message.split('#')
         for i in range(1, len(parameters) - 1):
             vals = parameters[i].split()
             ip = vals[0]
             port = int(vals[1])
             name = vals[2]
             lowRange = int(vals[3])
             highRange = int(vals[4])
             newConn = Conn(ip, port, name, lowRange, highRange)
             if (newConn in self.state.lastlevel):
                 print(newConn.name + " already exists in the last level!")
             else:
                 self.state.lastlevel.append(newConn)
                 print(newConn.name + " added to the last level!")
     lowMin = float("inf")
     highMax = 0
     for peer in self.state.lastlevel:
         lowMin = min(lowMin, peer.lowRange)
         highMax = max(highMax, peer.highRange)
     self.state.lowRange = lowMin
     self.state.highRange = highMax
     for peer in self.state.lastlevel:
         peer.lowRange = lowMin
         peer.highRange = highMax
     newlist = self.state.lastlevel[:]
     newlist.remove(self.state.myconn)
     for peer in newlist:
         print("Shared with Q!!!!!!!!!!!!!!!!!!!! " + peer.name)
         ClientHandler(self.state, peer,
                       'InsertLastLevelDeleteN-1LevelShareWithLastLevel',
                       (self, self.state.lastlevel[:])).startup()
     print(len(self.state.conns))
     self.state.conns = self.state.conns[:len(self.state.conns) - 1]
     print(len(self.state.conns))
     self.printinfowithranges()
Esempio n. 6
0
 def joinatbottom(self, parameters):
     # lock
     # send messages to all the peers in the group
     # and then send client a contact list
     # check split
     print('This function was called!!!!!!!!!!!!!!!!!!!!!!!!')
     joinerip = parameters[2]
     joinerport = int(parameters[3])
     joinername = parameters[4]
     joinerconn = Conn(joinerip, joinerport, joinername,
                       self.state.lowRange, self.state.highRange)
     print('self.state.lowRange', self.state.lowRange,
           'self.state.highRange', self.state.highRange, 'joinername',
           joinername)
     lastlevel = self.state.lastlevel[:]
     for peer in self.state.lastlevel:
         ClientHandler(self.state, peer, 'join2', joinerconn).startup()
         #ClientHandler(self.state,peer,'join2',Conn(joinerip,joinerport,joinername)).startup()
     lastlevel.append(joinerconn)
     self.printinfowithranges()
     # Whenever adding someone to my last level, I will also send it my range!
     ClientHandler(
         self.state, joinerconn, 'join6',
         (lastlevel, self.state.lowRange, self.state.highRange)).startup()
Esempio n. 7
0
def setupclient(state, ip, port):
    client = ClientHandler(state, Conn(ip, port), 'join')
    client.startup()
    return client
Esempio n. 8
0
    def processFeedback(self, protocol, data):
        if self.mode=='join':
            if data=="WAIT":
                protocol.sendMessage("JOIN_OKAY")
        elif self.mode=='join2':
            if data=="JOIN_OKAY":
                protocol.transport.loseConnection()
        elif self.mode=='join3':
            if data=="JOIN_OKAY":
                protocol.transport.loseConnection()
        elif self.mode=='join4':
            # should save the info received
            # callback with tuple (level, maxpeer, key range, ip)
            # be careful because python types are strange
            if data.startswith("JOIN_PRLY"):
                extra1,callback,level=self.extra
                temp=data.split()
                datum=(temp[2],temp[3],temp[4],self.remote)
                callback.joinatnormal(datum,level,1)
                protocol.sendMessage("JOIN_OKAY")
        elif self.mode=='join5':
            if data=="JOIN_OKAY":
                protocol.transport.loseConnection()
        elif self.mode=='join6':
            if data=="JOIN_OKAY":
                protocol.transport.loseConnection()
        elif self.mode=='exit1':
            protocol.transport.loseConnection()
        elif self.mode=='exit2':
            protocol.transport.loseConnection()
        elif self.mode=='exit3':
            if data.startswith("EXIT_PRLY"):
                callback=self.extra
                temp=data.split()
                datum=(temp[2],temp[3],temp[4],self.remote)
                callback.exitatbottom4(datum)
            protocol.transport.loseConnection()
        elif self.mode=='exit4':
            protocol.transport.loseConnection()
        elif self.mode=='exit5':
            protocol.transport.loseConnection()
        elif self.mode=='exit6':
            protocol.transport.loseConnection()
        elif self.mode=='exit7':
            protocol.transport.loseConnection()

        elif self.mode=='RequestingInfoLastLevel':
            if(data.startswith('LAST_LEVEL_INFO')):
                parameters = data.split()
                peerAddr = parameters[2]
                peerPort = int(parameters[3])
                peerName = parameters[4]
                peerConn = Conn(peerAddr, peerPort, peerName)
                peerLastLevelCount = int(parameters[5])
                peerStatusList, callback = self.extra
                print("OMG, Connection received, length was " + str(peerLastLevelCount))
                peerStatusList.append((peerConn, peerLastLevelCount))
                if(len(peerStatusList) == len(callback.state.conns[len(callback.state.conns)-1])):
                    callback.UsingInfoReceivedForShrinkage(peerStatusList)

        elif self.mode=='RequestNodesLastLevel':
            if(data.startswith('LAST_LEVEL_NODE_RESULT')):
                lastLevelNodesPeerListMessage, callback = self.extra
                lastLevelNodesPeerListMessage.append(data)
                protocol.transport.loseConnection()
                if(len(lastLevelNodesPeerListMessage) == len(self.state.conns[len(self.state.conns)-1])):
                    print("received results from all nodes")
                    callback.reduceByOneLevelAndShareInfo(lastLevelNodesPeerListMessage)
                    #print(lastLevelNodesPeerListMessage)
        elif self.mode=='RequestInfoLastLevel':
            print('Got response for last level')
            callback, requesterListOfLastLevelNodes = self.extra
            parameters = data.split('\n')
            lowRange = int(parameters[0].split()[1])
            highRange = int(parameters[0].split()[2])
            lenConns = int(parameters[0].split()[3])
            for i in range(1, len(parameters)):
                print(parameters[i])
                requesterListOfLastLevelNodes.append(parameters[i])
            callback.AddingInfoToLastLevel(requesterListOfLastLevelNodes, lowRange, highRange, lenConns)
            protocol.transport.loseConnection()
        elif self.mode=='HelpUpdateThisLevel':
            if data.startswith('INFO_FOR_LEVEL_N'):
                callback, l = self.extra
                params = data.split('\n')
                levels = int(params[0].split()[1])
                newPeerName = params[0].split()[3] 
                print('levels',levels)
                newConnectionLayer = []
                for i in range(1, len(params)):
                    peerDetails = params[i].split()
                    peerAddr = peerDetails[0]
                    peerPort = int(peerDetails[1])
                    peerName = peerDetails[2]
                    peerLowRange = int(peerDetails[3])
                    peerHighRange = int(peerDetails[4])
                    if(peerName == newPeerName):
                        connection = Conn(self.state.myconn.addr, self.state.myconn.port,self.state.myconn.name, peerLowRange, peerHighRange)
                    else:
                        connection = Conn(peerAddr, peerPort,peerName, peerLowRange, peerHighRange)
                    newConnectionLayer.append(connection)
                self.state.conns[levels] = newConnectionLayer
                print("We were successful")
                callback.printinfowithranges()
        elif self.mode=='HeartBeat':
            callback, remoteConn, level = self.extra
            parameters = data.split()
            if(parameters[1]!='CORRECT'):
                callback.cleanup(remoteConn.addr, remoteConn.port, remoteConn.name, level)
            protocol.transport.loseConnection()
        elif self.mode=='FindAlternateValue':
            if(data.startswith('FIND_ALTERNATE_VALUE_RESPONSE')):
                callback, replacementConnection, level, peerConnectionsForHelp = self.extra
                parameters = data.split()
                if(len(parameters)<3 or parameters[1]=='FAILED'):
                    print("Will have to look for an Alternate!!!!!!!!!!!!")
                    callback.startPollingConnectionHelpForReplacement(peerConnectionsForHelp, replacementConnection, level)
                else:
                    addr = parameters[2]
                    port = int(parameters[3])
                    name = parameters[4]
                    newAlternateConnection = Conn(addr, port, name)
                    print("Finallly Found SomeOne!!!!!!!!!!!!!!!!!!!!!!")
                    callback.checkAliveStatus(peerConnectionsForHelp, replacementConnection, level, newAlternateConnection)
            protocol.transport.loseConnection()
        elif self.mode=='checkAlternateAliveStatus':
            callback, replacementConnection, level, peerConnectionsForHelp = self.extra
            #protocol.transport.loseConnection()
            if(data.startswith('CHECK_ALTERNATE_ALIVE_STATUS True')):
                parameters = data.split('\n')
                totalPeersSent = int(parameters[0].split()[2])
                connections = []
                for i in range(1,1+totalPeersSent):
                    peerDets = parameters[i].split()
                    addr = peerDets[0]
                    port=int(peerDets[1])
                    name = peerDets[2]
                    connection = Conn(addr, port, name)
                    connections.append(connection)
                print("Time to replace the connection@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
                callback.replacePeerwithAlternatePeer(connections, replacementConnection, level)    
            else:
                callback.startPollingConnectionHelpForReplacement(peerConnectionsForHelp, replacementConnection, level)
        elif self.mode=='findAllDepths':
            print('Got a reply from',self.remote.name)
            callback, l = self.extra
            if data.startswith('RESPONSE_FIND_ALL_DEPTHS'):
                parameters = data.split()
                maxlevel = int(parameters[1])
                maxname = parameters[2]
                maxaddr = parameters[3]
                maxport = int(parameters[4])
                lastlevelnodecount = int(parameters[5])
                maxlowRange = int(parameters[6])
                maxhighRange = int(parameters[7])
                print(data)
                print(maxlevel,'maxlevel')
                print('connsLength',len(self.state.conns))
                print('lastlevelnodecount', lastlevelnodecount)
                print('self.state.minnumberofpeeratlastlevel',self.state.minnumberofpeeratlastlevel)
                if((maxlevel>len(self.state.conns)) or (maxlevel==len(self.state.conns) and lastlevelnodecount > self.state.minnumberofpeeratlastlevel)):
                    print('We came here')
                    while(True):pass
                    callback.doStealSequenceFromThisGuy(maxaddr, maxport, maxname, maxlowRange, maxhighRange)
                else:
                    print("No we came here!!!")
                    while(True):pass
                    callback.reduceALevel()
            else:
                callback.reduceALevel()

        elif self.mode=='findAllDepths2':
            callback, level, parentConnection, responses = self.extra
            responses.append(data)
            if(len(responses) == len(self.state.conns[level-1])):
                callback.sendParentRandomizedResponse(parentConnection, responses)
        else:
            print('closing connection')
            protocol.transport.loseConnection()
 def processRequest(self, protocol, data):
     ## reply a greeting, to test out connection
     if data=="HELLO":
         protocol.sendMessage("HELLO")
     elif data.startswith("JOIN_INIT"):
         protocol.sendMessage("WAIT")
         self.join(protocol,data.split(),0)
     elif data.startswith("JOIN_OKAY"):
         protocol.transport.loseConnection()
     ## this was sent by a peer multicasting about a new peer joining last level
     elif data.startswith("JOIN_BOTT"):
         # seperate out argument passed in and construct a connection object
         parameters=data.split()
         joinerip=parameters[2]
         joinerport=int(parameters[3])
         joinername=parameters[4]
         joinerconn=Conn(joinerip,joinerport,joinername)
         # add the connection to my last level
         self.state.lastlevel.append(joinerconn)
         protocol.sendMessage("JOIN_OKAY")
         self.checksplit()
         self.state.printinfo()
     ## this was sent to new peer, receives a list of peers to add to one level
     elif data.startswith("JOIN_LIST"):
         partialparam=data.split("\n")
         leveloflist=int((partialparam[0].split())[2])
         numberofpeer=int((partialparam[0].split())[3])
         # construct new peer list of that level, note order is important
         newlist=[]
         for i in range(0,numberofpeer):
             parameters=partialparam[1+i].split()
             contactip=parameters[0]
             contactport=int(parameters[1])
             contactname=parameters[2]
             newconn=Conn(contactip,contactport,contactname)
             newlist.append(newconn)
         if leveloflist==len(self.state.conns):
             self.state.addlevel(newlist)
         else:
             stdout.write("joinlist:\twhere am I getting the list from?\n")
         protocol.sendMessage("JOIN_OKAY")
         self.state.printinfo()
     ## this was sent to peers at a none last level, polling its states
     elif data.startswith("JOIN_POLL"):
         # response with level, max peer, key range
         #replymsg="JOIN_PRLY "+self.state.myconn.name+" "+str(self.state.curmaxlv)+" "+str(len(self.state.conns[self.state.curmaxlv]))+" "+str('123')
         replymsg="JOIN_PRLY "+self.state.myconn.name+" "+str(len(self.state.conns))+" "+str(len(self.state.lastlevel))+" "+str('123')
         protocol.sendMessage(replymsg)
     ## forwarded the join request to me
     elif data.startswith("JOIN_FRWD"):
         protocol.sendMessage("JOIN_OKAY")
         # remember [a:b] takes a to b-1, b is not included
         self.join(protocol,data.split()[:5],int(data.split()[5]))
     ## this was sent to new peer, receives a list of peers to add to last level
     elif data.startswith("JOIN_LAST"):
         partialparam=data.split("\n")
         numberofpeer=int((partialparam[0].split())[2])
         newlist=[]
         for i in range(0,numberofpeer):
             parameters=partialparam[1+i].split()
             contactip=parameters[0]
             contactport=int(parameters[1])
             contactname=parameters[2]
             newconn=Conn(contactip,contactport,contactname)
             newlist.append(newconn)
         self.state.lastlevel=newlist
         protocol.sendMessage("JOIN_OKAY")
         self.checksplit()
         self.state.printinfo()
     elif data.startswith("EXIT_INIT"):
         parameters=data.split()
         exiteraddr=parameters[2]
         exiterport=int(parameters[3])
         exitername=parameters[4]
         exiterlevel=int(parameters[5])
         exiterconn=Conn(exiteraddr,exiterport,exitername)
         if exiterlevel==len(self.state.conns):
             # exiting at last level
             self.exitatbottom(exiterconn)
         # do nothing if not at last level
         self.state.printinfo()
     elif data.startswith("EXIT_ELCT"):
         # receive a election request
         self.exitatbottom2()
     elif data.startswith("EXIT_POLL"):
         # response with number level, number of peers at last level, key range
         replymsg="EXIT_PRLY "+self.state.myconn.name+" "+str(len(self.state.conns))+" "+str(len(self.state.lastlevel))+" "+str('123')
         protocol.sendMessage(replymsg)
     elif data.startswith("EXIT_FRWD"):
         # receive request for peer transfer
         parameters=data.split()
         exiteraddr=parameters[2]
         exiterport=int(parameters[3])
         exitername=parameters[4]
         exiterlevel=int(parameters[5])
         exiterpos=int(parameters[6])
         exiterconn=Conn(exiteraddr,exiterport,exitername)
         self.exitatbottom6(exiterconn,exiterlevel,exiterpos)
     elif data.startswith("EXIT_JOIN"):
         parameters=data.split()
         joineraddr=parameters[2]
         joinerport=int(parameters[3])
         joinername=parameters[4]
         joinerconn=Conn(joineraddr,joinerport,joinername)
         self.exitatbottom7(joinerconn)
     elif data.startswith("EXIT_BRCT"):
         parameters=data.split()
         joineraddr=parameters[2]
         joinerport=int(parameters[3])
         joinername=parameters[4]
         joinerconn=Conn(joineraddr,joinerport,joinername)
         self.exitatbottom8(joinerconn)
         self.state.printinfo()
     elif data.startswith("EXIT_LIST"):
         partialparam=data.split("\n")
         numberofpeer=int((partialparam[0].split())[2])
         newlist=[]
         for i in range(0,numberofpeer):
             parameters=partialparam[1+i].split()
             contactaddr=parameters[0]
             contactport=int(parameters[1])
             contactname=parameters[2]
             newconn=Conn(contactaddr,contactport,contactname)
             newlist.append(newconn)
         self.exitatbottom9(newlist)
         self.state.printinfo()
     else:
         protocol.sendMessage("NO SUPPORT")
Esempio n. 10
0
 def doStealSequenceFromThisGuy(self, addr, port, name, lowRange,
                                highRange):
     winnerconn = Conn(addr, port, name, lowRange, highRange)
     print("Trying to steal a node!!!!!!!!!!!!")
     time.sleep(30)
     self.beginStealSequence(winnerConn)
Esempio n. 11
0
    def processRequest(self, protocol, data):
        ## reply a greeting, to test out connection
        print(data)
        if data.startswith('HEARTBEAT'):
            parameters = data.split()
            replymsg = 'HEARTBEAT_RESPONSE '
            lowRange = int(parameters[4])
            highRange = int(parameters[5])
            if (self.state.isAlive == False):
                replymsg += 'REPLACE'
            elif (self.state.lowRange < lowRange
                  or self.state.lowRange > highRange
                  or self.state.highRange < lowRange
                  or self.state.highRange > highRange):
                replymsg += 'REPLACE'
            else:
                replymsg += 'CORRECT'
            protocol.sendMessage(replymsg)
        elif (self.state.isAlive == False):
            replymsg = 'I_AM_DEAD'
            protocol.sendMessage(replymsg)
        elif data == "HELLO":
            protocol.sendMessage("HELLO")
        elif data.startswith("JOIN_INIT"):
            protocol.sendMessage("WAIT")
            self.join(protocol, data.split(), 0)
        elif data.startswith("JOIN_OKAY"):
            protocol.transport.loseConnection()
        ## this was sent by a peer multicasting about a new peer joining last level
        elif data.startswith("JOIN_BOTT"):
            # seperate out argument passed in and construct a connection object
            parameters = data.split()
            joinerip = parameters[2]
            joinerport = int(parameters[3])
            joinername = parameters[4]
            joinerlowrange = int(parameters[5])
            joinerhighrange = int(parameters[6])
            joinerconn = Conn(joinerip, joinerport, joinername, joinerlowrange,
                              joinerhighrange)
            # add the connection to my last level
            self.state.lastlevel.append(joinerconn)
            protocol.sendMessage("JOIN_OKAY")
            self.checksplit()
            self.printinfowithranges()
        ## this was sent to new peer, receives a list of peers to add to one level
        elif data.startswith("JOIN_LIST"):
            partialparam = data.split("\n")
            leveloflist = int((partialparam[0].split())[2])
            numberofpeer = int((partialparam[0].split())[3])
            # construct new peer list of that level, note order is important
            newlist = []
            for i in range(0, numberofpeer):
                parameters = partialparam[1 + i].split()
                contactip = parameters[0]
                contactport = int(parameters[1])
                contactname = parameters[2]
                contactlowRange = int(parameters[3])
                contacthighRange = int(parameters[4])
                newconn = Conn(contactip, contactport, contactname,
                               contactlowRange, contacthighRange)
                newlist.append(newconn)
            if leveloflist == len(self.state.conns):
                self.state.addlevel(newlist)
            else:
                stdout.write("joinlist:\twhere am I getting the list from?\n")
            protocol.sendMessage("JOIN_OKAY")
            self.printinfowithranges()
        ## this was sent to peers at a none last level, polling its states
        elif data.startswith("JOIN_POLL"):
            # response with level, max peer, key range
            #replymsg="JOIN_PRLY "+self.state.myconn.name+" "+str(self.state.curmaxlv)+" "+str(len(self.state.conns[self.state.curmaxlv]))+" "+str('123')
            replymsg = "JOIN_PRLY " + self.state.myconn.name + " " + str(
                len(self.state.conns)) + " " + str(len(
                    self.state.lastlevel)) + " " + str('123')
            protocol.sendMessage(replymsg)
        ## forwarded the join request to me
        elif data.startswith("JOIN_FRWD"):
            protocol.sendMessage("JOIN_OKAY")
            # remember [a:b] takes a to b-1, b is not included
            self.join(protocol, data.split()[:5], int(data.split()[5]))
        ## this was sent to new peer, receives a list of peers to add to last level
        elif data.startswith("JOIN_LAST"):
            partialparam = data.split("\n")
            self.state.lowRange = int((partialparam[0].split())[2])
            self.state.highRange = int((partialparam[0].split())[3])
            numberofpeer = int((partialparam[0].split())[4])
            newlist = []
            for i in range(0, numberofpeer):
                parameters = partialparam[1 + i].split()
                contactip = parameters[0]
                contactport = int(parameters[1])
                contactname = parameters[2]
                newconn = Conn(contactip, contactport, contactname,
                               self.state.lowRange, self.state.highRange)
                newlist.append(newconn)
            self.state.lastlevel = newlist
            protocol.sendMessage("JOIN_OKAY")
            self.checksplit()
            self.printinfowithranges()
        elif data.startswith("EXIT_INIT"):
            parameters = data.split()
            exiteraddr = parameters[2]
            exiterport = int(parameters[3])
            exitername = parameters[4]
            exiterlevel = int(parameters[5])
            exiterconn = Conn(exiteraddr, exiterport, exitername)
            if exiterlevel == len(self.state.conns): pass
            # exiting at last level
            #self.exitatbottom(exiterconn)
            # do nothing if not at last level
            self.printinfowithranges()
        elif data.startswith("EXIT_ELCT"):
            pass
            # receive a election request
            #self.exitatbottom2()
        elif data.startswith("EXIT_POLL"):
            # response with number level, number of peers at last level, key range
            replymsg = "EXIT_PRLY " + self.state.myconn.name + " " + str(
                len(self.state.conns)) + " " + str(len(
                    self.state.lastlevel)) + " " + str('123')
            protocol.sendMessage(replymsg)
        elif data.startswith("EXIT_FRWD"):
            # receive request for peer transfer
            parameters = data.split()
            exiteraddr = parameters[2]
            exiterport = int(parameters[3])
            exitername = parameters[4]
            exiterlevel = int(parameters[5])
            exiterpos = int(parameters[6])
            exiterconn = Conn(exiteraddr, exiterport, exitername)
            #self.exitatbottom6(exiterconn,exiterlevel,exiterpos)
        elif data.startswith("EXIT_JOIN"):
            parameters = data.split()
            joineraddr = parameters[2]
            joinerport = int(parameters[3])
            joinername = parameters[4]
            joinerconn = Conn(joineraddr, joinerport, joinername)
            #self.exitatbottom7(joinerconn)
        elif data.startswith("EXIT_BRCT"):
            parameters = data.split()
            joineraddr = parameters[2]
            joinerport = int(parameters[3])
            joinername = parameters[4]
            joinerconn = Conn(joineraddr, joinerport, joinername)
            #self.exitatbottom8(joinerconn)
            self.printinfowithranges()
        elif data.startswith("EXIT_LIST"):
            partialparam = data.split("\n")
            numberofpeer = int((partialparam[0].split())[2])
            newlist = []
            for i in range(0, numberofpeer):
                parameters = partialparam[1 + i].split()
                contactaddr = parameters[0]
                contactport = int(parameters[1])
                contactname = parameters[2]
                newconn = Conn(contactaddr, contactport, contactname)
                newlist.append(newconn)
            #self.exitatbottom9(newlist)
            self.printinfowithranges()
        elif data.startswith("DELETE_ME"):
            print("We have received the Delete Me message")
            parameters = data.split()
            leaverAddr = parameters[2]
            leaverPort = int(parameters[3])
            leaverName = parameters[4]
            leaverLowRange = int(parameters[5])
            leaverHighRange = int(parameters[6])
            leaverConn = Conn(leaverAddr, leaverPort, leaverName,
                              leaverLowRange, leaverHighRange)
            self.removeFromLastLevel(leaverConn)
            self.checkForShrinkage()
        elif data.startswith("REQUESTING_INFO_LAST_LEVEL "):
            print("We did get a request")
            replymsg = "LAST_LEVEL_INFO " + self.state.myconn.name + " " + self.state.myconn.addr + " " + str(
                self.state.myconn.port
            ) + " " + self.state.myconn.name + " " + str(
                len(self.state.lastlevel))
            protocol.sendMessage(replymsg)
        elif data.startswith('REQUEST_NODES_LAST_LEVEL'):
            replymsg = "LAST_LEVEL_NODE_RESULT " + self.state.myconn.name + '#'
            lastlevelList = self.state.lastlevel[:]
            for lastLevelNode in lastlevelList:
                replymsg += lastLevelNode.addr + " " + str(
                    lastLevelNode.port) + " " + lastLevelNode.name + ' ' + str(
                        lastLevelNode.lowRange) + ' ' + str(
                            lastLevelNode.highRange) + "#"
            protocol.sendMessage(replymsg)
        elif data.startswith(
                'INSERT_LAST_LEVEL_DELETE_N-1_LEVEL_SHARE_WITH_LASTLEVEL'):
            print('GOT A MESSAGE FINALLY !!!!!!!!!!!!!!!!!!!!!!!!!')
        elif data.startswith('SHRINK_ONE_LAYER_UPDATE_LAST_LEVEL'):
            self.state.lastlevel = []
            parameters = data.split('\n')
            self.state.lowRange = int(parameters[0].split()[4])
            self.state.highRange = int(parameters[0].split()[5])
            for i in range(1, len(parameters)):
                newPeer = parameters[i].split()
                newPeerAddr = newPeer[0]
                newPeerPort = int(newPeer[1])
                newPeerName = newPeer[2]
                newPeerLowRange = int(newPeer[3])
                newPeerHighRange = int(newPeer[4])
                newPeerConnection = Conn(newPeerAddr, newPeerPort, newPeerName,
                                         newPeerLowRange, newPeerHighRange)
                self.state.lastlevel.append(newPeerConnection)
            self.state.conns = self.state.conns[:len(self.state.conns) - 1]
            self.printinfowithranges()
        elif data.startswith('FIND_ALTERNATE_VALUE'):
            parameters = data.split()
            level = int(parameters[1])
            lowRange = int(parameters[2])
            highRange = int(parameters[3])
            replymsg = 'FIND_ALTERNATE_VALUE_RESPONSE '
            if (level > len(self.state.conns)):
                replymsg += 'FAILED'
            else:
                for conn in self.state.conns[level]:
                    if (conn.lowRange == lowRange
                            and conn.highRange == highRange):
                        replymsg += 'SUCCESS ' + conn.addr + ' ' + str(
                            conn.port) + ' ' + conn.name
                        break

            protocol.sendMessage(replymsg)
            '''
            nodes = data.split('#')
            

            for i in range(1, len(nodes)-1):
                site = nodes[i].split()
                print(site)
            print("The length of the last level is "+str(len(self.state.lastlevel)))

            #self.state.printinfo()
            '''
        elif data.startswith('DELETE_ONE_NODE_GRANT_ONE_NODE'):
            protocol.sendMessage("OK_HANDLING")
            parameters = data.split()
            addr = parameters[2]
            port = int(parameters[3])
            name = parameters[4]
            requesterConn = Conn(addr, port, name)
            self.findNodeToSacrifice(requesterConn)

        elif data.startswith('DELETE_SACRIFICE_NODE_LAST_LEVEL'):
            print("Got the message")
            parameters = data.split()
            sacrificeNodeAddr = parameters[2]
            sacrificeNodePort = int(parameters[3])
            sacrificeNodeName = parameters[4]
            sacrificeNodeConn = Conn(sacrificeNodeAddr, sacrificeNodePort,
                                     sacrificeNodeName)
            self.printinfowithranges()
            self.state.lastlevel.remove(sacrificeNodeConn)
            self.printinfowithranges()
            protocol.sendMessage('WILL_DELETE_SACRIFICE_NODE')

        elif data.startswith('BECOME_SACRIFICE_AND_JOIN_ANOTHER_NETWORK'):
            parameters = data.split()
            requesterConnAddr = parameters[2]
            requesterConnPort = int(parameters[3])
            requesterConnName = parameters[4]
            requesterConnLowRange = int(parameters[5])
            requesterConnHighRange = int(parameters[6])
            requesterConn = Conn(requesterConnAddr, requesterConnPort,
                                 requesterConnName, requesterConnLowRange,
                                 requesterConnHighRange)
            winnerConnAddr = parameters[7]
            winnerConnPort = int(parameters[8])
            winnerConnName = parameters[9]
            winnerConnLowRange = int(parameters[10])
            winnerConnHighRange = int(parameters[11])
            winnerConn = Conn(winnerConnAddr, winnerConnPort, winnerConnName,
                              winnerConnLowRange, winnerConnHighRange)
            self.handleOwnSacrifice(winnerConn, requesterConn)
            protocol.sendMessage("SacrificingAndJoiningAnotherNetwork")

        elif data.startswith('REQUEST_INFO_LAST_LEVEL'):
            replymsg = 'LAST_LEVEL_DETAILS ' + str(
                self.state.lowRange) + " " + str(
                    self.state.highRange) + ' ' + str(len(self.state.conns))
            for peer in self.state.lastlevel:
                replymsg += '\n' + peer.addr + ' ' + str(
                    peer.port) + ' ' + peer.name
            print('replymsg', replymsg)
            protocol.sendMessage(replymsg)
        elif data.startswith('JOIN_THIS_SACRIFICE_NODE_TO_YOUR_NETWORK'):
            parameters = data.split()
            newJoinerAddr = parameters[1]
            newJoinerPort = int(parameters[2])
            newJoinerName = parameters[3]
            connection = Conn(newJoinerAddr, newJoinerPort, newJoinerName,
                              self.state.lowRange, self.state.highRange)
            protocol.sendMessage("we'll handle this")
            for peer in self.state.lastlevel:
                ClientHandler(self.state, peer,
                              'joinThisSacrificeNodeToOurNetwork',
                              connection).startup()
        elif data.startswith('JOIN_THIS_SACRIFICE_NODE_TO_OUR_NETWORK'):
            parameters = data.split()
            addr = parameters[1]
            port = int(parameters[2])
            name = parameters[3]
            lowRange = int(parameters[4])
            highRange = int(parameters[5])
            connection = Conn(addr, port, name, lowRange, highRange)
            self.state.lastlevel.append(connection)
            self.printinfowithranges()
            protocol.sendMessage('Done. Close Connection')

        elif data.startswith('HELP_UPDATE_THIS_LEVEL'):
            parameters = data.split()
            level = int(parameters[2])
            replymsg = 'INFO_FOR_LEVEL_N ' + str(level) + ' ' + str(
                len(self.state.conns)) + ' ' + self.state.myconn.name
            for peer in self.state.conns[level]:
                replymsg += '\n' + peer.addr + ' ' + str(
                    peer.port) + ' ' + peer.name + ' ' + str(
                        peer.lowRange) + ' ' + str(peer.highRange)
            print(replymsg)
            protocol.sendMessage(replymsg)
        elif data.startswith('REQUEST_DETAILS_FOR_N+1_LEVEL_FOR_SACRIFICE'):
            parameters = data.split()
            level = str(parameters[1])
            if (level > len(self.state.conns)):
                replymsg = 'RESPONSE_DETAILS_FOR_N+1_LEVEL_FOR_SACRIFICE ' + str(
                    len(self.state.conns) + 1) + ' ' + str(
                        self.state.lowRange) + ' ' + str(self.state.highRange)
                protocol.sendMessage(replymsg)
            else:
                self.searchNetworkForAnExtraNode(level)
        elif data.startswith('CHECK_ALTERNATE_ALIVE_STATUS'):
            parameters = data.split()
            lRange = int(parameters[1])
            hRange = int(parameters[2])
            print(
                "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@someone is requesting things from me!!!!"
            )
            if (self.state.isAlive == True and self.state.lowRange >= lRange
                    and self.state.highRange <= hRange):
                replymsg = 'CHECK_ALTERNATE_ALIVE_STATUS True ' + str(
                    len(self.state.lastlevel))
                for conn in self.state.lastlevel:
                    replymsg += '\n' + conn.addr + ' ' + str(
                        conn.port) + ' ' + conn.name

            else:
                replymsg = 'CHECK_ALTERNATE_ALIVE_STATUS False'
            protocol.sendMessage(replymsg)
        elif data.startswith('FIND_ALL_DEPTHS'):
            parameters = data.split()
            print("~~~~~~~~~~~Got the message from", parameters[1])
            level = int(parameters[2])
            self.checkDepth(protocol, level)
        else:
            print(data)
            protocol.sendMessage("NO SUPPORT")