def main(self):
		self.commCondition.acquire()
		while self.switch:
			if len( self.packQueue ) <= 0:
				self.commCondition.wait()
			( pid, code, package ) = self.packQueue.pop()
			Logger.info('received head from %s : [%d, %2x]' % ( self.address, 
																pid,
																code ) )
			Logger.debug('received package from %s : '%self.address, package)
			
			#检查权限
			if not self.packager.codeAuthorized( code, self.myAuth ):
				Logger.warning( '%s do not allow to response the package %d'%( self.address, code ) )
				continue
			
			#解析包
			data = self.packager.parsePackage( self.mainThreadName, code, package )
			packInfo = self.packager.codeFindPackage( code )
			
			#发送回应包
			if packInfo['ExistReply'] == 1:
				respPackage = self.packager.genPackage( self.mainThreadName, 'Response', pid )
				self.send( 'Response', respPackage )

			func = getattr( self, packInfo['Name'] )
			unitNum = packInfo['StructLabel'].split( ',' ).__len__()
			data = [data[i : i + unitNum] for i in range( 0, len( data ), unitNum )]
			func( data )
		self.commCondition.release()
	def WOL(self, data):
		udp = socket( AF_INET, SOCK_DGRAM )
		for groupData in data:
			terminalInfo = self.terminalManager.selectTerminal( groupData[0] )
			wolPackage = a2b_hex( 'f' * 12 + sub( '-', '', terminalInfo['Mac'] ) * 16 )
			Logger.info( '%s WOL %s:'%( self.address, terminalInfo['Name'] ), wolPackage )
			udp.sendto( wolPackage, ( BROADCASTADDR, 6666 ) )
	def TerminalInfo(self, data):
		data = data[0]
		terminalNames = data.split( ',' )
		self.tStatus = []
		print 'Terminal'+'='*30
		for terminal in terminalNames:
			self.tStatus.append([terminal,0])
			Logger.info('terminal: %s'%terminal)
	def open(self):
		print '='*38
		while self.switch:
			name = raw_input( 'which terminal: ' )
			for i in range( len( self.tStatus ) ):
				if self.tStatus[i][0] == name:
					if self.tStatus[i][1] == 0:
						wolPackager = Packager.genPackage( self.tname, 'WOL', 3, [ i ] )
						self.send( wolPackager )
						Logger.info( 'open %s!'%self.tStatus[i][0] )
					else:
						Logger.info( '%s is online!'%self.tStatus[i][0] )
					break;
 def receive(self):
     try:
         sourceStream = ""
         while self.switch:
             Logger.debug("%s receiving..." % self.address)
             recvStream = self.sock.recv(1000)
             Logger.debug("receive raw stream from %s : " % self.address, recvStream)
             if recvStream:
                 self.isAlive = True
                 sourceStream = self.parseHeader("%s%s" % (sourceStream, recvStream))
             elif self.switch == True:
                 Logger.info("%s disconnect..." % self.address)
                 self.shutdown()
     except:
         Logger.error("%s receive error : \n%s" % (self.address, format_exc()))
         self.shutdown()
		def changeStatus( delay, destAddr ):
			if delay:
				if self.__status.has_key( destAddr ):
					if self.__status[ destAddr ][ 'Status' ] == False:
						self.activateTerminal( destAddr )
					else:
						return;
				else:
					try:
						host = socket.gethostbyaddr( destAddr )[0]
					except socket.herror, e:
						if e.errno != 11004:
							raise Exception( 'get host name error!' )
						host = destAddr
					mac = getMac( destAddr )
					terminalID = self.setTerminal( destAddr, mac, host )
					self.__status[ destAddr ] = { 'TerminalID':terminalID, 'Name':host, 'IPv4':destAddr, 'Mac':mac }
					queryPackInfo = Packager.nameFindPackage( 'QueryTerminals' )
					OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] )
				Logger.info( 'Terminal %s login!'%destAddr )
				self.__status[ destAddr ][ 'Status' ] = True
				queryPackInfo = Packager.nameFindPackage( 'QueryStatus' )
				OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] )
						if e.errno != 11004:
							raise Exception( 'get host name error!' )
						host = destAddr
					mac = getMac( destAddr )
					terminalID = self.setTerminal( destAddr, mac, host )
					self.__status[ destAddr ] = { 'TerminalID':terminalID, 'Name':host, 'IPv4':destAddr, 'Mac':mac }
					queryPackInfo = Packager.nameFindPackage( 'QueryTerminals' )
					OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] )
				Logger.info( 'Terminal %s login!'%destAddr )
				self.__status[ destAddr ][ 'Status' ] = True
				queryPackInfo = Packager.nameFindPackage( 'QueryStatus' )
				OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] )
			else:
				if self.__status.has_key( destAddr ):
					if self.__status[ destAddr ][ 'Status' ] == True:
						Logger.info( 'Terminal %s logout!'%destAddr )
						self.__status[ destAddr ][ 'Status' ] = False
						queryPackInfo = Packager.nameFindPackage( 'QueryStatus' )
						OuterService.broadcast( [ 0, queryPackInfo[ 'Code' ], '' ] )
		
		while self.__switch:
			for existsAddr in self.__status.keys():
				ICMP.send_one_ping(mySocket, existsAddr, my_ID)
				delay = ICMP.receive_one_ping(mySocket, my_ID, 1)
				changeStatus( delay, existsAddr )
			
			for i in range( 1, ipCount ):
				destAddr = socket.inet_ntoa( pack( '>L', netAddrBit + i ) )
				if destAddr == IP or self.__status.has_key( destAddr ):
					continue
				ICMP.send_one_ping(mySocket, destAddr, my_ID)
	def QueryStatus(self, data):
		terminalStatus = self.terminalManager.getStatus()
		Logger.info('terminal status:%s'%terminalStatus)
		terminalStatusPackage = self.packager.genPackage( self.mainThreadName, 'TerminalStatus', self.pid, terminalStatus )
		self.send( 'TerminalStatus', terminalStatusPackage )
		
	def QueryTerminals(self, data):
		terminalList = self.terminalManager.findAllTerminal()
		terminalNames = [ ','.join( [theTerminal['Name'] for theTerminal in terminalList] ) ]
		Logger.info('terminal name:%s'%terminalNames)
		terminalInfoPackage = self.packager.genPackage( self.mainThreadName, 'TerminalInfo', self.pid, terminalNames )
		self.send( 'TerminalInfo', terminalInfoPackage )