Class: Einhorn::Event::Connection

Inherits:
AbstractTextDescriptor show all
Includes:
Persistent
Defined in:
lib/einhorn/event/connection.rb

Instance Attribute Summary

Attributes inherited from AbstractTextDescriptor

#client_id, #read_buffer, #write_buffer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistent

included, persistent?

Methods inherited from AbstractTextDescriptor

#close, #log_debug, #log_error, #log_info, #notify_readable, #notify_writeable, open, #process_read_buffer, #read, #to_io, #write, #write_pending?

Constructor Details

#initialize(*args) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
# File 'lib/einhorn/event/connection.rb', line 5

def initialize(*args)
  @subscriptions = {}
  super
end

Class Method Details

.from_state(state) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/einhorn/event/connection.rb', line 32

def self.from_state(state)
  fd = state[:socket]
  socket = Socket.for_fd(fd)
  conn = self.open(socket)
  conn.read_buffer = state[:read_buffer] if state[:read_buffer]
  conn.write_buffer = state[:write_buffer] if state[:write_buffer]
  # subscriptions could be empty if upgrading from an older version of einhorn
  state.fetch(:subscriptions, {}).each do |tag, id|
    conn.subscribe(tag, id)
  end
  conn
end

Instance Method Details

#consume_record(command) ⇒ Object



19
20
21
# File 'lib/einhorn/event/connection.rb', line 19

def consume_record(command)
  Einhorn::Command::Interface.process_command(self, command)
end

#deregister!Object



65
66
67
68
69
# File 'lib/einhorn/event/connection.rb', line 65

def deregister!
  log_debug("client disconnected") if Einhorn::TransientState.whatami == :master
  Einhorn::Event.deregister_connection(@socket.fileno)
  super
end

#parse_recordObject



10
11
12
13
14
15
16
17
# File 'lib/einhorn/event/connection.rb', line 10

def parse_record
  split = @read_buffer.split("\n", 2)
  if split.length > 1
    split
  else
    nil
  end
end

#register!Object



59
60
61
62
63
# File 'lib/einhorn/event/connection.rb', line 59

def register!
  log_debug("client connected")
  Einhorn::Event.register_connection(self, @socket.fileno)
  super
end

#subscribe(tag, request_id) ⇒ Object



45
46
47
48
49
# File 'lib/einhorn/event/connection.rb', line 45

def subscribe(tag, request_id)
  if request_id
    @subscriptions[tag] = request_id
  end
end

#subscription(tag) ⇒ Object



51
52
53
# File 'lib/einhorn/event/connection.rb', line 51

def subscription(tag)
  @subscriptions[tag]
end

#to_stateObject



23
24
25
26
27
28
29
30
# File 'lib/einhorn/event/connection.rb', line 23

def to_state
  state = {:class => self.class.to_s, :socket => @socket.fileno}
  # Don't include by default because it's not that pretty
  state[:read_buffer] = @read_buffer if @read_buffer.length > 0
  state[:write_buffer] = @write_buffer if @write_buffer.length > 0
  state[:subscriptions] = @subscriptions
  state
end

#unsubscribe(tag) ⇒ Object



55
56
57
# File 'lib/einhorn/event/connection.rb', line 55

def unsubscribe(tag)
  @subscriptions.delete(tag)
end