//
//  server_time.c - Receives and answers current time requests
//
//  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:     server_time
//  Usage:    server_time  <OpenAMQ broker IP address> <time server ID>
//  Example:  server_time 127.0.0.1 timeserver1
//  Function: Receives and answers current time requests

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

int
main (int argc, char *argv [])
{
    if (argc != 3) {
        printf ("Usage: server_time "
            "<OpenAMQ broker IP address> <time server ID>\n");
        return 0;
    }

    amq_client_connection_t *connection = NULL;
    amq_client_session_t    *session = NULL;
    icl_longstr_t           *auth_data;
    amq_content_basic_t     *content;

    char                    message_text [1024];
    char                    message_from [1024];
    size_t                  message_size;

    icl_system_initialise (2, argv);
    auth_data  = amq_client_connection_auth_plain ("guest", "guest");
    connection = amq_client_connection_new (argv [1], "/", auth_data, "test",
        0, 30000);
    icl_longstr_destroy (&auth_data);
    assert (connection);

    //  Print server info to stdout
    printf ("%s: ", argv [1]);
    printf ("%s/%s - %s - %s\n", connection->server_product, 
        connection->server_version, connection->server_platform, 
        connection->server_information);

    //  Taken as a second argument from command line
    printf ("My ID: %s\n", argv [2]);

    session = amq_client_session_new (connection);
    assert (session);
   

    //  Make sure that 'services' exchange exists, if it does not, create it
    amq_client_session_exchange_declare (
        session,                        //  session
        0,                              //  ticket
        "services",                     //  exchange name
        "direct",                       //  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 'time_pool' queue (if it does not exist yet)
    amq_client_session_queue_declare (
        session,                        //  session
        0,                              //  ticket
        "time_pool",                    //  queue name
        FALSE,                          //  passive
        FALSE,                          //  durable
        FALSE,                          //  exclusive
        TRUE,                           //  auto-delete
        NULL);                          //  arguments

    //  Bind the 'time_pool' queue to the 'services' exchange
    amq_client_session_queue_bind (
        session,                        //  session
        0,                              //  ticket
        "time_pool",                    //  queue
        "services",                     //  exchange
        "cur_time",                     //  routing-key
        NULL);                          //  arguments
    
    //  Consume from the 'time_pool' queue 
    amq_client_session_basic_consume (
        session,                        //  session
        0,                              //  ticket
        "time_pool",                    //  queue
        NULL,                           //  consumer-tag
        TRUE,                           //  no-local
        TRUE,                           //  no-ack
        FALSE,                          //  exclusive
        NULL);                          //  arguments
 
    while (1) {
        if (!connection->alive)
            break;

        while (1) {
            content = amq_client_session_basic_arrived (session);
            if (!content)
                break;
            
            //  Get the request message body
            message_size = amq_content_basic_get_body (content,
                (byte*) message_text, sizeof (message_text));

            if (message_size) {
                message_text [message_size] = 0;
                printf ("Message from %s: \'%s\'\n", message_from,
                    message_text);
            }

            // Copy reply_to request message preperty to message_from variable
            assert (strlen (content->reply_to) < sizeof (message_from));
            strcpy (message_from, content->reply_to);

            amq_content_basic_unlink (&content);

            //  Parse request message text
            if (strlen (message_text) > 3 && message_text [0] == 'G' 
                && message_text [1] == 'E' && message_text [2] == 'T') {

                //  Get system current time and create answer message
                time_t now_time = time (NULL);
                assert (now_time != ((time_t)-1));
                struct tm *now_tm = localtime (&now_time);
                assert (now_tm);
                sprintf (message_text, "I'm %s, time: %i:%i:%i.", argv [2],
                    now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec);

                printf ("Sending : \'%s\'\n", message_text);

                content = amq_content_basic_new ();
                amq_content_basic_set_body (content, message_text,
                    strlen (message_text), NULL);
                amq_content_basic_set_message_id (content, "ID001");

                //  Publish answer message with routing key value the same as 
                //  was used for reply_to property of the request
                amq_client_session_basic_publish (
                    session,                    //  session
                    content,                    //  content to send
                    0,                          //  ticket
                    "responses",                //  exchange to send message to
                    message_from,               //  routing-key
                    FALSE,                      //  mandatory //true
                    FALSE);                     //  immediate //true
    
                amq_content_basic_unlink (&content);

            } else {
                printf ("Unknown message type.\n");
            }
            
        }
        printf ("Waiting: ");
        fflush (stdout);

        //  Wait for incomming message
        amq_client_session_wait (session, 0);
    }

    //  Close the session and connection
    amq_client_session_destroy (&session); 
    amq_client_connection_destroy (&connection);
    icl_system_terminate ();
    return 0;
}

