Ejemplo n.º 1
0
 def terminate(self):
     if (self._listener != None):
         self._listener.close()
         
     # if it ISEN'T the server
     if (self._tss != None):
         try:
             self._tss.sendMsg(CLOSEPEER)
             self._tss.closePeer()
         except Exception as e:
             print(e.args)
     
     # if it IS the server
     else:
         self._svProcess.kill()
         
         # if there are no peers connected
         if (self._peers == []):
             updateDDNS.update('1.1.1.1')
             print("TS server is beeing closed on local machine, and there are no peers connected.")
             print("Setting ddns ip to 1.1.1.1")
     
         else:
             for p in self._peers:
                 try:
                     p.sendMsg(CLOSEPEER)
                     p.closePeer()
                 except Exception as e:
                     print(e.args)
                     
     print("TS_handler has been terminated")
     return
Ejemplo 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)