Name: instantiation/server
| 1: | #!/usr/bin/ruby |
| 2: | # |
| 3: | # Apache License, Version 2.0 |
| 4: | # |
| 5: | # Copyright (c) 2013 Juergen Mangler |
| 6: | # |
| 7: | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8: | # you may not use this file except in compliance with the License. |
| 9: | # You may obtain a copy of the License at |
| 10: | # |
| 11: | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12: | # |
| 13: | # Unless required by applicable law or agreed to in writing, software |
| 14: | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15: | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16: | # See the License for the specific language governing permissions and |
| 17: | # limitations under the License. |
| 18: | |
| 19: | require 'rubygems' |
| 20: | require 'riddl/server' |
| 21: | require 'xml/smart' |
| 22: | require 'pp' |
| 23: | |
| 24: | # $cpee = 'xmpp://[email protected]' |
| 25: | $cpee = 'http://fp7-adventure.eu:9298/' |
| 26: | |
| 27: | module SPE |
| 28: | def create_instance(srv,pid) |
| 29: | srv.post [ |
| 30: | Riddl::Parameter::Simple.new("info",pid) |
| 31: | ] |
| 32: | end |
| 33: | |
| 34: | def set_description(srv,ins,desc) |
| 35: | res = srv.resource("/#{ins}/properties/values/description") |
| 36: | res.put [ |
| 37: | Riddl::Parameter::Simple.new("content","<content>#{desc}</content>") |
| 38: | ] |
| 39: | end |
| 40: | |
| 41: | def set_data(srv,ins,data) |
| 42: | res = srv.resource("/#{ins}/properties/values/dataelements") |
| 43: | res.post [ |
| 44: | Riddl::Parameter::Simple.new("value",data) |
| 45: | ] |
| 46: | end |
| 47: | |
| 48: | def set_state(srv,ins,state) |
| 49: | res = srv.resource("/#{ins}/properties/values/state") |
| 50: | res.put [ |
| 51: | Riddl::Parameter::Simple.new("value",state) |
| 52: | ] |
| 53: | end |
| 54: | |
| 55: | def set_endpoints(srv,ins,data) |
| 56: | res = srv.resource("/#{ins}/properties/values/endpoints") |
| 57: | res.post [ |
| 58: | Riddl::Parameter::Simple.new("value",data) |
| 59: | ] |
| 60: | end |
| 61: | |
| 62: | def set_properties(srv,ins,data) |
| 63: | res = srv.resource("/#{ins}/properties/values/properties") |
| 64: | res.put [ |
| 65: | Riddl::Parameter::Simple.new("content",data) |
| 66: | ] |
| 67: | end |
| 68: | |
| 69: | def set_transformation(srv,ins) |
| 70: | res = srv.resource("/#{ins}/properties/values/transformation") |
| 71: | |
| 72: | data = XML::Smart.string('<content><description type="rest">http://localhost:9295</description><dataelements type="rest"/><endpoints type="rest">http://localhost:9295</endpoints></content>') |
| 73: | res.put [ |
| 74: | Riddl::Parameter::Simple.new("content",data.to_s) |
| 75: | ] |
| 76: | end |
| 77: | |
| 78: | def initialize_monitoring(srv,ins) |
| 79: | res = srv.resource("/#{ins}/properties/values") |
| 80: | status, response = srv.resource("/#{ins}/notifications/subscriptions/").post [ |
| 81: | Riddl::Parameter::Simple.new("url","xmpp://[email protected]"), |
| 82: | Riddl::Parameter::Simple.new("topic","properties/state"), |
| 83: | Riddl::Parameter::Simple.new("events","change"), |
| 84: | Riddl::Parameter::Simple.new("topic","properties/dataelements"), |
| 85: | Riddl::Parameter::Simple.new("events","change"), |
| 86: | Riddl::Parameter::Simple.new("topic","properties/position"), |
| 87: | Riddl::Parameter::Simple.new("events","change"), |
| 88: | Riddl::Parameter::Simple.new("topic","running"), |
| 89: | Riddl::Parameter::Simple.new("events","activity_calling,activity_failed,activity_manipulating,activity_done") |
| 90: | ] |
| 91: | status, response = srv.resource("/#{ins}/notifications/subscriptions/").post [ |
| 92: | Riddl::Parameter::Simple.new("url","xmpp://[email protected]"), |
| 93: | Riddl::Parameter::Simple.new("topic","properties/state"), |
| 94: | Riddl::Parameter::Simple.new("events","change"), |
| 95: | Riddl::Parameter::Simple.new("topic","properties/dataelements"), |
| 96: | Riddl::Parameter::Simple.new("events","change"), |
| 97: | Riddl::Parameter::Simple.new("topic","properties/position"), |
| 98: | Riddl::Parameter::Simple.new("events","change"), |
| 99: | Riddl::Parameter::Simple.new("topic","running"), |
| 100: | Riddl::Parameter::Simple.new("events","activity_calling,activity_failed,activity_manipulating,activity_done") |
| 101: | ] |
| 102: | status, response = srv.resource("/#{ins}/notifications/subscriptions/").post [ |
| 103: | Riddl::Parameter::Simple.new("url","xmpp://[email protected]/events"), |
| 104: | Riddl::Parameter::Simple.new("topic","properties/state"), |
| 105: | Riddl::Parameter::Simple.new("events","change"), |
| 106: | Riddl::Parameter::Simple.new("topic","properties/state"), |
| 107: | Riddl::Parameter::Simple.new("votes","change"), |
| 108: | Riddl::Parameter::Simple.new("topic","simulating"), |
| 109: | Riddl::Parameter::Simple.new("events","step") |
| 110: | ] |
| 111: | status, response = srv.resource("/#{ins}/notifications/subscriptions/").post [ |
| 112: | Riddl::Parameter::Simple.new("url","xmpp://[email protected]"), |
| 113: | Riddl::Parameter::Simple.new("topic","running"), |
| 114: | Riddl::Parameter::Simple.new("events","activity_done"), |
| 115: | Riddl::Parameter::Simple.new("topic","semantic"), |
| 116: | Riddl::Parameter::Simple.new("events","geolocation,alert,co2,oee,sensor,energyconsumption") |
| 117: | ] |
| 118: | status, response = srv.resource("/#{ins}/notifications/subscriptions/").post [ |
| 119: | Riddl::Parameter::Simple.new("url","xmpp://[email protected]"), |
| 120: | Riddl::Parameter::Simple.new("topic","running"), |
| 121: | Riddl::Parameter::Simple.new("events","activity_done"), |
| 122: | Riddl::Parameter::Simple.new("topic","semantic"), |
| 123: | Riddl::Parameter::Simple.new("events","geolocation,alert,co2,oee,sensor,energyconsumption") |
| 124: | ] |
| 125: | end |
| 126: | end |
| 127: | |
| 128: | class Spawn < Riddl::Implementation |
| 129: | include SPE |
| 130: | |
| 131: | def response |
| 132: | doc = XML::Smart.string(@p[0].value.read) |
| 133: | doc.register_namespace 'g', 'http://www.fp7-adventure.eu/xmlSchema/Gateways/' |
| 134: | mop = doc.find('string(//g:header/g:operationName)') |
| 135: | |
| 136: | if mop == 'spawn' |
| 137: | pid = doc.find('string(//g:payload/g:inputParameter[@paramName="instance"])') |
| 138: | |
| 139: | srv = Riddl::Client.new("xmpp://[email protected]", File.dirname(__FILE__) + '/processfetch.xml', :xmpp => @env['xmpp']) |
| 140: | status, res = srv.get [ |
| 141: | Riddl::Parameter::Complex.new('pid','text/plain',pid) |
| 142: | ] |
| 143: | |
| 144: | if status == 200 |
| 145: | desc = XML::Smart.string(res.first.value.read) |
| 146: | |
| 147: | srv = Riddl::Client.new($cpee, "http://fp7-adventure.eu:9298/?riddl-description", :xmpp => @env['xmpp']) |
| 148: | status, response = create_instance srv, pid |
| 149: | |
| 150: | ins = -1 |
| 151: | if status == 200 |
| 152: | ins = response.first.value |
| 153: | initialize_monitoring srv, ins |
| 154: | |
| 155: | status, response = set_description srv, ins, desc.root.dump |
| 156: | if status == 200 |
| 157: | sleep 1 |
| 158: | set_transformation(srv,ins) |
| 159: | |
| 160: | doc.find('//g:payload/g:inputParameter[starts-with(@paramName,"data_")]').each do |d| |
| 161: | tdoc = XML::Smart.string("<#{d.attributes['paramName'][5..-1]}/>") |
| 162: | tdoc.root.text = d.text |
| 163: | set_data srv, ins, tdoc.root.dump |
| 164: | end |
| 165: | |
| 166: | doc.find('//g:payload/g:inputParameter[starts-with(@paramName,"endpoint_")]').each do |ep| |
| 167: | tdoc = XML::Smart.string("<#{ep.attributes['paramName'][9..-1]}/>") |
| 168: | tdoc.root.text = ep.text |
| 169: | set_endpoints srv, ins, tdoc.root.dump |
| 170: | end |
| 171: | |
| 172: | if @h['CPEE_CALLBACK'] |
| 173: | gwr = XML::Smart.open_unprotected(File.dirname(__FILE__) + "/gatewayresponse.xml") |
| 174: | gwr.register_namespace 'g', 'http://www.fp7-adventure.eu/xmlSchema/Gateways/' |
| 175: | gwr.find('//g:payload/g:outputData/g:summary/g:info[@name="spawn"]/g:info').first.attributes['value'] = ins |
| 176: | |
| 177: | srv = Riddl::Client.new("xmpp://" + @h['CPEE_CALLBACK'], File.dirname(__FILE__) + "/callback.xml", :xmpp => @env['xmpp']) |
| 178: | srv.put [ |
| 179: | Riddl::Parameter::Complex.new("gatewayResponse","text/xml",gwr.root.dump) |
| 180: | ] |
| 181: | end |
| 182: | end |
| 183: | end |
| 184: | |
| 185: | end |
| 186: | end |
| 187: | nil |
| 188: | end |
| 189: | end |
| 190: | |
| 191: | class Instantiate < Riddl::Implementation |
| 192: | include SPE |
| 193: | |
| 194: | def response |
| 195: | srv = Riddl::Client.new($cpee, "http://fp7-adventure.eu:9298/?riddl-description", :xmpp => @env['xmpp']) |
| 196: | tdoc = @p[2].value.read |
| 197: | File::write(File.dirname(__FILE__) + '/currentmodel.xml', tdoc) |
| 198: | doc = XML::Smart::string(tdoc) |
| 199: | doc.register_namespace 'b', 'http://www.omg.org/spec/BPMN/20100524/MODEL' |
| 200: | pid = doc.find('string(/b:definitions/b:process/@id)') |
| 201: | |
| 202: | ins = -1 |
| 203: | case @p[3].value |
| 204: | when 'simulating' |
| 205: | status, response = create_instance srv, pid + '_sim' |
| 206: | else |
| 207: | status, response = create_instance srv, pid |
| 208: | end |
| 209: | |
| 210: | if status == 200 |
| 211: | ins = response.first.value |
| 212: | |
| 213: | initialize_monitoring srv, ins |
| 214: | |
| 215: | pdoc = XML::Smart.string("<content/>") |
| 216: | pdoc.root.add('processname',@p[0].value == 'ready' ? 'Oscar, fix it you magnificient bastard!' : @p[0].value) |
| 217: | pdoc.root.add('factory',@p[1].value == 'ready' ? '[email protected]' : @p[1].value) |
| 218: | set_properties srv, ins, pdoc.root.dump |
| 219: | |
| 220: | status, response = set_description srv, ins, doc.root.dump |
| 221: | |
| 222: | if status == 200 |
| 223: | sleep 1 |
| 224: | set_transformation(srv,ins) |
| 225: | |
| 226: | case @p[3].value |
| 227: | when 'running' |
| 228: | # set_state srv, ins, 'running' |
| 229: | when 'simulating' |
| 230: | set_state srv, ins, 'simulating' |
| 231: | end |
| 232: | return Riddl::Parameter::Simple.new("id",ins.to_i) |
| 233: | end |
| 234: | end |
| 235: | @status = 500 |
| 236: | end |
| 237: | end |
| 238: | |
| 239: | class Forward < Riddl::Implementation |
| 240: | def response |
| 241: | srv = Riddl::Client.new($cpee, "http://fp7-adventure.eu:9298/?riddl-description", :xmpp => @env['xmpp']) |
| 242: | res = srv.resource("/#{@r[0]}") |
| 243: | status, response = res.get |
| 244: | if status == 200 |
| 245: | @headers << Riddl::Header.new("location", "#{$cpee}#{@r[0]}") |
| 246: | @status = 302 |
| 247: | return Riddl::Parameter::Complex.new("info","application/xml","<html><body>go to #{$cpee}#{@r[0]}</body></html>") |
| 248: | end |
| 249: | end |
| 250: | end |
| 251: | |
| 252: | Riddl::Server.new(File.dirname(__FILE__) + '/instantiation.xml', :port => 9296, :debug => STDOUT) do |
| 253: | xmpp '[email protected]', 'adventure_processinstantiation' |
| 254: | accessible_description true |
| 255: | cross_site_xhr true |
| 256: | |
| 257: | on resource do |
| 258: | run Instantiate if post 'instantiate' |
| 259: | run Spawn if post 'spawn' |
| 260: | on resource do |
| 261: | run Forward if get |
| 262: | end |
| 263: | end |
| 264: | end.loop! |
