|
| 1 | +local trpeer = require"loadconf".trpeer |
| 2 | +assert("string" == type(trpeer), "trpeer must be set in mako.conf") |
| 3 | +assert("localhost" ~= trpeer and "127.0.0.1" ~= trpeer, "Can't use 'localhost'") |
| 4 | + |
| 5 | +local sfmt,sbyte=string.format,string.byte |
| 6 | +local start -- function set below |
| 7 | + |
| 8 | +-- Must match C code |
| 9 | +local TR_set <const> = 1 |
| 10 | +local TR_data <const> = 2 |
| 11 | +local TR_acquired <const> = 3 |
| 12 | + |
| 13 | +local function log(msg) |
| 14 | + tracep(false,0,sfmt("[%s] %s",ba.datetime"NOW",msg)) |
| 15 | +end |
| 16 | + |
| 17 | +local function notTR() |
| 18 | + log"The peer does not appear to be a TraceLogger" |
| 19 | +end |
| 20 | + |
| 21 | +local function restart() |
| 22 | + ba.timer(function() start(restart) end):set(1000,true) |
| 23 | +end |
| 24 | + |
| 25 | +local function traceLoggerReceiver(s) |
| 26 | + local data,err = s:read(2000) |
| 27 | + if not data or sbyte(data) ~= TR_set then |
| 28 | + notTR() |
| 29 | + return |
| 30 | + end |
| 31 | + s:setoption("keepalive",true,15,5) |
| 32 | + local tinsert,tconcat=table.insert,table.concat |
| 33 | + local collected |
| 34 | + while true do |
| 35 | + data,err = s:read() |
| 36 | + if not data then break end |
| 37 | + local cmd=sbyte(data) |
| 38 | + if TR_data == cmd then |
| 39 | + data=data:sub(2) |
| 40 | + if #data > 0 then |
| 41 | + local eol = 10 == sbyte(data,#data) |
| 42 | + if eol then |
| 43 | + if collected then |
| 44 | + tinsert(collected,data) |
| 45 | + data=tconcat(collected) |
| 46 | + collected=nil |
| 47 | + end |
| 48 | + log(data:sub(1,-2)) |
| 49 | + elseif eol then |
| 50 | + log(data:sub(1,-2)) |
| 51 | + else |
| 52 | + if not collected then collected={} end |
| 53 | + tinsert(collected,data) |
| 54 | + end |
| 55 | + elseif TR_acquired == cmd then |
| 56 | + log"Connection closed by another client" |
| 57 | + return |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + if err and "sysshutdown" ~= err then |
| 62 | + log("Socket read failed "..tostring(err)) |
| 63 | + end |
| 64 | + restart() |
| 65 | +end |
| 66 | + |
| 67 | +start=function(callback) |
| 68 | + ba.thread.run(function() |
| 69 | + local s,ok,err |
| 70 | + local c = require"httpc".create() |
| 71 | + local url="http://"..trpeer.."/rtl/tracelogger.service/" |
| 72 | + ok,err=c:request{url=url,header={["x-requested-with"]="Lua Client"}} |
| 73 | + if ok then |
| 74 | + local status=c:status() |
| 75 | + if status == 204 then |
| 76 | + c:close() |
| 77 | + url="ws://"..trpeer.."/rtl/tracelogger.service/" |
| 78 | + ok,err=c:request{url=url} |
| 79 | + if ok then |
| 80 | + s,err=ba.socket.http2sock(c) |
| 81 | + if s then |
| 82 | + log("Connected to "..url) |
| 83 | + s:event(traceLoggerReceiver, "s") |
| 84 | + return true |
| 85 | + end |
| 86 | + end |
| 87 | + elseif status == 503 then |
| 88 | + log"The TraceLogger is serving another client" |
| 89 | + return |
| 90 | + elseif status == 401 then |
| 91 | + log"Server requires authentication, but it's not implemented" |
| 92 | + return |
| 93 | + else |
| 94 | + notTR() |
| 95 | + return |
| 96 | + end |
| 97 | + end |
| 98 | + log(sfmt("Cannot connect to %s: %s",url, err)) |
| 99 | + if callback then callback() end |
| 100 | + end) |
| 101 | +end |
| 102 | + |
| 103 | +start() |
| 104 | + |
| 105 | + |
| 106 | + |
0 commit comments