//
//  news_publisher.c - Publishes random news
//
//  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:     news_publisher
//  Usage:    news_publisher <broker-addeess>
//  Example:  news_publisher 127.0.0.1:5672
//  Function: Publishes random news

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

//  Repository of news items
//  The program will pick the news randomly from this set
//  Left string will be used as routing key, right one is news body

char *news [][6] = {
    {"article", "culture", "Spain", "Fernando Ferreira", "Flamenco festival starting", "Blah..."},
    {"article", "investigative", "France", "Jean Valjean", "Toulon galleys", "Blah..."},
    {"article", "science", "UK", "John Heathcliff", "Finally: 100%% waterproof raincoats invented!", "Blah..."},
    {"digest", "sport", "China", "Wu Lei", "Chinese hockey league in 2007", "Blah..."},
    {"digest", "business", "Japan", "Takahashi Sigismund", "Tokyo stock market stagnates", "Blah..."},
    {"article", "politics", "USA", "Leon Cargill", "Democrats win the elections!", "Blah..."},
    {"article", "politics", "USA", "Leon Cargill", "Republicans win the elections!", "Blah..."},
    {"article", "business", "Spain", "Bartolomeu de Aixela", "Catalonia is still No.1", "Blah..."},
    {"article", "sport", "France", "Wu Lei", "French hockey is no match to Chinese hockey", "Blah..."},
    {"article", "science", "Japan", "Suzuki Hachiro", "Japanese scientists present steam-based chipsets", "Blah..."},
    {"digest", "culture", "China", "Xiao Wang", "Cultural events in Shanghai province", "Blah..."}
};

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_body;
    int
        news_item;
        
    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, "news_publisher", 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

    while (1) {

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

        //  Choose random news item
        news_item = random () % (sizeof (news) / sizeof (char*[6]));

        //  Create the message body
        message_body =
            malloc (strlen (news [news_item][5]) + 1);
        assert (message_body);
        strcpy (message_body, news [news_item][5]);

        //  Create the message itself
        content = amq_content_basic_new ();
        assert (content);

        //  Fill in the body
        amq_content_basic_set_body (content, message_body,
            strlen (message_body), free);

        //  Fill in the headers
        amq_content_basic_set_headers_field (
            content, "type", news [news_item][0]);
        amq_content_basic_set_headers_field (
            content, "category", news [news_item][1]);
        amq_content_basic_set_headers_field (
            content, "region", news [news_item][2]);
        amq_content_basic_set_headers_field (
            content, "author", news [news_item][3]);
        amq_content_basic_set_headers_field (
            content, "title", news [news_item][4]);

        //  Send the message
        amq_client_session_basic_publish (
            session,                        //  session
            content,                        //  content to send
            0,                              //  ticket
            "news",                         //  exchange to send message to
            NULL,                           //  routing-key
            FALSE,                          //  mandatory
            FALSE);                         //  immediate

        //  Release the message
        amq_content_basic_unlink (&content);

        //  Wait a bit
        sleep (1);
    }

    //  Close the channel
    amq_client_session_destroy (&session);

    //  Close the connection
    amq_client_connection_destroy (&connection);

    //  Uninitialise system
    icl_system_terminate ();

    return 0;
}

