Beispiel #1
0
def handle_client_connection(client_socket, create_env, create_action_space):
    """handle a client connection """
    msg = comms.recv_message(client_socket)
    config = pickle.loads(msg)
    print('Received env config {}'.format(repr(config)))

    if isinstance(config, gym.Env):
        # Client sent an actual environment - use it.
        env = config
    if isinstance(config, str):
        # Use string with gym env registry.
        env = gym.make(config)
    else:
        # Config is an object we pass to our server env factory method.
        env = create_env(config)

    print("created env " + repr(env))

    a_s = create_action_space(env, config)
    o_s = env.observation_space
    reply = cloudpickle.dumps((a_s, o_s))

    comms.send_message(client_socket, reply)

    while True:
        msg = comms.recv_message(client_socket)
        if msg is None:
            print("eof")
            break
        request = pickle.loads(msg)
        # print("Proxy env request " + repr(request))
        cmd, arg = request
        if cmd == 'r':
            obs = env.reset()
            reply = cloudpickle.dumps(obs)
            comms.send_message(client_socket, reply)
        elif cmd == 's':
            result = env.step(arg)
            reply = cloudpickle.dumps(result)
            comms.send_message(client_socket, reply)
        elif cmd == 'se':
            result = env.seed(arg)
            reply = cloudpickle.dumps(result)
            comms.send_message(client_socket, reply)
        elif cmd == 're':
            result = env.render(arg)
            reply = cloudpickle.dumps(result)
            comms.send_message(client_socket, reply)
        else:
            print('Skip unknown proxy env command!')
            break
    env.close()
    client_socket.close()
Beispiel #2
0
 def seed(self, seed=None):
     """proxy seed"""
     msg = cloudpickle.dumps(('se', seed))
     comms.send_message(self.client_sock, msg)
     reply = comms.recv_message(self.client_sock)
     seed_response = pickle.loads(reply)
     return seed_response
Beispiel #3
0
 def render(self, mode='human'):
     """proxy render"""
     msg = cloudpickle.dumps(('re', mode))
     comms.send_message(self.client_sock, msg)
     reply = comms.recv_message(self.client_sock)
     render_response = pickle.loads(reply)
     return render_response
Beispiel #4
0
 def reset(self):
     """proxy reset"""
     msg = cloudpickle.dumps(('r', None))
     comms.send_message(self.client_sock, msg)
     reply = comms.recv_message(self.client_sock)
     reset_response = pickle.loads(reply)
     return reset_response
Beispiel #5
0
 def step(self, action):
     """proxy step"""
     msg = cloudpickle.dumps(('s', action))
     comms.send_message(self.client_sock, msg)
     reply = comms.recv_message(self.client_sock)
     step_response = pickle.loads(reply)
     return step_response
Beispiel #6
0
    def __init__(self, host, port, config):
        """create a proxy env"""
        self.host = host
        self.port = port

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(TIMEOUT)
        sock.connect((self.host, self.port))
        self.client_sock = sock

        msg = cloudpickle.dumps(config)
        comms.send_message(self.client_sock, msg)

        reply = comms.recv_message(self.client_sock)
        r = pickle.loads(reply)
        action_space, observation_space = r

        print("created env with action space {} observation space {}".format(
            repr(action_space), repr(observation_space)))

        self.action_space = action_space
        self.observation_space = observation_space
        self.metadata = {'render.modes': []}
        self.reward_range = (-float('inf'), float('inf'))