Ejemplo n.º 1
0
    def start_client(self):
        #print("starting client")
        #### USE FACTORY
        while True:
            try:
                resp = self.client_socket.recv(self.buffer)
                resp = resp.decode("UTF-8")
            except ConnectionResetError:
                self.client_socket.close()
                break
            #there is nothing coming from the server, so it has disconnected
            if not resp:
                #self.client_socket.shutdown(socket.SHUT_WR)
                self.client_socket.close()
                #print("Client closed")
                break
            elif resp:
                resp_json = readJSON(resp)
                output = self.parse_command(resp_json[0])
                print(output)
                if not output:
                    continue
                if output == "close":
                    self.client_socket.shutdown(1)
                    self.client_socket.close()
                    break

                output = '"' + output + '"'
                self.client_socket.send(output.encode('utf-8'))
Ejemplo n.º 2
0
    def parse_command(self):
        #figure out what to call on proxy and call it
        #Let's change this to a factory ---- $$$$
        commands = readJSON(sys.stdin.read())
        outputs = []
        while True:
            try:
                command = commands.pop(0)
            except IndexError:
                break

            if command[0] == "register":
                output = self.player.get_name(command)

            elif command[0] == "receive-stones":
                output = self.player.set_color(command)

            elif command[0] == "make-a-move":
                output = self.player.make_move(command)

            else:
                output = str.encode(CRAZY_GO)

            output = output.decode('UTF-8')
            if output.strip() == CRAZY_GO:
                outputs.append(output)
                break
            elif output == 'RECEIVE':
                continue
            elif output:
                outputs.append(output)

        self.close_connection()
        return outputs
Ejemplo n.º 3
0
 def load_config(self):
     config_file = open(GO_CONFIG_PATH, 'r')
     config_file = config_file.read()
     netData = readJSON(config_file)
     netData = netData[0]
     self.host = netData['IP']
     self.port = netData['port']
Ejemplo n.º 4
0
def load_config():
    config_file = open(GO_CONFIG_PATH, 'r')
    config_file = config_file.read()
    netData = readJSON(config_file)
    netData = netData[0]
    host = netData['IP']
    port = netData['port']
    default_player_path = netData['default-player']
    return host, port, default_player_path
Ejemplo n.º 5
0
 def start_client(self):
     #### USE FACTORY
     while True: 
         resp = self.client_socket.recv(self.buffer) 
         resp = resp.decode("UTF-8")
         if resp == "close":
             self.client_socket.shutdown(1)
             self.client_socket.close()
             #print("Client closed")
             break
         elif resp: 
             resp_json = readJSON(resp)
             output = self.parse_command(resp_json[0])
             self.client_socket.send(str.encode(output))
Ejemplo n.º 6
0
 def send(self, message):
     #print("sent message", message)
     try:
         message = json.dumps(message)
         self.conn.sendall(message.encode("UTF-8"))
         resp = self.conn.recv(4096).decode("UTF-8")
         resp = utilities.readJSON(resp)[0]
         print("received message", resp)
         if not resp:
             self.client_connected = False
             return False
         return resp
     except (BrokenPipeError, OSError):
         print("remote player not connected")
         #self.client_connected = False
         return False
Ejemplo n.º 7
0
 def start_client(self):
     #### USE FACTORY
     while True:
         resp = self.client_socket.recv(self.buffer)
         resp = resp.decode("UTF-8")
         #there is nothing coming from the server, so it has disconnected
         if not resp:
             #self.client_socket.shutdown(socket.SHUT_WR)
             self.client_socket.close()
             #print("Client closed")
             break
         elif resp:
             resp_json = readJSON(resp)
             output = self.parse_command(resp_json[0])
             #print(output)
             if output == "close":
                 self.client_socket.shutdown(1)
                 self.client_socket.close()
                 break
             self.client_socket.send(str.encode(output))