def __init__(self, port, channels, test=False): self.app = NescApp() # only take the variable names without self.vars = self.app.variables.variables() # I pass the variables to the simulator self.sim = Tossim(self.vars) self.nodes = {} # the serial forwarder stuff is needed only by the java interaface # or the listen.py script # in testing mode this is only a problem self.test = test if not (self.test): self.sf = SerialForwarder(port) self.throttle = Throttle(self.sim, 10) # operations on the topology and the radio channel self.topology = RadioNetwork(self.sim.radio()) self.channels = list(channels) # adding all the channels for c in channels: self.add_channel(c)
new_k = 4 return new_k enable_main = 1 if enable_main: def main(): rssi_level = sys.argv[1] return {'y0': rssi_level} rssi_level = int(main()['y0']) else: rssi_level = -20 t = Tossim([]) t.addChannel('printf', sys.stdout) t.addChannel("DataFeedback", sys.stdout) r = t.radio() # offline network topology initialization to make 1-DR = p_1*d^2+p_2*d^2+p_3 #print "linkError: ", str(rssi_linkError[rssi_level]) #subprocess.call(["a.out", str(rssi_linkError[rssi_level])]) def initialize_network(): # this is topology for channel 26 f = open("topo.txt", "r") #wireless rssi topology injection & model establishment
new_k=3 if num_node in kconnect4 and res < kconnect4[num_node]: res = kconnect4[num_node] new_k=4 return new_k enable_main=1 if enable_main: def main(): rssi_level=sys.argv[1] return {'y0':rssi_level} rssi_level=int(main()['y0']) else: rssi_level=-20; t = Tossim([]) t.addChannel('printf', sys.stdout) t.addChannel("DataFeedback", sys.stdout) r = t.radio() # offline network topology initialization to make 1-DR = p_1*d^2+p_2*d^2+p_3 #print "linkError: ", str(rssi_linkError[rssi_level]) #subprocess.call(["a.out", str(rssi_linkError[rssi_level])]) def initialize_network(): # this is topology for channel 26 f = open("topo.txt", "r") #wireless rssi topology injection & model establishment
from simx.base.testutil import import_assert_functions import_assert_functions(globals()) import random, time from TOSSIM import Tossim from SimxSync import SimxSync from simx.sync import TimeControl _tossim = Tossim([]) _sync = SimxSync() control = None def advance_some_time(): # advance time some each time boot_time = _tossim.time() + random.randint(17, 42) node = _tossim.getNode(0) node.bootAtTime(boot_time) while _tossim.time() < boot_time: _tossim.runNextEvent() def setUp(): global control control = TimeControl(_tossim, _sync) advance_some_time() def test_advance_some_time():
from simx.base.testutil import import_assert_functions import_assert_functions(globals()) import random, time, sys from simx.sensor import SensorControl, SensorExtension from simx.base import TossimBase, ChannelBridge from TOSSIM import Tossim from SimxPushback import Pushback _tossim = Tossim([]) tossim = None sensor = None report = ChannelBridge() #f = open("res.txt", "w") _tossim.addChannel("SensorTest", report.get_writefile()) #_tossim.addChannel("SensorTest", f) sensor = SensorControl(Pushback()) tossim = TossimBase(_tossim) tossim.register_extension(SensorExtension(sensor)) def setUp(): global sensor, tossim # tossim = TossimBase(_tossim) # tossim.register_extension(SensorExtension(sensor))
class Simulation(object): def __init__(self, port, channels, test=False): self.app = NescApp() # only take the variable names without self.vars = self.app.variables.variables() # I pass the variables to the simulator self.sim = Tossim(self.vars) self.nodes = {} # the serial forwarder stuff is needed only by the java interaface # or the listen.py script # in testing mode this is only a problem self.test = test if not (self.test): self.sf = SerialForwarder(port) self.throttle = Throttle(self.sim, 10) # operations on the topology and the radio channel self.topology = RadioNetwork(self.sim.radio()) self.channels = list(channels) # adding all the channels for c in channels: self.add_channel(c) def single_node(self): print "adding a simple lonely node" self.add_node(0) def process(self): if not (self.test): self.sf.process() # handle separately colors and moreoveor def add_channel(self, channel): self.sim.addChannel(channel, sys.stdout) if not (channel in self.channels): self.channels.append(channel) def remove_channel(self, channel): self.sim.removeChannel(channel, sys.stdout) if channel in self.channels: self.channels.remove(channel) def add_node(self, idx): # otherwise add to the dictionary the correct node if not (idx in self.nodes): if len(self.nodes) == MAX_NODES: print "Not possible to add more nodes, reached limit" else: self.nodes[idx] = self.sim.getNode(idx) def start(self, batch=False): "Starts the simulation" for n in self.nodes.values(): n.bootAtTime(random.randint(100001, 900009)) self.process() self.throttle.initialize() # make sure they all boot self.run_some_events() if not (batch): self.cycle() def cycle(self): "Loop at infinite runnign all the events in the queue" print "start cycling, use C-c to send data interactively" while True: try: self.throttle.checkThrottle() self.sim.runNextEvent() self.process() # processing what it's got from it except KeyboardInterrupt: # with the first interrupt we go in interactive mode, the second quits the program try: self.interactive() print "\nrestarting the cycle, running other events...\n" continue except KeyboardInterrupt: sys.exit() def connected(self, node1, node2): return self.topology.connected(node1, node2) def make_topology(self, topo_file): "Creates the topology from the given file" for line in open(topo_file): vals = line.split() vals = (int(vals[0]), int(vals[1]), float(vals[2])) self.add_connection(*vals) def make_rand_graph(self): "Creates a random graph" for vals in rand_graph(MAX_NODES, 5): self.add_connection(*vals) def make_bin_tree(self, len): "Creates a binary tree structure for the topology" for vals in bin_tree(len): self.add_connection(*vals) print self.topology def make_given_topology(self, couples): for x, y in couples: self.add_connection(x, y) def setup_noise(self, noise_file): for line in open(noise_file): val = int(line.strip()) for n in self.nodes.values(): n.addNoiseTraceReading(val) for n in self.nodes.values(): n.createNoiseModel() def add_connection(self, n1, n2, distance=-59): """ Add to the radio channel a connection between the two nodes If the given nodes are not present already add them """ self.add_node(n1) self.add_node(n2) self.topology.add_connection(n1, n2, distance) def remove_connection(self, n1, n2): # here we don't need to remove the nodes themselves self.topology.remove_connection(n1, n2) def interactive(self): # Use a dictionary and function calls instead def send_interactive(): self.send_packet(self.make_serial()) self.send_packet(self.make_non_serial()) choices = ( ("topology management", self.manipulate_topology), ("packet creation", send_interactive), ("variable inspection", self.inspect_variable), ("node inspection", self.inspect_node), ("channel management", self.manage_channels), ) MenuMaker(choices).call_option() def run_some_events(self): "Run some of the events" # TODO: pass some arguments to make sure they're enough time = self.sim.time() while time + RUNTIME * 10000000000 > self.sim.time(): self.throttle.checkThrottle() self.sim.runNextEvent() self.throttle.printStatistics() def check_vars_all_nodes(self, var, value): return self.check_vars_nodes(sorted(self.nodes.keys()), var, value) def check_vars_nodes(self, nodes, var, value): "Check that all the variables of nodes have that value" for n in nodes: val = self.get_variable(n, var) print "node %d = %d" % (n, val) if val != value: return False return True def inspect_variable(self): "Ask for a variable to inspect and returns it" readline.parse_and_bind("tab: complete") node = input("which node you want to inspect?\n") print_var_table(self.vars) # see if this is actually correct c = rlcompleter.Completer(dict(zip(self.vars, self.vars))) readline.set_completer(c.complete) var = raw_input("which variable do you want to inspect?\n") print "node %d:var %s = %s" % (node, var, self.get_variable(node, var)) def inspect_node(self): "Show all the variables in a certain node" node = input("which node you want to inspect?\n") self.print_node_vars(node) def get_variable(self, node, var): return self.nodes[node].getVariable(var).getData() def filter_variable(self, mod=MODULES_REGEXP): for v in self.vars: if match(mod, v): yield v def print_node_vars(self, node): for v in self.filter_variable(): print self.get_variable(node, v) def print_var_nodes(self, var): for x in sorted(self.nodes): print "%d -> %s" % (x, self.get_variable(x, var)) def inactive_channels(self): "returns the inactive debug channels" return list(set(CHANNELS) - set(self.channels)) def manage_channels(self): def add_channel(): # only give the remaining to add not all of them readline.parse_and_bind("tab: complete") c = rlcompleter.Completer(dict(zip(CHANNELS, CHANNELS))) readline.set_completer(c.complete) channel = raw_input("what channel you want to add?\n") # TODO: see how to add for only one mote self.add_channel(channel) def rem_channel(): readline.parse_and_bind("tab: complete") c = rlcompleter.Completer(dict(zip(self.channels, self.channels))) readline.set_completer(c.complete) channel = raw_input("what channel you want to remove?\n") self.remove_channel(channel) def activate_all(): for c in self.inactive_channels(): self.add_channel(c) def see_channels(): print self.channels choices = ( ("Add a new debug channel", add_channel), ("Remove a debug channel", rem_channel), ("See debug channels activated", see_channels), ("Activate all debug channels", activate_all), ) MenuMaker(choices).call_option() def manipulate_topology(self): # only way to have a print inside of a lambda call print_out = lambda: sys.stdout.write(str(self.topology)) def add_nodes(): try: n1, n2 = input("first node\n"), input("second node\n") except Exception: print "input error" self.manipulate_topology() else: self.add_connection(n1, n2) def rem_nodes(): nodes = raw_input("what are the nodes to remove (symmetrically) write X Y?\n") try: n1, n2 = map(int, nodes.split(" ")) except ValueError: print "write better values" self.manipulate_topology() else: self.remove_connection(n1, n2) def disconnect_node(): try: node = input("what node you want to disconnect\n") except Exception: self.manipulate_topology() else: self.topology.disconnect_node(node) choices = ( ("see topology", print_out), ("add one connection", add_nodes), ("remove one connection", rem_nodes), ("disconnect one node", disconnect_node), ) MenuMaker(choices).call_option() def make_serial(self): return self.sim.newSerialPacket() def make_non_serial(self): return self.sim.newPacket() def send_packet(self, pkt): "Takes a BlinkMsg already generated and sends it via serial" msg = make_packet() pkt = self.sim.newSerialPacket() pkt.setData(msg.get_data()) pkt.setType(msg.am_type) pkt.setDestination(0) pkt.deliver(0, self.sim.time() + 3) self.run_some_events() print "sended packet:\n%s" % str(msg)
#!/usr/bin/env python """ Startup Runner for Simx Act/React. @author Paul Stickney - Oct 2008 """ import sys from TOSSIM import Tossim import SimxProbe from simx.probe import Loader, Probe tossim = Tossim([]) loader = Loader('app.xml', SimxProbe) #tossim.addChannel("Tossim", sys.stdout) #tossim.addChannel("TossimPacketModelC", sys.stdout) #tossim.addChannel("SimMoteP", sys.stdout) #tossim.addChannel("Gain", sys.stdout) #tossim.addChannel("AM", sys.stdout) #tossim.addChannel("Acks", sys.stdout) #tossim.addChannel("Packet", sys.stdout) #tossim.addChannel("Scheduler", sys.stdout) #tossim.addChannel("Insert", sys.stdout) #tossim.addChannel("HASH", sys.stdout) #tossim.addChannel("NoiseAudit", sys.stdout) #tossim.addChannel("Noise_c", sys.stdout) #tossim.addChannel("Noise", sys.stdout)
enable_main=1 if enable_main: def main(): rssi_level=sys.argv[1] fault_seed=sys.argv[2] faultModel = sys.argv[3] return {'y0':rssi_level, 'y1':fault_seed, 'y2':faultModel} rssi_level=float(main()['y0']) fault_seed=int(main()['y1']) faultModel=main()['y2'] else: rssi_level=-20; t = Tossim([]) t.addChannel('printf', sys.stdout) t.addChannel("DataFeedback", sys.stdout) r = t.radio() def initialize_network(): # this is topology for channel 26 f = open("topo.txt", "r") #wireless rssi topology injection & model establishment lines = f.readlines() #initialize schedule L_topo=list()
#!/usr/bin/env python import re import sys import unittest import random from TOSSIM import Tossim import SimxProbe from simx.probe import Loader # Tossim /must/ be initialized first -- doesn't matter if not used directly Tossim([]) # Only load once, should be tested elsewhere :-) loader = Loader('app.xml', SimxProbe) #hack(s) to get unittest asserts class _Check(unittest.TestCase): def runTest(self): pass check = _Check() # PST- beautiful, but perhaps too much magic # allows direct use of assertEquals, etc. by importing them for f in (x for x in dir(check) if re.match("(?:assert|fail)", x)): globals()[f] = getattr(check, f)
from random import * from TestNetworkMsg import * import sys import socket import os import time enable_main=1; if enable_main: def main(): rssi_level=sys.argv[1] return {'y0':rssi_level} rssi_level=int(main()['y0']) else: rssi_level=-20; t = Tossim([]) t.addChannel('printf', sys.stdout) t.addChannel("DataFeedback", sys.stdout) r = t.radio() L=list() for channel_1 in [22, 23, 24, 25, 26]: channel=channel_1 rssi_strength=rssi_level neignbour_strength=-20; sync_rssi_strength=-10 for sender in [181, 153, 152, 155, 154, 170, 171, 162, 172, 160, 163, 173, 161, 169, 157, 180, 182, 253, 252, 255, 254, 175, 108]: for receiver in [181, 153, 152, 155, 154, 170, 171, 162, 172, 160, 163, 173, 161, 169, 157, 180, 182, 253, 252, 255, 254, 175, 108]: r.add(sender, receiver, neignbour_strength, channel_1) r.add(181, 170, rssi_strength, channel_1) r.add(170, 181, rssi_strength, channel_1)
#noise_offset = 7 #84 #noise_offset = 9 #82 #noise_offset = 10 #81 #noise_offset = 11 #80 #noise_offset = 12 #79 #noise_offset = 13 #78 #noise_offset = 14 #77 #noise_offset = 15 #76 #noise_offset = 16 #75 #noise_offset = 17 #74 ##noise levels are changed by offset change--yh #noise_offset = 18 #73 #noise_offset = 19 #72 #noise_offset = 14 #77 #noise_offset = 16 #75 #noise_offset = 18 #73 t = Tossim([]) #t.addChannel('printf', sys.stdout) t.addChannel("DataFeedback", sys.stdout) t.addChannel("receive", sys.stdout) t.addChannel("transmission", sys.stdout) #t.addChannel("TossimPacketModelC", sys.stdout) t.addChannel("receive_ack", sys.stdout) #t.addChannel("Gain", sys.stdout) #t.addChannel("receive_power", sys.stdout) #t.addChannel("SimMote_power", sys.stdout) #t.addChannel("AM", sys.stdout) #t.addChannel('Gain', sys.stdout)
#!/usr/bin/env python """ Startup Runner for Simx Act/React. @author Paul Stickney - Oct 2008 """ import sys from TOSSIM import Tossim import SimxProbe from simx.probe import Loader, Probe tossim = Tossim([]) loader = Loader('app.xml', SimxProbe) #tossim.addChannel("Tossim", sys.stdout) #tossim.addChannel("TossimPacketModelC", sys.stdout) #tossim.addChannel("SimMoteP", sys.stdout) #tossim.addChannel("Gain", sys.stdout) #tossim.addChannel("AM", sys.stdout) #tossim.addChannel("Acks", sys.stdout) #tossim.addChannel("Packet", sys.stdout) #tossim.addChannel("Scheduler", sys.stdout) #tossim.addChannel("Insert", sys.stdout) #tossim.addChannel("HASH", sys.stdout) #tossim.addChannel("NoiseAudit", sys.stdout) #tossim.addChannel("Noise_c", sys.stdout) #tossim.addChannel("Noise", sys.stdout) #tossim.addChannel("Noise_output", sys.stdout)
from simx.base.testutil import import_assert_functions import_assert_functions(globals()) import random, time, sys from simx.sensor import SensorControl, SensorExtension from simx.base import TossimBase, ChannelBridge from TOSSIM import Tossim from SimxPushback import Pushback _tossim = Tossim([]) tossim = None sensor = None report = ChannelBridge() #f = open("res.txt", "w") _tossim.addChannel("SensorTest", report.get_writefile()) #_tossim.addChannel("SensorTest", f) sensor = SensorControl(Pushback()) tossim = TossimBase(_tossim) tossim.register_extension(SensorExtension(sensor)) def setUp(): global sensor, tossim # tossim = TossimBase(_tossim) # tossim.register_extension(SensorExtension(sensor)) def sensor_readings_results(): return """\ id=1 sensor=0 data=00101 count=0 err=00