#
# This is a script example, which illustrates the use of multi-hop topologies
#
# The scenario of this experiment involves several groups of nodes: a 'sender' and
# multiple 'receiver' groups. The 'sender' group will generate some broadcast traffic, 
# which will be received by the 'receiver' groups.
#
# Each group contains nodes that are not explicitly defined within this script, but 
# rather randomly drawn from the set of active nodes on the tested where this script is 
# running. In other words, this script example does not specifically name which node belongs to 
# which group. 
#
# The multi-hop connectivity map for this scenario is as follows
#
#   myNode_1 -> myNode_2 -> myNode_3 -> myNode4
#
#  where "A -> B" means that node A has an asymmetric link to node B
#
# More information on the available commands to define a topology are available on the 
# following page: 
# http://www.orbit-lab.org/wiki/Documentation/NodeHandler/Commands/defTopology
#
# In this example we:
#
# 1) Define a 1st Topology, which will be used to build the multi-hop connectivity map
# 2) Define a 1st sub-topology, which will be used to build the 'sender' group of nodes
# 3) Define the 'sender' group of nodes
# 4) Define other sub-topologies, which will be used to build the different 'receiver' groups of nodes
# 5) Define the multiple 'receiver' groups of nodes
# 6) Configure the wireless interfaces on all the nodes and enforce the multi-hop topology 
# 7) Finally run the experiment
#


# 1)
# Define the Main Topology for this experiment
#
# This topology will hold all the nodes involved in this experiment and it will also
# define the links between them in our multi-hop scenario
#
# A topology is a set of logical nodes (vertices) with a mapping to 
# real nodes of a testbed. Optionally, a set of logical links (edges) 
# can also be added to a topology to connect different nodes, thus
# "emulating" a multi-hop configuration. 
#
# This topology will have nodes and links which will be as follows: 
#   myNode_1 -> myNode_2 -> myNode_3 -> myNode4
#
defTopology('mainTopology') { |t|

  # 1.1 - Load a "base" topology with all the currently active nodes
  #     The use of 'system:topo:active' is only possible when an "imageNodes4"
  #     process has been performed previously from the same path as the one
  #     where this script is currently ran.
  baseTopo = Topology['system:topo:active']
  puts "Number of Active nodes on this tesbed: #{baseTopo.size}"

  # 1.2 - Select a set of nodes from the base topology
  #     A given ':number' of nodes with the required ':features' are selected 
  #     using the given ':method'. These nodes are given the node-name ':name'
  #     where %i% will be repaced my an incremental count from 0..'number'
  #
  #     ':features' is a hash which holds the required characteristics for these nodes
  #
  #     NOTE: So far (Nov.07) no 'features' selection is currently implemented, thus
  #     the following 'features' are just here as placeholders / illustrations.
  someNodes = baseTopo.select( :method => :random,
                               :number => 4,
			       :name => "myNode_%i%",
                               :features => {:wifi => "atheros" , :bt => "false" , :mem => "512" , :channel => "all"})

  # 1.3 - Add the selected nodes to this topology
  t.addNodes(someNodes)

  # 1.3 bis - Nodes can also be explicitly added using the following methods, which 
  #         can replace or be combined with the above steps 2 and 3:
  # t.addNode(x, y) -> add node [x,y], and give it the node-name "[x,y]"
  # t.addNode("myNode", [x,y]) -> add node [x,y], and give it the node-name "myNode"

  # 1.4 - Define a set of edges between these nodes
  # (This step is optional if you don't need a multi-hop scenario)
  # When present, this step allows the emulation of multi-hop experiment. 
  # When absent, the nodes connectivity will follow their "normal" radio coverage
  #
  # addLink(A,B,spec) -> add a link between nodes A and B, and configure that link
  #                      with the characteristics given in the 'spec' hash
  #                      e.g. spec = [ rate=54 , per=0.10 , etc... ]
  #  So far (Nov.07) no 'spec' selection other than 'asymmetric' is currently implemented, 
  #  thus the other 'specs' are just here as placeholders / illustrations.
  t.addLink("myNode_1","myNode_2",{ :rate =>54, :per =>0.1, :asymmetric => true })
  t.addLink("myNode_2","myNode_3",{ :rate =>12, :per =>0.2, :asymmetric => true })
  t.addLink("myNode_3","myNode_4",{ :rate =>6, :per =>0.4, :asymmetric => true })

  # 1.5 - Optional 
  # Save the defined connectivity graph of this topology to a file, which
  # can be viewed with graphviz
  # The filename is: 'ID-Graph.dot' where 'ID' is this experiment ID
  # It will be located in the current directory
  t.saveGraphToFile()
}

# 2)
# Define a sub-Topology, which will hold a subset of "mainTopology"
# Typically, this would be used to put a selection of nodes from the 
# main topology into a group of node running a same type of application.
# Thus, there are no connectivity states/constraints defined here.
#
defTopology('senderSubTopology') { |t|

  # load the main topology defined above
  mainT = Topology['mainTopology']

  # Add nodes myNode_1..3 from the "mainTopology" into this sub-topology
  for i in 1..3
    node = mainT.getNodeByLabel("myNode_#{i}")
    t.addNode(node)
  end
}

# 3)
# Define a group of node "senderGroup" 
# The nodes within this group will all run a broadcast traffic generator 
#
defGroup('senderGroup', 'senderSubTopology') {|node|
	node.prototype("test:proto:sender", {
		'broadcast' => 'on',
		'destinationHost' => '192.168.255.255',
		'packetSize' => 512,
		'rate' => 400,
		'protocol' => 'udp'
	})
}

# 4) and 5) 
# Define 3 other sub-Topologies 'receiverSubTopology_2..4'
# Define 3 other group of node 'receiverGroup_2..4'
# The single node within each of this group runs a traffic sink
#
for i in 2..4
  
  # 4) define 3 other sub-topologies for the receivers
  defTopology("receiverSubTopology_#{i}") { |t|
    # Get a given node from the main topology defined above
    node = Topology['mainTopology'].getNodeByLabel("myNode_#{i}")
    t.addNode(node)
  }

  # 5) define 3 receiver groups
  defGroup("receiverGroup_#{i}", "receiverSubTopology_#{i}") {|node|
    node.prototype("test:proto:receiver" , { 'protocol' => 'udp' })
  }

end

# 6)
# 
# 6.1 Configures the wireless interfaces of all the nodes in this experiment
#
AllGroups.net.w0 { |w|
  w.mode = "ad-hoc"
  w.type = "g"
  w.channel = "6"
  w.essid = "exp1234"
  w.ip = "%192.168.%x.%y"
}

# 6.2
# Implement/deploy the topology "mainTopology" on all the experiment nodes.
# It is at this point that the MAC filtering tables on each node will be set 
# according to the connectivity graph associated with "mainTopology".
# The interfaces that will be added to the filtering tables on the nodes will be
# the ones corresponding to "w0" (which currently maps to "ath0").
# (NOTE to developers: this mapping w0->ath0 is temporary, ideally as suggested 
# on the dev-list, we should NOT use software-specific name such as "ath0". But 
# the current INVENTORY database does not support that. This will be changed in 
# the near future)
#
# Here we use the iptable tool to set up the MAC filtering tables on each node.
# Other options are "ebtable" and "mackill"
#
AllGroups.net.w0.enforce_link = {:topology => 'mainTopology', :method => 'iptable'}
#AllGroups.net.w0.enforce_link = {:topology => 'mainTopology', :method => 'ebtable'}
#AllGroups.net.w0.enforce_link = {:topology => 'mainTopology', :method => 'mackill'}

# 7)
# Everything is ready, start the applications on the nodes...
#
whenAllInstalled() {|node|
	wait 10
	AllGroups.startApplications
	wait 60
	Experiment.done
}
