Class: OmfCommon::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/omf_common/message.rb,
lib/omf_common/message/xml/message.rb,
lib/omf_common/message/json/json_message.rb

Direct Known Subclasses

Json::Message, XML::Message

Defined Under Namespace

Classes: Json, XML

Constant Summary collapse

OMF_NAMESPACE =
"http://schema.mytestbed.net/omf/#{OmfCommon::PROTOCOL_VERSION}/protocol"
OMF_CORE_READ =
[:operation, :ts, :src, :mid, :replyto, :cid, :itype, :rtype, :guard, :res_id]
OMF_CORE_WRITE =
[:replyto, :itype, :guard]
@@providers =
{
  xml: {
    require: 'omf_common/message/xml/message',
    constructor: 'OmfCommon::Message::XML::Message'
  },
  json: {
    require: 'omf_common/message/json/json_message',
    constructor: 'OmfCommon::Message::Json::Message'
  }
}
@@message_class =
nil
@@authenticate_messages =
false
@@authorisation_hook =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#issuerObject (readonly)

Returns the value of attribute issuer.



100
101
102
# File 'lib/omf_common/message.rb', line 100

def issuer
  @issuer
end

Class Method Details

.authenticate?Boolean

Return true if all messages will be authenticated, return false otherwise

Returns:

  • (Boolean)


48
49
50
# File 'lib/omf_common/message.rb', line 48

def self.authenticate?
  @@authenticate_messages
end

.create(type, properties, body = {}) ⇒ Object



37
38
39
# File 'lib/omf_common/message.rb', line 37

def self.create(type, properties, body = {})
  @@message_class.create(type, properties || {}, body)
end

.create_inform_message(itype = nil, properties = {}, body = {}) ⇒ Object



41
42
43
44
# File 'lib/omf_common/message.rb', line 41

def self.create_inform_message(itype = nil, properties = {}, body = {})
  body[:itype] = itype if itype
  create(:inform, properties, body)
end

.init(opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/omf_common/message.rb', line 68

def self.init(opts = {})
  puts opts.inspect
  if @@message_class
    raise "Message provider already iniitalised"
  end
  unless provider = opts[:provider]
    provider = @@providers[opts[:type]]
  end
  unless provider
    raise "Missing Message provider declaration. Either define 'type' or 'provider'"
  end

  require provider[:require] if provider[:require]

  if class_name = provider[:constructor]
    @@message_class = class_name.split('::').inject(Object) {|c,n| c.const_get(n) }
  else
    raise "Missing provider class info - :constructor"
  end
  aopts = opts[:authenticate] || {}
  @@authenticate_messages = opts[:authenticate] && !(aopts[:authenticate] == false)
  if pdp_opts = (opts[:authenticate] || {})[:pdp]
    require pdp_opts.delete(:require) if pdp_opts[:require]
    unless pdp_constructor = pdp_opts.delete(:constructor)
      raise "Missing PDP provider declaration."
    end

    pdp_class = pdp_constructor.split('::').inject(Object) {|c,n| c.const_get(n) }
    @@authorisation_hook = pdp_class.new(pdp_opts)
  end
end

.parse(str, content_type = nil, &block) ⇒ Object

Parse message from ‘str’ and pass it to ‘block’. If authentication is on, the message will only be handed to ‘block’ if the source of the message can be authorized.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/omf_common/message.rb', line 56

def self.parse(str, content_type = nil, &block)
  raise ArgumentError, 'Need message handling block' unless block
  @@message_class.parse(str, content_type) do |msg|
    if @@authorisation_hook
      # Hook will return message if it's authorized. Handing in
      # dispatch block in case hook needs more time for authorization.
      msg = @@authorisation_hook.authorize(msg, &block)
    end
    block.call(msg) if msg
  end
end

Instance Method Details

#[](name, ns = nil) ⇒ Object

To access properties

Parameters:

  • name (String)

    of the property

  • ns (Hash) (defaults to: nil)

    namespace of property



120
121
122
# File 'lib/omf_common/message.rb', line 120

def [](name, ns = nil)
  _get_property(name.to_sym, ns)
end

#[]=(name, ns = nil, value) ⇒ Object

To set properties

Parameters:

  • name (String)

    of the property

  • ns (Hash) (defaults to: nil)

    namespace of property



128
129
130
131
132
133
134
135
136
# File 'lib/omf_common/message.rb', line 128

def []=(name, ns = nil, value)
  # TODO why itype cannot be set?
  #raise if name.to_sym == :itype
  if ns
    @props_ns ||= {}
    @props_ns.merge!(ns)
  end
  _set_property(name.to_sym, value, ns)
end

#create_inform_reply_message(itype = nil, properties = {}, body = {}) ⇒ Object



181
182
183
184
# File 'lib/omf_common/message.rb', line 181

def create_inform_reply_message(itype = nil, properties = {}, body = {})
  body[:cid] = self.mid
  self.class.create_inform_message(itype, properties, body)
end

#default_props_nsObject

Construct default namespace of the props from resource type



220
221
222
223
# File 'lib/omf_common/message.rb', line 220

def default_props_ns
  resource_type = _get_core(:rtype)
  resource_type ? { resource_type.to_s => "#{OMF_NAMESPACE}/#{resource_type}" } : {}
end

#each_bound_request_property(&block) ⇒ Object

Loop over all the bound (sent with a value) properties of a request message.

Raises:

  • (NotImplementedError)


152
153
154
# File 'lib/omf_common/message.rb', line 152

def each_bound_request_property(&block)
  raise NotImplementedError
end

#each_property(&block) ⇒ Object

Raises:

  • (NotImplementedError)


138
139
140
# File 'lib/omf_common/message.rb', line 138

def each_property(&block)
  raise NotImplementedError
end

#each_unbound_request_property(&block) ⇒ Object

Loop over all the unbound (sent without a value) properties of a request message.

Raises:

  • (NotImplementedError)


145
146
147
# File 'lib/omf_common/message.rb', line 145

def each_unbound_request_property(&block)
  raise NotImplementedError
end

#error?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/omf_common/message.rb', line 177

def error?
  (itype || '') =~ /(error|ERROR|FAILED)/
end

#guard?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


164
165
166
# File 'lib/omf_common/message.rb', line 164

def guard?
  raise NotImplementedError
end

#has_properties?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/omf_common/message.rb', line 160

def has_properties?
  not properties.empty?
end

#itype(format = nil) ⇒ Object

Fetch inform type

When no format provided, return the value as it is.

Parameters:

  • format (Symbol) (defaults to: nil)

    to render itype, valid formats: :ruby, :frcp



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/omf_common/message.rb', line 192

def itype(format = nil)
  if format && !_get_core(:itype).nil?
    case format.to_sym
    when :ruby
      _get_core(:itype).to_s.downcase.gsub(/\./, '_')
    when :frcp
      _get_core(:itype).to_s.upcase.gsub(/_/, '.')
    else
      raise ArgumentError, "Unknown format '#{format}'. Please use ':ruby, :frcp' instead."
    end
  else
    _get_core(:itype)
  end
end

#marshall(include_cert = false) ⇒ Object

Raises:

  • (NotImplementedError)


211
212
213
# File 'lib/omf_common/message.rb', line 211

def marshall(include_cert = false)
  raise NotImplementedError
end

#propertiesObject

Raises:

  • (NotImplementedError)


156
157
158
# File 'lib/omf_common/message.rb', line 156

def properties
  raise NotImplementedError
end

#props_nsObject

Get all property namespace defs



226
227
228
229
# File 'lib/omf_common/message.rb', line 226

def props_ns
  @props_ns ||= {}
  default_props_ns.merge(@props_ns).stringify_keys
end

#resourceObject



168
169
170
171
# File 'lib/omf_common/message.rb', line 168

def resource
  name = _get_property(:res_id)
  OmfCommon.comm.create_topic(name)
end

#success?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/omf_common/message.rb', line 173

def success?
  ! error?
end

#to_sObject

Raises:

  • (NotImplementedError)


207
208
209
# File 'lib/omf_common/message.rb', line 207

def to_s
  raise NotImplementedError
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


215
216
217
# File 'lib/omf_common/message.rb', line 215

def valid?
  raise NotImplementedError
end