void
mock_server_replies (request_t *request,
                     mongoc_reply_flags_t flags,
                     int64_t cursor_id,
                     int32_t starting_from,
                     int32_t number_returned,
                     const char *docs_json)
{
   char *quotes_replaced;
   bson_t doc;
   bson_error_t error;
   bool r;

   assert (request);

   if (docs_json) {
      quotes_replaced = single_quotes_to_double (docs_json);
      r = bson_init_from_json (&doc, quotes_replaced, -1, &error);
      if (!r) {
         MONGOC_WARNING ("%s", error.message);
         return;
      }

      bson_free (quotes_replaced);
   } else {
      r = bson_init_from_json (&doc, "{}", -1, &error);
   }

   mock_server_reply_multi (request,
                            flags,
                            &doc,
                            1,
                            cursor_id);
   bson_destroy (&doc);
}
Exemple #2
0
static void dump_message(ullong number, str_t type, msg_content_array_t* content)
{
    bson_t document;
    bson_error_t error;
    time_t t;
    mongoc_collection_t* collection = mongoc_database_get_collection(robot.mongoc_database, "message");
    char* json = msg_content_array_to_json_object_string(content, "content");

    time(&t);
    if (!bson_init_from_json(&document, json, strlen(json), &error)) MONGOC_WARNING("%s\n", error.message);
    BSON_APPEND_INT64(&document, "from", number);
    BSON_APPEND_UTF8(&document, "type", type.ptr);
    BSON_APPEND_TIME_T(&document, "time", t);
    if (!mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &document, NULL, &error)) MONGOC_WARNING("%s\n", error.message);
    bson_destroy(&document);
    free(json);
}
static bool
auto_ismaster (request_t *request,
               void *data)
{
   const char *response_json = (const char *) data;
   char *quotes_replaced;
   bson_t response;
   bson_error_t error;

   if (!request->is_command || strcasecmp (request->command_name, "ismaster")) {
      return false;
   }

   quotes_replaced = single_quotes_to_double (response_json);

   if (!bson_init_from_json (&response, quotes_replaced, -1, &error)) {
      fprintf (stderr, "%s\n", error.message);
      fflush (stderr);
      abort ();
   }

   if (mock_server_get_rand_delay (request->server)) {
      _mongoc_usleep ((int64_t) (rand () % 10) * 1000);
   }

   if (mock_server_get_verbose (request->server)) {
      printf ("%5.2f  %hu <- %hu \t%s\n",
              mock_server_get_uptime_sec (request->server),
              request->client_port,
              mock_server_get_port (request->server),
              quotes_replaced);
      fflush (stdout);
   }

   mock_server_reply_simple (request->server,
                             request->client,
                             &request->request_rpc,
                             MONGOC_REPLY_NONE,
                             &response,
                             0);

   bson_destroy (&response);
   bson_free (quotes_replaced);
   request_destroy (request);
   return true;
}
bool
bson_init_from_json_file (bson_t     *bson,
                          const char *file_name)
{
    char *json, *json_wrapped;
    size_t len;
    bson_error_t error;
    bool ret;

    json = file_to_s (file_name);
    len = strlen (json) + 16;
    json_wrapped = malloc (len);
    snprintf (json_wrapped, len, "{\n\"json\": %s\n}\n", json);
    ret = bson_init_from_json (bson, json_wrapped, len, &error) || WARN_ERROR;
    free (json);
    free (json_wrapped);
    return ret;
}
void
mock_server_replies (request_t *request,
                     uint32_t flags,
                     int64_t cursor_id,
                     int32_t starting_from,
                     int32_t number_returned,
                     const char *docs_json)
{
   char *quotes_replaced = single_quotes_to_double (docs_json);
   bson_t doc;
   bson_error_t error;
   bool r;

   assert (request);

   r = bson_init_from_json (&doc, quotes_replaced, -1, &error);
   if (!r) {
      MONGOC_WARNING ("%s", error.message);
      return;
   }

   if (mock_server_get_verbose (request->server)) {
      printf ("%5.2f  %hu <- %hu \t%s\n",
              mock_server_get_uptime_sec (request->server),
              request->client_port,
              mock_server_get_port (request->server),
              quotes_replaced);
      fflush (stdout);
   }

   mock_server_reply_simple (request->server,
                             request->client,
                             &request->request_rpc,
                             MONGOC_REPLY_NONE,
                             &doc,
                             cursor_id);

   bson_destroy (&doc);
   bson_free (quotes_replaced);
}
Exemple #6
0
static void route_result(cJSON* result)
{
    cJSON* cjson_current;
    size_t i;
    for (cjson_current = result->child; cjson_current; cjson_current = cjson_current->next)
    {
        if (strcmp(cJSON_GetObjectItem(cjson_current, "poll_type")->valuestring, "message") == 0)
        {
            cJSON* cjson_value = cJSON_GetObjectItem(cjson_current, "value");
            ullong from_uin = cJSON_GetObjectItem(cjson_value, "from_uin")->valuedouble;
            ullong number = get_friend_number(from_uin);
            msg_content_array_t content = fetch_content(cJSON_GetObjectItem(cjson_value, "content"));
            dump_message(number, str_from("friend_message"), &content);
#ifdef _DEBUG
            {
                char* str = msg_content_array_to_json_object_string(&content, "content");
                fprintf(stdout, "Received message from: %llu\nContent: %s\n", number, str);
                fflush(stdout);
                free(str);
            }
#endif

            for (i = 0; i < robot.received_message_funcs_count; ++i) robot.received_message_funcs[i](from_uin, number, &content);
            msg_content_array_free(&content);
        }
        else if (strcmp(cJSON_GetObjectItem(cjson_current, "poll_type")->valuestring, "group_message") == 0)
        {
            cJSON* cjson_value = cJSON_GetObjectItem(cjson_current, "value");
            ullong from_uin = cJSON_GetObjectItem(cjson_value, "from_uin")->valuedouble;
            ullong number = get_group_number(cJSON_GetObjectItem(cjson_value, "group_code")->valuedouble);
            msg_content_array_t content = fetch_content(cJSON_GetObjectItem(cjson_value, "content"));
            dump_message(number, str_from("group_message"), &content);
#ifdef _DEBUG
            {
                char* str = msg_content_array_to_json_object_string(&content, "content");
                fprintf(stdout, "Received group_message from: %llu\nContent: %s\n", number, str);
                fflush(stdout);
                free(str);
            }
#endif

            for (i = 0; i < robot.received_group_message_funcs_count; ++i) robot.received_group_message_funcs[i](from_uin, number, &content);
            msg_content_array_free(&content);
        }
        else
        {
            bson_t document;
            bson_t content;
            bson_error_t error;
            time_t t;
            char* ptr = cJSON_PrintUnformatted(cjson_current);
            mongoc_collection_t* collection = mongoc_database_get_collection(robot.mongoc_database, "unprocessed");

            time(&t);
            if (!bson_init_from_json(&content, ptr, strlen(ptr), &error))
            {
                MONGOC_WARNING("%s\n", error.message);
                return;
            }
            bson_init(&document);
            BSON_APPEND_TIME_T(&document, "time", t);
            BSON_APPEND_DOCUMENT(&document, "content", &content);
            if (!mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &document, NULL, &error)) MONGOC_WARNING("%s\n", error.message);
            bson_destroy(&document);
            bson_destroy(&content);
        }
    }
}