Exemplo n.º 1
0
    def _clientTick(self):
        # main cycle for server state
        while True:
            #new connections
            self._checkListener()
            
            #check peers status
            self._checkPeers()
            self._checkTSS()
            
            #new msgs from Peers nad isTss
            lstMsg = self._recvAllMsgs()
            for msg, p in lstMsg:
                if (p == self._tss):
                    self._count = msg.count
                
                if (msg.type == ACK):   
                    continue
                
                if (msg.type == KEEPALIVE):
                    p.sendMsg(ACK, self._count)
                    continue
                
                if (msg.type == SENDPEERS and p == self._tss):
                    self._tssPeers = self._parsePeerLst(msg.body) # isto é o k deve tar normalmente
                    print("Received new Peer list from Tss:")
                    for peer in self._tssPeers:
                        print("-"+str(peer[0]) + ":" + str(peer[1]))
                    continue
                    
                if (msg.type == CLOSEPEER):
                    p.markToClose()
                    continue
                
                print("WARNING: An unknown/currupted msg was received from : " + p.getAddrf())
                
                
            # client operations
            if (self._status == ONLINE):
                pass
                
            elif (self._status == TSSDOWN):
                #checks if connections lost is due to loss of net
                if (checkNet.check() == False):
                    self.terminate()
                    raise NoNet()
    
                print("TS Server is down")
                newTss = self._assertNewTss()
                if (newTss[0] == self.externalIp):
#                     if (self._tss != None):
#                         self._tss.setIsTSS(False)
#                         self._peers.append(self._tss)
#                         self._checkPeers() # caution, if the tss status is CLOSED this funtions changes sel._status to NEWCOUNT
                    self._tss = None
                    self._status = STARTINGUP # that is why this line is extremely important
                    self._tssPeers = []
                    return 
                
                else:
                    print(self._tssPeers)
                    print("newTss: ")
                    print(newTss)
                    self._tssPeers.remove(newTss)
                    self._tss = self._connectToPeer(socket.gethostname(), newTss[0], info.connTimeout, info.connRetrys)
                    self._status = STARTINGUP
                
            elif (self._status == STARTINGUP):
                print("Current TS Server at: " + self._tss.getAddrf())
                if (not info.debug0 ):
                    self._tss.sendMsg(REGISTER, self._count, socket.gethostname() + ';' + 'True')
                else:
                    self._tss.sendMsg(REGISTER, self._count, socket.gethostname() + ';' + 'True' + ';' +str(self._listener._port))#TODO:dev only
                
                self._status = ONLINE
            
            time.sleep(info.tickPeriod)      
Exemplo n.º 2
0
 def _serverTick(self):
     # main cycle for server
     while True:
         #new connections
         self._checkListener()
         
         #check peers status
         self._checkPeers()
                
         #new msgs
         lstMsg = self._recvAllMsgs()
         for msg, p in lstMsg:
             if (msg.type == ACK):
                 continue                
             
             if (msg.type == KEEPALIVE):
                 if(not p.sendMsg(ACK, self._count)):
                     pass
                 continue
              
             if (msg.type == REGISTER):
                 msgBody = msg.body
                 msgBody = msgBody.split(sep=';') #TODO: meter isto a dar com o CHTSS
                 p.hostname = msgBody[0]
                 if (msgBody[1] == 'True'):
                     p.sendMsg(ACK, self._count)
                 else:
                     print("ERROR: at register")
                 
                 if (info.debug0 == 1):#dev TODO: dev only
                     p.addr = (p.addr[0], int(msgBody[2]))
                     self._status = NEWCOUNT
                     print("New peer: " + p.getAddrf())
                 
                 continue   
                 
             if (msg.type == CLOSEPEER):
                 p.markToClose()
                 continue
             
             if (msg.type == REQPEERS):
                 if(not p.sendMsg(SENDPEERS, self._count, self._buildPeerLst())):
                     pass
                 continue
             
             print("WARNING: An unknown/currupted msg was received from : " + str(p.addr[info.debug0]))
             print(msg.toStrf())
             
         if (self._status == ONLINE):
             if (self._peers == [] and self._noNetTimeout < time.time()):
                 if (checkNet.check() == True):
                     self._noNetTimeout = time.time() + info.timeout
                 else:
                     self.terminate()
                     raise NoNet()
             
         elif (self._status == NEWCOUNT):
             peerLst = self._buildPeerLst()
             if (peerLst == None):
                 print("No connected Peers")
             else:
                 print("Connected peers: ")
                 for peer in self._peers:
                     print("-"+peer.getAddrf())
                 for p in self._peers:
                     if(not p.sendMsg(SENDPEERS , self._count, peerLst)):
                         pass
             self._status = ONLINE
             
         elif (self._status == STARTINGUP):
             self._count = 1
             self._status = ONLINE
             self._noNetTimeout = time.time() + info.timeout
             self._svProcess = exeCaller.Process(info.tsPath)
             updateDDNS.update()
             print("TS server started on local machine")
             
         time.sleep(info.tickPeriod)