diff options
Diffstat (limited to 'example')
| -rw-r--r-- | example/receiver.py | 28 | ||||
| -rw-r--r-- | example/sender.py | 47 | ||||
| -rwxr-xr-x | example/simulator.py | 41 |
3 files changed, 116 insertions, 0 deletions
diff --git a/example/receiver.py b/example/receiver.py new file mode 100644 index 0000000..a2ed690 --- /dev/null +++ b/example/receiver.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +import sys, random, time + +lr=random.Random(6) + +def execute(api): + uptime=api.args + endoff=0 + for i in range(0,24): + startoff=random.randint(0,3600-uptime) + api.turn_off() + api.wait(startoff+endoff) + api.turn_on() + wakeat=api.read("clock") + wakeuntil=wakeat+uptime + # Receive until uptime seconds if elapsed + while api.read("clock") < wakeuntil: + code, data=api.receivet("wlan0",wakeuntil-api.read("clock")) + if code == 0: + api.log("Receive "+data) + api.log("Was up for {}s".format(api.read("clock")-wakeat)) + endoff=3600*(i+1)-api.read("clock") + api.turn_off() + api.wait(endoff) + api.turn_on() + + diff --git a/example/sender.py b/example/sender.py new file mode 100644 index 0000000..8389c95 --- /dev/null +++ b/example/sender.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +###################### +# _ ____ ___ # +# / \ | _ \_ _| # +# / _ \ | |_) | | # +# / ___ \| __/| | # +# /_/ \_\_| |___| # +# # +###################### +# api.args # Contains node arguments +# api.send(interface, data,size, dst) # If interface is "wlan0" dst is not used +# api.sendt(interface, data,size, dst,timeout) # Similar to api.send() but with timeout +# api.receive(interface) # Receive the data by returning the following tuple (code,data) if code is 0 receive succeed +# api.receivet(interface,timeout) # Similar to api.receive() but with timeout +# api.read(register) # Read from the simulator registers (ex: clock) +# api.log(msg) # Print log in the simulation console +# api.wait(duration) # Wait for "duration" seconds of simulated time +# api.turn_off(duration) # Turn the node off for "duration" seconds (no data can be receive during this time period) + +import sys, random + +# Note that the following is required to have different instance from thread to thread +lr=random.Random(6) + +def execute(api): + uptime=api.args + endoff=0 + for i in range(0,24): + startoff=random.randint(0,3600-uptime) + api.turn_off() + api.wait(startoff+endoff) + api.turn_on() + wakeat=api.read("clock") + wakeuntil=wakeat+uptime + # Send until uptime seconds if elapsed + while api.read("clock") < wakeuntil: + api.sendt("wlan0","hello",10,None, wakeuntil-api.read("clock")) + api.log("Was up for {}s".format(api.read("clock")-wakeat)) + endoff=3600*(i+1)-api.read("clock") + api.turn_off() + api.wait(endoff) + api.turn_on() + + + + diff --git a/example/simulator.py b/example/simulator.py new file mode 100755 index 0000000..9b00887 --- /dev/null +++ b/example/simulator.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +# Load ESDS +import sys +sys.path.append("../") +import esds + +# Use numpy to construct bandwidth and latencies matrix +import numpy as np + +##### Scenario +# The simulated scenario comprises 1 that wakes up randomly +# for a duration called "uptime" every hour. The sender try to transmit +# his data during that uptime. Other nodes are receivers that have similar +# random wake up parterns and strive to receive data from the sender. + +##### Bandwidth matrix +# Bandwidth value can be 0 for unreachable nodes +# Diagonal entries impact the transmission duration for wireless transmissions (wlan0) +n=2 # Number of nodes including the sender +B=np.full((n,n),5) # 5Mbps + +##### Latency matrix +# If the latency entries match one with a bandwidth of 0 +# then it will be ignore since node is unreachable. +L=np.full((n,n),0) # 0s + +##### Create the simulator +s=esds.Simulator(B,L) + +##### Instantiate nodes +uptime=180 # 180s uptime +s.create_node("sender",args=uptime) # Load sender.py for the first node with 5 as argument (first row in B and L) + +# Aguments can be passed to nodes via: s.create_node("sender",args="my argument") +for n in range(0,n-1): # Load receiver.py for the remaining nodes + s.create_node("receiver",args=uptime) + +##### Run the simulation +#s.run(debug=True) # Generate a "esds.debug" file +s.run() |
