tmm1 (owner)

Revisions

  • 2a71f2 tmm1 Thu Jul 09 15:27:41 -0700 2009
gist: 144052 Download_button fork
public
Description:
test to see if a ip/port is valid using EM
Public Clone URL: git://gist.github.com/144052.git
Embed All Files: show embed
port_checker.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require 'rubygems'
require 'eventmachine'
 
module PortTester
  def initialize blk
    @blk = blk
    @timeout = EM::Timer.new(2){ close_connection }
  end
  def connection_completed
    @blk.call(true) if @blk
    @blk = nil
    @timeout.cancel
    close_connection
  end
  def unbind
    @blk.call(false) if @blk
    @blk = nil
  end
  def self.test ip, port, &blk
    EM.connect ip, port, self, blk
  end
end
 
EM.run{
  PortTester.test('google.com', 80) do |open|
    p open
  end
}