[IOMultiplexerV2] Added simple Server example
[NextIRCd.git] / src / IOHandler_test / server / 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 <string.h>
20 #include "../../IOHandler/IOHandler.h"
21 #include "../../IOHandler/IOSockets.h"
22 #include "../../IOHandler/IOLog.h"
23
24 static IOSOCKET_CALLBACK(io_callback);
25 static IOLOG_CALLBACK(io_log);
26
27 static struct IOSocket *irc_iofd = NULL;
28
29 int main(int argc, char *argv[]) {
30         iohandler_init();
31         
32     iolog_register_callback(io_log);
33     
34     irc_iofd = iosocket_listen("0.0.0.0", 12345, io_callback);
35         
36         iohandler_run();
37         
38         return 0;
39 }
40
41 static IOSOCKET_CALLBACK(io_callback) {
42     switch(event->type) {
43         case IOSOCKETEVENT_ACCEPT:
44             printf("[client accepted]\n");
45                         struct IOSocket *client = event->data.accept_socket;
46                         client->callback = io_callback;
47                         
48                         char *html = "<html><head><title>Test Page</title></head><body><h1>IOHandler SSL Test</h1></body></html>";
49             iosocket_printf(client, "HTTP/1.1 200 OK\r\n");
50                         iosocket_printf(client, "Server: Apache\r\n");
51                         iosocket_printf(client, "Content-Length: %d\r\n", strlen(html));
52                         iosocket_printf(client, "Content-Type: text/html\r\n");
53                         iosocket_printf(client, "\r\n");
54                         iosocket_printf(client, "%s", html);
55                         
56             break;
57         case IOSOCKETEVENT_CLOSED:
58                         if(event->socket->listening)
59                                 printf("[server closed]\n");
60                         else
61                                 printf("[client disconnect]\n");
62             break;
63         case IOSOCKETEVENT_RECV:
64                         {
65                                 struct IOSocketBuffer *recv_buf = event->data.recv_buf;
66                                 int i;
67                                 for(i = 0; i < recv_buf->bufpos; i++)
68                                         putchar(recv_buf->buffer[i]);
69                                 recv_buf->bufpos = 0;
70                                 printf("\n");
71             }
72             break;
73         
74         default:
75             break;
76     }
77 }
78
79 static IOLOG_CALLBACK(io_log) {
80         printf("%s", message);
81 }