src/nexus.c
author Tero Marttila <terom@fixme.fi>
Sat, 28 Feb 2009 17:39:37 +0200
changeset 11 14e79683c48c
parent 10 9fe218576d13
child 12 4147fae232d9
permissions -rw-r--r--
working event-based operation for sock_tcp

#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 "6667"
#define LINE_LENGTH 512

void on_line (const char *line, void *arg) {
    printf("<<< %s\n", line);
}

int main (int argc, char **argv) {
    struct event_base *ev_base;
    struct sock_stream *sock;
    struct line_proto *lp;
    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_tcp_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err))
        errx(1, "sock_gnutls_connect: %s", error_msg(&_err));

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

    // run event loop
    if (event_base_dispatch(ev_base))
        errx(1, "event_base_dispatch");
    
    // ok, no cleanup
    return 0;
}