Class: Loghandler::Client

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/loghandler/client.rb

Class Method Summary collapse

Instance Method Summary collapse

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(options)
  EM.run do
    # EM.error_handler{ |e|
    #   puts "Error raised during event loop: #{e.message} "
    # }
    Signal.trap("INT") { EM.stop }
    options.merge!({max_reconnect:10})
    options.merge!({uid:SecureRandom.hex(10)}) if options[:uid].nil?
    channel = EventMachine::Channel.new
    
    client = EventMachine.connect options[:url], options[:port].to_i, Loghandler::Client, options
    # TODO : move into Loghandler::Client
    channel.subscribe do |data|
        to_send={content:data,logged_at:Time.now}.merge(options)
        client.send_data "#{to_send.to_json}\n"
    end
    Loghandler::Tailer.new(options,channel)
  end
end

Instance Method Details

#connection_completedObject



24
25
26
# File 'lib/loghandler/client.rb', line 24

def connection_completed
  @reconnect_try = 0
end

#hostnameObject



9
10
11
# File 'lib/loghandler/client.rb', line 9

def hostname
  Socket.gethostname
end

#local_ipObject



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

#unbindObject



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