Class: Loghandler::Client
- Inherits:
-
EM::Connection
- Object
- EM::Connection
- Loghandler::Client
- Defined in:
- lib/loghandler/client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #connection_completed ⇒ Object
- #hostname ⇒ Object
-
#initialize(args) ⇒ Client
constructor
A new instance of Client.
- #local_ip ⇒ Object
- #unbind ⇒ Object
Constructor Details
#initialize(args) ⇒ Client
Returns a new instance of Client.
3 4 5 6 7 |
# File 'lib/loghandler/client.rb', line 3 def initialize(args) super @options = args @reconnect_try = 0 end |
Class Method Details
.run(options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/loghandler/client.rb', line 34 def self.run() EM.run do # EM.error_handler{ |e| # puts "Error raised during event loop: #{e.message} " # } Signal.trap("INT") { EM.stop } .merge!({max_reconnect:10}) .merge!({uid:SecureRandom.hex(10)}) if [:uid].nil? channel = EventMachine::Channel.new client = EventMachine.connect [:url], [:port].to_i, Loghandler::Client, # TODO : move into Loghandler::Client channel.subscribe do |data| to_send={content:data,logged_at:Time.now}.merge() client.send_data "#{to_send.to_json}\n" end Loghandler::Tailer.new(,channel) end end |
Instance Method Details
#connection_completed ⇒ Object
24 25 26 |
# File 'lib/loghandler/client.rb', line 24 def connection_completed @reconnect_try = 0 end |
#hostname ⇒ Object
9 10 11 |
# File 'lib/loghandler/client.rb', line 9 def hostname Socket.gethostname end |
#local_ip ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/loghandler/client.rb', line 13 def local_ip orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily UDPSocket.open do |s| s.connect '64.233.187.99', 1 s.addr.last end ensure Socket.do_not_reverse_lookup = orig end |
#unbind ⇒ Object
28 29 30 31 32 |
# File 'lib/loghandler/client.rb', line 28 def unbind @reconnect_try+=1 raise "Connection Lost - Max Try Reconnect #{@options[:max_reconnect]}" if @reconnect_try > @options[:max_reconnect] EM.add_timer(1) do reconnect @options[:url], @options[:port] end end |