Class: Cyc::Connection::ConnectionClient

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/cyc/connection/synchrony.rb

Instance Method Summary collapse

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/cyc/connection/synchrony.rb', line 21

def connected?
  @connected
end

#connection_completedObject



16
17
18
19
# File 'lib/cyc/connection/synchrony.rb', line 16

def connection_completed
  @connected = true
  succeed
end

#post_initObject



10
11
12
13
14
# File 'lib/cyc/connection/synchrony.rb', line 10

def post_init
  @req = []
  @connected = false
  @buffer = DataBuffer.new
end

#readObject



42
43
44
# File 'lib/cyc/connection/synchrony.rb', line 42

def read
  EventMachine::Synchrony.sync @req.last unless @req.empty?
end

#receive_data(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cyc/connection/synchrony.rb', line 25

def receive_data(data)
  @buffer << data

  begin
    while (result = @buffer.next_result(@req.first))
      unless @req.empty?
        msg, req = @req.shift(2)
        req.succeed(result << msg)
      end
    end
  rescue RuntimeError => err
    @req.each_slice(2) {|_, r| r.fail [:error, err]}
    @req.clear
    close_connection
  end
end

#send(data) ⇒ Object



46
47
48
49
50
# File 'lib/cyc/connection/synchrony.rb', line 46

def send(data)
  @req << data << EventMachine::DefaultDeferrable.new
  callback { send_data data + EOL }
  @req.last
end

#unbindObject



52
53
54
55
56
57
58
59
60
# File 'lib/cyc/connection/synchrony.rb', line 52

def unbind
  @connected = false
  @buffer.discard!
  unless @req.empty?
    @req.each_slice(2) {|_, r| r.fail [:error, Errno::ECONNRESET]}
    @req.clear
  end
  fail
end