Skip to content
Snippets Groups Projects
Commit 427f11db authored by spmohanty's avatar spmohanty
Browse files

Addresses #117 - Refactors command send and receive interface

parent 84e7660b
No related branches found
No related tags found
No related merge requests found
...@@ -203,18 +203,13 @@ class FlatlandRemoteEvaluationService: ...@@ -203,18 +203,13 @@ class FlatlandRemoteEvaluationService:
return command return command
def handle_ping(self, command): def send_response(self, _command_response, command, suppress_logs=False):
"""
Handles PING command from the client.
"""
_redis = self.get_redis_connection() _redis = self.get_redis_connection()
command_response_channel = command['response_channel'] command_response_channel = command['response_channel']
_command_response = {} if self.verbose and not suppress_logs:
_command_response['type'] = messages.FLATLAND_RL.PONG
_command_response['payload'] = {}
if self.verbose:
print("Responding with : ", _command_response) print("Responding with : ", _command_response)
_redis.rpush( _redis.rpush(
command_response_channel, command_response_channel,
msgpack.packb( msgpack.packb(
...@@ -222,6 +217,16 @@ class FlatlandRemoteEvaluationService: ...@@ -222,6 +217,16 @@ class FlatlandRemoteEvaluationService:
default=m.encode, default=m.encode,
use_bin_type=True) use_bin_type=True)
) )
def handle_ping(self, command):
"""
Handles PING command from the client.
"""
_command_response = {}
_command_response['type'] = messages.FLATLAND_RL.PONG
_command_response['payload'] = {}
self.send_response(_command_response, command)
def handle_env_create(self, command): def handle_env_create(self, command):
""" """
...@@ -230,9 +235,6 @@ class FlatlandRemoteEvaluationService: ...@@ -230,9 +235,6 @@ class FlatlandRemoteEvaluationService:
Add a high level summary of everything thats Add a high level summary of everything thats
hapenning here. hapenning here.
""" """
_redis = self.get_redis_connection()
command_response_channel = command['response_channel']
if self.simulation_count < len(self.env_file_paths): if self.simulation_count < len(self.env_file_paths):
""" """
There are still test envs left that are yet to be evaluated There are still test envs left that are yet to be evaluated
...@@ -276,15 +278,6 @@ class FlatlandRemoteEvaluationService: ...@@ -276,15 +278,6 @@ class FlatlandRemoteEvaluationService:
_command_response['payload'] = {} _command_response['payload'] = {}
_command_response['payload']['observation'] = _observation _command_response['payload']['observation'] = _observation
_command_response['payload']['env_file_path'] = test_env_file_path _command_response['payload']['env_file_path'] = test_env_file_path
if self.verbose:
print("Responding with : ", _command_response)
_redis.rpush(
command_response_channel,
msgpack.packb(
_command_response,
default=m.encode,
use_bin_type=True)
)
else: else:
""" """
All test env evaluations are complete All test env evaluations are complete
...@@ -294,15 +287,8 @@ class FlatlandRemoteEvaluationService: ...@@ -294,15 +287,8 @@ class FlatlandRemoteEvaluationService:
_command_response['payload'] = {} _command_response['payload'] = {}
_command_response['payload']['observation'] = False _command_response['payload']['observation'] = False
_command_response['payload']['env_file_path'] = False _command_response['payload']['env_file_path'] = False
if self.verbose:
print("Responding with : ", _command_response) self.send_response(_command_response, command)
_redis.rpush(
command_response_channel,
msgpack.packb(
_command_response,
default=m.encode,
use_bin_type=True)
)
def handle_env_step(self, command): def handle_env_step(self, command):
""" """
...@@ -311,8 +297,6 @@ class FlatlandRemoteEvaluationService: ...@@ -311,8 +297,6 @@ class FlatlandRemoteEvaluationService:
Add a high level summary of everything thats Add a high level summary of everything thats
hapenning here. hapenning here.
""" """
_redis = self.get_redis_connection()
command_response_channel = command['response_channel']
_payload = command['payload'] _payload = command['payload']
if not self.env: if not self.env:
...@@ -348,16 +332,7 @@ class FlatlandRemoteEvaluationService: ...@@ -348,16 +332,7 @@ class FlatlandRemoteEvaluationService:
_command_response['payload']['reward'] = all_rewards _command_response['payload']['reward'] = all_rewards
_command_response['payload']['done'] = done _command_response['payload']['done'] = done
_command_response['payload']['info'] = info _command_response['payload']['info'] = info
if self.verbose: self.send_response(_command_response, command)
# print("Responding with : ", _command_response)
print("Current Step : ", self.simulation_steps[-1])
_redis.rpush(
command_response_channel,
msgpack.packb(
_command_response,
default=m.encode,
use_bin_type=True)
)
def handle_env_submit(self, command): def handle_env_submit(self, command):
""" """
...@@ -366,8 +341,6 @@ class FlatlandRemoteEvaluationService: ...@@ -366,8 +341,6 @@ class FlatlandRemoteEvaluationService:
Add a high level summary of everything thats Add a high level summary of everything thats
hapenning here. hapenning here.
""" """
_redis = self.get_redis_connection()
command_response_channel = command['response_channel']
_payload = command['payload'] _payload = command['payload']
# Register simulation time of the last episode # Register simulation time of the last episode
...@@ -380,37 +353,28 @@ class FlatlandRemoteEvaluationService: ...@@ -380,37 +353,28 @@ class FlatlandRemoteEvaluationService:
""" """
) )
_response = {} _command_response = {}
_response['type'] = messages.FLATLAND_RL.ENV_SUBMIT_RESPONSE _command_response['type'] = messages.FLATLAND_RL.ENV_SUBMIT_RESPONSE
_payload = {} _payload = {}
_payload['mean_reward'] = np.mean(self.simulation_rewards) _payload['mean_reward'] = np.mean(self.simulation_rewards)
_payload['mean_percentage_complete'] = \ _payload['mean_percentage_complete'] = \
np.mean(self.simulation_percentage_complete) np.mean(self.simulation_percentage_complete)
_response['payload'] = _payload _command_response['payload'] = _payload
if self.verbose: self.send_response(_command_response, command)
print("Responding with : ", _response)
print("Registering Env Submit call")
_redis.rpush(
command_response_channel,
msgpack.packb(
_response,
default=m.encode,
use_bin_type=True)
)
def report_error(self, error_message, command_response_channel): def report_error(self, error_message, command_response_channel):
""" """
A helper function used to report error back to the client A helper function used to report error back to the client
""" """
_redis = self.get_redis_connection() _redis = self.get_redis_connection()
_response = {} _command_response = {}
_response['type'] = messages.FLATLAND_RL.ERROR _command_response['type'] = messages.FLATLAND_RL.ERROR
_response['payload'] = error_message _command_response['payload'] = error_message
_redis.rpush( _redis.rpush(
command_response_channel, command_response_channel,
msgpack.packb( msgpack.packb(
_response, _command_response,
default=m.encode, default=m.encode,
use_bin_type=True) use_bin_type=True)
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment