Class: SML::ListEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-sml/sml-listentry.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, status, status_type, value_time, unit, scaler, value, value_type, signature) ⇒ ListEntry

Returns a new instance of ListEntry.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby-sml/sml-listentry.rb', line 12

def initialize(name, status, status_type, value_time, unit, scaler, value, value_type, signature)
  @name = name
  @status = status
  @status_type = status_type
  @value_time = value_time
  @unit = unit
  @scaler = scaler
  @value = value
  @value_type = value_type
  @signature = signature
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def name
  @name
end

#scalerObject

Returns the value of attribute scaler.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def scaler
  @scaler
end

#signatureObject

Returns the value of attribute signature.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def signature
  @signature
end

#statusObject

Returns the value of attribute status.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def status
  @status
end

#status_typeObject

Returns the value of attribute status_type.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def status_type
  @status_type
end

#unitObject

Returns the value of attribute unit.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def unit
  @unit
end

#valueObject

Returns the value of attribute value.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def value
  @value
end

#value_timeObject

Returns the value of attribute value_time.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def value_time
  @value_time
end

#value_typeObject

Returns the value of attribute value_type.



10
11
12
# File 'lib/ruby-sml/sml-listentry.rb', line 10

def value_type
  @value_type
end

Class Method Details

.construct(array_rep) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby-sml/sml-listentry.rb', line 24

def self.construct(array_rep)
  return nil if array_rep.nil?
  name = array_rep.shift
  status = array_rep.shift
  status_type = array_rep.shift unless status.nil?
  value_time = SML::Time.construct(array_rep.shift)
  unit = array_rep.shift
  array_rep.shift unless unit.nil?
  scaler = array_rep.shift
  array_rep.shift unless scaler.nil?
  value = array_rep.shift
  case value
  when Fixnum
    value_type = array_rep.shift
  when String
    value_type = :string
  end
  signature = array_rep.shift

  return nil if (value.nil? or name.nil?)
  return SML::ListEntry.new(name, status, status_type, value_time, unit, scaler, value, value_type, signature)
end

.pconstruct(o = {}) ⇒ Object



47
48
49
# File 'lib/ruby-sml/sml-listentry.rb', line 47

def self.pconstruct(o={})
  return SML::ListEntry.new(o[:name], o[:status], o[:status_type], o[:value_time], o[:unit], o[:scaler], o[:value], o[:value_type], o[:signature])
end

Instance Method Details

#calculate_hash(server_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby-sml/sml-listentry.rb', line 57

def calculate_hash(server_id)
  return nil unless [:int8, :int16, :int32, :int64, :uint8, :uint16, :uint32, :uint64].include?(@value_type)
  return nil unless @value_time.type == :timestamp
  return nil unless @name.length == 6

  bytes = String.new
  bytes += server_id
  (10-server_id.length).times do
    bytes += [0x00].pack('c')
  end
  bytes += [@value_time.value].pack('N')
  bytes += [@status].pack('c')
  bytes += @name
  bytes += [@unit].pack('c')
  bytes += [@scaler].pack('c')
  bytes += [(@value & 0xffff0000) >> 32,(@value & 0x0000ffff)].pack('NN')
  17.times do
    bytes += [0x00].pack('c')
  end

  hash = Digest::SHA2.new(256).digest(bytes)[0,24]
  
  return hash
end

#sign(server_id, private_key) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/ruby-sml/sml-listentry.rb', line 81

def sign(server_id, private_key)
  hash = calculate_hash(server_id)
  return nil if hash.nil?

  signature = OpenSSL::PKey::EC.new(private_key).dsa_sign_asn1(hash)

  @signature = signature
end

#to_aObject



51
52
53
54
55
# File 'lib/ruby-sml/sml-listentry.rb', line 51

def to_a
  result = [] << name << status << status_type << value_time.to_a << unit << :uint8 << scaler << :int8 << value
  result << value_type if value.class == Fixnum
  return result << signature
end

#verify(server_id, public_key) ⇒ Object



89
90
91
92
93
94
# File 'lib/ruby-sml/sml-listentry.rb', line 89

def verify(server_id, public_key)
  hash = calculate_hash(server_id)
  return nil if hash.nil?
  
  return OpenSSL::PKey::EC.new(public_key).dsa_verify_asn1(hash,@signature)
end