Class: CPEE::Instantiation::Instantiate

Inherits:
Riddl::Implementation
  • Object
show all
Defined in:
lib/cpee/instantiation.rb

Overview

}}}

Instance Method Summary collapse

Instance Method Details

#responseObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
# File 'lib/cpee/instantiation.rb', line 78

def response
  cpee = @a[0]
  tdoc = case @p[@p.length - 1].name
    when 'url'
      status, res = Riddl::Client.new(@p[@p.length - 1].value).get
      if status >= 200 && status < 300
        res[0].value.read
      else
        (@status = 500) && return
      end
    when 'xml'
      if @p[@p.length - 1].additional =~ /base64/
        Base64.decode64(@p[@p.length - 1].value.read)
      else
        @p[@p.length - 1].value.read
      end
  end

  if (ins = Testset::load(tdoc,cpee)) == -1
    @status = 500
  else
    if @p[0].value =~ /^wait/
      @headers << Riddl::Header.new('CPEE_CALLBACK','true')
      cb = @h['CPEE_CALLBACK']

      if cb
        srv = Riddl::Client.new(cpee, cpee + "?riddl-description")
        status, response = srv.resource("/#{ins}/notifications/subscriptions/").post [
          Riddl::Parameter::Simple.new("topic","state"),
          Riddl::Parameter::Simple.new("events","change"),
        ]
        key = response.first.value

        srv.resource("/#{ins}/notifications/subscriptions/#{key}/ws/").ws do |conn|
          conn.on :message do |msg|
            if JSON::parse(XML::Smart::string(msg.data).find('string(/event/notification)'))['state'] == 'finished'
              conn.close
              Riddl::Client.new(cb).put [
                # TODO extract all dataelements from instance
                Riddl::Parameter::Simple.new('instance',ins)
              ]
            end
          end
        end
      end
    end
    if @p.length > 1 && @p[0].value =~ /_running$/
      srv = Riddl::Client.new(cpee, cpee + "?riddl-description")
      res = srv.resource("/#{ins}/properties/values")
      status, response = res.put [
        Riddl::Parameter::Simple.new('name', 'state'),
        Riddl::Parameter::Simple.new('value','running')
      ]
    end
    return Riddl::Parameter::Simple.new("url",cpee + ins)
  end
end