Ejemplo n.º 1
0
 def close(self):
     """
     Sends a shutdown signal to the unity environment, and closes the socket connection.
     """
     if self._socket is not None and self._conn is not None:
         message_input = UnityMessage()
         message_input.header.status = 400
         self._communicator_send(message_input.SerializeToString())
     if self._socket is not None:
         self._socket.close()
         self._socket = None
     if self._socket is not None:
         self._conn.close()
         self._conn = None
Ejemplo n.º 2
0
 def initialize(self, inputs: UnityInput) -> UnityOutput:
     try:
         # Establish communication grpc
         self.server = grpc.server(ThreadPoolExecutor(max_workers=10))
         self.unity_to_external = UnityToExternalServicerImplementation()
         add_UnityToExternalServicer_to_server(self.unity_to_external,
                                               self.server)
         self.server.add_insecure_port('[::]:' + str(self.port))
         self.server.start()
     except:
         raise UnityTimeOutException(
             "Couldn't start socket communication because worker number {} is still in use. "
             "You may need to manually close a previously opened environment "
             "or use a different worker number.".format(str(
                 self.worker_id)))
     if not self.unity_to_external.parent_conn.poll(30):
         raise UnityTimeOutException(
             "The Unity environment took too long to respond. Make sure that :\n"
             "\t The environment does not need user interaction to launch\n"
             "\t The Academy and the External Brain(s) are attached to objects in the Scene\n"
             "\t The environment and the Python interface have compatible versions."
         )
     aca_param = self.unity_to_external.parent_conn.recv().unity_output
     self.is_open = True
     message = UnityMessage()
     message.header.status = 200
     message.unity_input.CopyFrom(inputs)
     self.unity_to_external.parent_conn.send(message)
     self.unity_to_external.parent_conn.recv()
     return aca_param
Ejemplo n.º 3
0
 def exchange(self, inputs: UnityInput) -> UnityOutput:
     message = UnityMessage()
     message.header.status = 200
     message.unity_input.CopyFrom(inputs)
     self.unity_to_external.parent_conn.send(message)
     output = self.unity_to_external.parent_conn.recv()
     if output.header.status != 200:
         return None
     return output.unity_output
Ejemplo n.º 4
0
 def close(self):
     """
     Sends a shutdown signal to the unity environment, and closes the grpc connection.
     """
     if self.is_open:
         message_input = UnityMessage()
         message_input.header.status = 400
         self.unity_to_external.parent_conn.send(message_input)
         self.unity_to_external.parent_conn.close()
         self.server.stop(False)
         self.is_open = False
Ejemplo n.º 5
0
 def initialize(self, inputs: UnityInput) -> UnityOutput:
     try:
         # Establish communication socket
         self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         self._socket.bind(("localhost", self.port))
     except:
         raise UnityTimeOutException(
             "Couldn't start socket communication because worker number {} is still in use. "
             "You may need to manually close a previously opened environment "
             "or use a different worker number.".format(str(
                 self.worker_id)))
     try:
         self._socket.settimeout(30)
         self._socket.listen(1)
         self._conn, _ = self._socket.accept()
         self._conn.settimeout(30)
     except:
         raise UnityTimeOutException(
             "The Unity environment took too long to respond. Make sure that :\n"
             "\t The environment does not need user interaction to launch\n"
             "\t The Academy and the External Brain(s) are attached to objects in the Scene\n"
             "\t The environment and the Python interface have compatible versions."
         )
     message = UnityMessage()
     message.header.status = 200
     message.unity_input.CopyFrom(inputs)
     self._communicator_send(message.SerializeToString())
     initialization_output = UnityMessage()
     initialization_output.ParseFromString(self._communicator_receive())
     return initialization_output.unity_output
Ejemplo n.º 6
0
 def exchange(self, inputs: UnityInput) -> UnityOutput:
     message = UnityMessage()
     message.header.status = 200
     message.unity_input.CopyFrom(inputs)
     self._communicator_send(message.SerializeToString())
     outputs = UnityMessage()
     outputs.ParseFromString(self._communicator_receive())
     if outputs.header.status != 200:
         return None
     return outputs.unity_output