private JsonObject calculateSummary(
      JsonArray regIds, HashMap<String, JsonObject> response, long multicastId) {
    int success = 0;
    int failure = 0;
    int canonicalIds = 0;
    JsonArray deliveries = new JsonArray();
    for (Object regId : regIds) {
      JsonObject result = response.get(regId);
      if (!voidNull(result.getString("message_id")).isEmpty()) {
        success++;
        if (!voidNull(result.getString("registration_id")).isEmpty()) {
          canonicalIds++;
        }
      } else {
        failure++;
      }
      // add results, in the same order as the input
      deliveries.add(result);
    }
    // build a new object with the overall result
    JsonObject reply = new JsonObject();
    reply.putNumber("multicast_id", multicastId);
    reply.putNumber("success", success);
    reply.putNumber("failure", failure);
    reply.putNumber("canonical_ids", canonicalIds);
    reply.putArray("results", deliveries);

    return reply;
  }
 private JsonObject setDefaults(JsonObject config) {
   config = config.copy();
   // Set the defaults
   if (config.getNumber("session_timeout") == null) {
     config.putNumber("session_timeout", 5l * 1000);
   }
   if (config.getBoolean("insert_JSESSIONID") == null) {
     config.putBoolean("insert_JSESSIONID", true);
   }
   if (config.getNumber("heartbeat_period") == null) {
     config.putNumber("heartbeat_period", 25l * 1000);
   }
   if (config.getNumber("max_bytes_streaming") == null) {
     config.putNumber("max_bytes_streaming", 128 * 1024);
   }
   if (config.getString("prefix") == null) {
     config.putString("prefix", "/");
   }
   if (config.getString("library_url") == null) {
     config.putString("library_url", "http://cdn.sockjs.org/sockjs-0.2.1.min.js");
   }
   if (config.getArray("disabled_transports") == null) {
     config.putArray("disabled_transports", new JsonArray());
   }
   return config;
 }
  @Override
  public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    address = "vertx.memcached";

    JsonObject config = new JsonObject();
    config.putString("address", "vertx.memcached");
    config.putString("address", address);
    config.putString("memcached.servers", "localhost:11211");
    config.putNumber("memcached.timeout.ms", 1000);
    config.putNumber("memcached.tasks.check.ms", 10);
    config.putNumber("memcached.connections", 2);

    container.deployVerticle(
        "net.atarno.vertx.memcached.client.MemClient",
        config,
        1,
        new Handler<String>() {
          @Override
          public void handle(String s) {
            HashMap<String, Object> cacheCall = new HashMap<String, Object>();

            cacheCall.put("shouldReply", true);
            cacheCall.put("command", "set");
            cacheCall.put("key", "CCC");
            cacheCall.put("value", 100);
            /*
            build any other command here
             */
            act(cacheCall);
          }
        });
  }
  /**
   * Notify SXA Plugin-CC that a new CPE has been discovered.
   *
   * @param cpe
   */
  public static void sendCpeDiscoveryToPluginCc(final Cpe cpe) {
    /** Build request payload */
    JsonObject payload = new JsonObject();
    payload.putString("id", cpe.getCpeKey());
    payload.putString("ip", cpe.deviceId.ipAddress == null ? "" : cpe.deviceId.ipAddress);
    payload.putString("mac", cpe.deviceId.macAddress == null ? "" : cpe.deviceId.macAddress);
    payload.putString("ser-no", cpe.deviceId.sn);
    payload.putString("model", cpe.deviceId.modelName == null ? "" : cpe.deviceId.modelName);
    payload.putString("mfr", cpe.deviceId.manufacturer);
    payload.putString("sw-ver", cpe.deviceId.swVersion == null ? "" : cpe.deviceId.swVersion);
    payload.putString(
        "reg-id", cpe.deviceId.registrationId == null ? "" : cpe.deviceId.registrationId);
    payload.putString(
        "prod-class", cpe.deviceId.productClass == null ? "" : cpe.deviceId.productClass);
    payload.putString(
        "prov-code", cpe.getParamValue("InternetGatewayDevice.DeviceInfo.ProvisioningCode"));
    payload.putString(
        "persistent-data", cpe.getParamValue("InternetGatewayDevice.DeviceConfig.PersistentData"));
    payload.putNumber("last-comm-time", cpe.informTime / 1000);

    // Send it
    sendRequest(
        CWMP_PLUGIN_CC_MODULE_NAME,
        HttpMethod.POST,
        CPE_DISCOVERY_URL_PATH,
        cpe.deviceId.orgId, // use "orgId" as username
        PASSWORD,
        payload.encode(),
        new Handler<HttpClientResponse>() {
          @Override
          public void handle(HttpClientResponse response) {
            /** Simply log the response */
            if (response.statusCode() == HttpResponseStatus.OK.code()
                || response.statusCode() == HttpResponseStatus.NO_CONTENT.code()) {
              log.info(
                  cpe.getCpeKey()
                      + ": Successfully sent CPE discovery notification to SXA Plugin-CC.");
            } else {
              log.error(
                  cpe.getCpeKey()
                      + ": Failed to send CPE discovery notification to SXA Plugin-CC!"
                      + " HTTP Status "
                      + response.statusCode()
                      + " "
                      + response.statusMessage());
            }
          }
        },
        new Handler<Throwable>() {
          @Override
          public void handle(Throwable exception) {
            log.error(
                cpe.getCpeKey()
                    + ": Failed to send CPE discovery notification to SXA Plugin-CC due to"
                    + " exception "
                    + exception.getMessage()
                    + "!");
          }
        });
  }
  private void processSave(final Message<JsonObject> message) {
    System.out.println(message.body);

    JsonObject replyJson = new JsonObject();
    replyJson.putString("reply", "Saved data");
    replyJson.putNumber("id", System.currentTimeMillis());

    message.reply(replyJson);
  }
Exemple #6
0
 public void testEchoJson() {
   JsonObject obj = new JsonObject();
   obj.putString("foo", "bar");
   obj.putNumber("num", 12124);
   obj.putBoolean("x", true);
   obj.putBoolean("y", false);
   Handler<Message<JsonObject>> handler = echoHandler(obj);
   eb.send(ECHO_ADDRESS, obj, handler);
 }
  private static void writeHeaderFields(JsonObject result) {
    JsonObject train = findFirstTrain(result);

    if (train == null) {
      return;
    }

    result.putString("StopAreaName", train.getString("StopAreaName"));
    result.putNumber("SiteId", train.getInteger("SiteId"));
  }
Exemple #8
0
 public static void list(
     JsonArray attributes, Integer skip, Integer limit, TransactionHelper transactionHelper) {
   StringBuilder query = new StringBuilder("MATCH (s:Structure) ");
   JsonObject params = new JsonObject();
   if (attributes != null && attributes.size() > 0) {
     query.append("RETURN DISTINCT");
     for (Object attribute : attributes) {
       query.append(" s.").append(attribute).append(" as ").append(attribute).append(",");
     }
     query.deleteCharAt(query.length() - 1);
     query.append(" ");
   } else {
     query.append("RETURN DISTINCT s ");
   }
   if (skip != null && limit != null) {
     query.append("ORDER BY externalId ASC " + "SKIP {skip} " + "LIMIT {limit} ");
     params.putNumber("skip", skip);
     params.putNumber("limit", limit);
   }
   transactionHelper.add(query.toString(), params);
 }
Exemple #9
0
  public static void logSANodeValue(String nodeID, String nodeName, String nodeValue) {
    JsonObject jsonObj = new JsonObject();
    Long timestamp = System.currentTimeMillis();

    jsonObj.putNumber("time", timestamp);
    jsonObj.putString("type", "SANode");
    jsonObj.putString("nodeID", nodeID);
    jsonObj.putString("nodeName", nodeName);
    jsonObj.putObject("data", new JsonObject(nodeValue));
    jsonObj.putArray("users", AccountManager.getRegisteredUserIDList(nodeID));

    collLog.insert(jsonToDBObject(jsonObj));
  }
  public void act(HashMap<String, Object> cmd) {
    if (cmd == null) return;

    JsonObject notif = new JsonObject();

    for (String key : cmd.keySet()) {
      Object value = cmd.get(key);

      if (value != null) {
        if (value instanceof byte[]) notif.putBinary(key, (byte[]) value);
        else if (value instanceof Boolean) notif.putBoolean(key, (Boolean) value);
        else if (value instanceof Number) notif.putNumber(key, (Number) value);
        else if (value instanceof String) notif.putString(key, (String) value);
        else if (value instanceof JsonArray) notif.putArray(key, (JsonArray) value);
      }
    }
    System.out.println("sent: \n" + notif.encode());
    push(notif);
  }
Exemple #11
0
  public static void logControllerCommand(
      String request, int statusCode, String respond, String userID) {
    JsonObject jsonObj = new JsonObject();
    Long timestamp = System.currentTimeMillis();

    jsonObj.putNumber("time", timestamp);
    jsonObj.putString("type", "Controller");
    if (userID != null) jsonObj.putString("userID", userID);
    jsonObj.putObject("request", new JsonObject(request));

    JsonObject resJsonObj = new JsonObject();
    resJsonObj.putString("code", "" + statusCode);
    if (respond != null && respond.length() != 0) {
      if (respond.length() > 1024) resJsonObj.putString("data", "deprecated because too long");
      else resJsonObj.putObject("data", new JsonObject(respond));
    }

    jsonObj.putObject("resonse", resJsonObj);
    collLog.insert(jsonToDBObject(jsonObj));
  }