# -*- coding: utf-8 -*-
defProperty('contr', 'node2-1', 'WiSHFUL Controller Node')
defProperty('path','/root/wishful/examples/edca/',"Path to WiSHFUL configuration directory")
defProperty('accesspoints', 'node1-3,node1-5', "node ID for access point")
defProperty('clients', 'node1-4,node1-6', "node ID for client nodes")
defProperty('channel', 11, "WiFi channel to use")
defProperty('duration', 60, "Seconds to run the application")

defApplication('controller') do |app|
  app.description = 'WiSHFUL Simple Controller Program'
  app.path = property.path+'wishful_simple_controller'
  app.defProperty('config', 'Configuration file', '--config', {:type => :string})
end

defApplication('agent') do |app|
  app.description = 'WiSHFUL Simple Agent Program'
  app.path = property.path+'wishful_simple_agent'
  app.defProperty('config', 'Configuration file', '--config', {:type => :string})
end


defGroup( 'Controllers', property.contr ) do |node|
  node.addApplication( "controller" ) do |app|
    app.setProperty('config', property.path+'controller_config.yaml')
  end
end

@i = 0
defGroup('AP', property.accesspoints) do |node|
  node.addApplication( "agent" ) do |app|
    app.setProperty('config', property.path+'agent_config.yaml')
  end
  node.addApplication("test:app:iperf") do |app|
    app.setProperty('server', true)
    app.setProperty('reportstyle','O')
    app.setProperty('oml-server', "oml:3003")
    app.setProperty('oml-id', "#{@i}")
    app.setProperty('oml-exp-id', "#{Experiment.ID}")    
  end
  node.net.w0.mode = "master"
  node.net.w0.type = 'g'
  node.net.w0.channel = property.channel.to_s
  @i = @i + 1
  node.net.w0.essid = "WiSHFUL#{@i}"
  node.net.w0.ip = "192.168.0.#{@i}"
end

@j = 0
defGroup('client', property.clients) do |node|
  @j = @j + 1
  node.addApplication( "agent" ) do |app|
    app.setProperty('config', property.path+'agent_config.yaml')
  end
  node.addApplication("test:app:iperf") do |app|
    app.setProperty('client', "192.168.0.#{@j}")
    app.setProperty('time', 20)
    app.setProperty('interval', 5)
    app.setProperty('reportstyle','O')
    app.setProperty('oml-server', "oml:3003")
    app.setProperty('oml-id', "#{@i}")
    app.setProperty('oml-exp-id', "#{Experiment.ID}")
  end
  node.net.w0.mode = "managed"
  node.net.w0.type = 'g'
  node.net.w0.channel = property.channel.to_s
  node.net.w0.essid = "WiSHFUL#{@j}"
  @i = @i + 1
  node.net.w0.ip = "192.168.0.#{@i}"
end

onEvent(:ALL_UP_AND_INSTALLED) do |event|
  info "Wait for all nodes to come up" 
  wait 20
  allGroups.startApplications
  info "Both controller and agent are started; running for #{property.duration}" 
  wait property.duration
  allGroups.stopApplications
  info "Both controller and agent are stopped..." 
  Experiment.done
end

