Class: Swiftcore::Analogger::ClientProtocol

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/swiftcore/Analogger/EMClient.rb

Constant Summary collapse

Cauthentication =
'authentication'.freeze
Ci =
'i'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectedObject

Returns the value of attribute connected.



18
19
20
# File 'lib/swiftcore/Analogger/EMClient.rb', line 18

def connected
  @connected
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/swiftcore/Analogger/EMClient.rb', line 18

def host
  @host
end

#keyObject

Returns the value of attribute key.



18
19
20
# File 'lib/swiftcore/Analogger/EMClient.rb', line 18

def key
  @key
end

#msg_queueObject

Returns the value of attribute msg_queue.



18
19
20
# File 'lib/swiftcore/Analogger/EMClient.rb', line 18

def msg_queue
  @msg_queue
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/swiftcore/Analogger/EMClient.rb', line 18

def port
  @port
end

#senderObject

Returns the value of attribute sender.



18
19
20
# File 'lib/swiftcore/Analogger/EMClient.rb', line 18

def sender
  @sender
end

Class Method Details

.connect(service = 'default', host = '127.0.0.1', port = 6766, key = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/swiftcore/Analogger/EMClient.rb', line 20

def self.connect(service = 'default', host = '127.0.0.1', port = 6766, key = nil)
  connection = ::EventMachine.connect(host, port.to_i, self) do |conn|
    conn.connected = false
    conn.msg_queue ||= ''
    conn.service = service
    conn.host = host
    conn.port = port
    conn.key = key
  end
end

Instance Method Details

#closeObject



56
57
58
# File 'lib/swiftcore/Analogger/EMClient.rb', line 56

def close
  close_connection_after_writing
end

#closed?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/swiftcore/Analogger/EMClient.rb', line 60

def closed?
  @connected
end

#connection_completedObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/swiftcore/Analogger/EMClient.rb', line 31

def connection_completed
  @connected = true
  pos = 0
  log(Cauthentication,"#{@key}",true)
  send_data @msg_queue
#       @sender = EM::Timer.new(1) {send_data @msg_queue if @connected; @msg_queue = ''}
#       while @msg_queue.length > pos
#       msg = @msg_queue[pos]
#       pos += 1
#       break unless log(*msg)
#     end
#     if pos > 0
#       @msg_queue.slice!(0..(pos - 1))
#     end
end

#log(severity, msg, immediate = false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/swiftcore/Analogger/EMClient.rb', line 70

def log(severity,msg,immediate=false)
  len = [@service_length + severity.length + msg.length + 3].pack(Ci)
  fullmsg = "#{len}#{len}:#{@service}:#{severity}:#{msg}"
  if immediate
    send_data fullmsg
  elsif @connected
    send_data fullmsg
  else
    @msg_queue << fullmsg
  end
  #if @connected
    #send_data "#{len}#{len}:#{@service}:#{severity}:#{msg}"
  #else
  # @msg_queue << fullmsg
  # false
  #end
rescue Exception => e
  puts e
  @msg_queue << fullmsg if msg and severity
  false
end

#serviceObject



47
48
49
# File 'lib/swiftcore/Analogger/EMClient.rb', line 47

def service
  @service
end

#service=(val) ⇒ Object



51
52
53
54
# File 'lib/swiftcore/Analogger/EMClient.rb', line 51

def service=(val)
  @service = val
  @service_length = val.length
end

#unbindObject



64
65
66
67
68
# File 'lib/swiftcore/Analogger/EMClient.rb', line 64

def unbind
  @connected = false
  #@sender.cancel
  ::EventMachine.add_timer(rand(2)) {self.class.connect(@service, @host, @port, @key)}
end