8359b8cfafb6e21893d30d71b0e59b7a20b8c402
[profile/ivi/libwebsockets.git] / test-server.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <getopt.h>
5 #include <string.h>
6
7 #include "libwebsockets.h"
8
9 /*
10  * libwebsocket Example server  Copyright 2010 Andy Green <andy@warmcat.com>
11  * 
12  * Shows how to use libwebsocket 
13  */
14
15 static int port = 7681;
16 static int ws_protocol = 76;
17
18 /*
19  * libwebsockets needs this one callback in your server application, it's
20  * called for a handful of different reasons during the connection lifecycle.
21  * 
22  * All the serving actions occur in the callback but the websocket protocol
23  * stuff is already handled by the library.
24  */
25
26 static int websocket_callback(struct libwebsocket * wsi,
27                 enum libwebsocket_callback_reasons reason, void *in, size_t len)
28 {
29         int n;
30         char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
31                                                   LWS_SEND_BUFFER_POST_PADDING];
32         static int bump;
33         char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
34         const char *uri;
35         
36         switch (reason) {
37         /*
38          * Websockets session handshake completed and is established
39          */
40         case LWS_CALLBACK_ESTABLISHED:
41                 fprintf(stderr, "Websocket connection established\n");
42                 break;
43
44         /*
45          * Websockets session is closed
46          */
47         case LWS_CALLBACK_CLOSED:
48                 fprintf(stderr, "Websocket connection closed\n");
49                 break;
50
51         /*
52          * Opportunity for us to send something on the connection
53          */
54         case LWS_CALLBACK_SEND: 
55                 n = sprintf(p, "%d", bump++);
56                 n = libwebsocket_write(wsi, (unsigned char *)p, n, 0);
57                 if (n < 0) {
58                         fprintf(stderr, "ERROR writing to socket");
59                         exit(1);
60                 }
61                 break;
62         /*
63          * Something has arrived for us on the connection, it's len bytes long
64          * and is available at *in
65          */
66         case LWS_CALLBACK_RECEIVE:
67                 fprintf(stderr, "Received %d bytes payload\n", (int)len);
68                 break;
69
70         /*
71          * The client has asked us for something in normal HTTP mode,
72          * not websockets mode.  Normally it means we want to send
73          * our script / html to the client, and when that script runs
74          * it will start up separate websocket connections.
75          * 
76          * Interpret the URI string to figure out what is needed to send
77          */
78                  
79         case LWS_CALLBACK_HTTP:
80
81                 uri = libwebsocket_get_uri(wsi);
82                 if (uri && strcmp(uri, "/favicon.ico") == 0) {
83                         if (libwebsockets_serve_http_file(wsi, "./favicon.ico",
84                                                                 "image/x-icon"))
85                                 fprintf(stderr, "Failed to send favicon\n");
86                         break;
87                 }
88                 
89                 /* send the script... when it runs it'll start websockets */
90
91                 if (libwebsockets_serve_http_file(wsi, "./test.html",
92                                                                    "text/html"))
93                         fprintf(stderr, "Failed to send HTTP file\n");
94
95                 break;
96         }
97
98         return 0;
99 }
100
101 static struct option options[] = {
102         { "help",       no_argument, NULL, 'h' },
103         { "port",       required_argument, NULL, 'p' },
104         { "protocol",   required_argument, NULL, 'r' },
105         { NULL, 0, 0, 0 }
106 };
107
108 int main(int argc, char **argv)
109 {
110         int n = 0;
111
112         fprintf(stderr, "libwebsockets test server\n"
113                         "Copyright 2010 Andy Green <andy@warmcat.com> "
114                                                        "licensed under GPL2\n");
115         
116         while (n >= 0) {
117                 n = getopt_long(argc, argv, "hp:r:", options, NULL);
118                 if (n < 0)
119                         continue;
120                 switch (n) {
121                 case 'p':
122                         port = atoi(optarg);
123                         break;
124                 case 'r':
125                         ws_protocol = atoi(optarg);
126                         break;
127                 case 'h':
128                         fprintf(stderr, "Usage: test-server "
129                                              "[--port=<p>] [--protocol=<v>]\n");
130                         exit(1);
131                 }
132         }
133         
134         if (libwebsocket_create_server(port, websocket_callback, ws_protocol) <
135                                                                             0) {
136                 fprintf(stderr, "libwebsocket init failed\n");
137                 return -1;
138         }
139         
140         /* just sit there until killed */
141                 
142         while (1)
143                 sleep(10);
144
145         return 0;
146 }