Thu, 25 Dec 2014 10:48:06 +0000
Initial commit. Tortoises are fun.
0
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | local socket = require "socket"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local server = require "net.server_select"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local http_server = require"net.http.server"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | local log = require "util.logger".init("clients"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | local response_head = table.concat({ |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | "HTTP/1.1 200 OK"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | "Max-Age: 0"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | "Expires: 0"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | "Cache-Control: no-cache, private"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | "Pragma: no-cache"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | "Content-Type: multipart/x-mixed-replace; boundary=--BoundaryString"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | "Keep-Alive: timeout=5, max=99"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | "Connection: Keep-Alive"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | "Transfer-Encoding: chunked"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | ""; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | ""; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | }, "\r\n"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | local listener = { onconnect = function () end; onincoming = function () end; } |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | local last_chunk; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | local clients = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | -- Called when a HTTP stream client closes |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | function listener.ondisconnect(conn) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | clients[conn] = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | if not next(clients) then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | log("debug", "No more clients"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | events.fire_event("no-clients"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | listener.ondetach = listener.ondisconnect; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | function handle_request(event, path) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | local path = event.request.url.path; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | if path ~= "/cam" then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | return 404; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | if not next(clients) then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | log("debug", "Have clients now"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | events.fire_event("have-clients"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | local conn = event.response.conn; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | conn:write(response_head); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | clients[conn] = true; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | if last_chunk then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | conn:write(last_chunk); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | conn:setlistener(listener); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | return true; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | events.add_handler("image-changed", function (event) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | log("debug", "New image"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | local chunk_data = table.concat({ |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | "--BoundaryString", |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | "Content-Type: image/jpeg"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | "Content-Length: "..#event.image; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | ""; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | event.image; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | }, "\r\n"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | last_chunk = ("%x\r\n%s\r\n"):format(#chunk_data, chunk_data); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | for client in pairs(clients) do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | client:write(last_chunk); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | end); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | http_server.add_host("localhost"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | http_server.set_default_host("localhost"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | http_server.add_handler("GET localhost/*", handle_request); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | http_server.listen_on(8006); |