forked from UCMAndesLab/CSE160-Project-Skeleton-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestSim.py
149 lines (121 loc) · 4.09 KB
/
TestSim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ANDES Lab - University of California, Merced
#Author: UCM ANDES Lab
#$Author: abeltran2 $
#$LastChangedDate: 2014-08-31 16:06:26 -0700 (Sun, 31 Aug 2014) $
#! /usr/bin/python
import sys
from TOSSIM import *
from CommandMsg import *
class TestSim:
moteids=[]
# COMMAND TYPES
CMD_PING = 0
CMD_NEIGHBOR_DUMP = 1
CMD_ROUTE_DUMP=3
# CHANNELS - see includes/channels.h
COMMAND_CHANNEL="command";
GENERAL_CHANNEL="general";
# Project 1
NEIGHBOR_CHANNEL="neighbor";
FLOODING_CHANNEL="flooding";
# Project 2
ROUTING_CHANNEL="routing";
# Project 3
TRANSPORT_CHANNEL="transport";
# Personal Debuggin Channels for some of the additional models implemented.
HASHMAP_CHANNEL="hashmap";
# Initialize Vars
numMote=0
def __init__(self):
self.t = Tossim([])
self.r = self.t.radio()
#Create a Command Packet
self.msg = CommandMsg()
self.pkt = self.t.newPacket()
self.pkt.setType(self.msg.get_amType())
# Load a topo file and use it.
def loadTopo(self, topoFile):
print 'Creating Topo!'
# Read topology file.
topoFile = 'topo/'+topoFile
f = open(topoFile, "r")
self.numMote = int(f.readline());
print 'Number of Motes', self.numMote
for line in f:
s = line.split()
if s:
print " ", s[0], " ", s[1], " ", s[2];
self.r.add(int(s[0]), int(s[1]), float(s[2]))
if not int(s[0]) in self.moteids:
self.moteids=self.moteids+[int(s[0])]
if not int(s[1]) in self.moteids:
self.moteids=self.moteids+[int(s[1])]
# Load a noise file and apply it.
def loadNoise(self, noiseFile):
if self.numMote == 0:
print "Create a topo first"
return;
# Get and Create a Noise Model
noiseFile = 'noise/'+noiseFile;
noise = open(noiseFile, "r")
for line in noise:
str1 = line.strip()
if str1:
val = int(str1)
for i in self.moteids:
self.t.getNode(i).addNoiseTraceReading(val)
for i in self.moteids:
print "Creating noise model for ",i;
self.t.getNode(i).createNoiseModel()
def bootNode(self, nodeID):
if self.numMote == 0:
print "Create a topo first"
return;
self.t.getNode(nodeID).bootAtTime(1333*nodeID);
def bootAll(self):
i=0;
for i in self.moteids:
self.bootNode(i);
def moteOff(self, nodeID):
self.t.getNode(nodeID).turnOff();
def moteOn(self, nodeID):
self.t.getNode(nodeID).turnOn();
def run(self, ticks):
for i in range(ticks):
self.t.runNextEvent()
# Rough run time. tickPerSecond does not work.
def runTime(self, amount):
self.run(amount*1000)
# Generic Command
def sendCMD(self, ID, dest, payloadStr):
self.msg.set_dest(dest);
self.msg.set_id(ID);
self.msg.setString_payload(payloadStr)
self.pkt.setData(self.msg.data)
self.pkt.setDestination(dest)
self.pkt.deliver(dest, self.t.time()+5)
def ping(self, source, dest, msg):
self.sendCMD(self.CMD_PING, source, "{0}{1}".format(chr(dest),msg));
def neighborDMP(self, destination):
self.sendCMD(self.CMD_NEIGHBOR_DUMP, destination, "neighbor command");
def routeDMP(self, destination):
self.sendCMD(self.CMD_ROUTE_DUMP, destination, "routing command");
def addChannel(self, channelName, out=sys.stdout):
print 'Adding Channel', channelName;
self.t.addChannel(channelName, out);
def main():
s = TestSim();
s.runTime(10);
s.loadTopo("long_line.topo");
s.loadNoise("no_noise.txt");
s.bootAll();
s.addChannel(s.COMMAND_CHANNEL);
s.addChannel(s.GENERAL_CHANNEL);
s.addChannel(s.NEIGHBOR_CHANNEL); # Added neighbor channel
s.runTime(20);
s.ping(1, 2, "Hello, World");
s.runTime(10);
s.ping(1, 3, "Hi!");
s.runTime(20);
if __name__ == '__main__':
main()