File

plugins/mod_selftests.lua @ 397:d07ae6788196

Backed out the backout. Now we're back out.
author Matthew Wild <mwild1@gmail.com>
date Sun, 23 Nov 2008 05:47:55 +0000
parent 375:a6a4ea3633b0
child 428:e717ecc97d2e
line wrap: on
line source


local st = require "util.stanza";
local register_component = require "core.componentmanager".register_component;
local core_route_stanza = core_route_stanza;
local socket = require "socket";
local config = require "core.configmanager";
local ping_hosts = config.get("*", "mod_selftests", "ping_hosts") or { "jabber.org" };

local open_pings = {};

local t_insert = table.insert;

local log = require "util.logger".init("mod_selftests");

local tests_jid = "self_tests@getjabber.ath.cx";
local host = "getjabber.ath.cx";

if not (tests_jid and host) then
	for currhost in pairs(host) do
		if currhost ~= "localhost" then
			tests_jid, host = "self_tests@"..currhost, currhost;
		end
	end
end

if tests_jid and host then
	local bot = register_component(tests_jid, 	function(origin, stanza, ourhost)
										local time = open_pings[stanza.attr.id];
										
										if time then
											log("info", "Ping reply from %s in %fs", tostring(stanza.attr.from), socket.gettime() - time);
										else
											log("info", "Unexpected reply: %s", stanza:pretty_print());
										end
									end);


	local our_origin = hosts[host];
	add_event_hook("server-started", 
					function ()
						local id = st.new_id();
						local ping_attr = { xmlns = 'urn:xmpp:ping' };
						local function send_ping(to)
							log("info", "Sending ping to %s", to);
							core_route_stanza(our_origin, st.iq{ to = to, from = tests_jid, id = id, type = "get" }:tag("ping", ping_attr));
							open_pings[id] = socket.gettime();
						end
						
						for _, host in ipairs(ping_hosts) do
							send_ping(host);
						end
					end);
end