mod_statistics/prosodytop.lua
changeset 1343 7dbde05b48a9
parent 1085 56fc7a86eb20
child 1585 64f55bcab9f7
equal deleted inserted replaced
1342:0ae065453dc9 1343:7dbde05b48a9
    12 	local view = top.new({
    12 	local view = top.new({
    13 		stdscr = stdscr;
    13 		stdscr = stdscr;
    14 		prosody = { up_since = os.time() };
    14 		prosody = { up_since = os.time() };
    15 		conn_list = {};
    15 		conn_list = {};
    16 	});
    16 	});
    17 	
    17 
    18 	timer.add_task(0.01, function ()
    18 	timer.add_task(0.01, function ()
    19 		local ch = stdscr:getch();
    19 		local ch = stdscr:getch();
    20 		if ch then
    20 		if ch then
    21 			if stdscr:getch() == 410 then
    21 			if stdscr:getch() == 410 then
    22 				view:resized();
    22 				view:resized();
    24 				curses.ungetch(ch);
    24 				curses.ungetch(ch);
    25 			end
    25 			end
    26 		end
    26 		end
    27 		return 0.2;
    27 		return 0.2;
    28 	end);
    28 	end);
    29 	
    29 
    30 	timer.add_task(0, function ()
    30 	timer.add_task(0, function ()
    31 		view:draw();
    31 		view:draw();
    32 		return 1;
    32 		return 1;
    33 	end);
    33 	end);
    34 	
    34 
    35 	--[[
    35 	--[[
    36 	posix.signal(28, function ()
    36 	posix.signal(28, function ()
    37 		table.insert(view.conn_list, { jid = "WINCH" });
    37 		table.insert(view.conn_list, { jid = "WINCH" });
    38 		--view:draw();
    38 		--view:draw();
    39 	end);
    39 	end);
    40 	]]
    40 	]]
    41 	
    41 
    42 	-- Fake socket object around stdin
    42 	-- Fake socket object around stdin
    43 	local stdin = {
    43 	local stdin = {
    44         	getfd = function () return 0; end;
    44         	getfd = function () return 0; end;
    45         	dirty = function (self) return false; end;
    45         	dirty = function (self) return false; end;
    46         	settimeout = function () end;
    46         	settimeout = function () end;
    63 		if text:lower() == "q" then os.exit(); end
    63 		if text:lower() == "q" then os.exit(); end
    64 	end
    64 	end
    65 	stdin = server.wrapclient(stdin, "stdin", 0, {
    65 	stdin = server.wrapclient(stdin, "stdin", 0, {
    66 		onincoming = on_incoming, ondisconnect = function () end
    66 		onincoming = on_incoming, ondisconnect = function () end
    67 	}, "*a");
    67 	}, "*a");
    68 	
    68 
    69 	local function handle_line(line)
    69 	local function handle_line(line)
    70 		local e = {
    70 		local e = {
    71 			STAT = function (name) return function (value)
    71 			STAT = function (name) return function (value)
    72 				view:update_stat(name, value);
    72 				view:update_stat(name, value);
    73 			end end;
    73 			end end;
    77 		};
    77 		};
    78 		local chunk = assert(loadstring(line));
    78 		local chunk = assert(loadstring(line));
    79 		setfenv(chunk, e);
    79 		setfenv(chunk, e);
    80 		chunk();
    80 		chunk();
    81 	end
    81 	end
    82 	
    82 
    83 	local stats_listener = {};
    83 	local stats_listener = {};
    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 		if partial then
    93 			partial, data = nil, partial..data;
    93 			partial, data = nil, partial..data;
   105 			partial = nil;
   105 			partial = nil;
   106 		else
   106 		else
   107 			partial = data:sub(lastpos);
   107 			partial = data:sub(lastpos);
   108 		end
   108 		end
   109 	end
   109 	end
   110 	
   110 
   111 	function stats_listener.ondisconnect(conn, err)
   111 	function stats_listener.ondisconnect(conn, err)
   112 		stdscr:mvaddstr(6, 0, "DISCONNECTED: "..(err or "unknown"));
   112 		stdscr:mvaddstr(6, 0, "DISCONNECTED: "..(err or "unknown"));
   113 	end
   113 	end
   114 	
   114 
   115 	local conn = require "socket".tcp();
   115 	local conn = require "socket".tcp();
   116 	assert(conn:connect("localhost", 5782));
   116 	assert(conn:connect("localhost", 5782));
   117 	handler = server.wrapclient(conn, "localhost", 5279, stats_listener, "*a");
   117 	handler = server.wrapclient(conn, "localhost", 5279, stats_listener, "*a");
   118 end
   118 end
   119 
   119