Example #1
0
 protected void deactivate(ComponentContext componentContext) {
   // close the application client.
   // this will unsubscribe all open subscriptions
   s_logger.info("Releasing CloudApplicationClient for {}...", m_applicationId);
   if (m_cloudClient != null) {
     m_cloudClient.release();
   }
   m_cloudService = null;
 }
Example #2
0
  protected void activate(ComponentContext componentContext) {
    // get the mqtt client for this application
    try {

      s_logger.info("Getting CloudApplicationClient for {}...", m_applicationId);
      m_cloudClient = m_cloudService.newCloudClient(m_applicationId);
      m_cloudClient.addCloudClientListener(this);

      // Don't subscribe because these are handled by the default subscriptions and we don't want to
      // get messages twice
      m_ctx = componentContext;
    } catch (KuraException e) {
      s_logger.error("Cannot activate", e);
      throw new ComponentException(e);
    }
  }
Example #3
0
  @Override
  public Void call() throws Exception {
    s_logger.debug("Control Arrived on topic: {}", m_appTopic);

    // Prepare the default response
    KuraRequestPayload reqPayload = KuraRequestPayload.buildFromKuraPayload(m_msg);
    KuraResponsePayload respPayload = new KuraResponsePayload(KuraResponsePayload.RESPONSE_CODE_OK);

    try {

      CloudletTopic reqTopic = CloudletTopic.parseAppTopic(m_appTopic);
      CloudletTopic.Method method = reqTopic.getMethod();
      switch (method) {
        case GET:
          s_logger.debug("Handling GET request topic: {}", m_appTopic);
          m_cloudApp.doGet(reqTopic, reqPayload, respPayload);
          break;

        case PUT:
          s_logger.debug("Handling PUT request topic: {}", m_appTopic);
          m_cloudApp.doPut(reqTopic, reqPayload, respPayload);
          break;

        case POST:
          s_logger.debug("Handling POST request topic: {}", m_appTopic);
          m_cloudApp.doPost(reqTopic, reqPayload, respPayload);
          break;

        case DEL:
          s_logger.debug("Handling DEL request topic: {}", m_appTopic);
          m_cloudApp.doDel(reqTopic, reqPayload, respPayload);
          break;

        case EXEC:
          s_logger.debug("Handling EXEC request topic: {}", m_appTopic);
          m_cloudApp.doExec(reqTopic, reqPayload, respPayload);
          break;

        default:
          s_logger.error("Bad request topic: {}", m_appTopic);
          respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_BAD_REQUEST);
          break;
      }
    } catch (IllegalArgumentException e) {
      s_logger.error("Bad request topic: {}", m_appTopic);
      respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_BAD_REQUEST);
    } catch (KuraException e) {
      s_logger.error("Error handling request topic: {}\n{}", m_appTopic, e);
      respPayload.setResponseCode(KuraResponsePayload.RESPONSE_CODE_ERROR);
      respPayload.setException(e);
    }

    try {

      CloudClient cloudClient = m_cloudApp.getCloudApplicationClient();
      respPayload.setTimestamp(new Date());

      StringBuilder sb = new StringBuilder("REPLY").append("/").append(reqPayload.getRequestId());

      String requesterClientId = reqPayload.getRequesterClientId();

      s_logger.debug("Publishing response topic: {}", sb.toString());
      cloudClient.controlPublish(
          requesterClientId,
          sb.toString(),
          respPayload,
          Cloudlet.DFLT_PUB_QOS,
          Cloudlet.DFLT_RETAIN,
          Cloudlet.DFLT_PRIORITY);
    } catch (KuraException e) {
      s_logger.error("Error publishing response for topic: {}\n{}", m_appTopic, e);
    }

    return null;
  }