Class: ICuke::Simulator

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Timeout
Defined in:
lib/icuke/waxsim.rb,
lib/icuke/simulator.rb

Defined Under Namespace

Classes: Error, Process

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_processObject

Returns the value of attribute current_process.



8
9
10
# File 'lib/icuke/waxsim.rb', line 8

def current_process
  @current_process
end

Instance Method Details

#fire_event(event) ⇒ Object



42
43
44
# File 'lib/icuke/simulator.rb', line 42

def fire_event(event)
  get '/event', :query => event.to_json
end

#get(path, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/icuke/simulator.rb', line 50

def get(path, options = {})
  options[:query] = URI.escape(options[:query]) if options.has_key?(:query)
  response = self.class.get(path, options)
  if response.code != 200
    raise Simulator::Error, response.body
  end
  response.body
end

#launch(process) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/icuke/waxsim.rb', line 10

def launch(process)
  process = process.with_launch_options({
    :env => {
      'CFFIXED_USER_HOME' => Dir.mktmpdir
    }
  })

  process.setup_commands.each do |cmd|
    fail "Unable to run setup command #{cmd}" if !system(cmd)
    fail "Setup command #{cmd} failed with exit status #{$?}" if $? != 0
  end

  @simulator = BackgroundProcess.run(process.command)
  self.current_process = process

  timeout(30) do
    begin
      view
    rescue Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError
      sleep(0.5)
      retry
    end
  end
end

#load(path) ⇒ Object



30
31
32
# File 'lib/icuke/simulator.rb', line 30

def load(path)
  get '/load', :query => path
end

#load_module(path) ⇒ Object



38
39
40
# File 'lib/icuke/simulator.rb', line 38

def load_module(path)
  get '/module', :query => path
end

#playObject



34
35
36
# File 'lib/icuke/simulator.rb', line 34

def play
  get '/play'
end

#quitObject



35
36
37
38
39
40
# File 'lib/icuke/waxsim.rb', line 35

def quit
  return unless @simulator
  get '/quit' rescue nil # results in a hard exit(0)
  @simulator.wait
  self.current_process = nil
end

#recordObject



18
19
20
# File 'lib/icuke/simulator.rb', line 18

def record
  get '/record'
end

#resumeObject



48
49
50
# File 'lib/icuke/waxsim.rb', line 48

def resume
  launch(self.current_process)
end

#save(path) ⇒ Object



26
27
28
# File 'lib/icuke/simulator.rb', line 26

def save(path)
  get '/save', :query => path
end

#set_defaults(defaults) ⇒ Object



46
47
48
# File 'lib/icuke/simulator.rb', line 46

def set_defaults(defaults)
  get '/defaults', :query => defaults.to_json
end

#stopObject



22
23
24
# File 'lib/icuke/simulator.rb', line 22

def stop
  get '/stop'
end

#suspendObject



42
43
44
45
46
# File 'lib/icuke/waxsim.rb', line 42

def suspend
  @simulator.kill('QUIT') # invokes the normal app exit routine
  @simulator.wait
  sleep 1
end

#viewObject



14
15
16
# File 'lib/icuke/simulator.rb', line 14

def view
  get('/view')
end