Name: simulation/lib/simulation.rb
| 1: | require 'pp' |
| 2: | |
| 3: | $container = {} |
| 4: | |
| 5: | class SimulationSave < Riddl::Implementation |
| 6: | def response |
| 7: | if @p[1].value == 'properties/state' && @p[2].value == 'change' |
| 8: | noti = JSON.parse(@p[3].value) |
| 9: | instance = noti['instance'].to_i |
| 10: | if noti['state'] == 'simulating' |
| 11: | sleep 0.5 until $container[instance].nil? |
| 12: | $container[instance] = [] |
| 13: | unless File.exists?(File.dirname(__FILE__) + "/../instances/#{instance}") |
| 14: | Dir.mkdir(File.dirname(__FILE__) + "/../instances/#{instance}") |
| 15: | File.write(File.dirname(__FILE__) + "/../instances/#{instance}/meta",noti['info'].gsub(/_sim$/,'')) |
| 16: | end |
| 17: | begin |
| 18: | meta = { :date => Time.now, :offset => "", :status => "running" } |
| 19: | id = id.nil? ? 0 : id + 1 |
| 20: | Dir.mkdir(File.dirname(__FILE__) + "/../instances/#{instance}/#{id}") |
| 21: | |
| 22: | File.write(File.dirname(__FILE__) + "/../instances/#{instance}/#{id}/meta",JSON.generate(meta)) |
| 23: | |
| 24: | desc = @h['CPEE_INSTANCE'].split(',')[0] + "/properties/values/endpoints?riddl-resource-description" |
| 25: | source = "xmpp://" + @h['CPEE_INSTANCE'].split(',')[1] + "/properties/values/endpoints" |
| 26: | status, res = Riddl::Client.new(source,desc,:xmpp => @env['xmpp']).get |
| 27: | File.write(File.dirname(__FILE__) + "/../instances/#{instance}/#{id}/endpoints",res[0].value.read) |
| 28: | |
| 29: | desc = @h['CPEE_INSTANCE'].split(',')[0] + "/properties/values/dslx?riddl-resource-description" |
| 30: | source = "xmpp://" + @h['CPEE_INSTANCE'].split(',')[1] + "/properties/values/dslx" |
| 31: | status, res = Riddl::Client.new(source,desc,:xmpp => @env['xmpp']).get |
| 32: | File.write(File.dirname(__FILE__) + "/../instances/#{instance}/#{id}/dslx",res[0].value.read) |
| 33: | |
| 34: | FileUtils.cp(File.dirname(__FILE__) + "/../resources/result.html",File.dirname(__FILE__) + "/../instances/#{instance}/#{id}/result.html") |
| 35: | rescue SystemCallError |
| 36: | retry |
| 37: | end |
| 38: | end |
| 39: | end |
| 40: | return [ |
| 41: | Riddl::Parameter::Simple.new("continue","true"), |
| 42: | Riddl::Parameter::Simple.new("fingerprint-with-consumer-secret","") |
| 43: | ] |
| 44: | end |
| 45: | end |
| 46: | |
| 47: | class Simulation < Riddl::Implementation |
| 48: | def response |
| 49: | if @p[1].value == 'properties/state' && @p[2].value == 'change' |
| 50: | noti = JSON.parse(@p[3].value) |
| 51: | instance = File.basename(noti['instance'].split(',',2)[0]).to_i |
| 52: | if noti['state'] != 'simulating' && !$container[instance].nil? |
| 53: | sleep 3 # save when all events have arrived |
| 54: | noti = JSON.parse(@p[3].value) |
| 55: | curr = Dir.glob(File.dirname(__FILE__) + "/../instances/#{instance}/*").map{ |x| File.basename(x).to_i }.max |
| 56: | unless $container[instance].nil? |
| 57: | File.write(File.dirname(__FILE__) + "/../instances/#{instance}/#{curr}/model",JSON.pretty_generate($container[instance])) |
| 58: | $container.delete(instance) |
| 59: | end |
| 60: | |
| 61: | pid = fork do |
| 62: | p File.expand_path(File.dirname(__FILE__)) + "/standalone.rb #{instance}/#{curr}" |
| 63: | system File.expand_path(File.dirname(__FILE__)) + "/standalone.rb #{instance}/#{curr}" |
| 64: | end |
| 65: | Process.detach(pid) |
| 66: | end |
| 67: | end |
| 68: | if @p[1].value == 'simulating' && @p[2].value == 'step' |
| 69: | noti = JSON.parse(@p[3].value) |
| 70: | instance = File.basename(noti['instance'].split(',',2)[0]).to_i |
| 71: | curr = Dir.glob(File.dirname(__FILE__) + "/../instances/#{instance}/*").map{ |x| File.basename(x).to_i }.max |
| 72: | |
| 73: | $container[instance] << JSON.parse(@p[3].value) |
| 74: | end |
| 75: | nil |
| 76: | end |
| 77: | end |
