Class: MQRPC::Message

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

Direct Known Subclasses

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.



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

def initialize
  @data = Hash.new
  want_buffer(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.



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

def data=(value)
  @data = value
end

Class Method Details

.inherited(subclass) ⇒ Object



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

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



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

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



93
94
95
# File 'lib/mqrpc/message.rb', line 93

def age
  return Time.now.to_f - timestamp
end

#buffer?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/mqrpc/message.rb', line 97

def buffer?
  return @buffer
end

#generate_id!Object



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

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



105
106
107
# File 'lib/mqrpc/message.rb', line 105

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

#want_buffer(want_buffer = true) ⇒ Object



101
102
103
# File 'lib/mqrpc/message.rb', line 101

def want_buffer(want_buffer=true)
  @buffer = want_buffer
end