Esempio n. 1
0
 def delegate(self, params, t_out=None):
     """Delegate the command to the RCM platform node instance specified in the request. Return an RCMPMessage."""
     result = RCMPMessage()
     # the timeout provide the capability to decrease or increase the time available to perform
     # the task and can be useful let it change in base of the required operation
     if t_out:
         c = RCMPInterCommunicationClient(params[self.I_ADDRESS_KEY],
                                          t_out=t_out)
     else:
         c = RCMPInterCommunicationClient(params[self.I_ADDRESS_KEY])
     try:
         c.connect()
         # propagate the request as it is
         c.send(RCMPMessage(params).get_txt())
         # take the result of the operation
         res = c.receive_all_so_far()
         if res:
             result.set_content(res)
         else:
             reason = "No response received"
             result.create_error_response(reason)
     except Exception as e:
         reason = "Error delegating the request to the RCM platform node at %s: %s" % (
             params[self.I_ADDRESS_KEY], e)
         self._logger.error(reason)
         result.create_error_response(reason)
     finally:
         try:
             c.close()
         except Exception as e:
             # this exception doesn't change the result but we want to trace in the log
             self._logger.warning(
                 "Error closing the delegating client: %s" % e)
     return result
Esempio n. 2
0
 def delegate(self, params, t_out=None):
     """Delegate the command to the RCM platform node instance specified in the request. Return an RCMPMessage."""
     result = RCMPMessage()
     # the timeout provide the capability to decrease or increase the time available to perform
     # the task and can be useful let it change in base of the required operation
     if t_out:
         c = RCMPInterCommunicationClient(params[self.I_ADDRESS_KEY], t_out=t_out)
     else:
         c = RCMPInterCommunicationClient(params[self.I_ADDRESS_KEY])
     try:
         c.connect()
         # propagate the request as it is
         c.send(RCMPMessage(params).get_txt())
         # take the result of the operation
         res = c.receive_all_so_far()
         if res:
             result.set_content(res)
         else:
             reason = "No response received"
             result.create_error_response(reason)
     except Exception as e:
         reason = "Error delegating the request to the RCM platform node at %s: %s" % (params[self.I_ADDRESS_KEY], e)
         self._logger.error(reason)
         result.create_error_response(reason)
     finally:
         try:
             c.close()
         except Exception as e:
             # this exception doesn't change the result but we want to trace in the log
             self._logger.warning("Error closing the delegating client: %s" % e)
     return result
Esempio n. 3
0
 def notify(self, params):
     """Notify an event to the RCM platform node instance specified in the request. Return an RCMPMessage."""
     result = RCMPMessage()
     self._logger.debug("event notify with params: %s" % params)
     # the events are asynchronous so we don't need a timeout to wait the response
     c = RCMPInterCommunicationClient(params[self.I_ADDRESS_KEY], t_out=2.0)
     try:
         c.connect()
         # propagate the request as it is
         c.send(RCMPMessage(params).get_txt())
         # take the result of the operation
         # res = c.receive()
         res = c.receive_all_so_far()
         if res:
             result.set_content(res)
         else:
             reason = "No response received"
             result.create_error_response(reason)
     except Exception as e:
         reason = "Error notifying the event to the RCM platform node at %s: %s" % (params[self.I_ADDRESS_KEY], e)
         self._logger.error(reason)
         result.create_error_response(reason)
     finally:
         try:
             c.close()
         except Exception as e:
             # this exception doesn't change the result but we want to trace in the log
             self._logger.warning("Error closing the notifying client: %s" % e)
     return result
Esempio n. 4
0
 def render_POST(self, request):
     """Dispatch the post request to the internal communication server and return the result."""
     ret = RCMPMessage()
     try:
         # created using default parameters means calling to a local server
         icc = RCMPInterCommunicationClient()
         msg = None
         try:
             icc.connect()
             ext_data = self.get_ext_data(request)
             msg = self.generate_internal_msg(ext_data)
             icc.send(msg.get_txt())
             res = icc.receive_all_so_far()
             if res:
                 ret.set_content(res)
             else:
                 reason = "No response received"
                 request.setResponseCode(http.INTERNAL_SERVER_ERROR)
                 ret.create_error_response(reason)
         except SyntaxError as se:
             reason = "Syntax error in the request: %s" % se
             self.logger.exception(reason)
             request.setResponseCode(http.BAD_REQUEST)
             ret.create_error_response(reason)
         except Exception as e:
             reason = "Unable to accomplish the request %s: %s" % (msg, e)
             self.logger.exception(reason)
             request.setResponseCode(http.INTERNAL_SERVER_ERROR)
             ret.create_error_response(reason)
         finally:
             icc.close()
     except Exception as e:
         reason = "Internal communication server error: %s" % e
         self.logger.exception(reason)
         request.setResponseCode(http.INTERNAL_SERVER_ERROR)
         ret.create_error_response(reason)
     request.setHeader("Content-Type", "application/json")
     request.setHeader("Access-Control-Allow-Origin", "*")
     request.setHeader("Access-Control-Allow-Headers", "Content-Type")
     request.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
     return ret.get_txt()
Esempio n. 5
0
 def render_POST(self, request):
     """Dispatch the post request to the internal communication server and return the result."""
     ret = RCMPMessage()
     try:
         # created using default parameters means calling to a local server
         icc = RCMPInterCommunicationClient()
         msg = None
         try:
             icc.connect()
             ext_data = self.get_ext_data(request)
             msg = self.generate_internal_msg(ext_data)
             icc.send(msg.get_txt())
             res = icc.receive_all_so_far()
             if res:
                 ret.set_content(res)
             else:
                 reason = "No response received"
                 request.setResponseCode(http.INTERNAL_SERVER_ERROR)
                 ret.create_error_response(reason)
         except SyntaxError as se:
             reason = "Syntax error in the request: %s" % se
             self.logger.exception(reason)
             request.setResponseCode(http.BAD_REQUEST)
             ret.create_error_response(reason)
         except Exception as e:
             reason = "Unable to accomplish the request %s: %s" % (msg, e)
             self.logger.exception(reason)
             request.setResponseCode(http.INTERNAL_SERVER_ERROR)
             ret.create_error_response(reason)
         finally:
             icc.close()
     except Exception as e:
         reason = "Internal communication server error: %s" % e
         self.logger.exception(reason)
         request.setResponseCode(http.INTERNAL_SERVER_ERROR)
         ret.create_error_response(reason)
     request.setHeader("Content-Type", "application/json")
     request.setHeader("Access-Control-Allow-Origin", "*")
     request.setHeader("Access-Control-Allow-Headers", "Content-Type")
     request.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
     return ret.get_txt()