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, :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

#issuerObject (readonly)

Returns the value of attribute issuer.



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

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
# File '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

.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



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

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



126
127
128
129
130
131
132
133
134
# File '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

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



183
184
185
186
# File '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

#default_props_nsObject

Construct default namespace of the props from resource type



222
223
224
225
# File '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

#each_bound_request_property(&block) ⇒ Object

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

Raises:

  • (NotImplementedError)


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

def each_bound_request_property(&block)
  raise NotImplementedError
end

#each_property(&block) ⇒ Object

Raises:

  • (NotImplementedError)


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

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)


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

def each_unbound_request_property(&block)
  raise NotImplementedError
end

#error?Boolean

Returns:

  • (Boolean)


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

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

#guard?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def guard?
  raise NotImplementedError
end

#has_properties?Boolean

Returns:

  • (Boolean)


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

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File '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

#marshall(include_cert = false) ⇒ Object

Raises:

  • (NotImplementedError)


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

def marshall(include_cert = false)
  raise NotImplementedError
end

#propertiesObject

Raises:

  • (NotImplementedError)


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

def properties
  raise NotImplementedError
end

#props_nsObject

Get all property namespace defs



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

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

#resourceObject



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

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

#resource_addressObject



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

def resource_address
  _get_property(:res_id)
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  ! error?
end

#to_sObject

Raises:

  • (NotImplementedError)


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

def to_s
  raise NotImplementedError
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def valid?
  raise NotImplementedError
end