lib/sfpagent/agent.rb in sfpagent-0.1.9 vs lib/sfpagent/agent.rb in sfpagent-0.1.10
- old
+ new
@@ -23,10 +23,11 @@
ModelFile = "#{CachedDir}/sfpagent.model"
AgentsDataFile = "#{CachedDir}/sfpagent.agents"
BSigFile = "#{CachedDir}/bsig.model"
BSigPIDFile = "#{CachedDir}/bsig.pid"
+ BSigThreadsLockFile = "#{CachedDir}/bsig.threads.lock.#{Time.now.nsec}"
@@logger = WEBrick::Log.new(LogFile, WEBrick::BasicLog::INFO ||
WEBrick::BasicLog::ERROR ||
WEBrick::BasicLog::FATAL ||
WEBrick::BasicLog::WARN)
@@ -88,10 +89,11 @@
# trap signal
['INT', 'KILL', 'HUP'].each { |signal|
trap(signal) {
Sfp::Agent.logger.info "Shutting down web server"
+ bsig_engine.disable
server.shutdown
}
}
# send request to local web server to save its PID
@@ -691,17 +693,18 @@
# There is an error on setting the model!
[500, '', '']
end
def get_model
- model = Sfp::Agent.get_model
-
# The model is not exist.
- return [404, '', ''] if model.nil?
+ return [404, '', ''] if not File.exist?(Sfp::Agent::ModelFile)
- # The model is exist, and then send it in JSON.
- return [200, 'application/json', JSON.generate(model)] if !!model
+ begin
+ # The model is exist, and then send it in JSON.
+ return [200, 'application/json', File.read(Sfp::Agent::ModelFile)]
+ rescue
+ end
# There is an error when retrieving the model!
[500, '', '']
end
@@ -748,11 +751,9 @@
[500, '', '']
end
def satisfy_bsig_request(p={})
return [400, '', ''] if not p[:query]
-
-Sfp::Agent.logger.info Sfp::Agent.bsig_engine.to_s
return [500, '', ''] if Sfp::Agent.bsig_engine.nil?
req = p[:query]
return [200, '', ''] if Sfp::Agent.bsig_engine.receive_goal_from_agent(req['id'].to_i, JSON[req['goal']], req['pi'].to_i)