src/nexus.c
author Tero Marttila <terom@fixme.fi>
Sat, 28 Feb 2009 19:08:51 +0200
changeset 13 ca16f3a8f3b7
parent 12 4147fae232d9
child 14 3a70e5901f17
permissions -rw-r--r--
add support for \n line endings as well

#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 "localhost"
#define CONNECT_SERV "5002"
#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, with safety margin for buffer
    if (line_proto_create(&lp, sock, LINE_LENGTH * 2, 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;
}