Blame Examples/amqp_bind.c

Packit Service cd0f1b
/*
Packit Service cd0f1b
 * ***** BEGIN LICENSE BLOCK *****
Packit Service cd0f1b
 * Version: MIT
Packit Service cd0f1b
 *
Packit Service cd0f1b
 * Portions created by Alan Antonuk are Copyright (c) 2012-2013
Packit Service cd0f1b
 * Alan Antonuk. All Rights Reserved.
Packit Service cd0f1b
 *
Packit Service cd0f1b
 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
Packit Service cd0f1b
 * All Rights Reserved.
Packit Service cd0f1b
 *
Packit Service cd0f1b
 * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
Packit Service cd0f1b
 * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
Packit Service cd0f1b
 *
Packit Service cd0f1b
 * Permission is hereby granted, free of charge, to any person
Packit Service cd0f1b
 * obtaining a copy of this software and associated documentation
Packit Service cd0f1b
 * files (the "Software"), to deal in the Software without
Packit Service cd0f1b
 * restriction, including without limitation the rights to use, copy,
Packit Service cd0f1b
 * modify, merge, publish, distribute, sublicense, and/or sell copies
Packit Service cd0f1b
 * of the Software, and to permit persons to whom the Software is
Packit Service cd0f1b
 * furnished to do so, subject to the following conditions:
Packit Service cd0f1b
 *
Packit Service cd0f1b
 * The above copyright notice and this permission notice shall be
Packit Service cd0f1b
 * included in all copies or substantial portions of the Software.
Packit Service cd0f1b
 *
Packit Service cd0f1b
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit Service cd0f1b
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit Service cd0f1b
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit Service cd0f1b
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit Service cd0f1b
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit Service cd0f1b
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service cd0f1b
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit Service cd0f1b
 * SOFTWARE.
Packit Service cd0f1b
 * ***** END LICENSE BLOCK *****
Packit Service cd0f1b
 */
Packit Service cd0f1b
Packit Service cd0f1b
#include <stdint.h>
Packit Service cd0f1b
#include <stdio.h>
Packit Service cd0f1b
#include <stdlib.h>
Packit Service cd0f1b
#include <string.h>
Packit Service cd0f1b
Packit Service cd0f1b
#include <amqp.h>
Packit Service cd0f1b
#include <amqp_tcp_socket.h>
Packit Service cd0f1b
Packit Service cd0f1b
#include "utils.h"
Packit Service cd0f1b
Packit Service cd0f1b
int main(int argc, char const *const *argv) {
Packit Service cd0f1b
  char const *hostname;
Packit Service cd0f1b
  int port, status;
Packit Service cd0f1b
  char const *exchange;
Packit Service cd0f1b
  char const *bindingkey;
Packit Service cd0f1b
  char const *queue;
Packit Service cd0f1b
  amqp_socket_t *socket = NULL;
Packit Service cd0f1b
  amqp_connection_state_t conn;
Packit Service cd0f1b
Packit Service cd0f1b
  if (argc < 6) {
Packit Service cd0f1b
    fprintf(stderr, "Usage: amqp_bind host port exchange bindingkey queue\n");
Packit Service cd0f1b
    return 1;
Packit Service cd0f1b
  }
Packit Service cd0f1b
Packit Service cd0f1b
  hostname = argv[1];
Packit Service cd0f1b
  port = atoi(argv[2]);
Packit Service cd0f1b
  exchange = argv[3];
Packit Service cd0f1b
  bindingkey = argv[4];
Packit Service cd0f1b
  queue = argv[5];
Packit Service cd0f1b
Packit Service cd0f1b
  conn = amqp_new_connection();
Packit Service cd0f1b
Packit Service cd0f1b
  socket = amqp_tcp_socket_new(conn);
Packit Service cd0f1b
  if (!socket) {
Packit Service cd0f1b
    die("creating TCP socket");
Packit Service cd0f1b
  }
Packit Service cd0f1b
Packit Service cd0f1b
  status = amqp_socket_open(socket, hostname, port);
Packit Service cd0f1b
  if (status) {
Packit Service cd0f1b
    die("opening TCP socket");
Packit Service cd0f1b
  }
Packit Service cd0f1b
Packit Service cd0f1b
  die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
Packit Service cd0f1b
                               "guest", "guest"),
Packit Service cd0f1b
                    "Logging in");
Packit Service cd0f1b
  amqp_channel_open(conn, 1);
Packit Service cd0f1b
  die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
Packit Service cd0f1b
Packit Service cd0f1b
  amqp_queue_bind(conn, 1, amqp_cstring_bytes(queue),
Packit Service cd0f1b
                  amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey),
Packit Service cd0f1b
                  amqp_empty_table);
Packit Service cd0f1b
  die_on_amqp_error(amqp_get_rpc_reply(conn), "Unbinding");
Packit Service cd0f1b
Packit Service cd0f1b
  die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
Packit Service cd0f1b
                    "Closing channel");
Packit Service cd0f1b
  die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
Packit Service cd0f1b
                    "Closing connection");
Packit Service cd0f1b
  die_on_error(amqp_destroy_connection(conn), "Ending connection");
Packit Service cd0f1b
  return 0;
Packit Service cd0f1b
}