Class: Lignite::Connection::Tap

Inherits:
Lignite::Connection show all
Includes:
Bytes
Defined in:
lib/lignite/connection/tap.rb

Overview

An adapter that delegates to another connection and records the communication

Instance Method Summary collapse

Methods included from Bytes

#bin_to_hex, #f32, #hex_to_bin, #u16, #u32, #u8, #unpack_f32, #unpack_u16, #unpack_u32, #unpack_u8

Methods inherited from Lignite::Connection

create, #read, reset, #write

Methods included from Logger

default_logger, #logger

Constructor Details

#initialize(conn, filename) ⇒ Tap

Returns a new instance of Tap.



10
11
12
13
14
15
# File 'lib/lignite/connection/tap.rb', line 10

def initialize(conn, filename)
  raise "File #{filename} exists, will not overwrite" if File.exist?(filename)
  @conn = conn
  @filename = filename
  @packets = []
end

Instance Method Details

#closeObject



31
32
33
34
35
36
# File 'lib/lignite/connection/tap.rb', line 31

def close
  y = YAML.dump(@packets)
  File.write(@filename, y)
  super
  @conn.close
end

#receiveByteString

Returns a complete message.

Returns:



25
26
27
28
29
# File 'lib/lignite/connection/tap.rb', line 25

def receive
  s = @conn.receive
  @packets << { "RECV" => bin_to_hex(s) }
  s
end

#send(payload) ⇒ Object

Parameters:



18
19
20
21
22
# File 'lib/lignite/connection/tap.rb', line 18

def send(payload)
  r = @conn.send(payload)
  @packets << { "SEND" => bin_to_hex(payload) }
  r
end