Name: gateway-timer/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 'chronic_duration'
23:
 
24:
class Sleep < Riddl::Implementation
25:
  def response
26:
    doc = XML::Smart.string(@p[0].value.read)
27:
    doc.register_namespace 'g', 'http://www.fp7-adventure.eu/xmlSchema/Gateways/'
28:
    mop = doc.find('string(//g:header/g:operationName)')
29:
 
30:
    if mop == 'sleep'
31:
      sleep = doc.find('string(//g:payload/g:inputParameter[@paramName="sleep"])')
32:
 
33:
      secs = ChronicDuration.parse(sleep)
34:
 
35:
      sleep(secs) unless secs.nil?
36:
 
37:
      if @h['CPEE_CALLBACK']
38:
        gwr = XML::Smart.open_unprotected(File.dirname(__FILE__) + "/gatewayresponse.xml")
39:
        gwr.register_namespace 'g', 'http://www.fp7-adventure.eu/xmlSchema/Gateways/'
40:
 
41:
        srv = Riddl::Client.new("xmpp://" + @h['CPEE_CALLBACK'], File.dirname(__FILE__) + "/callback.xml", :xmpp => @env['xmpp'])
42:
        srv.put [
43:
          Riddl::Parameter::Complex.new("gatewayResponse","text/xml",gwr.root.dump)
44:
        ]  
45:
      end
46:
    end
47:
    nil
48:
  end
49:
end
50:
 
51:
Riddl::Server.new(File.dirname(__FILE__) + '/gateway.xml', :port => 9293, :debug => STDOUT) do
52:
  xmpp '[email protected]', 'adventure_gateway_timer'
53:
  accessible_description true
54:
  cross_site_xhr true
55:
 
56:
  on resource do
57:
    run Sleep if post 'gateway-request'
58:
  end
59:
end.loop!