src/nexus.c
author Tero Marttila <terom@fixme.fi>
Sun, 22 Feb 2009 08:48:21 +0200
changeset 9 4c4c906cc649
parent 8 be88e543c8ff
child 10 9fe218576d13
permissions -rw-r--r--
add sock_stream_callbacks and ev_base

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#include <assert.h>

#include <err.h>
#include <event2/event.h>

#include "sock.h"
#include "line_proto.h"

#define CONNECT_HOST "irc.fixme.fi"
#define CONNECT_SERV "6697"
#define LINE_LENGTH 512

int main (int argc, char **argv) {
    struct event_base *ev_base;
    struct sock_stream *sock;
    struct line_proto *lp;
    char line_buf[LINE_LENGTH + 1];
    struct error_info _err;

    // initialize libevent
    if ((ev_base = event_base_new()) == NULL)
        err(1, "event_base_new");

    // initialize sock module
    if (sock_init(ev_base, &_err))
        errx(1, "sock_init: %s", error_msg(&_err));

    // over-simplified connect
    if (sock_gnutls_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err))
        errx(1, "sock_gnutls_connect: %s", error_msg(&_err));

    // line protocol
    if (line_proto_create(&lp, sock, &_err))
        errx(1, "line_proto_create: %s", error_msg(&_err));

    // read lines and dump them out
    do {
        // recv
        if (line_proto_read(lp, line_buf, sizeof(line_buf)))
            errx(1, "line_proto_read: %s", error_msg(line_proto_error(lp)));

        // printf
        printf("<<< %s\n", line_buf);

    } while (1);
    
    // ok
    return 0;
}