/** * create a new json_processing_st * * @param backend pointer to a burrow_backend_t * @return pointer to a new json_processing_st, or NULL if malloc failed. */ static json_processing_t* burrow_easy_json_st_create(burrow_backend_t* backend) { json_processing_t *jproc = malloc(sizeof(json_processing_t)); if (jproc == NULL) return 0; jproc->backend = backend; jproc->body = 0; jproc->body_size = 0; jproc->message_id = 0; jproc->is_key = 0; jproc->key = 0; jproc->attributes = burrow_attributes_create(0, 0); return jproc; }
int main(int argc, char **argv) { burrow_st *burrow; client_st client; msg_st *msg; int use_http = 0; argc--; argv++; if (strcmp(argv[0], "http") == 0) { use_http = 1; argc--; argv++; } if (argc < 4 || argc % 2 != 0) return -1; client.account = argv[0]; client.queue = argv[1]; argc -= 2; argv += 2; client.messages = NULL; client.message_count = 0; while (argc) { msg = malloc(sizeof(msg_st)); if (!msg) return -2; msg->msg_id = argv[0]; msg->body = (uint8_t *)argv[1]; msg->body_size = strlen(argv[1]); msg->next = client.messages; client.messages = msg; client.message_count++; argc -= 2; argv += 2; } client.return_code = 0; client.current_message = client.messages; if (use_http == 0) { burrow = burrow_create(NULL, "dummy"); printf("burrow = %p\n", burrow); } else { burrow = burrow_create(NULL, "http"); printf("burrow = %p\n", burrow); burrow_backend_set_option(burrow, "server", "localhost"); burrow_backend_set_option(burrow, "port", "8080"); } burrow_set_context(burrow, &client); burrow_set_log_fn(burrow, &_log); burrow_set_message_fn(burrow, &_message); (void) _complete; /* Insert the first one here to kick the loop off. This only sets start state, it doesn't run the loop. */ burrow_set_complete_fn(burrow, &_complete); msg = client.current_message; printf("Calling create_message(%s,%s,%s,\"%s\",..)\n\n", client.account, client.queue, msg->msg_id, msg->body); burrow_create_message(burrow, client.account, client.queue, msg->msg_id, msg->body, msg->body_size, NULL); /* This runs until there are no more tasks. */ burrow_process(burrow); printf("Ok, now let us see what messages the server has\n"); /* Now see what the server has */ burrow_get_messages(burrow, client.account, client.queue, NULL); burrow_result_t result; do { result = burrow_process(burrow); } while (result != BURROW_OK); /* Now get the first message */ { printf("Now we will get the first message\n"); burrow_filters_st *filters = burrow_filters_create(0,0); burrow_filters_set_detail(filters, BURROW_DETAIL_ALL); burrow_get_message(burrow, client.account, client.queue, client.messages->msg_id, filters ); burrow_process(burrow); } /* burrow_destroy(burrow); if (use_http == 0) { burrow = burrow_create(NULL, "dummy"); printf("burrow = %p\n", burrow); } else { burrow = burrow_create(NULL, "http"); printf("burrow = %p\n", burrow); burrow_backend_set_option(burrow, "server", "localhost"); burrow_backend_set_option(burrow, "port", "8080"); } */ /* Now update the first message to hide for a couple seconds */ uint32_t seconds = 3; printf("Now we will update the first message to hide it for %d seconds\n",seconds); burrow_filters_st *filters = burrow_filters_create(0,0); burrow_attributes_st *attributes = burrow_attributes_create(0,0); burrow_attributes_set_hide(attributes, seconds); burrow_filters_set_detail(filters, BURROW_DETAIL_ALL); burrow_update_message(burrow, client.account, client.queue, client.messages->msg_id, attributes, filters ); burrow_process(burrow); printf("Get all messages, one should be missing\n"); burrow_get_messages(burrow, client.account, client.queue, NULL); burrow_process(burrow); printf("Now sleep until the messages should reappear\n"); sleep(seconds + 1); printf("Now get all messages again\n"); burrow_get_messages(burrow, client.account, client.queue, NULL); burrow_process(burrow); printf("Ok, now let us delete one message\n"); burrow_filters_free(filters); filters = burrow_filters_create(0,0); burrow_filters_set_detail(filters, BURROW_DETAIL_ALL); burrow_delete_message(burrow, client.account, client.queue, client.messages->msg_id, NULL); burrow_process(burrow); printf("Get all messages, one should be missing\n"); burrow_get_messages(burrow, client.account, client.queue, NULL); burrow_process(burrow); printf("Get list of accounts\n"); burrow_set_account_fn(burrow, &_account); burrow_get_accounts(burrow, NULL); burrow_process(burrow); printf("finishing up\n"); burrow_destroy(burrow); exit(0); return client.return_code; }
static int burrow_backend_http_json_callback(void *ctx, int type, const JSON_value* value) { json_processing_t* jproc = (json_processing_t *)ctx; // determine what command is being processed. burrow_command_t bcommand = burrow_backend_http_get_command(jproc->backend); /* This section is for commands that return burrow messages, whether they return one message or a list of messages. */ if ( (bcommand == BURROW_CMD_GET_MESSAGE) || (bcommand == BURROW_CMD_GET_MESSAGES) || (bcommand == BURROW_CMD_UPDATE_MESSAGE) || (bcommand == BURROW_CMD_UPDATE_MESSAGES) || (bcommand == BURROW_CMD_DELETE_MESSAGE) || (bcommand == BURROW_CMD_DELETE_MESSAGES)) { switch(type) { case JSON_T_ARRAY_BEGIN: case JSON_T_ARRAY_END: break; case JSON_T_OBJECT_BEGIN: // make sure we have attributes, and they are unset jproc->attributes = burrow_attributes_create(jproc->attributes, 0); break; case JSON_T_OBJECT_END: /* * we got the end of an object. We take this to mean we have * a complete message at this point, which we should pass off * to the appropriate callback. */ burrow_callback_message(burrow_backend_http_get_burrow(jproc->backend), jproc->message_id, (uint8_t *)jproc->body, jproc->body_size, jproc->attributes); /* clean up for the next message, in case there is one */ if (jproc->message_id) { free(jproc->message_id); jproc->message_id = 0; } if (jproc->body) { free(jproc->body); jproc->body = 0; } jproc->body_size = 0; /* Make sure we have a clean set of attributes */ if (jproc->attributes) { burrow_attributes_unset_all(jproc->attributes); } else { jproc->attributes = burrow_attributes_create(0,0); } break; case JSON_T_KEY: // We don't check if the key is valid until later, // when we have the value jproc->is_key = 1; if (jproc->key) free(jproc->key); jproc->key = malloc(value->vu.str.length + 1); if (jproc->key == NULL) { burrow_error(burrow_backend_http_get_burrow(jproc->backend), ENOMEM, "ERROR! malloc failed during JSON parsing"); return 0; } memcpy(jproc->key, value->vu.str.value, value->vu.str.length + 1); break; case JSON_T_STRING: // Now check if key is valid if(jproc->is_key) { jproc->is_key = 0; if (strcmp(jproc->key, "id") == 0) { char *message_id = curl_easy_unescape(burrow_backend_http_get_curl_easy_handle(jproc->backend), value->vu.str.value, 0, 0); if (jproc->message_id) free(jproc->message_id); size_t len = strlen(message_id); jproc->message_id = malloc(len + 1); if(jproc->message_id == NULL) { burrow_error(burrow_backend_http_get_burrow(jproc->backend), ENOMEM, "ERROR! malloc failed!\n"); return 0; } memcpy(jproc->message_id, message_id, len + 1); curl_free(message_id); } else if (strcmp(jproc->key, "body") == 0) { jproc->body_size = value->vu.str.length; jproc->body = malloc(jproc->body_size); if (jproc->body == 0) { burrow_error(burrow_backend_http_get_burrow(jproc->backend), ENOMEM, "ERROR! malloc failed!\n"); return 0; } memcpy(jproc->body, value->vu.str.value, jproc->body_size + 1); } else { burrow_error(burrow_backend_http_get_burrow(jproc->backend), EINVAL, "ERROR! unrecognized string valued key \"%s\"=\"%s\"\n", jproc->key, value->vu.str.value); return 0; } } else { // We never got a key? // is this even possible without the json parser yakking? return 0; } break; case JSON_T_INTEGER: if(jproc->is_key) { jproc->is_key = 0; if (strcmp(jproc->key, "hide") == 0) { burrow_attributes_set_hide(jproc->attributes, (uint32_t)value->vu.integer_value); } else if (strcmp(jproc->key, "ttl") == 0) { burrow_attributes_set_ttl(jproc->attributes, (uint32_t)value->vu.integer_value); } else { burrow_error(burrow_backend_http_get_burrow(jproc->backend), EINVAL, "WARNING! JSON parsing found unrecognized integer key \"%s\"=%d\n", jproc->key, value->vu.integer_value); return 0; } } break; default: burrow_error(burrow_backend_http_get_burrow(jproc->backend), EINVAL, "WARNING! JSON parsing found unexpected type = %d\n", type); return 0; break; } } else if ((bcommand == BURROW_CMD_GET_QUEUES) || (bcommand == BURROW_CMD_GET_ACCOUNTS)) { /* This section is for commands that return lists of strings, such as when * you list the queues or the accounts */ switch(type) { case JSON_T_ARRAY_BEGIN: break; case JSON_T_ARRAY_END: /* Does not seem to be anything to do here. */ break; case JSON_T_STRING: if (bcommand == BURROW_CMD_GET_ACCOUNTS) { char *account = curl_easy_unescape(burrow_backend_http_get_curl_easy_handle(jproc->backend), value->vu.str.value, 0,0); burrow_callback_account(burrow_backend_http_get_burrow(jproc->backend), account); curl_free(account); } else if (bcommand == BURROW_CMD_GET_QUEUES) { char *queue = curl_easy_unescape(burrow_backend_http_get_curl_easy_handle(jproc->backend), value->vu.str.value, 0,0); burrow_callback_queue(burrow_backend_http_get_burrow(jproc->backend), queue); curl_free(queue); } break; default: burrow_error(burrow_backend_http_get_burrow(jproc->backend), EINVAL, "WARNING! JSON parsing found unexpected type = %d\n", type); return 0; break; } } return 1; }
int main(int argc, char **argv) { const char *server = "localhost"; const char *port = "8080"; const char *account = getlogin(); const char *queue = "demo"; int c; int verbose = 0; int seed = (int)time(NULL); int messages = 0; int endless = 0; int maxsleep = 25; app_state_st state = { 0, 0, {0}, 0 }; while ((c = getopt(argc, argv, "s:p:a:q:gchve:r:l:")) != -1) { switch(c) { case 'v': verbose++; break; case 'g': state.generator = 1; break; case 'c': state.generator = 0; break; case 's': server = optarg; break; case 'p': port = optarg; break; case 'a': account = optarg; break; case 'q': queue = optarg; break; case 'e': seed = atoi(optarg); break; case 'r': messages = atoi(optarg) + 1; break; case 'l': maxsleep = atoi(optarg); /* hundredths of a second */ break; default: case 'h': print_help(argv[0]); return 0; } } srand((unsigned int)seed); if (verbose > 1) printf("info: using random seed %d", seed); if (!messages) endless = 1; burrow_st *burrow; burrow = burrow_create(NULL, "http"); if (!burrow) FATAL("burrow creation failed"); burrow_set_backend_option(burrow, "server", server); burrow_set_backend_option(burrow, "port", port); burrow_add_options(burrow, BURROW_OPT_AUTOPROCESS); burrow_set_log_fn(burrow, &error_callback); burrow_set_message_fn(burrow, &message_callback); burrow_set_complete_fn(burrow, &complete_callback); burrow_set_context(burrow, &state); if (state.generator) { char buf[1024]; char uuidbuf[36 + 1]; uuid_t uuid; burrow_attributes_st *attr = burrow_attributes_create(NULL, burrow); if (!attr) FATAL("couldn't allocate attributes\n"); burrow_attributes_set_ttl(attr, 5); /* short ttl */ while (endless || messages--) { uuid_generate(uuid); uuid_unparse(uuid, uuidbuf); random_equation(buf, 1024); printf("Sending: %s\nExpected value: %f\n", buf, process_equation(buf)); burrow_create_message(burrow, account, queue, uuidbuf, (void*)buf, strlen(buf), attr); if (state.error) FATAL("encountered error"); if (maxsleep && (messages || endless)) { int sl = rand() % maxsleep; if (verbose) printf("info: sleeping for %d hundredths of a second\n", sl); usleep((useconds_t)sl * 10000); } } if (verbose) printf("info: done sending messages\n"); } else { /* Consumer */ burrow_filters_st *filters = burrow_filters_create(NULL, burrow); if (!filters) FATAL("couldn't create filters or attributes"); burrow_filters_set_detail(filters, BURROW_DETAIL_ALL); while (endless || messages--) { if (state.last_msg_id[0]) burrow_filters_set_marker(filters, state.last_msg_id); else burrow_filters_set_marker(filters, NULL); burrow_get_messages(burrow, account, queue, filters); if (state.error) FATAL("encountered error"); if (maxsleep && (messages || endless)) { int sl = rand() % maxsleep; if (verbose) printf("info: sleeping for %d hundredths of a second\n", sl); usleep((useconds_t)sl * 10000); } } if (verbose) printf("info: done receiving messages\n"); } burrow_destroy(burrow); return 0; }