Fix to expose openssl dependency
[platform/upstream/libwebsockets.git] / plugins / protocol_dumb_increment.c
1 /*
2  * ws protocol handler plugin for "dumb increment"
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * These test plugins are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20
21 #if !defined (LWS_PLUGIN_STATIC)
22 #define LWS_DLL
23 #define LWS_INTERNAL
24 #include "../lib/libwebsockets.h"
25 #endif
26
27 #include <string.h>
28
29 #if defined(LWS_WITH_ESP8266)
30 #define DUMB_PERIOD 50
31 #else
32 #define DUMB_PERIOD 50
33 #endif
34
35 struct per_vhost_data__dumb_increment {
36         uv_timer_t timeout_watcher;
37         struct lws_context *context;
38         struct lws_vhost *vhost;
39         const struct lws_protocols *protocol;
40 };
41
42 struct per_session_data__dumb_increment {
43         int number;
44 };
45
46 static void
47 uv_timeout_cb_dumb_increment(uv_timer_t *w
48 #if UV_VERSION_MAJOR == 0
49                 , int status
50 #endif
51 )
52 {
53         struct per_vhost_data__dumb_increment *vhd = lws_container_of(w,
54                         struct per_vhost_data__dumb_increment, timeout_watcher);
55         lws_callback_on_writable_all_protocol_vhost(vhd->vhost, vhd->protocol);
56 }
57
58 static int
59 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
60                         void *user, void *in, size_t len)
61 {
62         struct per_session_data__dumb_increment *pss =
63                         (struct per_session_data__dumb_increment *)user;
64         struct per_vhost_data__dumb_increment *vhd =
65                         (struct per_vhost_data__dumb_increment *)
66                         lws_protocol_vh_priv_get(lws_get_vhost(wsi),
67                                         lws_get_protocol(wsi));
68         unsigned char buf[LWS_PRE + 20];
69         unsigned char *p = &buf[LWS_PRE];
70         int n, m;
71
72         switch (reason) {
73         case LWS_CALLBACK_PROTOCOL_INIT:
74                 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
75                                 lws_get_protocol(wsi),
76                                 sizeof(struct per_vhost_data__dumb_increment));
77                 vhd->context = lws_get_context(wsi);
78                 vhd->protocol = lws_get_protocol(wsi);
79                 vhd->vhost = lws_get_vhost(wsi);
80
81                 uv_timer_init(lws_uv_getloop(vhd->context, 0),
82                               &vhd->timeout_watcher);
83                 uv_timer_start(&vhd->timeout_watcher,
84                                uv_timeout_cb_dumb_increment, DUMB_PERIOD, DUMB_PERIOD);
85
86                 break;
87
88         case LWS_CALLBACK_PROTOCOL_DESTROY:
89                 if (!vhd)
90                         break;
91                 lwsl_notice("di: LWS_CALLBACK_PROTOCOL_DESTROY: v=%p, ctx=%p\n", vhd, vhd->context);
92                 uv_timer_stop(&vhd->timeout_watcher);
93                 uv_close((uv_handle_t *)&vhd->timeout_watcher, NULL);
94                 break;
95
96         case LWS_CALLBACK_ESTABLISHED:
97                 pss->number = 0;
98                 break;
99
100         case LWS_CALLBACK_SERVER_WRITEABLE:
101                 n = lws_snprintf((char *)p, sizeof(buf) - LWS_PRE, "%d", pss->number++);
102                 m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
103                 if (m < n) {
104                         lwsl_err("ERROR %d writing to di socket\n", n);
105                         return -1;
106                 }
107                 break;
108
109         case LWS_CALLBACK_RECEIVE:
110                 if (len < 6)
111                         break;
112                 if (strcmp((const char *)in, "reset\n") == 0)
113                         pss->number = 0;
114                 if (strcmp((const char *)in, "closeme\n") == 0) {
115                         lwsl_notice("dumb_inc: closing as requested\n");
116                         lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
117                                          (unsigned char *)"seeya", 5);
118                         return -1;
119                 }
120                 break;
121
122         default:
123                 break;
124         }
125
126         return 0;
127 }
128
129 #define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
130         { \
131                 "dumb-increment-protocol", \
132                 callback_dumb_increment, \
133                 sizeof(struct per_session_data__dumb_increment), \
134                 10, /* rx buf size must be >= permessage-deflate rx size */ \
135         }
136
137 #if !defined (LWS_PLUGIN_STATIC)
138                 
139 static const struct lws_protocols protocols[] = {
140         LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT
141 };
142
143 LWS_EXTERN LWS_VISIBLE int
144 init_protocol_dumb_increment(struct lws_context *context,
145                              struct lws_plugin_capability *c)
146 {
147         if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
148                 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
149                          c->api_magic);
150                 return 1;
151         }
152
153         c->protocols = protocols;
154         c->count_protocols = ARRAY_SIZE(protocols);
155         c->extensions = NULL;
156         c->count_extensions = 0;
157
158         return 0;
159 }
160
161 LWS_EXTERN LWS_VISIBLE int
162 destroy_protocol_dumb_increment(struct lws_context *context)
163 {
164         return 0;
165 }
166
167 #endif