Class: LIS::Transfer::ASTM::E1394

Inherits:
PacketIO::Base
  • Object
show all
Defined in:
lib/lis/transfer/astm_e1394.rb

Overview

splits a stream into lis packets and only lets packets through that are inside a session delimited by ENQ .. EOT

checks the checksum and does 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

EOT =
"\004"
ENQ =
"\005"
ACK =
"\006"
NAK =
"\025"
RX =

format of a message

/(?:
  \004 | # EOT - ends a transaction
  \005 | # ENQ - start a transaction
  \006 | # ACK
  \025 | # NAK
  (?:\002 (.) (.*?) \015 \003 (.+?) \015 \012) # a message with a checksum
        #  |   |               `-- checksum
        #  |   `------------------ message
        #  `---------------------- frame number
  )
/xm

Instance Method Summary (collapse)

Constructor Details

- (E1394) initialize(*args)

Returns a new instance of E1394



37
38
39
40
41
# File 'lib/lis/transfer/astm_e1394.rb', line 37

def initialize(*args)
  super(*args)
  @data = StringScanner.new("")
  @inside_transmission = false
end

Instance Method Details

- (Object) receive(data)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lis/transfer/astm_e1394.rb', line 43

def receive(data)
  @data.concat(data)
  while @data.scan_until(RX)
    match = @data.matched
    case match
      when ENQ then transmission_start
      when EOT then transmission_end
      when ACK, NAK then nil
    else
      received_message(match)
      write :ack
    end
  end
  nil
end

- (Object) write(type, data = nil)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lis/transfer/astm_e1394.rb', line 59

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.wrap_message(data, @frame_number)
  else
    raise ArgumentError
  end
  super(str)
end