Class: LIS::Transfer::PacketizedProtocol
- Inherits:
-
Base
- Object
- Base
- LIS::Transfer::PacketizedProtocol
- Defined in:
- lib/lis/packetized_protocol.rb
Overview
splits a stream into lis packets and only lets packets through that are inside a session delimited by ENQ .. EOT
check the checksum and do acknowledgement of messages
forwards the following events:
:message, String
when a message is received
:idle
when a transmission is finished (after EOT is received)
Constant Summary
- ACK =
"\006"- NAK =
"\025"- ENQ =
"\005"- EOT =
"\004"- RX =
format of a message
/(?: \005 | # ENQ - start a transaction \004 | # EOT - ends a transaction \005 | # ACK \025 | # NAK (?:\002 (.) (.*?) \015 \003 (.+?) \015 \012) # a message with a checksum # | | `-- checksum # | `------------------ message # `---------------------- frame number ) /xm
Instance Method Summary (collapse)
-
- (PacketizedProtocol) initialize(*args)
constructor
A new instance of PacketizedProtocol.
- - (Object) receive(data)
- - (Object) write(type, data = nil)
Methods inherited from Base
Constructor Details
- (PacketizedProtocol) initialize(*args)
A new instance of PacketizedProtocol
33 34 35 36 37 |
# File 'lib/lis/packetized_protocol.rb', line 33 def initialize(*args) super(*args) @memo = "" @inside_transmission = false end |
Instance Method Details
- (Object) receive(data)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lis/packetized_protocol.rb', line 39 def receive(data) scanner = StringScanner.new(@memo + data) while scanner.scan_until(RX) match = scanner.matched case match when ENQ then transmission_start when EOT then transmission_end when ACK, NAK then nil else (match) write :ack end end @memo = scanner.rest nil end |
- (Object) write(type, data = nil)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lis/packetized_protocol.rb', line 56 def write(type, data = nil) str = case type when :ack then ACK when :nak then NAK when :begin then @frame_number = 0 ENQ when :idle then EOT when :message then @frame_number = (@frame_number + 1) % 8 self.class.(data, @frame_number) else raise ArgumentError end super(str) end |