Esempio n. 1
0
static void test_subscribe(void) {
  char buf[512];
  char *msg[] = {
    "SUBSCRIBE\n",
    "destination:/queue/test\n",
    "content-length:0\n",
    "id:test1\n",
    "\n",
    NULL,
  };
  int sock;
  char *headers[] = {
    "destination:/queue/test\n",
  };

  sock = connect_server();

  // success to connect with server
  CU_ASSERT(stomp_connect(sock) == RET_SUCCESS);

  // success to send test message
  CU_ASSERT(stomp_send(sock, "hoge\n", 5, headers, 1) == RET_SUCCESS);

  int i;
  for(i=0; msg[i] != NULL; i++) {
    send(sock, msg[i], strlen(msg[i]), 0);
  }
  send(sock, "\0", 1, 0);

  int len = test_recv(sock, buf, sizeof(buf));
  CU_ASSERT(len > 0);
  CU_ASSERT(strncmp(buf, "MESSAGE\n", 8) == 0);

  close(sock);
}
Esempio n. 2
0
int main(int argc, char **argv)
{
    stomp_status_code_t stat;

    /*
     * Initializes the messenger
     */
    stomp_messenger_t *messenger = stomp_messenger_init();

    if (!messenger) {
        fprintf(stderr, "%s\n", messenger->status.message);

        goto failure_without_message;
    }

    /*
     * Sets the endpoint address
     */
    char *url = argv[1];

    if (url == NULL) {
	url = "stomp://localhost:61613/queue/test.stomp.queue";
    }

    stat = stomp_set_endpoint(messenger, url);
    if (stat != STOMP_SUCCESS) {
        fprintf(stderr, "%s\n", messenger->status.message);

        goto failure_without_message;
    }


    /*
     * Connects to the endpoint
     */
    stat = stomp_connect(messenger, NULL, 1000);
    if (stat != STOMP_SUCCESS) {
        fprintf(stderr, "%s\n", messenger->status.message);

        goto failure_without_message;
    }


    /*
     * Creates a message to be sent
     */
    stomp_message_t *message = stomp_message_create(&messenger->status);
    if (!message) {
        fprintf(stderr, "%s\n", messenger->status.message);

        goto failure_with_message;
    }


    /*
     * Formats the message
     */
    char *text = "HIGH LEVEL API TEST";
    stomp_message_format(message, text, strlen(text));

    stomp_send_header_t send_header = {0};

    send_header.transaction_id = -1;
    send_header.receipt = 124;

    // Sets an arbitrary property to the exchange
    stomp_exchange_add(messenger->exchange_properties, "test", "123");

    stat = stomp_exchange_util_ctime(messenger->exchange_properties,
                                     &messenger->status);
    if (stat != STOMP_SUCCESS) {
        fprintf(stderr, "%s\n", messenger->status.message);

        goto failure_with_message;
    }

    /*
     * Sends the message
     */
    stat = stomp_send(messenger, &send_header, message);
    if (stat != STOMP_SUCCESS) {
        fprintf(stderr, "%s\n", messenger->status.message);

        goto failure_with_message;
    }


    /*
     * Disconnects from the broker after receiving the receipt response
     */
    stomp_disconnection_header_t disconn = {0};
    disconn.receipt = 124;
    stat = stomp_disconnect(messenger, &disconn);
    if (stat != STOMP_SUCCESS) {
        fprintf(stderr, "%s\n", messenger->status.message);

        goto failure_with_message;
    }


    /*
     * Cleanup the objects
     */
    stomp_message_destroy(&message);
    stomp_messenger_destroy(&messenger);
    return EXIT_SUCCESS;

    failure_with_message:
    stomp_message_destroy(&message);

    failure_without_message:
    stomp_messenger_destroy(&messenger);
    return EXIT_FAILURE;

}