src/nexus.c
changeset 11 14e79683c48c
parent 10 9fe218576d13
child 12 4147fae232d9
equal deleted inserted replaced
10:9fe218576d13 11:14e79683c48c
    11 
    11 
    12 #include "sock.h"
    12 #include "sock.h"
    13 #include "line_proto.h"
    13 #include "line_proto.h"
    14 
    14 
    15 #define CONNECT_HOST "irc.fixme.fi"
    15 #define CONNECT_HOST "irc.fixme.fi"
    16 #define CONNECT_SERV "6697"
    16 #define CONNECT_SERV "6667"
    17 #define LINE_LENGTH 512
    17 #define LINE_LENGTH 512
       
    18 
       
    19 void on_line (const char *line, void *arg) {
       
    20     printf("<<< %s\n", line);
       
    21 }
    18 
    22 
    19 int main (int argc, char **argv) {
    23 int main (int argc, char **argv) {
    20     struct event_base *ev_base;
    24     struct event_base *ev_base;
    21     struct sock_stream *sock;
    25     struct sock_stream *sock;
    22     struct line_proto *lp;
    26     struct line_proto *lp;
    23     char *line;
       
    24     struct error_info _err;
    27     struct error_info _err;
    25 
    28 
    26     // initialize libevent
    29     // initialize libevent
    27     if ((ev_base = event_base_new()) == NULL)
    30     if ((ev_base = event_base_new()) == NULL)
    28         err(1, "event_base_new");
    31         err(1, "event_base_new");
    30     // initialize sock module
    33     // initialize sock module
    31     if (sock_init(ev_base, &_err))
    34     if (sock_init(ev_base, &_err))
    32         errx(1, "sock_init: %s", error_msg(&_err));
    35         errx(1, "sock_init: %s", error_msg(&_err));
    33 
    36 
    34     // over-simplified connect
    37     // over-simplified connect
    35     if (sock_gnutls_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err))
    38     if (sock_tcp_connect(&sock, CONNECT_HOST, CONNECT_SERV, &_err))
    36         errx(1, "sock_gnutls_connect: %s", error_msg(&_err));
    39         errx(1, "sock_gnutls_connect: %s", error_msg(&_err));
    37 
    40 
    38     // line protocol
    41     // line protocol
    39     if (line_proto_create(&lp, sock, LINE_LENGTH, NULL, NULL, &_err))
    42     if (line_proto_create(&lp, sock, LINE_LENGTH, on_line, NULL, &_err))
    40         errx(1, "line_proto_create: %s", error_msg(&_err));
    43         errx(1, "line_proto_create: %s", error_msg(&_err));
    41 
    44 
    42     // read lines and dump them out
    45     // run event loop
    43     do {
    46     if (event_base_dispatch(ev_base))
    44         // recv
    47         errx(1, "event_base_dispatch");
    45         if (line_proto_read(lp, &line))
       
    46             errx(1, "line_proto_read: %s", error_msg(line_proto_error(lp)));
       
    47 
       
    48         // printf
       
    49         printf("<<< %s\n", line);
       
    50 
       
    51     } while (1);
       
    52     
    48     
    53     // ok
    49     // ok, no cleanup
    54     return 0;
    50     return 0;
    55 }
    51 }
    56 
    52