comparison mod_statistics/prosodytop.lua @ 1585:64f55bcab9f7

mod_statistics/prosodytop.lua: Simplify and fix buffering and line separation (thanks Ge0rG)
author Kim Alvefur <zash@zash.se>
date Mon, 08 Dec 2014 17:04:49 +0100
parents 7dbde05b48a9
children 54b93cf0f631
comparison
equal deleted inserted replaced
1584:4713f9d453ec 1585:64f55bcab9f7
84 84
85 function stats_listener.onconnect(conn) 85 function stats_listener.onconnect(conn)
86 --stdscr:mvaddstr(6, 0, "CONNECTED"); 86 --stdscr:mvaddstr(6, 0, "CONNECTED");
87 end 87 end
88 88
89 local partial; 89 local partial = "";
90 function stats_listener.onincoming(conn, data) 90 function stats_listener.onincoming(conn, data)
91 --print("DATA", data) 91 --print("DATA", data)
92 if partial then 92 data = partial..data;
93 partial, data = nil, partial..data;
94 end
95 if not data:match("\n") then
96 partial = data;
97 return;
98 end
99 local lastpos = 1; 93 local lastpos = 1;
100 for line, pos in data:gmatch("([^\n]+)\n()") do 94 for line, pos in data:gmatch("([^\n]+)\n()") do
101 lastpos = pos; 95 lastpos = pos;
102 handle_line(line); 96 handle_line(line);
103 end 97 end
104 if lastpos == #data then 98 partial = data:sub(lastpos);
105 partial = nil;
106 else
107 partial = data:sub(lastpos);
108 end
109 end 99 end
110 100
111 function stats_listener.ondisconnect(conn, err) 101 function stats_listener.ondisconnect(conn, err)
112 stdscr:mvaddstr(6, 0, "DISCONNECTED: "..(err or "unknown")); 102 stdscr:mvaddstr(6, 0, "DISCONNECTED: "..(err or "unknown"));
113 end 103 end