Class: FFWD::TCP::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ffwd/protocol/tcp/connection.rb

Constant Summary collapse

INITIAL_TIMEOUT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, host, port, handler, args, outbound_limit) ⇒ Connection



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ffwd/protocol/tcp/connection.rb', line 22

def initialize log, host, port, handler, args, outbound_limit
  @log = log
  @host = host
  @port = port
  @handler = handler
  @args = args
  @outbound_limit = outbound_limit

  @peer = "#{host}:#{port}"
  @closing = false
  @reconnect_timeout = INITIAL_TIMEOUT
  @reporter_meta = {:type => @handler.plugin_type, :peer => peer}

  @timer = nil
  @c = nil
  @open = false
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



20
21
22
# File 'lib/ffwd/protocol/tcp/connection.rb', line 20

def log
  @log
end

#peerObject (readonly)

Returns the value of attribute peer.



20
21
22
# File 'lib/ffwd/protocol/tcp/connection.rb', line 20

def peer
  @peer
end

#reporter_metaObject (readonly)

Returns the value of attribute reporter_meta.



20
21
22
# File 'lib/ffwd/protocol/tcp/connection.rb', line 20

def reporter_meta
  @reporter_meta
end

Instance Method Details

#connectObject

Start attempting to connect.



41
42
43
44
# File 'lib/ffwd/protocol/tcp/connection.rb', line 41

def connect
  log.info "Connecting to tcp://#{@host}:#{@port}"
  @c = EM.connect @host, @port, @handler, self, *@args
end

#connection_completedObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/ffwd/protocol/tcp/connection.rb', line 69

def connection_completed
  @open = true
  @log.info "Connected tcp://#{peer}"
  @reconnect_timeout = INITIAL_TIMEOUT

  unless @timer.nil?
    @timer.cancel
    @timer = nil
  end
end

#disconnectObject

Explicitly disconnect and discard any reconnect attempts..



47
48
49
50
51
52
53
54
55
# File 'lib/ffwd/protocol/tcp/connection.rb', line 47

def disconnect
  log.info "Disconnecting from tcp://#{@host}:#{@port}"
  @closing = true

  @c.close_connection if @c
  @timer.cancel if @timer
  @c = nil
  @timer = nil
end

#send_all(events, metrics) ⇒ Object



65
66
67
# File 'lib/ffwd/protocol/tcp/connection.rb', line 65

def send_all events, metrics
  @c.send_all events, metrics
end

#send_event(event) ⇒ Object



57
58
59
# File 'lib/ffwd/protocol/tcp/connection.rb', line 57

def send_event event
  @c.send_event event
end

#send_metric(metric) ⇒ Object



61
62
63
# File 'lib/ffwd/protocol/tcp/connection.rb', line 61

def send_metric metric
  @c.send_metric metric
end

#unbindObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ffwd/protocol/tcp/connection.rb', line 80

def unbind
  @open = false
  @c = nil

  if @closing
    return
  end

  @log.info "Disconnected from tcp://#{peer}, reconnecting in #{@reconnect_timeout}s"

  unless @timer.nil?
    @timer.cancel
    @timer = nil
  end

  @timer = EM::Timer.new(@reconnect_timeout) do
    @reconnect_timeout *= 2
    @timer = nil
    @c = EM.connect @host, @port, @handler, self, *@args
  end
end

#writable?Boolean

Check if a connection is writable or not.



103
104
105
# File 'lib/ffwd/protocol/tcp/connection.rb', line 103

def writable?
  not @c.nil? and @open and @c.get_outbound_data_size < @outbound_limit
end