Class: ROS::TCPROS::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/ros/tcpros/header.rb

Overview

header of rorpc's protocol

Constant Summary

WILD_CARD =

rosrpc uses this wild card for cancel md5sum check or any.

'*'

Instance Method Summary (collapse)

Constructor Details

- (Header) initialize(data = {})

initialize with hash

Parameters:

  • data (Hash) (defaults to: {})


22
23
24
# File 'lib/ros/tcpros/header.rb', line 22

def initialize(data={})
  @data = data
end

Instance Method Details

- (Header) deserialize(data)

deserialize the data to header. this does not contain total byte number.

Parameters:

  • data (String)

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ros/tcpros/header.rb', line 51

def deserialize(data)
  while data.length > 0
    len, data = data.unpack('Va*')
    msg = data[0..(len-1)]
    equal_position = msg.index('=')
    key = msg[0..(equal_position-1)]
    value = msg[(equal_position+1)..-1]
    @data[key] = value
    data = data[(len)..-1]
  end
  self
end

- (String) get_data(key) Also known as: []

Returns value of key

Parameters:

  • key (String)

Returns:

  • (String)

    value of key



40
41
42
# File 'lib/ros/tcpros/header.rb', line 40

def get_data(key)
  @data[key]
end

- (Header) push_data(key, value) Also known as: []=

add key-value data to this header.

Parameters:

  • key (String)

    key for header

  • value (String)

    value for key

Returns:



30
31
32
33
34
35
36
# File 'lib/ros/tcpros/header.rb', line 30

def push_data(key, value)
  if (not key.kind_of?(String)) or (not value.kind_of?(String))
    raise ArgumentError::new('header key and value must be string')
  end
  @data[key] = value
  self
end

- (Integer) serialize(buff)

serialize the data into header. return the byte of the serialized data.

Parameters:

  • buff (IO)

    where to write data

Returns:

  • (Integer)

    byte of serialized data



78
79
80
81
82
83
84
85
86
# File 'lib/ros/tcpros/header.rb', line 78

def serialize(buff)
  serialized_data = ''
  @data.each_pair do |key, value|
    data_str = key + '=' + value
    serialized_data = serialized_data + [data_str.length, data_str].pack('Va*')
  end
  total_byte = serialized_data.length
  return buff.write([total_byte, serialized_data].pack('Va*'))
end

- (Boolean) valid?(key, value)

validate the key with the value. if it is WILD_CARD, it is ok!

Parameters:

  • key (String)
  • value (String)

Returns:

  • (Boolean)

    check result



70
71
72
# File 'lib/ros/tcpros/header.rb', line 70

def valid?(key, value)
  (@data[key] == value) or value == WILD_CARD
end