Class: OMF::SFA::AM::AMLiaison

Inherits:
Base::LObject
  • Object
show all
Includes:
OmfCommon::DSL::Xmpp
Defined in:
lib/omf-sfa/am/am_liaison.rb

Overview

This class implements the AM Liaison

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAMLiaison

Returns a new instance of AMLiaison.



21
22
23
24
# File 'lib/omf-sfa/am/am_liaison.rb', line 21

def initialize
  #@comm = OmfCommon::Comm.new(:xmpp)
  #EM.next_tick { @comm.connect('am_liaison', 'pw', 'localhost') }
end

Instance Attribute Details

#commObject

Returns the value of attribute comm.



18
19
20
# File 'lib/omf-sfa/am/am_liaison.rb', line 18

def comm
  @comm
end

Instance Method Details

#enable_lease(lease, component) ⇒ Object

It will send the corresponding create messages to the components contained in the lease when the lease is about to start. At the end of the lease the corresponding release messages will be sent to the components.

Parameters:

  • lease (OLease)

    Contains the lease information “valid_from” and “valid_until” along with the reserved components



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/omf-sfa/am/am_liaison.rb', line 37

def enable_lease(lease, component)
  debug "enable_lease: lease: '#{lease.inspect}' component: '#{component.inspect}'"

  resource_topic = @comm.get_topic(component.name)
  raise UnknownResourceException.new "Cannot find resource's pubsub topic: '#{component.inspect}'" unless resource_topic

  msg = @comm.create_message do |message|
    message.property('name', component.name)
    message.property('type', component.type)
  end

  msg.on_inform_created do |message|
    logger.info "Resource '#{message.resource_id}' successfully created at #{Time.now}"
    created_topic = @comm.get_topic(message.resource_id)

    # TODO: we need to define whether we ask for specific resource_id in the create
    # message or we let the resource_factory to pick one. We need to keep it synced 
    # with the resource_id we have in the DB. 
    resource_id = created_topic.id

    r_msg = @comm.release_message { |m| m.element('resource_id', resource_id) }

    r_msg.on_inform_released do |message|
      logger.info "Resource (#{message.resource_id}) released at #{Time.now}"
    end

    timer = EventMachine::Timer.new(lease[:valid_until] - Time.now) do
      r_msg.publish resource_topic.id
      @leases[lease].delete(component.id)
      @leases.delete(lease) if @leases[lease].empty?
    end
    @leases[lease][component.id] = {:end => timer}
  end

  timer = EventMachine::Timer.new(lease[:valid_from] - Time.now) do
    resource_topic.subscribe do
      # If subscribed, we publish a 'create' message
      msg.publish resource_topic.id
    end
  end

  @leases[lease] = {} unless @leases[lease]
  @leases[lease] = { component.id => {:start => timer} }
end