示例#1
0
文件: main.c 项目: bq/qeo-core
int main(int argc, const char **argv)
{
    int done = 0;
    qeo_factory_t *qeo;
    /* R/W for chat participants */
    qeo_state_change_reader_t *part_change_reader;
    /* R/W for chat messages */
    qeo_event_writer_t *msg_writer;
    qeo_event_reader_t *msg_reader;

    /* local variables for storing the message before sending */
    char buf[128];
    org_qeo_sample_simplechat_ChatMessage_t chat_msg = { .message = buf };

    /* initialize */
    qeo = qeo_factory_create();
    if (qeo != NULL){
        /* initialize the state R/W */
        _part_writer = qeo_factory_create_state_writer(qeo, org_qeo_sample_simplechat_ChatParticipant_type, NULL, 0);
        _part_reader = qeo_factory_create_state_reader(qeo, org_qeo_sample_simplechat_ChatParticipant_type, NULL, 0);
        part_change_reader = qeo_factory_create_state_change_reader(qeo, org_qeo_sample_simplechat_ChatParticipant_type,
                                                                    &_part_listener, 0);
        /* initialize the message R/W */
        msg_writer = qeo_factory_create_event_writer(qeo, org_qeo_sample_simplechat_ChatMessage_type, NULL, 0);
        msg_reader = qeo_factory_create_event_reader(qeo, org_qeo_sample_simplechat_ChatMessage_type,
                                                     &_msg_listener, 0);

        /* set up some defaults */
        update_participant(NULL, ORG_QEO_SAMPLE_SIMPLECHAT_CHATSTATE_AVAILABLE);

        /* start conversing */
        printf("New chat session opened.  Type '/help' for commands.\n");
        printf("You can now start chatting...\n");
        while (!done) {
            if(fgets(buf, sizeof(buf), stdin) != NULL) {
                chomp(buf);
                if ('/' == buf[0]) {
                    handle_command(&buf[1], &done);
                }
                else {
                    chat_msg.from = _me.name;
                    qeo_event_writer_write(msg_writer, &chat_msg);
                }
            }
        }

        /* clean up */
        qeo_event_reader_close(msg_reader);
        qeo_event_writer_close(msg_writer);
        qeo_state_change_reader_close(part_change_reader);
        qeo_state_reader_close(_part_reader);
        qeo_state_writer_close(_part_writer);
        qeo_factory_close(qeo);
        free(_me.name);
    }
    return 0;
}
static void run_test()
{
    qeo_factory_t *factory;
    qeo_state_change_reader_t *reader;
    qeo_state_change_reader_listener_t r_cbs = {
        .on_data = on_data,
        .on_policy_update = reader_on_policy_update
    };
    qeo_state_writer_t *writer;
    qeo_state_writer_listener_t w_cbs = {
        .on_policy_update = writer_on_policy_update
    };
    test_type_t tt = { .key = _self };

    log_self("start application for user %" PRIx64, _self);
    /* initialize */
    sem_init(&_pol_sync, 0, 0);
    sem_init(&_data_sync, 0, 0);
    assert(NULL != (factory = qeo_factory_create()));
    assert(NULL != (writer = qeo_factory_create_state_writer(factory, _tsm, &w_cbs, 0)));
    assert(NULL != (reader = qeo_factory_create_state_change_reader(factory, _tsm, &r_cbs, 0)));

    /* wait for initial policy update (reader and writer) */
    if (_self != USER_INVALID) {
        sem_wait(&_pol_sync);
        sem_wait(&_pol_sync);
        log_self("reader/writer created and policies updated");
    }
    _rd_policy_updates = _wr_policy_updates = 0;
    /* write data */
    sleep(2);
    log_self("writing data");
    assert(QEO_OK == qeo_state_writer_write(writer, &tt));
    /* wait for data reception */
    if (_self != USER_INVALID) {
        sem_wait(&_data_sync);
        log_self("forward data received");
    }
    else {
        /* sleep for about 10 seconds and check that nothing has been received */
        sleep(10);
    }

    /* clean up */
    sleep(2);
    qeo_state_change_reader_close(reader);
    qeo_state_writer_close(writer);
    qeo_factory_close(factory);
    sem_destroy(&_data_sync);
    sem_destroy(&_pol_sync);
}