#
# This is a script example, which shows how to run an application on a set of nodes
#
# The scenario of this experiment involves one set of nodes: 'worker'. We will execute
# the appliation command 'tcpdump -A -i ath0 -w /tmp/dump.txt' on all the nodes within 
# that set. The output of this command for each node will be stored into a text file on 
# node itself. 
# Note: to retrieve these outputs, you will need to either use the OML Collection 
# Framework (see other tutorials), or directly copy the required text file from the 
# corresponding node.
#
# In this example we:
# 1) Define a new Application, which will be a wrapper around the tcpdump application
# 2) Define a set of nodes which contains 1 node: [1,1] that will run the application
# 3) Run the application 
#

# 1) 
# Define a new application
# The following declaration defines a new application which as a URI 'tcpdumpWrapper'
# and the name 'dumpApp'
#
# To use this Application definition in multiple experiment scripts, place it
# into a separate stand-alone file with the name 'tcpdumpWrapper.rb' in the same 
# directory as the experiment scripts that would use it. These experiment scripts
# will then NOT require this step 1) declaration.
#
# NOTE: tcpdump output its user message on STDERR and not STDOUT, thus when
# running this tutorial script, please ignore false error messages such as:
# "ERROR NodeApp: tcpdump: listening on ath0, link-type EN10MB (Ethernet), capture size 96 bytes"
#
defApplication('tcpdumpWrapper', 'dumpApp') {|app|
  app.shortDescription = "This is a simple wrapper application around tcpdump"
  app.path="/usr/sbin/tcpdump -A -i ath0 -w /tmp/dump.txt"
}

# 2)
# Define a set of node that would use the above application
#
defGroup('worker', [1,1]) {|node|

   # Configure the wireless interface on the node(s) in this set
   node.net.w0.mode = "Master"
   node.net.w0.type = "g"
   node.net.w0.essid = "tutorial"
   node.net.w0.ip = "192.168.0.1"

   # Add the 'tcpdumpWrapper' application to the node(s) in this set
   node.addApplication('tcpdumpWrapper', 'dumpApp', nil, nil)

   # Note: the signature of addApplication() is
   #
   #   addApplication(app, vName, bindings, env)
   #
   # 'app' - The application URI or instance
   # 'vName' - Virtual name given to this app
   # 'paramBindings' - Parameter bindings for this application
   # 'env' - Environment to set before starting application
}

# 3)
# When all the nodes are UP, execute the application on them
#
whenAllUp() {|node|

   # Wait 10 sec to make sure that the wireless interfaces are all 
   # configured
   wait 10
   
   # Start all the applications
   allGroups.startApplications

   # Wait 10 sec that will be the application duration time
   wait 10 
   
   # Stop the experiment
   Experiment.done
}

