//
//  subscribe_to_news.c - Subscribes to news updates
//
//  Copyright (c) 1996-2007 iMatix Corporation
//  All rights reserved.
//  
//  This file is licensed under the BSD license as follows:
//  
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//  
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in
//    the documentation andor other materials provided with the
//    distribution.
//  * Neither the name of iMatix Corporation nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//  
//  THIS SOFTWARE IS PROVIDED BY IMATIX CORPORATION "AS IS" AND ANY
//  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IMATIX CORPORATION BE
//  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
//  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

//  Name:     subscribe_to_news
//  Usage:    subscribe_to_news <broker-addeess> <user-name> <property1>
//                <value1> <property2> <value2> ...
//  Example:  subscribe_to_news 127.0.0.1:5672 johnm type article region france
//  Function: Subscribes user designated by user-name to news with the content
//            specified by properties

#include "base.h"
#include "amq_client_connection.h"
#include "amq_client_session.h"

int main (int argc, char *argv [])
{
    amq_client_connection_t
        *connection;
    amq_client_session_t
        *session;
    icl_longstr_t
        *auth_data;
    asl_field_list_t
        *headers;
    icl_longstr_t
        *bind_arguments;
    int
        arg_nbr;

    assert (argc >= 3);
    assert (argc % 2 == 1);

    //  Initialise system
    icl_system_initialise (argc, argv);

    //  Open a connection
    auth_data = amq_client_connection_auth_plain ("guest", "guest");
    connection = amq_client_connection_new (
            argv [1], "/", auth_data, "subscribe_to_news", 0, 30000);
    assert (connection);
    icl_longstr_destroy (&auth_data);

    //  Open a channel
    session = amq_client_session_new (connection);
    assert (session);

    //  Make sure that 'news' exchange exists, if it does not, create it
    amq_client_session_exchange_declare (
        session,                        //  session
        0,                              //  ticket
        "news",                         //  exchange name
        "headers",                      //  exchange type
        FALSE,                          //  shoudn't the exchange be created?
        FALSE,                          //  durable
        FALSE,                          //  auto-delete when unused
        FALSE,                          //  create internal exchange
        NULL);                          //  arguments for declaration

    //  Create a queue for the user (if it does not exist yet)
    amq_client_session_queue_declare (
        session,                        //  session
        0,                              //  ticket
        argv [2],                       //  queue name
        FALSE,                          //  passive
        FALSE,                          //  durable
        FALSE,                          //  exclusive
        FALSE,                          //  auto-delete
        NULL);                          //  arguments

    //  Fill in the binding properties
    headers = asl_field_list_new (NULL);
    assert (headers);
    for (arg_nbr = 3; arg_nbr != argc; arg_nbr += 2)
        asl_field_new_string  (headers, argv [arg_nbr], argv [arg_nbr + 1]);
    bind_arguments = asl_field_list_flatten (headers);
    assert (bind_arguments);
    asl_field_list_destroy (&headers);

    //  Bind the queue to the exchange
    amq_client_session_queue_bind (
        session,                        //  session
        0,                              //  ticket
        argv [2],                       //  queue
        "news",                         //  exchange
        NULL,                           //  routing-key
        bind_arguments);                //  arguments
    icl_longstr_destroy (&bind_arguments);

    //  Close the channel
    amq_client_session_destroy (&session);

    //  Close the connection
    amq_client_connection_destroy (&connection);

    //  Uninitialise system
    icl_system_terminate ();

    return 0;
}

