]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/prometheus-cpp/3rdparty/civetweb/test/websocket2.lua
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / third_party / prometheus-cpp / 3rdparty / civetweb / test / websocket2.lua
1
2 function trace(text)
3 local f = io.open("websocket2.trace", "a")
4 f:write(os.date() .. " - " .. text .. "\n")
5 f:close()
6 end
7
8 function iswebsocket()
9 return mg.lua_type == "websocket"
10 end
11
12 trace("called with Lua type " .. tostring(mg.lua_type))
13
14 if not iswebsocket() then
15 trace("no websocket")
16 mg.write("HTTP/1.0 403 Forbidden\r\n")
17 mg.write("Connection: close\r\n")
18 mg.write("\r\n")
19 mg.write("forbidden")
20 return
21 end
22
23
24 -- Serialize table to string
25 function ser(val)
26 local t
27 if type(val) == "table" then
28 for k,v in pairs(val) do
29 if t then
30 t = t .. ", " .. ser(k) .. "=" .. ser(v)
31 else
32 t = "{" .. ser(k) .. "=" .. ser(v)
33 end
34 end
35 t = t .. "}"
36 else
37 t = tostring(val)
38 end
39 return t
40 end
41
42 -- table of all active connection
43 allConnections = {}
44
45 -- function to get a client identification string
46 function who(tab)
47 local ri = allConnections[tab.client].request_info
48 return ri.remote_addr .. ":" .. ri.remote_port
49 end
50
51 -- Callback to accept or reject a connection
52 function open(tab)
53 allConnections[tab.client] = tab
54 trace("open[" .. who(tab) .. "]: " .. ser(tab))
55 return true -- return true to accept the connection
56 end
57
58 -- Callback for "Websocket ready"
59 function ready(tab)
60 trace("ready[" .. who(tab) .. "]: " .. ser(tab))
61 mg.write(tab.client, "text", "Websocket ready")
62 mg.write(tab.client, 1, "-->h 180");
63 mg.write(tab.client, "-->m 180");
64 senddata()
65 mg.set_interval(timer, 1)
66 return true -- return true to keep the connection open
67 end
68
69 -- Callback for "Websocket received data"
70 function data(tab)
71 trace("data[" .. who(tab) .. "]: " .. ser(tab))
72 senddata()
73 return true -- return true to keep the connection open
74 end
75
76 -- Callback for "Websocket is closing"
77 function close(tab)
78 trace("close[" .. who(tab) .. "]: " .. ser(tab))
79 mg.write("text", "end")
80 allConnections[tab.client] = nil
81 end
82
83 function senddata()
84 local date = os.date('*t');
85 local hand = (date.hour%12)*60+date.min;
86
87 mg.write("text", string.format("%u:%02u:%02u", date.hour, date.min, date.sec));
88
89 if (hand ~= lasthand) then
90 mg.write(1, string.format("-->h %f", hand*360/(12*60)));
91 mg.write( string.format("-->m %f", date.min*360/60));
92 lasthand = hand;
93 end
94
95 if bits and content then
96 data(bits, content)
97 end
98 end
99
100 function timer()
101 trace("timer")
102 senddata()
103 return true -- return true to keep an interval timer running
104 end
105