Class: Cosmos::Protocol

Inherits:
Object show all
Defined in:
lib/cosmos/interfaces/protocols/protocol.rb

Overview

Base class for all COSMOS protocols which defines a framework which must be implemented by a subclass.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allow_empty_data = nil) ⇒ Protocol

to be passed down to later Protocols (instead of returning :STOP). Can be true, false, or nil, where nil is interpreted as true unless the Protocol is the last Protocol of the chain.

Parameters:

  • allow_empty_data (true/false/nil) (defaults to: nil)

    Whether or not this protocol will allow an empty string



33
34
35
36
37
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 33

def initialize(allow_empty_data = nil)
  @interface = nil
  @allow_empty_data = ConfigParser.handle_true_false_nil(allow_empty_data)
  reset()
end

Instance Attribute Details

#allow_empty_dataObject

Returns the value of attribute allow_empty_data.



28
29
30
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 28

def allow_empty_data
  @allow_empty_data
end

#interfaceObject

Returns the value of attribute interface.



27
28
29
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 27

def interface
  @interface
end

Instance Method Details

#connect_resetObject



42
43
44
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 42

def connect_reset
  reset()
end

#disconnect_resetObject



46
47
48
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 46

def disconnect_reset
  reset()
end

#post_write_interface(packet, data) ⇒ Object



78
79
80
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 78

def post_write_interface(packet, data)
  return packet, data
end

#read_data(data) ⇒ Object

Ensure we have some data in case this is the only protocol



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 51

def read_data(data)
  if data.length <= 0
    if @allow_empty_data.nil?
      if @interface and @interface.read_protocols[-1] == self
        # Last read interface in chain with auto @allow_empty_data
        return :STOP
      end
    elsif !@allow_empty_data
      # Don't @allow_empty_data means STOP
      return :STOP
    end
  end
  data
end

#read_packet(packet) ⇒ Object



66
67
68
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 66

def read_packet(packet)
  return packet
end

#resetObject



39
40
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 39

def reset
end

#write_data(data) ⇒ Object



74
75
76
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 74

def write_data(data)
  return data
end

#write_packet(packet) ⇒ Object



70
71
72
# File 'lib/cosmos/interfaces/protocols/protocol.rb', line 70

def write_packet(packet)
  return packet
end