Class: MQRPC::Message

Inherits:
Object
  • Object
show all
Extended by:
BindToHash
Defined in:
lib/mqrpc/message.rb

Direct Known Subclasses

DummyMessage, RequestMessage, ResponseMessage

Constant Summary collapse

@@idseq =
0
@@idlock =
Mutex.new
@@knowntypes =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BindToHash

argument, header

Constructor Details

#initializeMessage

Returns a new instance of Message.



77
78
79
80
81
82
83
84
85
# File 'lib/mqrpc/message.rb', line 77

def initialize
  @data = Hash.new
  # Don't delay messages by defualt
  self.delayable = false

  generate_id!
  self.message_class = self.class.name
  self.args = Hash.new
end

Instance Attribute Details

#data=(value) ⇒ Object

Sets the attribute data

Parameters:

  • value

    the value to set the attribute data to.



44
45
46
# File 'lib/mqrpc/message.rb', line 44

def data=(value)
  @data = value
end

Class Method Details

.inherited(subclass) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/mqrpc/message.rb', line 54

def self.inherited(subclass)
  MQRPC::logger.debug "Message '#{subclass.name}' subclasses #{self.name}"
  @@knowntypes[subclass.name] = subclass

  # Call the class initializer if it has one.
  if subclass.respond_to?(:class_initialize)
    subclass.class_initialize
  end
end

.new_from_data(data) ⇒ Object

def self.inherited



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mqrpc/message.rb', line 64

def self.new_from_data(data)
  obj = nil
  name = data["message_class"]
  if @@knowntypes.has_key?(name)
    obj = @@knowntypes[name].new
  else
    $stderr.puts "No known message class: #{name}, #{data.inspect}"
    obj = Message.new
  end
  obj.data = data
  return obj
end

Instance Method Details

#ageObject



95
96
97
# File 'lib/mqrpc/message.rb', line 95

def age
  return Time.now.to_f - timestamp
end

#generate_id!Object



87
88
89
90
91
92
93
# File 'lib/mqrpc/message.rb', line 87

def generate_id!
  @@idlock.synchronize do
    self.id = @@idseq
    #puts "Generating id. #{self.class}.id == #{self.id}"
    @@idseq += 1
  end
end

#to_json(*args) ⇒ Object



99
100
101
# File 'lib/mqrpc/message.rb', line 99

def to_json(*args)
  return @data.to_json(*args)
end