Class: WiseOMF::Client::WiseGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/wise_omf/client.rb

Overview

The WiseGroup is the representation of an omf resource group which provides the ability to register callback for requests and configure messages. You should not create an instance of this group directly. This can cause unwanted side effects. The better way is to ask the ReservationManager (factory) for a group for a list of node urns.

Constant Summary collapse

@@default_message_types =

Message types to call callbacks for

[:response, :inform]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, uid) ⇒ WiseGroup

Creates a new WiseGroup which handles an OmfEc::Group for the given name And connects the resource with the given uid. Speaking in OMF, the resource represented by the given uid becomes a member of the newly created Group. The WiseGroup encapsulates the OMF group and handles the proper registration of callbacks for configure and request messages.

However, you are free to use the OmfEc::Group directly, but it’s highly recommended to use a WiseGroup as helper.

Parameters:

  • name (String)

    the name for the OmfEc::Group.

  • uid (String)

    the uid of the resource represented by this group.

See Also:

  • OmfEc::Group


56
57
58
59
60
61
62
63
64
65
# File 'lib/wise_omf/client.rb', line 56

def initialize(name, uid)
  @callback_cache = LRUCache.new(ttl: 30.minutes)
  @name = name
  @uid = uid
  group = OmfEc::Group.new(name)
  OmfEc.experiment.add_group(group)
  group.add_resource(uid)
  @group = group
  info "Finished intialization of WiseGroup (#{name})"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

This method translates calls to configure_xxx and request_xxx into proper FRCP messages (like in the OMF)

Parameters:

  • name (String)

    the name of the method

  • *args (Array)

    an array of arguments (empty for request_xxx, containing one argument for configure_xxx)

  • &block

    a block which should be set as callback for the request/ configure message



129
130
131
132
133
134
135
# File 'lib/wise_omf/client.rb', line 129

def method_missing(name, *args, &block)
  if name =~ /set_(.+)/
    configure($1, args[0], &block)
  else
    request(name, &block)
  end
end

Instance Attribute Details

#default_callbackObject

Returns the value of attribute default_callback.



38
39
40
# File 'lib/wise_omf/client.rb', line 38

def default_callback
  @default_callback
end

#groupObject

Returns the value of attribute group.



38
39
40
# File 'lib/wise_omf/client.rb', line 38

def group
  @group
end

#nameObject

Returns the value of attribute name.



38
39
40
# File 'lib/wise_omf/client.rb', line 38

def name
  @name
end

#uidObject

Returns the value of attribute uid.



38
39
40
# File 'lib/wise_omf/client.rb', line 38

def uid
  @uid
end

Instance Method Details

#configure(property, value, &block) ⇒ Object

Send a configure message for the given property and callback

Parameters:

  • property (String)

    the property to configure

  • &block

    the callback to be called for responses to this configuration attempt



116
117
118
119
120
121
122
# File 'lib/wise_omf/client.rb', line 116

def configure(property, value, &block)
  mid = WiseOMF::Client::ExperimentHelper.messageUID
  unless block.nil?
    @callback_cache.store(mid, block)
  end
  @group.topic.configure({property => {requestId: mid, value: value}})
end

#delete_callback(requestId) ⇒ Object

Method for deleting a callback from the callback cache. After deleting the callback, it will not be called for messages arriving with the given requestId

Parameters:

  • requestId (Integer)

    the requestId to delete the callback for



141
142
143
# File 'lib/wise_omf/client.rb', line 141

def delete_callback(requestId)
  @callback_cache.delete(requestId)
end

#doneObject

Terminates this group (unsubscribes topic…)



146
147
148
# File 'lib/wise_omf/client.rb', line 146

def done
  self.group.topic.unsubscribe(:wise_group_callback_handler)
end

#init_callback(&block) ⇒ Object

This method initializes the callback handler on the group topic. The topic might be nil direct after the intialization.

Parameters:

  • &block

    a block that should be called after initializing the topic callback



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
# File 'lib/wise_omf/client.rb', line 71

def init_callback(&block)
  if @group.topic.nil?
    debug "Delaying callback creation for 1 seconds"
    OmfCommon.el.after(1) {
      init_callback(&block)
    }
    return
  end
  info "Setting message callback for WiseGroup (#{self.name})"
  @group.topic.on_message(:wise_group_callback_handler) { |msg|
    rid = msg.content.properties.requestId
    if rid.nil? && @@default_message_types.include?(msg.content.type)
      self.default_callback.call(msg) unless self.default_callback.nil?
    else
      callback = @callback_cache.fetch(rid)
      unless callback.nil?
        callback.call(msg.content.properties)
      else
        if @@default_message_types.include? msg.content.type
          self.default_callback.call(msg) unless self.default_callback.nil?
        end
      end
    end
  }
  block.call(self) if block

end

#request(property, &block) ⇒ Object

Send a request message for the given property and callback

Parameters:

  • property (String)

    the property to request

  • &block

    the callback to be called for responses to this request



103
104
105
106
107
108
109
110
# File 'lib/wise_omf/client.rb', line 103

def request(property, &block)
  mid = WiseOMF::Client::ExperimentHelper.messageUID
  unless block.nil?
    @callback_cache.store(mid, block)
  end
  fail "Can't request topic here" if property.to_sym.eql? 'topic'.to_sym
  @group.topic.configure({property => mid})
end