Пример #1
0
	def run(self):
		serversocket	= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		hostname		= socket.gethostname() #hack to get my host name
		
		try:
			tokenized	= hostname.split('.')
			hostname	= '{}.{}.{}.{}'.format(tokenized[3], tokenized[2], tokenized[1], tokenized[0])
		except:
			hostname = 'localhost'
		
		print socket.gethostname()
		print hostname
	
		serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
		serversocket.bind((hostname, 8888))
		serversocket.listen(5)
		
		while True:
			clientsocket = serversocket.accept()[0]
			
			clientsocket.setblocking(False)
						
			clientsocket.send('\n\rWelcome! Enter your name:\n\r')

			playerInput = ''

			while playerInput == '':
				try:
					playerInput = clientsocket.recv(1024)
				except:
					playerInput = ''

				if len(playerInput) > 0:
					playerInput = playerInput.strip()

					if ActorEngine.playerExists(playerInput) == True:
						player		= ActorEngine.loadPlayer(playerInput)				
						connection	= Connection(clientsocket, player)
						loginEvent	= Event()

						player.attributes['connection']			= connection
						loginEvent.attributes['signature']		= 'player_login'
						loginEvent.attributes['data']['player'] = player

						ActorEngine.receiveEvent(loginEvent)
						ConnectionEngine.receiveEvent(loginEvent)
					else:
						clientsocket.send('\n\rPlayer not found.\n\rEnter your name:')

						playerInput = ''
				else:
					playerInput = ''		
			
			sleep(2)
	def removeClosedConnections(self):
		ConnectionEngine.lock('closedConnectionSemaphore')
		
		for connection in ConnectionEngine.attribute('closedConnections'):
			ConnectionEngine.attribute('connectionList').remove(connection)
			connection.attributes['socket'].close()
			
			player													= connection.attributes['player']
			playerName												= player.attributes['name']
			logoutNotificationEvent									= Event()
			logoutNotificationEvent.attributes['signature']			= 'broadcast_to_all_players'
			logoutNotificationEvent.attributes['data']['message']	= '{} logged off.'.format(playerName)
		
			ActorEngine.receiveEvent(logoutNotificationEvent)
		
		ConnectionEngine.setAttribute('closedConnections', [])
		
		ConnectionEngine.release('closedConnectionSemaphore')
	def addNewConnections(self):
		ConnectionEngine.lock('newConnectionSemaphore')
		
		for connection in ConnectionEngine.attribute('newConnections'):
			player									= connection.attributes['player']
			loginEvent								= Event()
			loginEvent.attributes['signature']		= 'player_login'
			loginEvent.attributes['data']['player'] = player

			RoomEngine.receiveEvent(loginEvent)
			
			ConnectionEngine.attribute('connectionList').append(connection)
			
			playerName												= player.attributes['name']
			loginNotificationEvent									= Event()
			loginNotificationEvent.attributes['signature']			= 'broadcast_to_all_players'
			loginNotificationEvent.attributes['data']['message']	= '{} just logged in.'.format(playerName)
		
			ActorEngine.receiveEvent(loginNotificationEvent)
		
		ConnectionEngine.setAttribute('newConnections', [])
		
		ConnectionEngine.release('newConnectionSemaphore')