Module: Readapt::Adapter

Includes:
Backport::Adapter
Defined in:
lib/readapt/adapter.rb

Constant Summary collapse

@@debugger =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.host(debugger) ⇒ Object



11
12
13
# File 'lib/readapt/adapter.rb', line 11

def self.host debugger
  @@debugger = debugger
end

.procid=(pid) ⇒ Object



15
16
17
# File 'lib/readapt/adapter.rb', line 15

def self.procid= pid
  @@procid = pid
end

Instance Method Details

#close_messageObject



27
28
29
# File 'lib/readapt/adapter.rb', line 27

def close_message
  @@close_message ||= "</readapt-#{procid}>"
end

#closingObject



43
44
45
# File 'lib/readapt/adapter.rb', line 43

def closing
  @@debugger.delete_observer(self)
end

#format(result) ⇒ Object



31
32
33
# File 'lib/readapt/adapter.rb', line 31

def format result
  write_line result.to_protocol.to_json
end

#open_messageObject



23
24
25
# File 'lib/readapt/adapter.rb', line 23

def open_message
  @@open_message ||= "<readapt-#{procid}>"
end

#openingObject



35
36
37
38
39
40
41
# File 'lib/readapt/adapter.rb', line 35

def opening
  @@debugger.add_observer self
  @data_reader = DataReader.new
  @data_reader.set_message_handler do |message|
    process message
  end
end

#procidObject



19
20
21
# File 'lib/readapt/adapter.rb', line 19

def procid
  @@procid
end

#receiving(data) ⇒ Object



47
48
49
# File 'lib/readapt/adapter.rb', line 47

def receiving data
  @data_reader.receive data
end

#update(event, data) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/readapt/adapter.rb', line 51

def update event, data
  obj = {
    type: 'event',
    event: event
  }
  obj[:body] = data unless data.nil?
  json = obj.to_json
  envelope = "#{open_message}Content-Length: #{json.bytesize}\r\n\r\n#{json}#{close_message}"
  write envelope
  write "#{open_message}__TERMINATE__#{close_message}" if event == 'terminated'
end