Class: Sfp::BSig

Inherits:
Object
  • Object
show all
Includes:
Nuri::Net::Helper
Defined in:
lib/sfpagent/bsig.rb

Constant Summary collapse

BSigSleepTime =
5
MaxTries =
5
SatisfierPath =
'/bsig/satisfier'
CachedDir =
(Process.euid == 0 ? '/var/sfpagent' : File.expand_path('~/.sfpagent'))
SatisfierLockFile =
CachedDir + '/bsig.satisfier.lock'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Nuri::Net::Helper

#get_data, #http_request, #post_data, #put_data

Constructor Details

#initializeBSig

Returns a new instance of BSig.



15
16
17
18
# File 'lib/sfpagent/bsig.rb', line 15

def initialize
  @lock = Mutex.new
  @enabled = false
end

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



13
14
15
# File 'lib/sfpagent/bsig.rb', line 13

def enabled
  @enabled
end

#modeObject

Returns the value of attribute mode.



13
14
15
# File 'lib/sfpagent/bsig.rb', line 13

def mode
  @mode
end

Instance Method Details

#achieve_local_goal(id, goal, operators, pi, mode = nil) ⇒ Object

 returns :no_flaw : there is no goal-flaw :failure : there is a failure on achieving the goal :ongoing : the selected operator is being executed :repaired : some goal-flaws have been repaired, but the goal may have other flaws



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/sfpagent/bsig.rb', line 104

def achieve_local_goal(id, goal, operators, pi, mode=nil)
  operator = nil

  current = get_current_state
  flaws = compute_flaws(goal, current)
  return :no_flaw if flaws.length <= 0
Sfp::Agent.logger.info "[#{mode}] Flaws: #{JSON.generate(flaws)}"

  operator = select_operator(flaws, operators, pi)
  return :failure if operator.nil?

  @lock.synchronize {
    return :ongoing if operator['selected']
    operator['selected'] = true
Sfp::Agent.logger.info "[#{mode}] Selected operator: #{JSON.generate(operator)}"
  }

  next_pi = pi + 1
  pre_local, pre_remote = split_preconditions(operator)

Sfp::Agent.logger.info "[#{mode}] local-flaws: #{JSON.generate(pre_local)}"
Sfp::Agent.logger.info "[#{mode}] remote-flaws: #{JSON.generate(pre_remote)}"

  status = nil
  tries = MaxTries
  begin
    status = achieve_local_goal(id, pre_local, operators, next_pi)
    if status == :no_flaw or status == :failure or not @enabled
      break
    elsif status == :ongoing
      sleep BSigSleepTime
      tries += 1
    elsif status == :repaired
      tries = MaxTries
    end
    tries -= 1
  end until tries <= 0

#Sfp::Agent.logger.info "[#{mode}] status local: " + status.to_s
  return :failure if status == :failure

  return :failure if not achieve_remote_goal(id, pre_remote, next_pi)

  return :failure if not invoke(operator)
  
  :repaired

ensure
  @lock.synchronize { operator['selected'] = false } if not operator.nil?
end

#achieve_remote_goal(id, goal, pi) ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'lib/sfpagent/bsig.rb', line 155

def achieve_remote_goal(id, goal, pi)
  if goal.length > 0
    agents = Sfp::Agent.get_agents
    split_goal_by_agent(goal).each do |agent,g|
      return false if not agents.has_key?(agent) or agents[agent]['sfpAddress'].to_s == ''
      return false if not send_goal_to_agent(agents[agent], id, g, pi)
    end
  end
  true
end

#disableObject



20
21
22
# File 'lib/sfpagent/bsig.rb', line 20

def disable
  @enabled = false
end

#enable(p = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sfpagent/bsig.rb', line 24

def enable(p={})
  @lock.synchronize {
    return if @enabled
    @enabled = true
  }

  if p[:mode] == :main
    enable_main_thread
  elsif p[:mode] == :satisfier
    enable_satisfier_thread
  end
end

#enable_main_threadObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sfpagent/bsig.rb', line 42

def enable_main_thread
  @mode = :main

  ['INT', 'KILL', 'HUP'].each { |signal|
    trap(signal) {
      Sfp::Agent.logger.info "Shutting down BSig engine"
      disable
    }
  }
  register_satisfier_thread(:reset)

  Sfp::Agent.logger.info "[main] BSig engine is running."

  puts "BSig Engine is running with PID #{$$}"
  File.open(Sfp::Agent::BSigPIDFile, 'w') { |f| f.write($$.to_s) }

  self.execute_model

  Sfp::Agent.logger.info "[main] BSig engine has stopped."
end

#enable_satisfier_threadObject



37
38
39
40
# File 'lib/sfpagent/bsig.rb', line 37

def enable_satisfier_thread
  @mode = :satisfier
  Sfp::Agent.logger.info "[satisfier] BSig engine is enabled."
end

#execute_modelObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sfpagent/bsig.rb', line 63

def execute_model
  while @enabled
    begin
      Sfp::Agent.logger.info "[main] Sfp::BSig enabled"

      wait_for_satisfier?

      bsig = Sfp::Agent.get_bsig
      if bsig.nil?
        sleep BSigSleepTime
      else
        status = achieve_local_goal(bsig['id'], bsig['goal'], bsig['operators'], 1, :main)
Sfp::Agent.logger.info "[main] execute model - status: " + status.to_s
        if status == :failure
          Sfp::Agent.logger.error "[main] Executing BSig model [Failed]"
          sleep BSigSleepTime
        elsif status == :no_flaw
          sleep BSigSleepTime
        end
      end
    rescue Exception => e
      Sfp::Agent.logger.error "Error on executing BSig model\n#{e}\n#{e.backtrace.join("\n")}"
      sleep BSigSleepTime
    end
  end
end

#receive_goal_from_agent(id, goal, pi) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/sfpagent/bsig.rb', line 166

def receive_goal_from_agent(id, goal, pi)
  register_satisfier_thread

Sfp::Agent.logger.info "[satisfier] enabled: " + @enabled.to_s
  return false if not @enabled

  bsig = Sfp::Agent.get_bsig

Sfp::Agent.logger.info "[satisfier] receive_goal_from_agent - " + id.inspect + " - " + goal.inspect + " - " + pi.inspect
Sfp::Agent.logger.info "[satisfier] " + bsig.inspect

  return false if bsig.nil? or id < bsig['id']

  status = nil
  tries = MaxTries
  begin
    status = achieve_local_goal(bsig['id'], goal, bsig['operators'], pi, :satisfier)
    if status == :no_flaw or status == :failure or not @enabled
      break
    elsif status == :ongoing
      sleep BSigSleepTime
      tries += 1
    elsif status == :repaired
      tries = MaxTries
    end
    tries -= 1
  end until tries <= 0

  return false if status == :failure

  true

ensure
  unregister_satisfier_thread
end

#wait_for_satisfier?Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
# File 'lib/sfpagent/bsig.rb', line 90

def wait_for_satisfier?
  total_satisfier = 1
  loop do
    total_satisfier = (File.exist?(SatisfierLockFile) ? File.read(SatisfierLockFile).to_i : 0)
    return if total_satisfier <= 0
    sleep 1
  end
end