Class: Cosmos::Packet
- Defined in:
- lib/cosmos/packets/packet.rb,
lib/cosmos/utilities/simulated_target.rb,
ext/cosmos/ext/packet/packet.c
Overview
Update Packet for Simulated Targets
Direct Known Subclasses
Constant Summary collapse
- VALUE_TYPES =
Valid format types
[:RAW, :CONVERTED, :FORMATTED, :WITH_UNITS]
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
Description of the packet.
-
#disabled ⇒ Boolean
Whether or not this is a ‘disabled’ packet.
-
#given_values ⇒ Hash<Item Name, Value>
Containst the values given by the user for a command (distinguished from defaults) These values should be used within command conversions if present because the order that values are written into the actual packet can vary.
-
#hazardous ⇒ Boolean
Flag indicating if the packet is hazardous (typically for commands).
-
#hazardous_description ⇒ String
Description of why the packet is hazardous.
-
#hidden ⇒ Boolean
Whether or not this is a ‘hidden’ packet.
-
#messages_disabled ⇒ Boolean
Whether or not messages should be printed for this packet.
-
#packet_name ⇒ String
readonly
Name of the packet.
-
#packet_rate ⇒ Object
Returns the value of attribute packet_rate.
-
#raw ⇒ Boolean
Whether or not this is a ‘raw’ packet.
-
#received_count ⇒ Integer
readonly
Number of times the packet has been received.
-
#received_time ⇒ Time
readonly
Time at which the packet was received.
-
#stale ⇒ Boolean
readonly
Flag indicating if the packet is stale (hasn’t been received recently).
-
#target_name ⇒ String
readonly
Name of the target this packet is associated with.
Instance Method Summary collapse
-
#append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item at the end of the packet.
-
#buffer=(buffer) ⇒ Object
Set the buffer to be used by the structure.
-
#check_bit_offsets ⇒ Array<String>
Review bit offset to look for overlapping definitions.
-
#check_limits(limits_set = :DEFAULT, ignore_persistence = false) ⇒ Object
Check all the items in the packet against their defined limits.
-
#clone ⇒ Packet
(also: #dup)
Make a light weight clone of this packet.
-
#define(item) ⇒ PacketItem
Add an item to the packet by adding it to the items hash.
-
#define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item in the packet.
-
#description(description) ⇒ Object
readonly
Sets the description of the packet.
-
#disable_limits(name) ⇒ Object
Disable limits on an item by name.
-
#enable_limits(name) ⇒ Object
Enable limits on an item by name.
-
#formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer) ⇒ String
Create a string that shows the name and value of each item in the packet.
-
#get_item(name) ⇒ StructureItem
StructureItem or one of its subclasses.
-
#id_items ⇒ Array<PacketItem>
Id items are used by the identify? method to determine if a raw buffer of data represents this packet.
-
#identified? ⇒ TrueClass or FalseClass
Indicates if the packet has been identified.
-
#identify?(buffer) ⇒ Boolean
Tries to identify if a buffer represents the currently defined packet.
-
#initialize(*args) ⇒ Object
constructor
Creates a new packet by initalizing the attributes.
-
#limits_change_callback=(limits_change_callback) ⇒ Object
Sets the callback object called when a limits state changes.
-
#limits_items ⇒ Array<PacketItem>
All items with defined limits.
-
#meta ⇒ Hash<Meta Name, Meta Values>
Returns packet specific metadata.
-
#out_of_limits ⇒ Array<Array<String, String, String, Symbol>>
Return an array of arrays indicating all items in the packet that are out of limits [[target name, packet name, item name, item limits state], …].
-
#packet_name(packet_name) ⇒ Object
readonly
Sets the packet name.
-
#processors ⇒ Hash
Hash of processors associated with this packet.
-
#read(name, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet by name.
-
#read_all(value_type = :CONVERTED, buffer = @buffer, top = true) ⇒ Array<Array>
Read all items in the packet into an array of arrays [[item name, item value], …].
-
#read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer) ⇒ Array<String, Object, Symbol|nil>
Read all items in the packet into an array of arrays [[item name, item value], [item limits state], …].
-
#read_item(item, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet.
-
#received_count(received_count) ⇒ Object
readonly
Sets the received count of the packet.
-
#received_time(received_time) ⇒ Object
readonly
Sets the received time of the packet.
-
#reset ⇒ Object
Reset temporary packet data This includes packet received time, received count, and processor state.
-
#restore_defaults(buffer = @buffer) ⇒ Object
Restore all items in the packet to their default value.
-
#set_all_limits_states(state) ⇒ Object
Set the limits state for all items to the given state.
-
#set_received_time_fast(received_time) ⇒ Object
Sets the received time of the packet (without cloning).
-
#set_stale ⇒ Object
Sets the overall packet stale state to true and sets each packet item limits state to :STALE.
-
#target_name(target_name) ⇒ Object
readonly
Sets the target name this packet is associated with.
-
#update_limits_items_cache(item) ⇒ Object
Add an item to the limits items cache if necessary.
-
#write(name, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet by name.
-
#write_item(item, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet.
Constructor Details
#initialize(*args) ⇒ Object
Creates a new packet by initalizing the attributes.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'ext/cosmos/ext/packet/packet.c', line 207
static VALUE packet_initialize(int argc, VALUE* argv, VALUE self) {
VALUE target_name = Qnil;
VALUE packet_name = Qnil;
VALUE default_endianness = Qnil;
VALUE description = Qnil;
VALUE buffer = Qnil;
VALUE item_class = Qnil;
VALUE super_args[3] = {Qnil, Qnil, Qnil};
switch (argc)
{
case 2:
target_name = argv[0];
packet_name = argv[1];
default_endianness = symbol_BIG_ENDIAN;
description = Qnil;
buffer = rb_str_new2("");
item_class = cPacketItem;
break;
case 3:
target_name = argv[0];
packet_name = argv[1];
default_endianness = argv[2];
description = Qnil;
buffer = rb_str_new2("");
item_class = cPacketItem;
break;
case 4:
target_name = argv[0];
packet_name = argv[1];
default_endianness = argv[2];
description = argv[3];
buffer = rb_str_new2("");
item_class = cPacketItem;
break;
case 5:
target_name = argv[0];
packet_name = argv[1];
default_endianness = argv[2];
description = argv[3];
buffer = argv[4];
item_class = cPacketItem;
break;
case 6:
target_name = argv[0];
packet_name = argv[1];
default_endianness = argv[2];
description = argv[3];
buffer = argv[4];
item_class = argv[5];
break;
default:
/* Invalid number of arguments given */
rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..6)", argc);
break;
};
super_args[0] = default_endianness;
super_args[1] = buffer;
super_args[2] = item_class;
rb_call_super(3, super_args);
target_name_equals(self, target_name);
packet_name_equals(self, packet_name);
description_equals(self, description);
rb_ivar_set(self, id_ivar_received_time, Qnil);
rb_ivar_set(self, id_ivar_received_count, INT2FIX(0));
rb_ivar_set(self, id_ivar_id_items, Qnil);
rb_ivar_set(self, id_ivar_hazardous, Qfalse);
rb_ivar_set(self, id_ivar_hazardous_description, Qnil);
rb_ivar_set(self, id_ivar_given_values, Qnil);
rb_ivar_set(self, id_ivar_limits_items, Qnil);
rb_ivar_set(self, id_ivar_processors, Qnil);
rb_ivar_set(self, id_ivar_stale, Qtrue);
rb_ivar_set(self, id_ivar_limits_change_callback, Qnil);
rb_ivar_set(self, id_ivar_read_conversion_cache, Qnil);
rb_ivar_set(self, id_ivar_raw, Qnil);
rb_ivar_set(self, id_ivar_messages_disabled, Qfalse);
rb_ivar_set(self, id_ivar_meta, Qnil);
rb_ivar_set(self, id_ivar_hidden, Qfalse);
rb_ivar_set(self, id_ivar_disabled, Qfalse);
return self;
}
|
Instance Attribute Details
#description ⇒ String (readonly)
Returns Description of the packet.
30 31 32 |
# File 'lib/cosmos/packets/packet.rb', line 30 def description @description end |
#disabled ⇒ Boolean
Returns Whether or not this is a ‘disabled’ packet.
60 61 62 |
# File 'lib/cosmos/packets/packet.rb', line 60 def disabled @disabled end |
#given_values ⇒ Hash<Item Name, Value>
Containst the values given by the user for a command (distinguished from defaults) These values should be used within command conversions if present because the order that values are written into the actual packet can vary
48 49 50 |
# File 'lib/cosmos/packets/packet.rb', line 48 def given_values @given_values end |
#hazardous ⇒ Boolean
Returns Flag indicating if the packet is hazardous (typically for commands).
39 40 41 |
# File 'lib/cosmos/packets/packet.rb', line 39 def hazardous @hazardous end |
#hazardous_description ⇒ String
Returns Description of why the packet is hazardous.
42 43 44 |
# File 'lib/cosmos/packets/packet.rb', line 42 def hazardous_description @hazardous_description end |
#hidden ⇒ Boolean
Returns Whether or not this is a ‘hidden’ packet.
57 58 59 |
# File 'lib/cosmos/packets/packet.rb', line 57 def hidden @hidden end |
#messages_disabled ⇒ Boolean
Returns Whether or not messages should be printed for this packet.
63 64 65 |
# File 'lib/cosmos/packets/packet.rb', line 63 def @messages_disabled end |
#packet_name ⇒ String (readonly)
Returns Name of the packet.
27 28 29 |
# File 'lib/cosmos/packets/packet.rb', line 27 def packet_name @packet_name end |
#packet_rate ⇒ Object
Returns the value of attribute packet_rate.
19 20 21 |
# File 'lib/cosmos/utilities/simulated_target.rb', line 19 def packet_rate @packet_rate end |
#raw ⇒ Boolean
Returns Whether or not this is a ‘raw’ packet.
54 55 56 |
# File 'lib/cosmos/packets/packet.rb', line 54 def raw @raw end |
#received_count ⇒ Integer (readonly)
Returns Number of times the packet has been received.
36 37 38 |
# File 'lib/cosmos/packets/packet.rb', line 36 def received_count @received_count end |
#received_time ⇒ Time (readonly)
Returns Time at which the packet was received.
33 34 35 |
# File 'lib/cosmos/packets/packet.rb', line 33 def received_time @received_time end |
#stale ⇒ Boolean (readonly)
Returns Flag indicating if the packet is stale (hasn’t been received recently).
51 52 53 |
# File 'lib/cosmos/packets/packet.rb', line 51 def stale @stale end |
#target_name ⇒ String (readonly)
Returns Name of the target this packet is associated with.
24 25 26 |
# File 'lib/cosmos/packets/packet.rb', line 24 def target_name @target_name end |
Instance Method Details
#append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item at the end of the packet. This creates a new instance of the item_class as given in the constructor and adds it to the items hash. It also resizes the buffer to accomodate the new item.
301 302 303 304 |
# File 'lib/cosmos/packets/packet.rb', line 301 def append_item(name, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) item = super(name, bit_size, data_type, array_size, endianness, overflow) packet_define_item(item, format_string, read_conversion, write_conversion, id_value) end |
#buffer=(buffer) ⇒ Object
Set the buffer to be used by the structure. The buffer is copied and thus further modifications to the buffer have no effect on the structure items.
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cosmos/packets/packet.rb', line 80 def buffer=(buffer) synchronize() do begin internal_buffer_equals(buffer) rescue RuntimeError Logger.instance.error "#{@target_name} #{@packet_name} received with actual packet length of #{buffer.length} but defined length of #{@defined_length}" end @read_conversion_cache.clear if @read_conversion_cache process() end end |
#check_bit_offsets ⇒ Array<String>
Review bit offset to look for overlapping definitions. This will allow gaps in the packet, but not allow the same bits to be used for multiple variables.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/cosmos/packets/packet.rb', line 173 def check_bit_offsets expected_next_offset = nil previous_item = nil warnings = [] @sorted_items.each do |item| if expected_next_offset and item.bit_offset < expected_next_offset msg = "Bit definition overlap at bit offset #{item.bit_offset} for packet #{@target_name} #{@packet_name} items #{item.name} and #{previous_item.name}" Logger.instance.warn(msg) warnings << msg end if item.array_size if item.array_size > 0 expected_next_offset = item.bit_offset + item.array_size else expected_next_offset = item.array_size end else expected_next_offset = nil if item.bit_offset > 0 # Handle little-endian bit fields byte_aligned = ((item.bit_offset % 8) == 0) if item.endianness == :LITTLE_ENDIAN and (item.data_type == :INT or item.data_type == :UINT) and !(byte_aligned and (item.bit_size == 8 or item.bit_size == 16 or item.bit_size == 32 or item.bit_size == 64)) # Bit offset always refers to the most significant bit of a bitfield bits_remaining_in_last_byte = 8 - (item.bit_offset % 8) if item.bit_size > bits_remaining_in_last_byte expected_next_offset = item.bit_offset + bits_remaining_in_last_byte end end end unless expected_next_offset if item.bit_size > 0 expected_next_offset = item.bit_offset + item.bit_size else expected_next_offset = item.bit_size end end end previous_item = item end warnings end |
#check_limits(limits_set = :DEFAULT, ignore_persistence = false) ⇒ Object
Check all the items in the packet against their defined limits. Update their internal limits state and persistence and call the limits_change_callback as necessary.
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/cosmos/packets/packet.rb', line 581 def check_limits(limits_set = :DEFAULT, ignore_persistence = false) # If check_limits is being called, then a new packet has arrived and # this packet is no longer stale if @stale @stale = false set_all_limits_states(nil) end return unless @limits_items @limits_items.each do |item| # Verify limits monitoring is enabled for this item if item.limits.enabled value = read_item(item) # Handle state monitoring and value monitoring differently if item.states handle_limits_states(item, value) elsif item.limits.values handle_limits_values(item, value, limits_set, ignore_persistence) end end end end |
#clone ⇒ Packet Also known as: dup
Make a light weight clone of this packet. This only creates a new buffer of data and clones the processors. The defined packet items are the same.
633 634 635 636 637 638 639 640 641 642 |
# File 'lib/cosmos/packets/packet.rb', line 633 def clone packet = super() if packet.instance_variable_get("@processors".freeze) packet.instance_variable_set("@processors".freeze, packet.processors.clone) packet.processors.each do |processor_name, processor| packet.processors[processor_name] = processor.clone end end packet end |
#define(item) ⇒ PacketItem
Add an item to the packet by adding it to the items hash. It also resizes the buffer to accomodate the new item.
279 280 281 282 283 284 |
# File 'lib/cosmos/packets/packet.rb', line 279 def define(item) item = super(item) update_id_items(item) update_limits_items_cache(item) item end |
#define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) ⇒ PacketItem
Define an item in the packet. This creates a new instance of the item_class as given in the constructor and adds it to the items hash. It also resizes the buffer to accomodate the new item.
269 270 271 272 |
# File 'lib/cosmos/packets/packet.rb', line 269 def define_item(name, bit_offset, bit_size, data_type, array_size = nil, endianness = @default_endianness, overflow = :ERROR, format_string = nil, read_conversion = nil, write_conversion = nil, id_value = nil) item = super(name, bit_offset, bit_size, data_type, array_size, endianness, overflow) packet_define_item(item, format_string, read_conversion, write_conversion, id_value) end |
#description=(description) ⇒ Object (readonly)
Sets the description of the packet
158 159 160 161 162 163 164 165 166 167 168 |
# File 'ext/cosmos/ext/packet/packet.c', line 158
static VALUE description_equals(VALUE self, VALUE description) {
if (RTEST(description)) {
if (rb_funcall(description, id_method_class, 0) != rb_cString) {
rb_raise(rb_eArgError, "description must be a String but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(description, id_method_class, 0), id_method_to_s, 0)));
}
rb_ivar_set(self, id_ivar_description, rb_funcall(rb_funcall(description, id_method_clone, 0), id_method_freeze, 0));
} else {
rb_ivar_set(self, id_ivar_description, Qnil);
}
return rb_ivar_get(self, id_ivar_description);
}
|
#disable_limits(name) ⇒ Object
Disable limits on an item by name
525 526 527 528 529 530 531 532 533 |
# File 'lib/cosmos/packets/packet.rb', line 525 def disable_limits(name) item = get_item(name) item.limits.enabled = false unless item.limits.state == :STALE old_limits_state = item.limits.state item.limits.state = nil @limits_change_callback.call(self, item, old_limits_state, nil, false) if @limits_change_callback end end |
#enable_limits(name) ⇒ Object
Enable limits on an item by name
518 519 520 |
# File 'lib/cosmos/packets/packet.rb', line 518 def enable_limits(name) get_item(name).limits.enabled = true end |
#formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer) ⇒ String
Create a string that shows the name and value of each item in the packet
488 489 490 |
# File 'lib/cosmos/packets/packet.rb', line 488 def formatted(value_type = :CONVERTED, indent = 0, buffer = @buffer) return super(value_type, indent, buffer) end |
#get_item(name) ⇒ StructureItem
Returns StructureItem or one of its subclasses.
307 308 309 310 311 |
# File 'lib/cosmos/packets/packet.rb', line 307 def get_item(name) super(name) rescue ArgumentError raise "Packet item '#{@target_name} #{@packet_name} #{name.upcase}' does not exist" end |
#id_items ⇒ Array<PacketItem>
Id items are used by the identify? method to determine if a raw buffer of data represents this packet.
218 219 220 |
# File 'lib/cosmos/packets/packet.rb', line 218 def id_items @id_items ||= [] end |
#identified? ⇒ TrueClass or FalseClass
Indicates if the packet has been identified
240 241 242 |
# File 'lib/cosmos/packets/packet.rb', line 240 def identified? !@target_name.nil? && !@packet_name.nil? end |
#identify?(buffer) ⇒ Boolean
Tries to identify if a buffer represents the currently defined packet. It does this by iterating over all the packet items that were created with an ID value and checking whether that ID value is present at the correct location in the buffer.
Incorrectly sized buffers will still positively identify if there is enough data to match the ID values. This is to allow incorrectly sized packets to still be processed as well as possible given the incorrectly sized data.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'ext/cosmos/ext/packet/packet.c', line 92
static VALUE identify(VALUE self, VALUE buffer)
{
VALUE id_items = rb_ivar_get(self, id_ivar_id_items);
VALUE item = Qnil;
VALUE id_value = Qnil;
VALUE raw_value = Qnil;
long id_items_length = 0;
int index = 0;
if (!RTEST(buffer)) {
return Qfalse;
}
if (!RTEST(id_items)) {
return Qtrue;
}
id_items_length = RARRAY_LEN(id_items);
for (index = 0; index < id_items_length; index++) {
item = rb_ary_entry(id_items, index);
id_value = rb_ivar_get(item, id_ivar_id_value);
raw_value = protected_read_item_internal(self, item, buffer);
if (!rb_eql(id_value, raw_value)) {
return Qfalse;
}
}
return Qtrue;
}
|
#limits_change_callback=(limits_change_callback) ⇒ Object
Sets the callback object called when a limits state changes
159 160 161 162 163 164 165 166 |
# File 'lib/cosmos/packets/packet.rb', line 159 def limits_change_callback=(limits_change_callback) if limits_change_callback raise ArgumentError, "limits_change_callback must respond to call" unless limits_change_callback.respond_to?(:call) @limits_change_callback = limits_change_callback else @limits_change_callback = nil end end |
#limits_items ⇒ Array<PacketItem>
Returns All items with defined limits.
223 224 225 |
# File 'lib/cosmos/packets/packet.rb', line 223 def limits_items @limits_items ||= [] end |
#meta ⇒ Hash<Meta Name, Meta Values>
Returns packet specific metadata
234 235 236 |
# File 'lib/cosmos/packets/packet.rb', line 234 def @meta ||= {} end |
#out_of_limits ⇒ Array<Array<String, String, String, Symbol>>
Return an array of arrays indicating all items in the packet that are out of limits
[[target name, packet name, item name, item limits state], ...]
554 555 556 557 558 559 560 561 562 563 564 |
# File 'lib/cosmos/packets/packet.rb', line 554 def out_of_limits items = [] return items unless @limits_items @limits_items.each do |item| if (item.limits.enabled && item.limits.state && PacketItemLimits::OUT_OF_LIMITS_STATES.include?(item.limits.state)) items << [@target_name, @packet_name, item.name, item.limits.state] end end return items end |
#packet_name=(packet_name) ⇒ Object (readonly)
Sets the packet name. Unidentified packets will have packet name set to nil.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'ext/cosmos/ext/packet/packet.c', line 143
static VALUE packet_name_equals(VALUE self, VALUE packet_name) {
if (RTEST(packet_name)) {
if (rb_funcall(packet_name, id_method_class, 0) != rb_cString) {
rb_raise(rb_eArgError, "packet_name must be a String but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(packet_name, id_method_class, 0), id_method_to_s, 0)));
}
rb_ivar_set(self, id_ivar_packet_name, rb_funcall(rb_funcall(rb_funcall(packet_name, id_method_upcase, 0), id_method_clone, 0), id_method_freeze, 0));
} else {
rb_ivar_set(self, id_ivar_packet_name, Qnil);
}
return rb_ivar_get(self, id_ivar_packet_name);
}
|
#processors ⇒ Hash
Returns Hash of processors associated with this packet.
228 229 230 |
# File 'lib/cosmos/packets/packet.rb', line 228 def processors @processors ||= {} end |
#read(name, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet by name
439 440 441 |
# File 'lib/cosmos/packets/packet.rb', line 439 def read(name, value_type = :CONVERTED, buffer = @buffer) return super(name, value_type, buffer) end |
#read_all(value_type = :CONVERTED, buffer = @buffer, top = true) ⇒ Array<Array>
Read all items in the packet into an array of arrays
[[item name, item value], ...]
460 461 462 |
# File 'lib/cosmos/packets/packet.rb', line 460 def read_all(value_type = :CONVERTED, buffer = @buffer, top = true) return super(value_type, buffer, top) end |
#read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer) ⇒ Array<String, Object, Symbol|nil>
Read all items in the packet into an array of arrays
[[item name, item value], [item limits state], ...]
472 473 474 475 476 477 478 479 480 |
# File 'lib/cosmos/packets/packet.rb', line 472 def read_all_with_limits_states(value_type = :CONVERTED, buffer = @buffer) result = nil synchronize_allow_reads(true) do result = read_all(value_type, buffer, false).map! do |array| array << @items[array[0]].limits.state end end return result end |
#read_item(item, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Read an item in the packet
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/cosmos/packets/packet.rb', line 322 def read_item(item, value_type = :CONVERTED, buffer = @buffer) value = super(item, :RAW, buffer) case value_type when :RAW # Done above when :CONVERTED, :FORMATTED, :WITH_UNITS if item.read_conversion using_cached_value = false check_cache = buffer.equal?(@buffer) if check_cache and @read_conversion_cache synchronize_allow_reads() do if @read_conversion_cache[item] value = @read_conversion_cache[item] using_cached_value = true end end end unless using_cached_value if item.array_size value.map! do |val, index| item.read_conversion.call(val, self, buffer) end else value = item.read_conversion.call(value, self, buffer) end if check_cache synchronize_allow_reads() do @read_conversion_cache ||= {} @read_conversion_cache[item] = value end end end end # Convert from value to state if possible if item.states if Array === value value = value.map do |val, index| if item.states.key(val) item.states.key(val) else apply_format_string_and_units(item, val, value_type) end end else state_value = item.states.key(value) if state_value value = state_value else value = apply_format_string_and_units(item, value, value_type) end end else if Array === value value = value.map do |val, index| apply_format_string_and_units(item, val, value_type) end else value = apply_format_string_and_units(item, value, value_type) end end else raise ArgumentError, "Unknown value type on read: #{value_type}" end return value end |
#received_count=(received_count) ⇒ Object (readonly)
Sets the received count of the packet
189 190 191 192 193 194 195 |
# File 'ext/cosmos/ext/packet/packet.c', line 189
static VALUE received_count_equals(VALUE self, VALUE received_count) {
if (rb_funcall(received_count, id_method_class, 0) != rb_cFixnum) {
rb_raise(rb_eArgError, "received_count must be a Fixnum but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(received_count, id_method_class, 0), id_method_to_s, 0)));
}
rb_ivar_set(self, id_ivar_received_count, received_count);
return rb_ivar_get(self, id_ivar_received_count);
}
|
#received_time=(received_time) ⇒ Object (readonly)
Sets the received time of the packet
173 174 175 176 177 178 179 180 181 182 183 |
# File 'ext/cosmos/ext/packet/packet.c', line 173
static VALUE received_time_equals(VALUE self, VALUE received_time) {
if (RTEST(received_time)) {
if (rb_funcall(received_time, id_method_class, 0) != rb_cTime) {
rb_raise(rb_eArgError, "received_time must be a Time but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(received_time, id_method_class, 0), id_method_to_s, 0)));
}
rb_ivar_set(self, id_ivar_received_time, rb_funcall(rb_funcall(received_time, id_method_clone, 0), id_method_freeze, 0));
} else {
rb_ivar_set(self, id_ivar_received_time, Qnil);
}
return rb_ivar_get(self, id_ivar_received_time);
}
|
#reset ⇒ Object
Reset temporary packet data This includes packet received time, received count, and processor state
614 615 616 617 618 619 620 621 622 623 624 625 626 |
# File 'lib/cosmos/packets/packet.rb', line 614 def reset @received_time = nil @received_count = 0 if @read_conversion_cache synchronize() do @read_conversion_cache.clear end end return unless @processors @processors.each do |processor_name, processor| processor.reset end end |
#restore_defaults(buffer = @buffer) ⇒ Object
Restore all items in the packet to their default value
509 510 511 512 513 |
# File 'lib/cosmos/packets/packet.rb', line 509 def restore_defaults(buffer = @buffer) @sorted_items.each do |item| write_item(item, item.default, :CONVERTED, buffer) end end |
#set_all_limits_states(state) ⇒ Object
Set the limits state for all items to the given state
569 570 571 |
# File 'lib/cosmos/packets/packet.rb', line 569 def set_all_limits_states(state) @sorted_items.each {|item| item.limits.state = state} end |
#set_received_time_fast(received_time) ⇒ Object
Sets the received time of the packet (without cloning)
117 118 119 120 |
# File 'lib/cosmos/packets/packet.rb', line 117 def set_received_time_fast(received_time) @received_time = received_time @received_time.freeze if @received_time end |
#set_stale ⇒ Object
Sets the overall packet stale state to true and sets each packet item limits state to :STALE.
607 608 609 610 |
# File 'lib/cosmos/packets/packet.rb', line 607 def set_stale @stale = true set_all_limits_states(:STALE) end |
#target_name=(target_name) ⇒ Object (readonly)
Sets the target name this packet is associated with. Unidentified packets will have target name set to nil.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'ext/cosmos/ext/packet/packet.c', line 127
static VALUE target_name_equals(VALUE self, VALUE target_name) {
if (RTEST(target_name)) {
if (rb_funcall(target_name, id_method_class, 0) != rb_cString) {
rb_raise(rb_eArgError, "target_name must be a String but is a %s", RSTRING_PTR(rb_funcall(rb_funcall(target_name, id_method_class, 0), id_method_to_s, 0)));
}
rb_ivar_set(self, id_ivar_target_name, rb_funcall(rb_funcall(rb_funcall(target_name, id_method_upcase, 0), id_method_clone, 0), id_method_freeze, 0));
} else {
rb_ivar_set(self, id_ivar_target_name, Qnil);
}
return rb_ivar_get(self, id_ivar_target_name);
}
|
#update_limits_items_cache(item) ⇒ Object
Add an item to the limits items cache if necessary. You MUST call this after adding limits to an item This is an optimization so we don’t have to iterate through all the items when checking for limits.
539 540 541 542 543 544 545 546 547 548 |
# File 'lib/cosmos/packets/packet.rb', line 539 def update_limits_items_cache(item) if item.limits.values || item.state_colors @limits_items ||= [] @limits_items_hash ||= {} unless @limits_items_hash[item] @limits_items << item @limits_items_hash[item] = true end end end |
#write(name, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet by name
449 450 451 |
# File 'lib/cosmos/packets/packet.rb', line 449 def write(name, value, value_type = :CONVERTED, buffer = @buffer) super(name, value, value_type, buffer) end |
#write_item(item, value, value_type = :CONVERTED, buffer = @buffer) ⇒ Object
Write an item in the packet
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/cosmos/packets/packet.rb', line 397 def write_item(item, value, value_type = :CONVERTED, buffer = @buffer) if @read_conversion_cache synchronize() do @read_conversion_cache.clear end end case value_type when :RAW super(item, value, value_type, buffer) when :CONVERTED if item.states # Convert from state to value if possible state_value = item.states[value.to_s.upcase] value = state_value if state_value end if item.write_conversion value = item.write_conversion.call(value, self, buffer) else raise "Cannot write DERIVED item without a write conversion" if item.data_type == :DERIVED end begin super(item, value, :RAW, buffer) unless item.data_type == :DERIVED rescue ArgumentError => err if item.states and String === value and err. =~ /invalid value for/ raise "Unknown state #{value} for #{item.name}" else raise err end end when :FORMATTED, :WITH_UNITS raise ArgumentError, "Invalid value type on write: #{value_type}" else raise ArgumentError, "Unknown value type on write: #{value_type}" end end |