예제 #1
0
    def __handle_existing_connections(self):
        # IF no_errors then:
		
        # Gets the player's move (who's turn is up)
        num, msg = Protocol.recv_all(self.players_sockets[self.turn])
        if num == Protocol.NetworkErrorCodes.DISCONNECTED:
            Protocol.send_all(self.players_sockets[1 - self.turn], 'EXIT')
            self.shut_down_server()
        # Sends the move to his opponent
        if num != Protocol.NetworkErrorCodes.FAILURE:
			eNum, eMsg = Protocol.send_all(self.players_sockets[1 - self.turn], msg)
			if eNum == Protocol.NetworkErrorCodes.FAILURE:
				Protocol.send_all(self.players_sockets[self.turn], 'EXIT')
				self.shut_down_server()
			# Receives the opponent's response
			num, msg = Protocol.recv_all(self.players_sockets[1- self.turn])
			if num == Protocol.NetworkErrorCodes.FAILURE:
				print msg
				self.shut_down_server()
			if num == Protocol.NetworkErrorCodes.DISCONNECTED:
				Protocol.send_all(self.players_sockets[self.turn], 'EXIT')
				self.shut_down_server()
			# Sends the response back to the player who's turn is up
			eNum, eMsg = Protocol.send_all(self.players_sockets[self.turn], msg)
			if eNum:
				print eMsg
				self.close_client()
예제 #2
0
    def __handle_new_connection(self):
        
        connection, client_address = self.l_socket.accept()

        # Request from new client to send his name
        eNum, eMsg = Protocol.send_all(connection, "ok_name")
        if eNum:
            sys.stderr.write(eMsg)
            self.shut_down_server()
        
        ################################################
        
        
        # Receive new client's name
        num, msg = Protocol.recv_all(connection)
        if num == Protocol.NetworkErrorCodes.FAILURE:
            sys.stderr.write(msg)
            self.shut_down_server()

        if num == Protocol.NetworkErrorCodes.DISCONNECTED:

            sys.stderr.write(msg)
            self.shut_down_server()
        
        self.players_names.append(msg)

        self.players_sockets.append(connection)
        self.all_sockets.append(connection)
        print "New client named '%s' has connected at address %s." % (msg,client_address[0])

        if len(self.players_sockets) == 2:  # we can start the game
            self.__set_start_game(0) 
            self.__set_start_game(1)
예제 #3
0
파일: Client.py 프로젝트: gilovi/python-ex3
    def __handle_server_request(self):
	"""
	handles all massages got from server
	"""

        num, msg = Protocol.recv_all(self.socket_to_server)
        if num == Protocol.NetworkErrorCodes.FAILURE:
            sys.stderr.write(msg)
            self.close_client(1)

		# the connection disconnected
        if num == Protocol.NetworkErrorCodes.DISCONNECTED:
            self.bye('disconnected')

        if "start" in msg:
            self.__start_game(msg)


        elif msg:
            (who, what, data) = msg.split(':')

			# the othe side left
            if data == 'EXIT' :
                self.bye('other '+ data)

			# a respond to my former attack or attacking me.
            if who == 'client':
                result = {'attacking': self.defend,
                          'defending': self.update_result,
                          }.get(what)(data.upper())

				# result is the outcome from the other's attack.
				#(if he didnt attacked in this message will be none)
                if result:

                    self.send_defend(result)
                    self.my_turn = True
                    print self.opponent_name + ' plays: ' + data
                    self.print_board()

					# iv'e lost ):
                    if result == 'LOST':
                        self.bye(result)
                    else:
                        print "It's your turn..."
                    
                else:
                    self.print_board()
            # the other tells us he lost the game... I guess we won.       
            if data ==  'LOST':
                self.bye('other '+ data)
                self.print_board()
예제 #4
0
파일: Client.py 프로젝트: orensam/pythonw
    def rcv_from_server(self):

        err_num, msg = Protocol.recv_all(self.socket_to_server)

        if err_num == Protocol.NetworkErrorCodes.FAILURE:
            sys.stderr.write(msg)
            self.close_client(EXIT_ERROR)

        elif err_num == Protocol.NetworkErrorCodes.DISCONNECTED:
            print "Server has closed connection."
            self.close_client(EXIT_OK)

        else:
            return msg
예제 #5
0
파일: Client.py 프로젝트: gilovi/python-ex3
    def connect_to_server(self):

        # Create a TCP/IP socket_to_server
        try:
            self.socket_to_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error as msg:

            self.socket_to_server = None
            sys.stderr.write(repr(msg) + '\n')
            exit(EXIT_ERROR)

        server_address = (self.server_name, int(self.server_port))
        try:
            self.socket_to_server.connect(server_address)
            self.all_sockets.append(
                self.socket_to_server)  # this will allow us to use Select System-call

        except socket.error as msg:
            self.socket_to_server.close()
            self.socket_to_server = None
            sys.stderr.write(repr(msg) + '\n')
            exit(EXIT_ERROR)

        # we wait to get ok from server to know we can send our name
        num, msg = Protocol.recv_all(self.socket_to_server)
        if num == Protocol.NetworkErrorCodes.FAILURE:
            sys.stderr.write(msg)
            self.close_client(1)

        if num == Protocol.NetworkErrorCodes.DISCONNECTED:
            self.bye('disconnected')

        # send our name to server
        eNum, eMsg = Protocol.send_all(self.socket_to_server, sys.argv[3])
        if eNum:
            sys.stderr.write(eMsg)
            self.close_client(1)

        print "*** Connected to server on %s ***" % server_address[0]
        print
        print "Waiting for an opponent..."
        print
예제 #6
0
파일: Server.py 프로젝트: orensam/pythonw
    def __handle_existing_connections(self):

        cur_con = self.players_sockets[self.turn]
        other_con = self.players_sockets[1 - self.turn]

        num, msg = Protocol.recv_all(cur_con)

        if num == Protocol.NetworkErrorCodes.FAILURE:
            sys.stderr.write(msg)
            self.shut_down_server(EXIT_ERROR)

        if num == Protocol.NetworkErrorCodes.DISCONNECTED:
            Protocol.send_all(other_con, Client.OP_DISC_PREFIX)
            self.shut_down_server(EXIT_OK)

        elif msg.startswith(Client.QUIT_PREFIX):
            Protocol.send_all(other_con, Client.OP_DISC_PREFIX)
            self.shut_down_server(EXIT_OK)

        else:
            Protocol.send_all(self.players_sockets[1 - self.turn], msg)
예제 #7
0
    def __handle_server_request(self):
        num, msg = Protocol.recv_all(self.socket_to_server)
        if num == Protocol.NetworkErrorCodes.FAILURE:
            sys.stderr.write(msg)
            self.close_client()

        if num == Protocol.NetworkErrorCodes.DISCONNECTED:
            print "Server has closed connection."
            self.close_client()
            
        if "EXIT" in msg:
            print "Your opponent has disconnected. You win!"
            self.close_client()

        if "start" in msg:
            self.__start_game(msg)
        
        else:
            msg_type = Client.server_msg_type(msg)  # Analyzing massage type
            #print "msg = " + msg				# ----------blllaaaaaaaaaaaaaaaaaaaaaa - ERASE
            if msg_type == OPPONENT_ANSWER:
                win = self.player.update_opponent_board(msg, self.latest_move[0], self.latest_move[1])
                self.print_board()
                if win:
                    #game ended - send massage
                    print "You won!"
                    self.close_client()
               
            else:	# A move was sent from the other client
                result = self.player.check_move(msg)
                Protocol.send_all(self.socket_to_server, result)    # Sends the response to the server
                print self.opponent_name + " plays: " + msg
                self.print_board()
                if "ALL" in result:
                    print "You lost :("
                    self.close_client()
                else:
                    print "It's your turn..."