Merge remote-tracking branch 'IOMultiplexer.git/master' into development
[NeonServV5.git] / src / test / socket / iotest.c
1 /* main.c - IOMultiplexer
2  * Copyright (C) 2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include <stdio.h>
19 #include "../IOHandler.h"
20
21 static IOHANDLER_CALLBACK(io_callback);
22 static IOHANDLER_LOG_BACKEND(io_log);
23
24 static struct IODescriptor *irc_iofd = NULL;
25
26 int main(int argc, char *argv[]) {
27     iolog_backend = io_log;
28     
29     irc_iofd = iohandler_connect("pk910.de", 6667, 0, NULL, io_callback);
30     
31     struct IODescriptor *iofd;
32     iofd = iohandler_add(0, IOTYPE_STDIN, NULL, io_callback);
33     iofd->read_lines = 1;
34     
35     while(1) {
36         iohandler_poll();
37     }
38 }
39
40 static IOHANDLER_CALLBACK(io_callback) {
41     switch(event->type) {
42         case IOEVENT_CONNECTED:
43             printf("[connect]\n");
44             break;
45         case IOEVENT_CLOSED:
46             printf("[disconnect]\n");
47             break;
48         case IOEVENT_RECV:
49             if(event->iofd->type == IOTYPE_STDIN) {
50                 iohandler_printf(irc_iofd, "%s\n", event->data.recv_str);
51                 printf("[out] %s\n", event->data.recv_str);
52             } else
53                 printf("[in] %s\n", event->data.recv_str);
54             break;
55         
56         default:
57             break;
58     }
59 }
60
61 static IOHANDLER_LOG_BACKEND(io_log) {
62     //printf("%s", line);
63 }