module ClientHelper require 'socket' def host_for_client(client) host = CLIENT_MAP[client] or client.gsub(/-fd$/, '') end def check_interminably(host, port) begin s = TCPSocket.new(host, port) s.close s ? "okay.png" : "error.png" rescue SocketError "dunno.png" rescue "error.png" end end def client_check(host, port = 9102) t = Thread.new { check_interminably(host, port) } t.join(5) ? t.value : "error.png" end def client_image @client = Client.find(params[:id]) host = host_for_client(@client.name) @image = client_check(host) end def check_tag(cid) if cid == 0 content_tag('td', content_tag('a', "OK?", :href => "#", :onclick => "return check_through(#{@clients.size})")) else content_tag('td', content_tag('a', content_tag('div', image_tag('dunno.png', :class => "em1"), :class => "center", :id => "client-#{cid}"), :href => "#", :onclick => "return check(#{cid})")) end end end