//
//  helpdesk_operator.c - terminal for helpdesk operator
//
//  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:     helpdesk_operator
//  Usage:    helpdesk_operator <broker-addeess>
//  Example:  helpdesk_operator 127.0.0.1:5672
//  Receives client questions and prints them out for the helpdesk operator

#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;
    amq_content_basic_t
        *content;
    char
        message_text [1024];
    size_t
        message_size;


    assert (argc == 2);

    //  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, "helpdesk_operator", 0, 30000);
    assert (connection);
    icl_longstr_destroy (&auth_data);

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

    //  Create the shared helpdesk queue if it does not exist
    amq_client_session_queue_declare (
        session,                        //  session
        0,                              //  ticket
        "helpdesk",                     //  queue name
        FALSE,                          //  passive
        FALSE,                          //  durable
        FALSE,                          //  exclusive
        FALSE,                          //  auto-delete
        NULL);                          //  arguments

    //  Bind the queue to the exchange
    amq_client_session_queue_bind (
        session,                        //  session
        0,                              //  ticket
        "helpdesk",                     //  queue
        "amq.direct",                   //  exchange
        "helpdesk",                     //  routing-key
        NULL);                          //  arguments

    //  Consume from the queue
    amq_client_session_basic_consume (
        session,                        //  session
        0,                              //  ticket
        NULL,                           //  queue
        NULL,                           //  consumer-tag
        TRUE,                           //  no-local
        TRUE,                           //  no-ack
        FALSE,                          //  exclusive
        NULL);                          //  arguments

    while (1) {

        while (1) {

            //  Get next message
            content = amq_client_session_basic_arrived (session);
            if (!content)
                break;

            //  Get the message body and write it to stdout
            message_size = amq_content_basic_get_body (content,
                  (byte*) message_text, sizeof (message_text));
            if (message_size) {
                message_text [message_size] = 0;
                fputs (message_text, stdout);
            }

            //  Destroy the message
            amq_content_basic_unlink (&content);
        }
        
        //  Wait while next message arrives
        amq_client_session_wait (session, 0);

        //  Exit the loop if Ctrl+C is encountered
        if (!connection->alive)
            break;
    }

    //  Close the channel
    amq_client_session_destroy (&session);

    //  Close the connection
    amq_client_connection_destroy (&connection);

    //  Uninitialise system
    icl_system_terminate ();

    return 0;
}

