Class: OmfCommon::Message

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

Direct Known Subclasses

Json::Message, XML::Message

Defined Under Namespace

Classes: Json, XML

Constant Summary

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, :assert]
OMF_CORE_WRITE =
[:replyto, :itype, :guard, :assert]
@@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

- (Object) issuer (readonly)

Returns the value of attribute issuer



98
99
100
# File 'omf_common/lib/omf_common/message.rb', line 98

def issuer
  @issuer
end

Class Method Details

+ (Boolean) authenticate?

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

Returns:

  • (Boolean)


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

def self.authenticate?
  @@authenticate_messages
end

+ (Object) create(type, properties, body = {})



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

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

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



41
42
43
44
# File 'omf_common/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

+ (Object) init(opts = {})



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
# File 'omf_common/lib/omf_common/message.rb', line 68

def self.init(opts = {})
  unless @@message_class
    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
end

+ (Object) parse(str, content_type = nil, &block)

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 'omf_common/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

- (Object) [](name, ns = nil)

To access properties

Parameters:

  • name (String)

    of the property

  • ns (Hash) (defaults to: nil)

    namespace of property



118
119
120
# File 'omf_common/lib/omf_common/message.rb', line 118

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

- (Object) []=(name, ns = nil, value)

To set properties

Parameters:

  • name (String)

    of the property

  • ns (Hash) (defaults to: nil)

    namespace of property



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

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

- (Object) _get_core(key) (private)

Raises:

  • (NotImplementedError)


247
248
249
# File 'omf_common/lib/omf_common/message.rb', line 247

def _get_core(key)
  raise NotImplementedError
end

- (Object) _get_property(name, ns = nil) (private)

Raises:

  • (NotImplementedError)


235
236
237
# File 'omf_common/lib/omf_common/message.rb', line 235

def  _get_property(name, ns = nil)
  raise NotImplementedError
end

- (Object) _set_core(key, value) (private)

Raises:

  • (NotImplementedError)


243
244
245
# File 'omf_common/lib/omf_common/message.rb', line 243

def _set_core(key, value)
  raise NotImplementedError
end

- (Object) _set_property(name, value, ns = nil) (private)

Raises:

  • (NotImplementedError)


239
240
241
# File 'omf_common/lib/omf_common/message.rb', line 239

def  _set_property(name, value, ns = nil)
  raise NotImplementedError
end

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



183
184
185
186
# File 'omf_common/lib/omf_common/message.rb', line 183

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

- (Object) default_props_ns

Construct default namespace of the props from resource type



222
223
224
225
# File 'omf_common/lib/omf_common/message.rb', line 222

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

- (Object) each_bound_request_property(&block)

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

Raises:

  • (NotImplementedError)


150
151
152
# File 'omf_common/lib/omf_common/message.rb', line 150

def each_bound_request_property(&block)
  raise NotImplementedError
end

- (Object) each_property(&block)

Raises:

  • (NotImplementedError)


136
137
138
# File 'omf_common/lib/omf_common/message.rb', line 136

def each_property(&block)
  raise NotImplementedError
end

- (Object) each_unbound_request_property(&block)

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

Raises:

  • (NotImplementedError)


143
144
145
# File 'omf_common/lib/omf_common/message.rb', line 143

def each_unbound_request_property(&block)
  raise NotImplementedError
end

- (Boolean) error?

Returns:

  • (Boolean)


179
180
181
# File 'omf_common/lib/omf_common/message.rb', line 179

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

- (Boolean) guard?

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


162
163
164
# File 'omf_common/lib/omf_common/message.rb', line 162

def guard?
  raise NotImplementedError
end

- (Boolean) has_properties?

Returns:

  • (Boolean)


158
159
160
# File 'omf_common/lib/omf_common/message.rb', line 158

def has_properties?
  not properties.empty?
end

- (Object) itype(format = nil)

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



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

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

- (Object) marshall(include_cert = false)

Raises:

  • (NotImplementedError)


213
214
215
# File 'omf_common/lib/omf_common/message.rb', line 213

def marshall(include_cert = false)
  raise NotImplementedError
end

- (Object) properties

Raises:

  • (NotImplementedError)


154
155
156
# File 'omf_common/lib/omf_common/message.rb', line 154

def properties
  raise NotImplementedError
end

- (Object) props_ns

Get all property namespace defs



228
229
230
231
# File 'omf_common/lib/omf_common/message.rb', line 228

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

- (Object) resource



166
167
168
169
# File 'omf_common/lib/omf_common/message.rb', line 166

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

- (Object) resource_address



171
172
173
# File 'omf_common/lib/omf_common/message.rb', line 171

def resource_address
  _get_property(:res_id)
end

- (Boolean) success?

Returns:

  • (Boolean)


175
176
177
# File 'omf_common/lib/omf_common/message.rb', line 175

def success?
  ! error?
end

- (Object) to_s

Raises:

  • (NotImplementedError)


209
210
211
# File 'omf_common/lib/omf_common/message.rb', line 209

def to_s
  raise NotImplementedError
end

- (Boolean) valid?

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


217
218
219
# File 'omf_common/lib/omf_common/message.rb', line 217

def valid?
  raise NotImplementedError
end