Class: Nanite::Packet

Inherits:
Object show all
Defined in:
lib/nanite/packets.rb

Overview

Base class for all Nanite packets, knows how to dump itself to JSON

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePacket

Returns a new instance of Packet.

Raises:

  • (NotImplementedError)


8
9
10
# File 'lib/nanite/packets.rb', line 8

def initialize
  raise NotImplementedError.new("#{self.class.name} is an abstract class.")
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'lib/nanite/packets.rb', line 6

def size
  @size
end

Instance Method Details

#id_to_s(id) ⇒ Object

Log friendly name for given agent id



32
33
34
35
36
37
38
# File 'lib/nanite/packets.rb', line 32

def id_to_s(id)
  case id
    when /^mapper-/ then 'mapper'
    when /^nanite-(.*)/ then Regexp.last_match(1)
    else id
  end
end

#to_json(*a) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/nanite/packets.rb', line 12

def to_json(*a)
  js = {
    'json_class'   => self.class.name,
    'data'         => instance_variables.inject({}) {|m,ivar| m[ivar.to_s.sub(/@/,'')] = instance_variable_get(ivar); m }
  }.to_json(*a)
  js = js.chop + ",\"size\":#{js.size}}"
  js
end

#to_s(filter = nil) ⇒ Object

Log representation



22
23
24
25
26
27
28
29
# File 'lib/nanite/packets.rb', line 22

def to_s(filter=nil)
  res = "[#{ self.class.to_s.split('::').last.
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    downcase }]"
  res += " (#{size.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")} bytes)" if size && !size.to_s.empty?
  res
end