//==================================================================
// README
// CSG520 Spring 2005 Final Project
// Daria Antonova
//==================================================================
files:
DaMultiHop.nc - wiring of the application

DaCounter.nc - implementation of node-counting
algorithm (the same code can be loaded on ordinary
nodes as well as the base station)

DaIntToLeds.nc, DaIntToLedsM.nc - utilities to output
the current count on the leds

tests:
This application was tested withe a number of
topologies. In particular it was run on three lossy 
topologies from files:
1. star-topology-1.nss (the tinyviz file for it is
star-topology-viz.mps (9 nodes, slightly lossy)
2. strange.nss (the tinyviz file for it is
strange.mps (16 nodes, slightly lossy)
3. 3x4-5.nss (file generated by LossyBuilder:
from /opt/tinyos-1.x/tools/java
>java net.tinyos.sim.LossyBuilder -d 3 4 -s 5 -o 3x4-5.nss
(12 nodes, lossy, the number of nodes counted is frequently
much smaller because of higher probability of DISCOVERY 
packet loss)

The latest CVS version (1.1.11) of TinyOS was used for testing.
Commands to run the tests and visualization:
>export DBG=temp
>cd /opt/tinyos-1.x/apps/DaMultiHop/
>make pc
>./build/pc/main.exe -gui -b=0 -rf=star-topology-1.nss 9
>cd /opt/tinyost-1.x/tools/java
>java net.tinyos.sim.SimDriver -gui
//==================================================================

/**
*
* Push-based Aggregation Tree and Node Count
*
* This is a TinyOS application that allows nodes from
* a sensor network to set up a spanning tree over the nodes
* reachable from the base station, and compute a count of the
* number of reachable nodes, using the tree.
*
* Below are the algorithms implemented to
* to accomplish this task. We assume the base station
* to be the node with id BASE_STATION_ID.
*
*
* Note: In TOSSIM not all nodes boot at the same instance.
* Thus if the DISCOVERY packet was broadcasted before some
* nodes booted, they will never receive it.
* We force all nodes to boot at the same time by running
* TOSSIM with option -b=0, which means that the boot times
* for all the nodes are uniformly distributed in the the
* interval [0,0].
*
*
*
* -------------------------------------------------------------
* Multi-hop network:
*
* A base station node periodically (DISCOVERY_EPOCH_GAP)
* issues a DISCOVERY packet with fresh sequence number to
* update information about the current number of nodes
* in the network (also the hop count initially is set to 1).
*
* The algorithm for handling DICOVERY packets and counting
* the number of children in the subtree for each node is as
* follows:
*
* 1. When a node receives a DISCOVERY packet with sequence
* number greater than last sequence number seen for the first
* time it performs the following steps:
*	  (i) set its parent to the source of the DISCOVERY packet
*	  (ii) set dist_to_base to the subtree_hop_count variable
*			from the packet
*     (iii) a broadcast DISCOVERY packet with
*			subtree_hop_count increased
*	  (iiii) after REPLY_GAP/dist_to_base send the
*			count of nodes in its subtree to the parent
*
*
* 2. When a node receives REPLY messages it performs the
* the following steps:
*	(i) if the sequence number of the REPLY packet is equal
*		to current epoch sequence number, the node increments
*		its subtree count
*	(ii) if the sequence number is smaller, the node resends
*		DISCOVERY packet, so that this child can 'synchronize'
*		with the current epoch
*
* 3. Sending REPLY packets: each node knows its hop distance
* from the base station. The node will wait
* REPLY_GAP/hop_count before sending its reply to the parent
*
* When the REPLY is sent, the node will check the ACK bit
* and retransmit up to MAX_RETRANSMIT number of times
* checking the ACK bit each time.
*
*
* Note: all transmission times are randomized to prevent
* high likelihood of collisions.
*
* -------------------------------------------------------------
* Time and Message bounds:
*
* The time before the base station gets an estimate of the
* node count is REPLY_GAP. The execution is continuous, if the
* nodes move in one epoch and are not reachable by the parent
* any more, their subtrees will be rediscovered in the
* subsequent epoch.
*
* The number of messages for every epoch is linear in the
* number of nodes. In fact the maximum is n+MAX_RETRANSMIT*n,
* and this would be approached only on extremely lossy networks
*
*
* See comments in CaCounter.nc for more details.
* Note: the summation of the nodes counted in the current
* epoch in the subtree is updated (only add the count from
* this REPLY packet) as the REPLY packets are
* received. It is only re-calculated from scratch when the
* node is about to send REPLY or (for base station node)
* when the new epoch is about to start. This is done in
* order not to go through the whole array of children every
* time a new REPLY packet is received.
* -------------------------------------------------------------
*
*
*/