tools/consume.c
author Tony Garnock-Jones <tonygarnockjones@gmail.com>
Wed Feb 24 13:51:36 2010 +1300 (2010-02-24)
branchamqp_0_9_1
changeset 86 bd809e834eef
parent 82 a872845bc42a
child 143 58b7a23890c9
permissions -rw-r--r--
Merge default into amqp_0_9_1 (the usual edit of amqp_basic_consume was the only manual change)
     1 /*
     2  * ***** BEGIN LICENSE BLOCK *****
     3  * Version: MPL 1.1/GPL 2.0
     4  *
     5  * The contents of this file are subject to the Mozilla Public License
     6  * Version 1.1 (the "License"); you may not use this file except in
     7  * compliance with the License. You may obtain a copy of the License at
     8  * http://www.mozilla.org/MPL/
     9  *
    10  * Software distributed under the License is distributed on an "AS IS"
    11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    12  * the License for the specific language governing rights and
    13  * limitations under the License.
    14  *
    15  * The Original Code is librabbitmq.
    16  *
    17  * The Initial Developers of the Original Code are LShift Ltd, Cohesive
    18  * Financial Technologies LLC, and Rabbit Technologies Ltd.  Portions
    19  * created before 22-Nov-2008 00:00:00 GMT by LShift Ltd, Cohesive
    20  * Financial Technologies LLC, or Rabbit Technologies Ltd are Copyright
    21  * (C) 2007-2008 LShift Ltd, Cohesive Financial Technologies LLC, and
    22  * Rabbit Technologies Ltd.
    23  *
    24  * Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
    25  * Ltd. Portions created by Cohesive Financial Technologies LLC are
    26  * Copyright (C) 2007-2009 Cohesive Financial Technologies
    27  * LLC. Portions created by Rabbit Technologies Ltd are Copyright (C)
    28  * 2007-2009 Rabbit Technologies Ltd.
    29  *
    30  * Portions created by Tony Garnock-Jones are Copyright (C) 2009-2010
    31  * LShift Ltd and Tony Garnock-Jones.
    32  *
    33  * All Rights Reserved.
    34  *
    35  * Contributor(s): ______________________________________.
    36  *
    37  * Alternatively, the contents of this file may be used under the terms
    38  * of the GNU General Public License Version 2 or later (the "GPL"), in
    39  * which case the provisions of the GPL are applicable instead of those
    40  * above. If you wish to allow use of your version of this file only
    41  * under the terms of the GPL, and not to allow others to use your
    42  * version of this file under the terms of the MPL, indicate your
    43  * decision by deleting the provisions above and replace them with the
    44  * notice and other provisions required by the GPL. If you do not
    45  * delete the provisions above, a recipient may use your version of
    46  * this file under the terms of any one of the MPL or the GPL.
    47  *
    48  * ***** END LICENSE BLOCK *****
    49  */
    50 
    51 #include "config.h"
    52 
    53 #include <stdio.h>
    54 
    55 #include <popt.h>
    56 
    57 #include "common.h"
    58 #include "common_consume.h"
    59 
    60 static void do_consume(amqp_connection_state_t conn, int no_ack,
    61 		       const char * const *argv)
    62 {
    63 	if (!amqp_basic_consume(conn, 1, setup_queue(conn),
    64 				AMQP_EMPTY_BYTES, 0, no_ack, 0, AMQP_EMPTY_TABLE))
    65 		die_rpc(amqp_get_rpc_reply(conn), "basic.consume");
    66 
    67 	for (;;) {
    68 		amqp_frame_t frame;
    69 		struct pipeline pl;
    70 		uint64_t delivery_tag;
    71 		int res = amqp_simple_wait_frame(conn, &frame);
    72 		if (res < 0)
    73 			die_errno(-res, "waiting for header frame");
    74 
    75 		if (frame.frame_type != AMQP_FRAME_METHOD
    76 		    || frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD)
    77 			continue;
    78 
    79 		amqp_basic_deliver_t *deliver
    80 			= (amqp_basic_deliver_t *)frame.payload.method.decoded;
    81 		delivery_tag = deliver->delivery_tag;
    82 		
    83 		pipeline(argv, &pl);
    84 		copy_body(conn, pl.infd);
    85 
    86 		if (finish_pipeline(&pl) && !no_ack)
    87 			die_errno(-amqp_basic_ack(conn, 1, delivery_tag, 0),
    88 				  "basic.ack");
    89 
    90 		amqp_maybe_release_buffers(conn);
    91 	}
    92 }
    93 
    94 int main(int argc, const char **argv)
    95 {
    96 	poptContext opts;
    97 	int no_ack;
    98 	amqp_connection_state_t conn;
    99 	const char * const *cmd_argv;
   100 	
   101 	struct poptOption options[] = {
   102 		INCLUDE_OPTIONS(connect_options),
   103 		INCLUDE_OPTIONS(consume_queue_options),
   104 		{"no-ack", 'A', POPT_ARG_NONE, &no_ack, 0,
   105 		 "consume in no-ack mode", NULL},
   106 		POPT_AUTOHELP
   107 		{ NULL, 0, 0, NULL, 0 }
   108 	};
   109 	
   110 	opts = process_options(argc, argv, options,
   111 			       "[OPTIONS]... <command> <args>");
   112 	
   113 	cmd_argv = poptGetArgs(opts);
   114 	if (!cmd_argv || !cmd_argv[0]) {
   115 		fprintf(stderr, "consuming command not specified\n");
   116 		poptPrintUsage(opts, stderr, 0);
   117 		goto error;
   118 	}
   119 
   120 	conn = make_connection();
   121 	do_consume(conn, no_ack, cmd_argv);
   122 	close_connection(conn);
   123 	return 0;
   124 
   125 error:
   126 	poptFreeContext(opts);
   127 	return 1;
   128 }