Class: GMSEC::Message

Inherits:
Object
  • Object
show all
Extended by:
API
Defined in:
lib/gmsec/message.rb

Instance Method Summary collapse

Methods included from API

bind, extended, has

Instance Method Details

#<<(data) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/gmsec/message.rb', line 113

def <<(data)
  # We can append a single Field or multiple fields encapsulated as a Hash.
  if data.is_a? GMSEC::Field
    gmsec_MsgAddField(self, data, status)
  elsif data.is_a? Hash
    data.each do |key, value|
      self << GMSEC::Field.new(key, value)
    end
  elsif data
    raise TypeError.new("#{data.class} is not supported as a GMSEC field type.")
  end

  if status.is_error?
    raise RuntimeError.new("Error adding data to message: #{status}")
  end
end

#[](name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gmsec/message.rb', line 97

def [](name)
  field = GMSEC::Field.new
  gmsec_MsgGetField(self, name.to_s, field, status)

  case status.code
  when GMSEC_STATUS_NO_ERROR
    field.value
  when !GMSEC_INVALID_FIELD_NAME
    raise RuntimeError.new("Error getting message field: #{status}")
  end
end

#[]=(name, value) ⇒ Object



109
110
111
# File 'lib/gmsec/message.rb', line 109

def []=(name, value)
  self << GMSEC::Field.new(name, value)
end

#clear_field(name) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/gmsec/message.rb', line 89

def clear_field(name)
  gmsec_MsgClearField(self, name.to_s, status)

  if status.is_error?
    raise RuntimeError.new("Error clearing message field: #{status}")
  end
end

#clear_fieldsObject



81
82
83
84
85
86
87
# File 'lib/gmsec/message.rb', line 81

def clear_fields
  gmsec_MsgClearFields(self, status)

  if status.is_error?
    raise RuntimeError.new("Error clearing message fields: #{status}")
  end
end

#config=(config) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/gmsec/message.rb', line 73

def config=(config)
  gmsec_MsgSetConfig(self, config, status)

  if status.is_error?
    raise RuntimeError.new("Error setting message config: #{status}")
  end
end

#fieldsObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gmsec/message.rb', line 136

def fields
  Enumerator.new do |y|
    field = GMSEC::Field.new
    gmsec_MsgGetFirstField(self, field, status)

    while status.code != GMSEC_FIELDS_END_REACHED && status.code == GMSEC_STATUS_NO_ERROR
      y << field
      gmsec_MsgGetNextField(self, field, status)
    end

    unless status.code == GMSEC_FIELDS_END_REACHED
      raise RuntimeError.new("Error reading message fields: #{status}")
    end
  end
end

#from_xml(xml) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/gmsec/message.rb', line 170

def from_xml(xml)
  gmsec_MsgFromXML(self, xml, status).tap do |_|
    if status.is_error?
      raise RuntimeError.new("Error converting xml to message: #{status}")
    end
  end
end

#lengthObject



130
131
132
133
134
# File 'lib/gmsec/message.rb', line 130

def length
  pointer = FFI::MemoryPointer.new(find_type(:GMSEC_I32))
  gmsec_MsgGetFieldCount(self, pointer, status)
  pointer.read_int32
end

#load_fields_from_config_file(config_file, message_name) ⇒ Object



8
9
10
# File 'lib/gmsec/message.rb', line 8

def load_fields_from_config_file(config_file, message_name)
  config_file.get_message(message_name, message: self)
end

#sizeObject



178
179
180
181
182
# File 'lib/gmsec/message.rb', line 178

def size
  pointer = FFI::MemoryPointer.new(find_type(:GMSEC_U32))
  gmsec_MsgGetSize(self, pointer, status)
  pointer.read_uint32
end

#subjectObject



55
56
57
58
59
60
61
62
63
# File 'lib/gmsec/message.rb', line 55

def subject
  with_string_pointer do |pointer|
    gmsec_GetMsgSubject(self, pointer, status)

    if status.is_error?
      raise RuntimeError.new("Error getting message subject: #{status}")
    end
  end
end

#subject=(subject) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/gmsec/message.rb', line 65

def subject=(subject)
  gmsec_SetMsgSubject(self, subject, status)

  if status.is_error?
    raise RuntimeError.new("Error setting message subject: #{status}")
  end
end

#timeObject



184
185
186
# File 'lib/gmsec/message.rb', line 184

def time
  gmsec_MsgGetTime
end

#to_hObject



156
157
158
# File 'lib/gmsec/message.rb', line 156

def to_h
  Hash[fields.map{|field| [field.name, field.value]}]
end

#to_sObject



152
153
154
# File 'lib/gmsec/message.rb', line 152

def to_s
  ::Terminal::Table.new(title: subject, rows: to_h)
end

#to_xmlObject



160
161
162
163
164
165
166
167
168
# File 'lib/gmsec/message.rb', line 160

def to_xml
  with_string_pointer do |pointer|
    gmsec_MsgToXML(self, pointer, status)

    if status.is_error?
      raise RuntimeError.new("Error converting message to xml: #{status}")
    end
  end
end

#typeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gmsec/message.rb', line 12

def type
  pointer = FFI::MemoryPointer.new(find_type(:GMSEC_MSG_KIND))
  gmsec_GetMsgKind(self, pointer, status)

  if status.is_error?
    raise RuntimeError.new("Error getting message type: #{status}")
  end

  case pointer.read_ushort
  when GMSEC_MSG_PUBLISH
    :publish
  when GMSEC_MSG_REPLY
    :reply
  when GMSEC_MSG_REQUEST
    :request
  when GMSEC_MSG_UNSET
    :unset
  else
    raise TypeError.new("Unrecognized GMSEC data type")
  end
end

#type=(type) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gmsec/message.rb', line 34

def type=(type)
  value = case type
          when :publish
            GMSEC_MSG_PUBLISH
          when :reply
            GMSEC_MSG_REPLY
          when :request
            GMSEC_MSG_REQUEST
          when :unset
            GMSEC_MSG_REQUEST
          else
            raise TypeError.new("#{type} is not supported as a GMSEC type.")
          end

  gmsec_SetMsgKind(self, value, status)

  if status.is_error?
    raise RuntimeError.new("Error setting message type: #{status}")
  end
end

#valid?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/gmsec/message.rb', line 188

def valid?
  gmsec_isMsgValid(self) == self.class.enum_type(:GMSEC_BOOL)[:GMSEC_TRUE]
end