Ejemplo n.º 1
0
/*
 * Main AMQP thread.
 *
 * The thread will create a connection and then loop over the following:
 *
 * - Open a connection
 * - Open a channel
 * - Create (declare) a "WICED_QUEUE" queue
 * - Bind the queue to "amq.direct" exchange
 * - Send data through to the exchange ( using publish and send string)
 * - Receive data from the queue ( using consume and receive string )
 * - Close the channel
 * - Close the connection
 */
void application_start( void )
{
    wiced_result_t  ret = WICED_SUCCESS;

    wiced_init( );

    wiced_rtos_init_semaphore( &semaphore );

    /* Bringup the network interface */
    wiced_network_up( WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL );

    WPRINT_APP_INFO(("[AMQP] Connecting to broker %u.%u.%u.%u ...", (uint8_t)(GET_IPV4_ADDRESS(broker_address) >> 24),
                                                          (uint8_t)(GET_IPV4_ADDRESS(broker_address) >> 16),
                                                          (uint8_t)(GET_IPV4_ADDRESS(broker_address) >> 8),
                                                          (uint8_t)(GET_IPV4_ADDRESS(broker_address) >> 0)));

    ret = amqp_connection_init( &broker_address, WICED_STA_INTERFACE, &callbacks, &connection, NULL );
    amqp_print_status( ret, NULL, NULL );

    while( ret == WICED_SUCCESS )
    {

        WPRINT_APP_INFO(("[AMQP] Opening connection..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_conn_open( &connection ), NULL, "Did you configure you broker IP address?\n" );

        WPRINT_APP_INFO(("[AMQP] Opening channel..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_open( &connection, 1 ), NULL, NULL );

        WPRINT_APP_INFO(("[AMQP] Declaring queue..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_queue_declare( &connection, 1, AMQP_RECEIVE_QUEUE_NAME ), NULL, NULL );

        WPRINT_APP_INFO(("[AMQP] Binding queue..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_queue_bind( &connection, 1, AMQP_RECEIVE_QUEUE_NAME, AMQP_ROUTING_KEY_NAME ), NULL, NULL );

        WPRINT_APP_INFO(("[AMQP] Consuming..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_consume( &connection, 1, AMQP_RECEIVE_QUEUE_NAME ), NULL, NULL );

        WPRINT_APP_INFO(("[AMQP] Publishing..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_publish( &connection, 1, AMQP_ROUTING_KEY_NAME ), NULL, NULL );

        WPRINT_APP_INFO(("[AMQP] Sending message..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_send_string( &connection, 1, AMQP_MESSAGE_STR ), AMQP_MESSAGE_STR, NULL );

        WPRINT_APP_INFO(("[AMQP] Receiving message..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_recv_string( &connection, 1 ), str, NULL );

        WPRINT_APP_INFO(("[AMQP] Closing channel..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_ch_close( &connection, 1 ), NULL, NULL);

        WPRINT_APP_INFO(("[AMQP] Closing connection..."));
        RUN_COMMAND_PRINT_STATUS_AND_BREAK_ON_ERROR( amqp_conn_close( &connection ), NULL, NULL );

        wiced_rtos_delay_milliseconds( 2000 );

    }
    WPRINT_APP_INFO(("[AMQP] Deinit connection..."));
    ret = amqp_connection_deinit( &connection );
    amqp_print_status( ret, NULL, NULL );

}
Ejemplo n.º 2
0
static int
create_conn(void)
{
    int res;

    res = 0;

    conn = amqp_conn_new("localhost", 5672, user, password, vhost, 0, 0, 0, AMQP_CAP_PUBLISHER_CONFIRMS);

    if (amqp_conn_open(conn) != 0) {
        res = 1;
        goto err;
    }

    if (amqp_conn_run(conn) != 0) {
        res = 1;
        goto err;
    }

end:
    return res;

err:
    TR(res);
    amqp_conn_close(conn, 0);
    amqp_conn_post_close(conn);
    amqp_conn_destroy(&conn);
    goto end;
}
Ejemplo n.º 3
0
static int
sigshutdown(UNUSED int argc, UNUSED void **argv)
{
    if (!shutting_down) {
        TRACE("Shutting down. Another signal will cause immediate exit.");
        shutting_down = 1;
        amqp_conn_close(conn, 0);
        amqp_conn_post_close(conn);
        amqp_conn_destroy(&conn);
        _shutdown();
    } else {
        TRACE("Exiting (sigshutdown)...");
        exit(0);
    }
    return 0;
}
Ejemplo n.º 4
0
static int
run0(UNUSED int argc, UNUSED void **argv)
{
    amqp_conn_t *conn;
    amqp_channel_t *chan;
    mnbytestream_t bs;

    mrkamqp_init();
    conn = amqp_conn_new(host,
                         port,
                         user,
                         password,
                         vhost,
                         0,
                         MAX(MRKCOMMON_ALIGN_CEILING(hi_payload_size, 1024), 0x20000),
                         0,
                         AMQP_CAP_PUBLISHER_CONFIRMS);

    if (amqp_conn_open(conn) != 0) {
        goto err;
    }
    if (amqp_conn_run(conn) != 0) {
        goto err;
    }

    if ((chan = amqp_create_channel(conn)) == NULL) {
        goto err;
    }

    bytestream_init(&bs, hi_payload_size);

    while (!shutting_down) {
        UNUSED int res;
        UNUSED double before, after;
        UNUSED uint64_t before_tocks, after_ticks;
        ssize_t payload_size;

        if (hi_payload_size != lo_payload_size) {
            payload_size = (ssize_t)(random() % (hi_payload_size - lo_payload_size)) + lo_payload_size;
        } else {
            payload_size = (ssize_t)lo_payload_size;
        }
        before = MRKTHR_GET_NOW_PRECISE_FSEC();
        res = amqp_channel_publish(chan,
                                   exchange,
                                   routing_key,
                                   0,
                                   mycb,
                                   NULL,
                                   (char *)SDATA(&bs, 0),
                                   payload_size);
        after = MRKTHR_GET_NOW_PRECISE_FSEC();
        //CTRACE("res=%d time=%lf", res, after - before);
        mrkthr_sleep(publish_sleep);
        mygauge_incr(&published, 1);
        mygauge_update(&oframes, amqp_conn_oframes_length(conn));
    }

end:
    amqp_conn_close(conn, 0);
    amqp_conn_post_close(conn);
    amqp_conn_destroy(&conn);
    mrkamqp_fini();
    bytestream_fini(&bs);

    return 0;

err:
    goto end;
}
Ejemplo n.º 5
0
static int
run_conn(int argc, char **argv)
{
    int res;
    amqp_channel_t *chan;
    amqp_consumer_t *cons;

    res = 0;
    assert(conn != NULL);

    if ((chan = amqp_create_channel(conn)) == NULL) {
        res = 1;
        goto err;
    }

    //if (amqp_channel_confirm(chan, 0) != 0) {
    //    res = 1;
    //    goto err;
    //}

    if (amqp_channel_declare_queue_ex(chan,
                                      src,
                                      DECLARE_QUEUE_FEXCLUSIVE,
                                      declare_queue_cb1,
                                      NULL,
                                      NULL,
                                      NULL) != 0) {
        res = 1;
        goto err;
    }

    if ((cons = amqp_channel_create_consumer(chan,
                                             src,
                                             NULL,
                                             CONSUME_FNOACK)) == NULL) {
        res = 1;
        goto err;
    }

    (void)MRKTHR_SPAWN("pub", mypub, chan, argc, argv);

    (void)amqp_consumer_handle_content(cons, my_content_cb, NULL, NULL);

    if (amqp_close_consumer(cons) != 0) {
        res = 1;
        goto err;
    }

    if (amqp_close_channel(chan) != 0) {
        res = 1;
        goto err;
    }

end:
    if (conn != NULL) {
        (void)amqp_conn_close(conn, 0);
        amqp_conn_post_close(conn);
    }
    amqp_conn_destroy(&conn);
    return res;

err:
    TR(res);
    goto end;
}