Class: Nanite::Packet
Overview
Base class for all Nanite packets, knows how to dump itself to JSON
Direct Known Subclasses
Advertise, FileChunk, FileEnd, FileStart, IntermediateMessage, Ping, Push, Register, Request, Result, UnRegister
Instance Attribute Summary collapse
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
-
#id_to_s(id) ⇒ Object
Log friendly name for given agent id.
-
#initialize ⇒ Packet
constructor
A new instance of Packet.
- #to_json(*a) ⇒ Object
-
#to_s(filter = nil) ⇒ Object
Log representation.
Constructor Details
#initialize ⇒ Packet
Returns a new instance of Packet.
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
#size ⇒ Object
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 |