Module: MaxCube::Messages::TCP::Handler

Includes:
Handler
Included in:
Parser, Serializer
Defined in:
lib/maxcube/messages/tcp/handler.rb

Overview

Extends Handler of routines connected to TCP Cube messages.

Constant Summary

Constants included from Handler

Handler::PACK_FORMAT

Constants included from MaxCube::Messages

DAYS_OF_WEEK, DEVICE_MODE, DEVICE_TYPE

Instance Method Summary collapse

Methods included from Handler

#check_data_type, #check_hash, #check_hash_keys, #check_hash_msg_type, #check_hash_values, #check_msg, #check_msg_msg_type, #check_msg_type, #msg_type_hash_keys, #msg_type_hash_opt_keys, #valid_data_type, #valid_hash, #valid_hash_keys, #valid_hash_msg_type, #valid_hash_values, #valid_msg, #valid_msg_msg_type, #valid_msg_type

Instance Method Details

#check_tcp_data(raw_data) ⇒ String

As #valid_tcp_data, but raises exception if raw data is not valid.

Parameters:

  • raw_data (String)

    input data with multiple separated messages.

Returns:

  • (String)

    input data.

Raises:



102
103
104
105
# File 'lib/maxcube/messages/tcp/handler.rb', line 102

def check_tcp_data(raw_data)
  raise InvalidMessageFormat unless valid_tcp_data(raw_data)
  raw_data
end

#check_tcp_hash(hash) ⇒ Hash

As #valid_tcp_hash, but raises exception if hash is not valid. It only calls Handler#check_hash.

Parameters:

  • hash (Hash)

    input hash.

Returns:

  • (Hash)

    input hash.



81
82
83
84
# File 'lib/maxcube/messages/tcp/handler.rb', line 81

def check_tcp_hash(hash)
  check_hash(hash)
  hash
end

#check_tcp_msg(msg) ⇒ String

As #valid_tcp_msg, but raises exception if message is not valid. It calls #check_tcp_msg_length, #check_tcp_msg_format and Handler#check_msg.

Parameters:

  • msg (String)

    input message.

Returns:

  • (String)

    input message.



61
62
63
64
65
66
# File 'lib/maxcube/messages/tcp/handler.rb', line 61

def check_tcp_msg(msg)
  check_tcp_msg_length(msg)
  check_tcp_msg_format(msg)
  check_msg(msg)
  msg
end

#check_tcp_msg_format(msg) ⇒ String

As #valid_tcp_msg_format, but raises exception if message format is not valid.

Parameters:

  • msg (String)

    input message.

Returns:

  • (String)

    input message.

Raises:



40
41
42
43
# File 'lib/maxcube/messages/tcp/handler.rb', line 40

def check_tcp_msg_format(msg)
  raise InvalidMessageFormat unless valid_tcp_msg_format(msg)
  msg
end

#check_tcp_msg_length(msg) ⇒ Integer

As #valid_tcp_msg_length, but raises exception if message length is not valid.

Parameters:

  • msg (String)

    input message.

Returns:

  • (Integer)

    message length.

Raises:



23
24
25
26
# File 'lib/maxcube/messages/tcp/handler.rb', line 23

def check_tcp_msg_length(msg)
  raise InvalidMessageLength unless valid_tcp_msg_length(msg)
  msg.length
end

#valid_tcp_data(raw_data) ⇒ Boolean

Validates whether input raw data containing multiple separated TCP Cube messages is valid. It only checks \r\n stuff. It does not validate data type (Handler#valid_data_type), yet? It does not validate particular messages.

Parameters:

  • raw_data (String)

    input data with multiple separated messages.

Returns:

  • (Boolean)

    whether input data is valid.



93
94
95
96
# File 'lib/maxcube/messages/tcp/handler.rb', line 93

def valid_tcp_data(raw_data)
  return true if raw_data.empty?
  raw_data[0..1] != "\r\n" && raw_data.chars.last(2).join == "\r\n"
end

#valid_tcp_hash(hash) ⇒ Boolean

Validates whether given hash with message contents is valid for TCP Cube messaging purposes. It only calls Handler#valid_hash.

Parameters:

  • hash (Hash)

    input hash.

Returns:

  • (Boolean)

    whether hash is valid.



73
74
75
# File 'lib/maxcube/messages/tcp/handler.rb', line 73

def valid_tcp_hash(hash)
  valid_hash(hash)
end

#valid_tcp_msg(msg) ⇒ Boolean

Validates whether given message is a valid TCP Cube message. It calls #valid_tcp_msg_length, #valid_tcp_msg_format and Handler#valid_msg.

Parameters:

  • msg (String)

    input message.

Returns:

  • (Boolean)

    whether message is valid.



50
51
52
53
54
# File 'lib/maxcube/messages/tcp/handler.rb', line 50

def valid_tcp_msg(msg)
  valid_tcp_msg_length(msg) &&
    valid_tcp_msg_format(msg) &&
    valid_msg(msg)
end

#valid_tcp_msg_format(msg) ⇒ Boolean

Validates whether message satisfies TCP Cube format (see MaxCube::Messages::TCP).

Parameters:

  • msg (String)

    input message.

Returns:

  • (Boolean)

    whether message format is valid.



31
32
33
# File 'lib/maxcube/messages/tcp/handler.rb', line 31

def valid_tcp_msg_format(msg)
  msg =~ /\A[[:alpha:]]:[[:print:]]*\z/
end

#valid_tcp_msg_length(msg) ⇒ Boolean

Validates whether message satisfies MSG_MAX_LEN.

Parameters:

  • msg (String)

    input message.

Returns:

  • (Boolean)

    whether message length is valid.



14
15
16
# File 'lib/maxcube/messages/tcp/handler.rb', line 14

def valid_tcp_msg_length(msg)
  msg.length.between?(2, MSG_MAX_LEN)
end