Exemplo n.º 1
0
static void clubby_proto_handle_frame(char *data, size_t len, void *context) {
  struct json_token *frame = parse_json2(data, len);

  if (frame == NULL) {
    LOG(LL_DEBUG, ("Error parsing clubby frame"));
    return;
  }

  struct json_token *tmp;

  tmp = find_json_token(frame, "resp");
  if (tmp != NULL) {
    clubby_proto_parse_resp(tmp, context);
  }

  tmp = find_json_token(frame, "cmds");
  if (tmp != NULL) {
    clubby_proto_parse_req(frame, tmp, context);
  }

  free(frame);
}
Exemplo n.º 2
0
static void clubby_proto_handle_frame(struct mg_str data) {
  struct json_token *frame = parse_json2(data.p, data.len);

  if (frame == NULL) {
    LOG(LL_DEBUG, ("Error parsing clubby frame"));
    return;
  }

  struct json_token *tmp;

  tmp = find_json_token(frame, "resp");
  if (tmp != NULL) {
    clubby_proto_parse_resp(tmp);
  }

  tmp = find_json_token(frame, "cmds");
  if (tmp != NULL) {
    clubby_proto_parse_req(frame, tmp);
  }

  free(frame);
}
Exemplo n.º 3
0
static void clubby_proto_handle_frame(char *data, size_t len, void *context) {
  struct clubby_event evt;
  struct json_token *frame = parse_json2(data, len);

  if (frame == NULL) {
    LOG(LL_DEBUG, ("Error parsing clubby frame"));
    return;
  }

  struct json_token *v_tok = find_json_token(frame, "v");
  if (v_tok == NULL || *v_tok->ptr != '2') {
    LOG(LL_ERROR, ("Only clubby v2 is supported (received: %.*s)",
                   v_tok ? 0 : v_tok->len, v_tok->ptr));
    goto clean;
  }

  memset(&evt, 0, sizeof(evt));
  evt.frame = frame;
  evt.context = context;

  struct json_token *id_tok = find_json_token(frame, "id");
  if (id_tok == NULL) {
    LOG(LL_ERROR, ("No id in frame"));
    goto clean;
  }

  evt.id = to64(id_tok->ptr);
  if (evt.id == 0) {
    LOG(LL_ERROR, ("Wrong id"));
    goto clean;
  }

  /* Allow empty dst */
  evt.dst = find_json_token(frame, "dst");

  evt.src = find_json_token(frame, "src");
  if (evt.src == NULL) {
    LOG(LL_ERROR, ("No src in frame"));
    goto clean;
  }

  struct json_token *method_tok = find_json_token(frame, "method");
  struct json_token *result_tok = find_json_token(frame, "result");
  struct json_token *error_tok = find_json_token(frame, "error");

  /*
   * if none of required token exist - this is positive response
   * if `method` and `error` (or `result`) are in the same
   * frame - this is an error
   */
  if (method_tok != NULL && (result_tok != NULL || error_tok != NULL)) {
    LOG(LL_ERROR, ("Malformed frame"));
    goto clean;
  }

  if (method_tok != NULL) {
    clubby_proto_parse_req(method_tok, frame, &evt);
  } else {
    clubby_proto_parse_resp(result_tok, error_tok, &evt);
  }

  s_clubby_cb(&evt);

clean:
  free(frame);
}