//
//  helpdesk_client.c - Sends messages to the helpdesk
//
//  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_client
//  Usage:    helpdesk_client <broker-addeess>
//  Example:  helpdesk_client 127.0.0.1:5672
//  Sends messages from stdio to the helpdesk

#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];
    char
        *message_body;
        
    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_client", 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

    while (1) {

        //  Read one line from stdin
        fgets (message_text, sizeof (message_text), stdin);

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

        //  Create the message body
        message_body =
            malloc (strlen (message_text) + 1);
        assert (message_body);
        strcpy (message_body, message_text);

        //  Create the message itself
        content = amq_content_basic_new ();
        amq_content_basic_set_body (content, message_body,
            strlen (message_body), free);

        //  Send the message
        amq_client_session_basic_publish (
            session,                        //  session
            content,                        //  content to send
            0,                              //  ticket
            "amq.direct",                   //  exchange to send message to
            "helpdesk",                     //  routing-key
            FALSE,                          //  mandatory
            FALSE);                         //  immediate

        //  Release the message
        amq_content_basic_unlink (&content);
    }

    //  Close the channel
    amq_client_session_destroy (&session);

    //  Close the connection
    amq_client_connection_destroy (&connection);

    //  Uninitialise system
    icl_system_terminate ();

    return 0;
}

