├── .gitattributes ├── filter.py ├── calculate.py ├── runSimulation - MasterCopy.sh ├── runSimulation.sh ├── old ├── summary_.csv └── summary.csv ├── summary.csv ├── xmlParser.py ├── test.cc ├── flowTest.cc ├── iot_PUF.cc ├── WSN.cc ├── iot_MasterCopy.cc ├── iot_ABE.cc ├── instruction.md ├── iot.cc ├── WSN1.cc ├── README.md └── NS3 walkthrough.html /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /filter.py: -------------------------------------------------------------------------------- 1 | import os 2 | pcapDir = '/home/soumya/ns3/' 3 | for readFile in [f for f in os.listdir(pcapDir) if f.endswith('.pcap')]: 4 | writeFile = readFile[4:9].replace('_','')+'.txt' 5 | print 'tshark -r '+readFile+' -Y udp >>'+writeFile 6 | os.system('echo _________________'+readFile[8:-5]+' >>'+writeFile) 7 | os.system('tshark -r '+readFile+' -Y udp >>'+writeFile) 8 | -------------------------------------------------------------------------------- /calculate.py: -------------------------------------------------------------------------------- 1 | #pcapDir = '/mnt/d/pcap' 2 | #for readFile in [f for f in os.listdir(pcapDir) if f.endswith('.txt')]: 3 | 4 | 5 | class Msg: 6 | def __init__(self,src,dest,time,size): 7 | self.src = src 8 | self.dest = dest 9 | self.time = time 10 | self.size = size 11 | def check_reply(self,src,dest): 12 | if int(self.src.replace('.','')) == int(dest.replace('.','')): 13 | if int(self.dest.replace('.','')) == int(src.replace('.','')): 14 | return true 15 | return false 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | import os 28 | allfiles = os.listdir('.') 29 | for i in xrange(len(allfiles)): 30 | if allfiles[i].endswith('.txt'): 31 | lines = open(allfiles[i],'r').readlines() 32 | #d = dict() 33 | #e2e = [] 34 | #totalbytes = 0 35 | maxtime = 0 36 | for line in lines: 37 | #print line 38 | parts = line.replace(' ',' ').replace(' ',' ').split(' ')[2:] 39 | if len(parts)>2: 40 | #item = parts[2].replace('.','')+parts[4].replace('.','') 41 | #totalbytes = totalbytes + (int(parts[7])-64) 42 | if parts[0] > maxtime : 43 | maxtime = parts[0] 44 | print allfiles[i].replace('.txt',',').replace('x',',')+str(maxtime) -------------------------------------------------------------------------------- /runSimulation - MasterCopy.sh: -------------------------------------------------------------------------------- 1 | ./waf --run "IOT --MU=2 --SD=3" 2 | ./waf --run "IOT --MU=2 --SD=5" 3 | ./waf --run "IOT --MU=2 --SD=10" 4 | ./waf --run "IOT --MU=2 --SD=15" 5 | ./waf --run "IOT --MU=2 --SD=20" 6 | ./waf --run "IOT --MU=2 --SD=35" 7 | ./waf --run "IOT --MU=2 --SD=50" 8 | 9 | 10 | ./waf --run "IOT --MU=3 --SD=3" 11 | ./waf --run "IOT --MU=3 --SD=5" 12 | ./waf --run "IOT --MU=3 --SD=10" 13 | ./waf --run "IOT --MU=3 --SD=15" 14 | ./waf --run "IOT --MU=3 --SD=20" 15 | ./waf --run "IOT --MU=3 --SD=35" 16 | ./waf --run "IOT --MU=3 --SD=50" 17 | 18 | 19 | ./waf --run "IOT --MU=5 --SD=3" 20 | ./waf --run "IOT --MU=5 --SD=5" 21 | ./waf --run "IOT --MU=5 --SD=10" 22 | ./waf --run "IOT --MU=5 --SD=15" 23 | ./waf --run "IOT --MU=5 --SD=20" 24 | ./waf --run "IOT --MU=5 --SD=35" 25 | ./waf --run "IOT --MU=5 --SD=50" 26 | 27 | 28 | ./waf --run "IOT --MU=8 --SD=3" 29 | ./waf --run "IOT --MU=8 --SD=5" 30 | ./waf --run "IOT --MU=8 --SD=10" 31 | ./waf --run "IOT --MU=8 --SD=15" 32 | ./waf --run "IOT --MU=8 --SD=20" 33 | ./waf --run "IOT --MU=8 --SD=35" 34 | ./waf --run "IOT --MU=8 --SD=50" 35 | 36 | 37 | ./waf --run "IOT --MU=10 --SD=3" 38 | ./waf --run "IOT --MU=10 --SD=5" 39 | ./waf --run "IOT --MU=10 --SD=10" 40 | ./waf --run "IOT --MU=10 --SD=15" 41 | ./waf --run "IOT --MU=10 --SD=20" 42 | ./waf --run "IOT --MU=10 --SD=35" 43 | ./waf --run "IOT --MU=10 --SD=50" 44 | 45 | 46 | 47 | 48 | 49 | #python filter.py 50 | 51 | #python calculate.py >> summary.csv 52 | 53 | python xmlParser.py 54 | 55 | 56 | cp summary.csv dropbox/ 57 | -------------------------------------------------------------------------------- /runSimulation.sh: -------------------------------------------------------------------------------- 1 | #./waf --run "IOT_ABE --MU=2 --SD=3" 2 | ./waf --run "IOT_ABE --MU=2 --SD=5" 3 | ./waf --run "IOT_ABE --MU=2 --SD=10" 4 | ./waf --run "IOT_ABE --MU=2 --SD=15" 5 | ./waf --run "IOT_ABE --MU=2 --SD=20" 6 | 7 | 8 | ./waf --run "IOT_ABE --MU=3 --SD=3" 9 | ./waf --run "IOT_ABE --MU=3 --SD=5" 10 | ./waf --run "IOT_ABE --MU=3 --SD=10" 11 | ./waf --run "IOT_ABE --MU=3 --SD=15" 12 | ./waf --run "IOT_ABE --MU=3 --SD=20" 13 | 14 | 15 | ./waf --run "IOT_ABE --MU=5 --SD=3" 16 | ./waf --run "IOT_ABE --MU=5 --SD=5" 17 | ./waf --run "IOT_ABE --MU=5 --SD=10" 18 | ./waf --run "IOT_ABE --MU=5 --SD=15" 19 | ./waf --run "IOT_ABE --MU=5 --SD=20" 20 | 21 | ./waf --run "IOT_ABE --MU=8 --SD=3" 22 | ./waf --run "IOT_ABE --MU=8 --SD=5" 23 | ./waf --run "IOT_ABE --MU=8 --SD=10" 24 | ./waf --run "IOT_ABE --MU=8 --SD=15" 25 | ./waf --run "IOT_ABE --MU=8 --SD=20" 26 | 27 | 28 | ./waf --run "IOT_ABE --MU=10 --SD=3" 29 | ./waf --run "IOT_ABE --MU=10 --SD=5" 30 | ./waf --run "IOT_ABE --MU=10 --SD=10" 31 | ./waf --run "IOT_ABE --MU=10 --SD=15" 32 | ./waf --run "IOT_ABE --MU=10 --SD=20" 33 | ./waf --run "IOT_ABE --MU=10 --SD=35" 34 | ./waf --run "IOT_ABE --MU=10 --SD=50" 35 | 36 | 37 | 38 | python xmlParser.py 39 | 40 | 41 | cp summary.csv dropbox/ 42 | 43 | 44 | 45 | ./waf --run "IOT_ABE --MU=2 --SD=35" 46 | ./waf --run "IOT_ABE --MU=2 --SD=50" 47 | 48 | 49 | ./waf --run "IOT_ABE --MU=3 --SD=35" 50 | ./waf --run "IOT_ABE --MU=3 --SD=50" 51 | 52 | 53 | ./waf --run "IOT_ABE --MU=5 --SD=35" 54 | ./waf --run "IOT_ABE --MU=5 --SD=50" 55 | 56 | 57 | ./waf --run "IOT_ABE --MU=8 --SD=35" 58 | ./waf --run "IOT_ABE --MU=8 --SD=50" 59 | 60 | 61 | ./waf --run "IOT_ABE --MU=10 --SD=35" 62 | ./waf --run "IOT_ABE --MU=10 --SD=50" 63 | 64 | 65 | 66 | 67 | #python filter.py 68 | 69 | #python calculate.py >> summary.csv 70 | 71 | python xmlParser.py 72 | 73 | 74 | cp summary.csv dropbox/ 75 | -------------------------------------------------------------------------------- /old/summary_.csv: -------------------------------------------------------------------------------- 1 | 10, 10, , 300, 40400, 0.76, 20.768, 1945.26, 2.532, 1.0 2 | 10, 15, , 450, 60600, 1.103, 30.77, 1969.426, 2.452, 1.0 3 | 10, 20, , 600, 80800, 1.503, 40.772, 1981.739, 2.505, 1.0 4 | 10, 35, , 1050, 140416, 3.443, 70.772, 1984.049, 3.279, 0.99 5 | 10, 3, , 90, 12120, 0.268, 6.769, 1790.632, 2.979, 1.0 6 | 10, 50, , 1500, 178548, 18.032, 100.77, 1771.835, 12.021, 0.88 7 | 10, 5, , 150, 20200, 0.405, 10.773, 1874.98, 2.699, 1.0 8 | 2, 10, , 60, 8080, 0.207, 3.977, 2031.916, 3.457, 1.0 9 | 2, 15, , 90, 12120, 0.287, 5.973, 2029.262, 3.191, 1.0 10 | 2, 20, , 120, 16160, 0.341, 7.973, 2026.956, 2.844, 1.0 11 | 2, 35, , 210, 28280, 0.637, 13.969, 2024.551, 3.035, 1.0 12 | 2, 3, , 18, 2424, 0.075, 1.177, 2059.692, 4.191, 1.0 13 | 2, 50, , 300, 40400, 0.967, 19.972, 2022.79, 3.225, 1.0 14 | 2, 5, , 30, 4040, 0.098, 1.974, 2046.619, 3.278, 1.0 15 | 3, 10, , 90, 12120, 0.22, 6.069, 1997.111, 2.443, 1.0 16 | 3, 15, , 135, 18180, 0.349, 9.074, 2003.601, 2.583, 1.0 17 | 3, 20, , 180, 24240, 0.528, 12.072, 2007.876, 2.931, 1.0 18 | 3, 35, , 315, 42420, 0.866, 21.077, 2012.648, 2.748, 1.0 19 | 3, 3, , 27, 3636, 0.081, 1.87, 1944.609, 2.995, 1.0 20 | 3, 50, , 450, 60600, 1.296, 30.074, 2015.057, 2.879, 1.0 21 | 3, 5, , 45, 6060, 0.146, 3.075, 1970.959, 3.237, 1.0 22 | 5, 10, , 150, 20200, 0.405, 10.271, 1966.612, 2.697, 1.0 23 | 5, 15, , 225, 30300, 0.564, 15.275, 1983.571, 2.508, 1.0 24 | 5, 20, , 300, 40400, 0.742, 20.27, 1993.054, 2.472, 1.0 25 | 5, 35, , 525, 70700, 1.376, 35.277, 2004.167, 2.62, 1.0 26 | 5, 3, , 45, 6060, 0.144, 3.272, 1851.802, 3.209, 1.0 27 | 5, 50, , 750, 100836, 1.865, 50.274, 2005.742, 2.487, 1.0 28 | 5, 5, , 75, 10100, 0.229, 5.276, 1914.51, 3.059, 1.0 29 | 8, 10, , 240, 32320, 0.621, 16.575, 1949.878, 2.587, 1.0 30 | 8, 15, , 360, 48480, 0.917, 24.571, 1973.096, 2.547, 1.0 31 | 8, 20, , 480, 64640, 1.143, 32.57, 1984.623, 2.381, 1.0 32 | 8, 35, , 840, 111944, 4.121, 56.533, 1980.147, 4.906, 0.99 33 | 8, 3, , 72, 9696, 0.193, 5.37, 1805.695, 2.686, 1.0 34 | 8, 50, , 1200, 161600, 2.897, 80.572, 2005.655, 2.414, 1.0 35 | 8, 5, , 120, 16160, 0.343, 8.574, 1884.854, 2.854, 1.0 36 | -------------------------------------------------------------------------------- /summary.csv: -------------------------------------------------------------------------------- 1 | 8, 10, , 160, 22548, 98.544, 3.005, 7503.988, 0.616, 0.99 2 | 10, 5, , 100, 14200, 25.804, 2.002, 7093.982, 0.258, 1.0 3 | 5, 35, , 350, 46180, 462.685, 3.077, 15006.032, 1.322, 0.93 4 | 3, 3, , 18, 2556, 2.342, 1.004, 2546.327, 0.13, 1.0 5 | 8, 15, , 240, 33228, 216.112, 3.01, 11039.302, 0.9, 0.97 6 | 10, 35, , 700, 56984, 722.776, 3.311, 17208.937, 1.033, 0.58 7 | 8, 50, , 800, 60412, 778.669, 3.391, 17815.54, 0.973, 0.53 8 | 10, 20, , 400, 51792, 572.205, 3.121, 16592.938, 1.431, 0.92 9 | 10, 50, , 1000, 59684, 795.116, 3.447, 17313.51, 0.795, 0.42 10 | 8, 5, , 80, 11360, 18.017, 1.018, 11155.521, 0.225, 1.0 11 | 3, 35, , 210, 29028, 156.936, 3.002, 9670.632, 0.747, 0.97 12 | 5, 3, , 30, 4260, 2.065, 1.002, 4253.312, 0.069, 1.0 13 | 5, 50, , 500, 53464, 664.896, 3.259, 16403.095, 1.33, 0.76 14 | 3, 10, , 60, 8520, 10.566, 2.002, 4256.474, 0.176, 1.0 15 | 10, 3, , 60, 8520, 15.944, 2.003, 4252.898, 0.266, 1.0 16 | 10, 15, , 300, 40224, 312.654, 3.013, 13350.211, 1.042, 0.94 17 | 8, 3, , 48, 6816, 9.529, 1.013, 6725.632, 0.199, 1.0 18 | 3, 5, , 30, 4260, 4.873, 1.007, 4231.283, 0.162, 1.0 19 | 2, 10, , 40, 5680, 3.946, 1.003, 5661.793, 0.099, 1.0 20 | 2, 20, , 80, 11360, 17.571, 1.02, 11142.076, 0.22, 1.0 21 | 5, 10, , 100, 14200, 23.246, 1.019, 13938.417, 0.232, 1.0 22 | 2, 5, , 20, 2840, 2.434, 1.003, 2830.728, 0.122, 1.0 23 | 5, 20, , 200, 27444, 120.444, 3.003, 9137.58, 0.602, 0.96 24 | 2, 35, , 140, 18184, 39.56, 1.036, 17554.723, 0.283, 0.91 25 | 8, 35, , 560, 55580, 684.793, 3.246, 17123.349, 1.223, 0.71 26 | 10, 10, , 200, 27496, 145.156, 3.003, 9155.117, 0.726, 0.96 27 | 3, 20, , 120, 16644, 38.635, 2.002, 8315.28, 0.322, 0.97 28 | 2, 50, , 200, 27504, 139.358, 3.01, 9138.311, 0.697, 0.96 29 | 2, 3, , 18, 2136, 0.33, 0.027, 80568.609, 0.018, 1.0 30 | 2, 15, , 60, 8520, 9.19, 1.008, 8450.648, 0.153, 1.0 31 | 5, 5, , 50, 7100, 13.391, 2.003, 3544.23, 0.268, 1.0 32 | 3, 15, , 90, 12780, 17.019, 2.001, 6386.061, 0.189, 1.0 33 | 5, 15, , 150, 20956, 76.456, 2.013, 10411.044, 0.51, 0.99 34 | 3, 50, , 300, 38692, 296.854, 3.011, 12848.288, 0.99, 0.9 35 | 8, 20, , 320, 43744, 392.59, 3.033, 14421.897, 1.227, 0.96 36 | -------------------------------------------------------------------------------- /old/summary.csv: -------------------------------------------------------------------------------- 1 | 10, 10, , 300, 23600, 20.199, 1.077, 21907.689, 67.33, 0.53 2 | 10, 15, , 450, 32484, 67.649, 2.07, 15695.845, 150.33, 0.48 3 | 10, 20, , 600, 44320, 147.039, 2.079, 21322.113, 245.066, 0.48 4 | 10, 35, , 1050, 73268, 480.481, 3.119, 23491.295, 457.601, 0.45 5 | 10, 3, , 90, 9096, 3.514, 1.069, 8506.501, 39.04, 0.77 6 | 10, 50, , 1500, 90244, 745.342, 3.259, 27688.9, 496.895, 0.39 7 | 10, 5, , 150, 13240, 6.802, 1.071, 12360.556, 45.347, 0.63 8 | 2, 10, , 60, 6736, 0.743, 0.102, 66075.524, 12.376, 0.77 9 | 2, 15, , 90, 9816, 3.568, 1.07, 9175.129, 39.645, 0.73 10 | 2, 20, , 120, 12896, 7.676, 1.068, 12079.713, 63.971, 0.72 11 | 2, 35, , 210, 22136, 19.587, 1.076, 20579.724, 93.27, 0.7 12 | 2, 3, , 18, 2424, 0.134, 0.079, 30531.153, 7.421, 1.0 13 | 2, 50, , 300, 31376, 81.238, 3.067, 10228.563, 270.792, 0.69 14 | 2, 5, , 30, 3656, 0.244, 0.085, 43265.165, 8.144, 0.87 15 | 3, 10, , 90, 10104, 2.55, 1.068, 9462.849, 28.335, 0.77 16 | 3, 15, , 135, 14724, 7.185, 1.074, 13715.294, 53.225, 0.73 17 | 3, 20, , 180, 19344, 12.982, 1.072, 18040.488, 72.122, 0.72 18 | 3, 35, , 315, 33204, 60.684, 2.074, 16007.529, 192.649, 0.7 19 | 3, 3, , 27, 3636, 0.195, 0.083, 44026.651, 7.24, 1.0 20 | 3, 50, , 450, 46736, 196.871, 3.067, 15236.051, 437.49, 0.68 21 | 3, 5, , 45, 5484, 2.382, 1.07, 5125.522, 52.931, 0.87 22 | 5, 10, , 150, 13960, 6.912, 1.071, 13029.088, 46.079, 0.63 23 | 5, 15, , 225, 20220, 12.472, 1.073, 18846.848, 55.431, 0.6 24 | 5, 20, , 300, 26480, 28.049, 1.087, 24358.876, 93.495, 0.58 25 | 5, 35, , 525, 45260, 119.171, 2.069, 21874.73, 226.992, 0.56 26 | 5, 3, , 45, 5196, 0.434, 0.094, 55496.636, 9.639, 0.87 27 | 5, 50, , 750, 63384, 381.275, 3.08, 20579.565, 508.367, 0.55 28 | 5, 5, , 75, 7700, 3.028, 1.071, 7192.643, 40.368, 0.73 29 | 8, 10, , 240, 19744, 13.979, 1.075, 18359.532, 58.245, 0.56 30 | 8, 15, , 360, 28300, 35.378, 2.068, 13686.937, 98.272, 0.52 31 | 8, 20, , 480, 35544, 68.708, 2.07, 17173.015, 143.142, 0.49 32 | 8, 35, , 840, 61048, 294.448, 3.069, 19890.874, 350.533, 0.47 33 | 8, 3, , 72, 7536, 1.964, 1.068, 7058.846, 27.276, 0.79 34 | 8, 50, , 1200, 84748, 715.082, 3.235, 26193.241, 595.902, 0.45 35 | 8, 5, , 120, 11024, 3.451, 1.068, 10324.176, 28.757, 0.66 36 | -------------------------------------------------------------------------------- /xmlParser.py: -------------------------------------------------------------------------------- 1 | import xml.etree.ElementTree as ET 2 | import pdb; 3 | import os 4 | allfiles = os.listdir('.') 5 | summary_file = open("summary.csv", "w") 6 | for i in xrange(len(allfiles)): 7 | if allfiles[i].endswith('flowMonitor.xml'): 8 | maxTime = 0; 9 | minTime = float('inf'); 10 | totalDelay = 0 11 | totalReceived = 0 12 | messageCount = 0 13 | tr =[] 14 | tree = ET.parse(allfiles[i]) 15 | root = tree.getroot() 16 | for child in root: 17 | if child.tag == 'FlowStats': 18 | messageCount = len(child) 19 | for flow in child: 20 | #pdb.set_trace() 21 | flowDetails = flow.attrib 22 | sent = int(flowDetails['timeFirstTxPacket'][1:-4]) 23 | recv = int(flowDetails['timeLastRxPacket'][1:-4]) 24 | if sent < minTime: 25 | minTime = sent 26 | if recv > maxTime: 27 | maxTime = recv 28 | totalDelay = totalDelay + int(flowDetails['delaySum'][1:-4]) 29 | totalReceived = totalReceived + int(flowDetails['rxBytes']) 30 | tr.append(int(flowDetails['rxPackets'])) 31 | 32 | #print allfiles[i] 33 | #print totalReceived 34 | #print totalDelay 35 | #print maxTime - minTime , maxTime , minTime 36 | # maxTime / 1000000000.0 37 | 38 | 39 | #print totalReceived / ((maxTime - minTime)/ 1000000000.0) 40 | #print (totalDelay/1000000.0) / messageCount 41 | 42 | #pdb.set_trace() 43 | pdr = round( sum(tr)*1.0/messageCount, 2) 44 | 45 | nodeCounts = str(allfiles[i].replace('IOT_','').replace('__flowMonitor.xml',', ').replace('x',', ') ) 46 | throughput = str( round( totalReceived / ((maxTime - minTime)/ 1000000000.0) ,3) ) # in bytes per second 47 | end2endDelay = str( round( (totalDelay/1000000000.0) / messageCount ,3) ) # in miliseconds 48 | print nodeCounts +'messageCount = '+ str(messageCount) +', recvd = '+ str(totalReceived) +', time = '+ str(round(totalDelay/1000000000.0,3) )+ ', throughput =' + throughput+ ', end2endDelay (sec) = '+ end2endDelay+'\n' 49 | output = nodeCounts + ', ' + str(messageCount) +', '+str(totalReceived)+ ', '+ str(round(totalDelay/1000000000.0,3) )+ ', '+str(round((maxTime - minTime)/1000000000.0,3) )+ ', '+ throughput + ', ' + end2endDelay + ', ' + str(pdr)+"\n" 50 | 51 | #print output 52 | #pdb.set_trace() 53 | #print sum(tr) 54 | #print messageCount 55 | #print pdr 56 | #print round(maxTime / 1000000000.0, 3) 57 | summary_file.write(output) 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /test.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 | /* 3 | * Copyright (c) 2015 Sébastien Deronne 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation; 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | * 18 | * Author: Sébastien Deronne 19 | */ 20 | 21 | #include "ns3/core-module.h" 22 | #include "ns3/applications-module.h" 23 | #include "ns3/wifi-module.h" 24 | #include "ns3/mobility-module.h" 25 | #include "ns3/internet-module.h" 26 | 27 | // This example considers two hidden stations in an 802.11n network which supports MPDU aggregation. 28 | // The user can specify whether RTS/CTS is used and can set the number of aggregated MPDUs. 29 | // 30 | // Example: ./waf --run "simple-ht-hidden-stations --enableRts=1 --nMpdus=8" 31 | // 32 | // Network topology: 33 | // 34 | // Wifi 192.168.1.0 35 | // 36 | // AP 37 | // * * * 38 | // | | | 39 | // n1 n2 n3 40 | // 41 | // Packets in this simulation aren't marked with a QosTag so they are considered 42 | // belonging to BestEffort Access Class (AC_BE). 43 | 44 | using namespace ns3; 45 | 46 | NS_LOG_COMPONENT_DEFINE ("SimplesHtHiddenStations"); 47 | 48 | int main (int argc, char *argv[]) 49 | { 50 | uint32_t payloadSize = 1472; //bytes 51 | uint64_t simulationTime = 10; //seconds 52 | uint32_t nMpdus = 1; 53 | uint32_t maxAmpduSize = 0; 54 | bool enableRts = 0; 55 | double minExpectedThroughput = 0; 56 | double maxExpectedThroughput = 0; 57 | 58 | CommandLine cmd; 59 | cmd.AddValue ("nMpdus", "Number of aggregated MPDUs", nMpdus); 60 | cmd.AddValue ("payloadSize", "Payload size in bytes", payloadSize); 61 | cmd.AddValue ("enableRts", "Enable RTS/CTS", enableRts); 62 | cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime); 63 | cmd.AddValue ("minExpectedThroughput", "if set, simulation fails if the lowest throughput is below this value", minExpectedThroughput); 64 | cmd.AddValue ("maxExpectedThroughput", "if set, simulation fails if the highest throughput is above this value", maxExpectedThroughput); 65 | cmd.Parse (argc, argv); 66 | 67 | if (!enableRts) 68 | { 69 | Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("999999")); 70 | } 71 | else 72 | { 73 | Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("0")); 74 | } 75 | 76 | //Set the maximum size for A-MPDU with regards to the payload size 77 | maxAmpduSize = nMpdus * (payloadSize + 200); 78 | 79 | // Set the maximum wireless range to 5 meters in order to reproduce a hidden nodes scenario, i.e. the distance between hidden stations is larger than 5 meters 80 | Config::SetDefault ("ns3::RangePropagationLossModel::MaxRange", DoubleValue (5)); 81 | 82 | NodeContainer wifiStaNodes; 83 | wifiStaNodes.Create (2); 84 | NodeContainer wifiApNode; 85 | wifiApNode.Create (1); 86 | 87 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 88 | channel.AddPropagationLoss ("ns3::RangePropagationLossModel"); //wireless range limited to 5 meters! 89 | 90 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 91 | phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO); 92 | phy.SetChannel (channel.Create ()); 93 | 94 | WifiHelper wifi; 95 | wifi.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ); 96 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("HtMcs7"), "ControlMode", StringValue ("HtMcs0")); 97 | WifiMacHelper mac; 98 | 99 | Ssid ssid = Ssid ("simple-mpdu-aggregation"); 100 | mac.SetType ("ns3::StaWifiMac", 101 | "Ssid", SsidValue (ssid), 102 | "BE_MaxAmpduSize", UintegerValue (maxAmpduSize)); 103 | 104 | NetDeviceContainer staDevices; 105 | staDevices = wifi.Install (phy, mac, wifiStaNodes); 106 | 107 | mac.SetType ("ns3::ApWifiMac", 108 | "Ssid", SsidValue (ssid), 109 | "EnableBeaconJitter", BooleanValue (false), 110 | "BE_MaxAmpduSize", UintegerValue (maxAmpduSize)); 111 | 112 | NetDeviceContainer apDevice; 113 | apDevice = wifi.Install (phy, mac, wifiApNode); 114 | 115 | // Setting mobility model 116 | MobilityHelper mobility; 117 | Ptr positionAlloc = CreateObject (); 118 | 119 | // AP is between the two stations, each station being located at 5 meters from the AP. 120 | // The distance between the two stations is thus equal to 10 meters. 121 | // Since the wireless range is limited to 5 meters, the two stations are hidden from each other. 122 | positionAlloc->Add (Vector (5.0, 0.0, 0.0)); 123 | positionAlloc->Add (Vector (0.0, 0.0, 0.0)); 124 | positionAlloc->Add (Vector (10.0, 0.0, 0.0)); 125 | mobility.SetPositionAllocator (positionAlloc); 126 | 127 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 128 | 129 | mobility.Install (wifiApNode); 130 | mobility.Install (wifiStaNodes); 131 | 132 | // Internet stack 133 | InternetStackHelper stack; 134 | stack.Install (wifiApNode); 135 | stack.Install (wifiStaNodes); 136 | 137 | Ipv4AddressHelper address; 138 | address.SetBase ("192.168.1.0", "255.255.255.0"); 139 | Ipv4InterfaceContainer StaInterface; 140 | StaInterface = address.Assign (staDevices); 141 | Ipv4InterfaceContainer ApInterface; 142 | ApInterface = address.Assign (apDevice); 143 | 144 | // Setting applications 145 | uint16_t port = 9; 146 | UdpServerHelper server (port); 147 | ApplicationContainer serverApp = server.Install (wifiApNode); 148 | serverApp.Start (Seconds (0.0)); 149 | serverApp.Stop (Seconds (simulationTime + 1)); 150 | 151 | UdpClientHelper client (ApInterface.GetAddress (0), port); 152 | client.SetAttribute ("MaxPackets", UintegerValue (4294967295u)); 153 | client.SetAttribute ("Interval", TimeValue (Time ("0.00002"))); //packets/s 154 | client.SetAttribute ("PacketSize", UintegerValue (payloadSize)); 155 | 156 | // Saturated UDP traffic from stations to AP 157 | ApplicationContainer clientApp1 = client.Install (wifiStaNodes); 158 | clientApp1.Start (Seconds (1.0)); 159 | clientApp1.Stop (Seconds (simulationTime + 1)); 160 | 161 | phy.EnablePcap ("SimpleHtHiddenStations_Ap", apDevice.Get (0)); 162 | phy.EnablePcap ("SimpleHtHiddenStations_Sta1", staDevices.Get (0)); 163 | phy.EnablePcap ("SimpleHtHiddenStations_Sta2", staDevices.Get (1)); 164 | 165 | Simulator::Stop (Seconds (simulationTime + 1)); 166 | 167 | Simulator::Run (); 168 | Simulator::Destroy (); 169 | 170 | uint32_t totalPacketsThrough = DynamicCast (serverApp.Get (0))->GetReceived (); 171 | double throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); 172 | std::cout << "Throughput: " << throughput << " Mbit/s" << '\n'; 173 | if (throughput < minExpectedThroughput || (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)) 174 | { 175 | NS_LOG_ERROR ("Obtained throughput " << throughput << " is not in the expected boundaries!"); 176 | exit (1); 177 | } 178 | return 0; 179 | } 180 | -------------------------------------------------------------------------------- /flowTest.cc: -------------------------------------------------------------------------------- 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 2 | /* 3 | * Copyright (c) 2009 University of Washington 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation; 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program; if not, write to the Free Software 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | * 18 | */ 19 | 20 | 21 | 22 | /* 23 | LAB Assignment #5 24 | 1. Setup a 5x5 wireless adhoc network with a grid. You may use 25 | examples/wireless/wifi-simple-adhoc-grid.cc as a base. 26 | 27 | 2. Install the OLSR routing protocol. 28 | 29 | 3. Setup three UDP traffic flows, one along each diagonal and one 30 | along the middle (at high rates of transmission). 31 | 32 | 4. Setup the ns-3 flow monitor for each of these flows. 33 | 34 | 5. Now schedule each of the flows at times 1s, 1.5s, and 2s. 35 | 36 | 6. Now using the flow monitor, observe the throughput of each of the 37 | UDP flows. Furthermore, use the tracing mechanism to monitor the number of 38 | packet collisions/drops at intermediary nodes. Around which nodes are most 39 | of the collisions/drops happening? 40 | 41 | 7. Now repeat the experiment with RTS/CTS enabled on the wifi devices. 42 | 43 | 8. Show the difference in throughput and packet drops if any. 44 | 45 | 46 | Solution by: Konstantinos Katsaros (K.Katsaros@surrey.ac.uk) 47 | based on wifi-simple-adhoc-grid.cc 48 | */ 49 | 50 | // The default layout is like this, on a 2-D grid. 51 | // 52 | // n20 n21 n22 n23 n24 53 | // n15 n16 n17 n18 n19 54 | // n10 n11 n12 n13 n14 55 | // n5 n6 n7 n8 n9 56 | // n0 n1 n2 n3 n4 57 | // 58 | // the layout is affected by the parameters given to GridPositionAllocator; 59 | // by default, GridWidth is 5 and numNodes is 25.. 60 | // 61 | // Flow 1: 0->24 62 | // Flow 2: 20->4 63 | // Flow 3: 10->4 64 | 65 | #include "ns3/core-module.h" 66 | #include "ns3/network-module.h" 67 | #include "ns3/mobility-module.h" 68 | #include "ns3/config-store-module.h" 69 | #include "ns3/wifi-module.h" 70 | #include "ns3/internet-module.h" 71 | #include "ns3/olsr-helper.h" 72 | #include "ns3/flow-monitor-module.h" 73 | #include "myapp.h" 74 | 75 | #include 76 | #include 77 | #include 78 | #include 79 | 80 | NS_LOG_COMPONENT_DEFINE ("Lab5"); 81 | 82 | using namespace ns3; 83 | 84 | uint32_t MacTxDropCount, PhyTxDropCount, PhyRxDropCount; 85 | 86 | void 87 | MacTxDrop(Ptr p) 88 | { 89 | NS_LOG_INFO("Packet Drop"); 90 | MacTxDropCount++; 91 | } 92 | 93 | void 94 | PrintDrop() 95 | { 96 | std::cout << Simulator::Now().GetSeconds() << "\t" << MacTxDropCount << "\t"<< PhyTxDropCount << "\t" << PhyRxDropCount << "\n"; 97 | Simulator::Schedule(Seconds(5.0), &PrintDrop); 98 | } 99 | 100 | void 101 | PhyTxDrop(Ptr p) 102 | { 103 | NS_LOG_INFO("Packet Drop"); 104 | PhyTxDropCount++; 105 | } 106 | void 107 | PhyRxDrop(Ptr p) 108 | { 109 | NS_LOG_INFO("Packet Drop"); 110 | PhyRxDropCount++; 111 | } 112 | int main (int argc, char *argv[]) 113 | { 114 | std::string phyMode ("DsssRate1Mbps"); 115 | double distance = 500; // m 116 | uint32_t numNodes = 25; // by default, 5x5 117 | double interval = 0.001; // seconds 118 | uint32_t packetSize = 600; // bytes 119 | uint32_t numPackets = 10000000; 120 | std::string rtslimit = "1500"; 121 | CommandLine cmd; 122 | 123 | cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode); 124 | cmd.AddValue ("distance", "distance (m)", distance); 125 | cmd.AddValue ("packetSize", "distance (m)", packetSize); 126 | cmd.AddValue ("rtslimit", "RTS/CTS Threshold (bytes)", rtslimit); 127 | cmd.Parse (argc, argv); 128 | // Convert to time object 129 | Time interPacketInterval = Seconds (interval); 130 | 131 | // turn off RTS/CTS for frames below 2200 bytes 132 | Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue (rtslimit)); 133 | // Fix non-unicast data rate to be the same as that of unicast 134 | Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode)); 135 | 136 | NodeContainer c; 137 | c.Create (numNodes); 138 | 139 | // The below set of helpers will help us to put together the wifi NICs we want 140 | WifiHelper wifi; 141 | 142 | YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); 143 | // set it to zero; otherwise, gain will be added 144 | wifiPhy.Set ("RxGain", DoubleValue (-10) ); 145 | // ns-3 supports RadioTap and Prism tracing extensions for 802.11b 146 | wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO); 147 | 148 | YansWifiChannelHelper wifiChannel; 149 | wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); 150 | wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); 151 | wifiPhy.SetChannel (wifiChannel.Create ()); 152 | 153 | // Add a non-QoS upper mac, and disable rate control 154 | NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); 155 | wifi.SetStandard (WIFI_PHY_STANDARD_80211b); 156 | wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", 157 | "DataMode",StringValue (phyMode), 158 | "ControlMode",StringValue (phyMode)); 159 | // Set it to adhoc mode 160 | wifiMac.SetType ("ns3::AdhocWifiMac"); 161 | NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c); 162 | 163 | MobilityHelper mobility; 164 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 165 | "MinX", DoubleValue (0.0), 166 | "MinY", DoubleValue (0.0), 167 | "DeltaX", DoubleValue (distance), 168 | "DeltaY", DoubleValue (distance), 169 | "GridWidth", UintegerValue (5), 170 | "LayoutType", StringValue ("RowFirst")); 171 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 172 | mobility.Install (c); 173 | 174 | // Enable OLSR 175 | OlsrHelper olsr; 176 | 177 | Ipv4ListRoutingHelper list; 178 | list.Add (olsr, 10); 179 | 180 | InternetStackHelper internet; 181 | internet.SetRoutingHelper (list); // has effect on the next Install () 182 | internet.Install (c); 183 | 184 | Ipv4AddressHelper ipv4; 185 | NS_LOG_INFO ("Assign IP Addresses."); 186 | ipv4.SetBase ("10.1.1.0", "255.255.255.0"); 187 | Ipv4InterfaceContainer ifcont = ipv4.Assign (devices); 188 | 189 | // Create Apps 190 | 191 | uint16_t sinkPort = 6; // use the same for all apps 192 | 193 | // UDP connection from N0 to N24 194 | 195 | Address sinkAddress1 (InetSocketAddress (ifcont.GetAddress (24), sinkPort)); // interface of n24 196 | PacketSinkHelper packetSinkHelper1 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort)); 197 | ApplicationContainer sinkApps1 = packetSinkHelper1.Install (c.Get (24)); //n2 as sink 198 | sinkApps1.Start (Seconds (0.)); 199 | sinkApps1.Stop (Seconds (100.)); 200 | 201 | Ptr ns3UdpSocket1 = Socket::CreateSocket (c.Get (0), UdpSocketFactory::GetTypeId ()); //source at n0 202 | 203 | // Create UDP application at n0 204 | Ptr app1 = CreateObject (); 205 | app1->Setup (ns3UdpSocket1, sinkAddress1, packetSize, numPackets, DataRate ("1Mbps")); 206 | c.Get (0)->AddApplication (app1); 207 | app1->SetStartTime (Seconds (31.)); 208 | app1->SetStopTime (Seconds (100.)); 209 | 210 | // UDP connection from N10 to N14 211 | 212 | Address sinkAddress2 (InetSocketAddress (ifcont.GetAddress (14), sinkPort)); // interface of n14 213 | PacketSinkHelper packetSinkHelper2 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort)); 214 | ApplicationContainer sinkApps2 = packetSinkHelper2.Install (c.Get (14)); //n14 as sink 215 | sinkApps2.Start (Seconds (0.)); 216 | sinkApps2.Stop (Seconds (100.)); 217 | 218 | Ptr ns3UdpSocket2 = Socket::CreateSocket (c.Get (10), UdpSocketFactory::GetTypeId ()); //source at n10 219 | 220 | // Create UDP application at n10 221 | Ptr app2 = CreateObject (); 222 | app2->Setup (ns3UdpSocket2, sinkAddress2, packetSize, numPackets, DataRate ("1Mbps")); 223 | c.Get (10)->AddApplication (app2); 224 | app2->SetStartTime (Seconds (31.5)); 225 | app2->SetStopTime (Seconds (100.)); 226 | 227 | // UDP connection from N20 to N4 228 | 229 | Address sinkAddress3 (InetSocketAddress (ifcont.GetAddress (4), sinkPort)); // interface of n4 230 | PacketSinkHelper packetSinkHelper3 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort)); 231 | ApplicationContainer sinkApps3 = packetSinkHelper3.Install (c.Get (4)); //n2 as sink 232 | sinkApps3.Start (Seconds (0.)); 233 | sinkApps3.Stop (Seconds (100.)); 234 | 235 | Ptr ns3UdpSocket3 = Socket::CreateSocket (c.Get (20), UdpSocketFactory::GetTypeId ()); //source at n20 236 | 237 | // Create UDP application at n20 238 | Ptr app3 = CreateObject (); 239 | app3->Setup (ns3UdpSocket3, sinkAddress3, packetSize, numPackets, DataRate ("1Mbps")); 240 | c.Get (20)->AddApplication (app3); 241 | app3->SetStartTime (Seconds (32.)); 242 | app3->SetStopTime (Seconds (100.)); 243 | 244 | // Install FlowMonitor on all nodes 245 | FlowMonitorHelper flowmon; 246 | Ptr monitor = flowmon.InstallAll(); 247 | 248 | 249 | // Trace Collisions 250 | Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/MacTxDrop", MakeCallback(&MacTxDrop)); 251 | Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop", MakeCallback(&PhyRxDrop)); 252 | Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxDrop", MakeCallback(&PhyTxDrop)); 253 | 254 | Simulator::Schedule(Seconds(5.0), &PrintDrop); 255 | 256 | Simulator::Stop (Seconds (100.0)); 257 | Simulator::Run (); 258 | 259 | PrintDrop(); 260 | 261 | // Print per flow statistics 262 | monitor->CheckForLostPackets (); 263 | Ptr classifier = DynamicCast (flowmon.GetClassifier ()); 264 | std::map stats = monitor->GetFlowStats (); 265 | 266 | for (std::map::const_iterator iter = stats.begin (); iter != stats.end (); ++iter) 267 | { 268 | Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first); 269 | 270 | if ((t.sourceAddress == Ipv4Address("10.1.1.1") && t.destinationAddress == Ipv4Address("10.1.1.25")) 271 | || (t.sourceAddress == Ipv4Address("10.1.1.11") && t.destinationAddress == Ipv4Address("10.1.1.15")) 272 | || (t.sourceAddress == Ipv4Address("10.1.1.21") && t.destinationAddress == Ipv4Address("10.1.1.5"))) 273 | { 274 | NS_LOG_UNCOND("Flow ID: " << iter->first << " Src Addr " << t.sourceAddress << " Dst Addr " << t.destinationAddress); 275 | NS_LOG_UNCOND("Tx Packets = " << iter->second.txPackets); 276 | NS_LOG_UNCOND("Rx Packets = " << iter->second.rxPackets); 277 | NS_LOG_UNCOND("Throughput: " << iter->second.rxBytes * 8.0 / (iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds()) / 1024 << " Kbps"); 278 | } 279 | } 280 | monitor->SerializeToXmlFile("lab-5.flowmon", true, true); 281 | 282 | Simulator::Destroy (); 283 | 284 | return 0; 285 | } 286 | -------------------------------------------------------------------------------- /iot_PUF.cc: -------------------------------------------------------------------------------- 1 | /* GNU General Public License for more details. 2 | * 3 | * You should have received a copy of the GNU General Public License 4 | * along with this program; if not, write to the Free Software 5 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | * 7 | * Author: Soumya Banerjee (Adapted from wireless-animation.cc) 8 | */ 9 | 10 | #include /* srand, rand */ 11 | #include 12 | #include "ns3/core-module.h" 13 | #include "ns3/point-to-point-module.h" 14 | #include "ns3/csma-module.h" 15 | #include "ns3/network-module.h" 16 | #include "ns3/applications-module.h" 17 | #include "ns3/olsr-helper.h" 18 | #include "ns3/wifi-module.h" 19 | #include "ns3/mobility-module.h" 20 | #include "ns3/internet-module.h" 21 | #include "ns3/netanim-module.h" 22 | #include "ns3/basic-energy-source.h" 23 | #include "ns3/flow-monitor-helper.h" 24 | //#include "ns3/simple-device-energy-model.h" 25 | //#include "ns3/v4ping-helper.h" 26 | //#include "ns3/v4ping.h" 27 | 28 | using namespace ns3; 29 | 30 | 31 | static bool verbose = 0; 32 | uint32_t M1 = 104, M2=84, M3 = 84; 33 | 34 | char * stringbuilder( char* prefix, char* sufix){ 35 | char* buf = (char*)malloc(50); 36 | snprintf(buf, 50, "%s%s", prefix, sufix); 37 | return buf; 38 | } 39 | 40 | 41 | ApplicationContainer sendMessage(ApplicationContainer apps, double time, Ptrsource,Ptrsink, uint32_t packetSize){ 42 | Ipv4Address remoteAddress = sink->GetObject ()->GetAddress (1, 0).GetLocal (); 43 | 44 | uint16_t port = 9; // well-known echo port number 45 | uint32_t maxPacketCount = 1; 46 | Time interPacketInterval = Seconds (20.); 47 | UdpClientHelper client (remoteAddress, port); 48 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 49 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 50 | client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 51 | client.SetAttribute ("StartTime", TimeValue (Seconds (time))); 52 | //std::cout<>"; 53 | apps.Add(client.Install (source)); 54 | return apps; 55 | } 56 | 57 | ApplicationContainer authenticate(ApplicationContainer appContainer, double time, Ptr user, Ptr gateway , Ptr device ){ 58 | 59 | if (verbose){ 60 | std::cout<<"user : "<< user->GetObject ()->GetAddress (1, 0).GetLocal (); 61 | std::cout<<" gateway : "<< gateway->GetObject ()->GetAddress (1, 0).GetLocal (); 62 | std::cout<<" device : "<< device->GetObject ()->GetAddress (1, 0).GetLocal ()<= 2 seconds" << std::endl; 129 | exit (1); 130 | } 131 | 132 | if (verbose) 133 | { 134 | //LogComponentEnable("UdpClient", LOG_LEVEL_INFO); 135 | //LogComponentEnable("UdpServer", LOG_LEVEL_INFO); 136 | LogComponentEnable("Simulator", LOG_LEVEL_INFO); 137 | } 138 | 139 | //Since default reference loss is defined for 5 GHz, it needs to be changed when operating at 2.4 GHz 140 | Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046)); 141 | 142 | // creating nodes 143 | //NodeContainer allNodes; 144 | NodeContainer wifiUserNodes; 145 | wifiUserNodes.Create (mobileUserNodes); 146 | //allNodes.Add (wifiUserNodes); 147 | NodeContainer wifiDeviceNodes; 148 | wifiDeviceNodes.Create (smartDeviceNodes); 149 | //allNodes.Add (wifiDeviceNodes); 150 | NodeContainer wifiGateway ; 151 | wifiGateway.Create (1); 152 | //allNodes.Add (wifiGateway); 153 | 154 | // creating wireless channel 155 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 156 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 157 | phy.SetChannel (channel.Create ()); 158 | 159 | WifiHelper wifi; 160 | wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); 161 | 162 | WifiMacHelper mac; 163 | Ssid ssid = Ssid ("ns-3-ssid"); 164 | mac.SetType ("ns3::StaWifiMac", 165 | "Ssid", SsidValue (ssid), 166 | "ActiveProbing", BooleanValue (false)); 167 | 168 | NetDeviceContainer UserDevices; 169 | UserDevices = wifi.Install (phy, mac, wifiUserNodes); 170 | mac.SetType ("ns3::StaWifiMac", 171 | "Ssid", SsidValue (ssid)); 172 | 173 | NetDeviceContainer SmartDevices; 174 | SmartDevices = wifi.Install (phy, mac, wifiDeviceNodes); 175 | mac.SetType ("ns3::ApWifiMac", 176 | "Ssid", SsidValue (ssid)); 177 | 178 | 179 | NetDeviceContainer apDevices; 180 | apDevices = wifi.Install (phy, mac, wifiGateway); 181 | 182 | 183 | // defining Mobility 184 | 185 | MobilityHelper mobility; 186 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 187 | "MinX", DoubleValue (-10.0), 188 | "MinY", DoubleValue (-10.0), 189 | "DeltaX", DoubleValue (5.0), 190 | "DeltaY", DoubleValue (5.0), 191 | "GridWidth", UintegerValue (5), 192 | "LayoutType", StringValue ("RowFirst")); 193 | //mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 194 | // "Bounds", RectangleValue (Rectangle (-50, 50, -25, 50))); 195 | 196 | mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", 197 | "Bounds", RectangleValue (Rectangle (-150, 150, -150, 150)), 198 | "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=3]"), 199 | "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.4]")); 200 | mobility.Install (wifiUserNodes); 201 | 202 | 203 | Ptr subnetAlloc = CreateObject (); 204 | subnetAlloc->Add (Vector (0.0, 0.0, 0.0)); //for gateway 205 | for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){ 206 | double theta = (j)*360/wifiDeviceNodes.GetN(); 207 | uint32_t r =((double)rand() / (RAND_MAX))*80 +20; 208 | subnetAlloc->Add (Vector (sin(theta)*r, cos(theta)*r, 0.0)); 209 | //std::cout <<"[ "< gateway = wifiGateway.Get (0); 244 | UdpServerHelper server(port); 245 | serverAppContainer.Add(server.Install (gateway)); 246 | 247 | 248 | double time = 1; 249 | for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i){ 250 | Ptr user = wifiUserNodes.Get (i); 251 | for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){ 252 | Ptr device = wifiDeviceNodes.Get (j); 253 | if(i==0){ 254 | serverAppContainer.Add(server.Install (device)); 255 | //std::cout <<"device "< stream = ascii.CreateFileStream (stringbuilder(saveFilePrefix,(char*)"_trace.tr")); 299 | //phy.EnableAsciiAll (stream); 300 | //stack.EnableAsciiIpv4All (stream); 301 | 302 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_users"), UserDevices, 0); 303 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_devices"), SmartDevices, 0); 304 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_gateway"), apDevices, 0); 305 | 306 | } 307 | 308 | 309 | if(enableAnim) { 310 | AnimationInterface anim (stringbuilder(saveFilePrefix,(char*)"-animation.xml")); // Mandatory 311 | for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i) 312 | { 313 | anim.UpdateNodeDescription (wifiUserNodes.Get (i), "MU"); // Optional 314 | anim.UpdateNodeColor (wifiUserNodes.Get (i), 255, 0, 0); // Optional 315 | } 316 | for (uint32_t i = 0; i < wifiDeviceNodes.GetN (); ++i) 317 | { 318 | anim.UpdateNodeDescription (wifiDeviceNodes.Get (i), "SD"); // Optional 319 | anim.UpdateNodeColor (wifiDeviceNodes.Get (i), 255, 255, 0); // Optional 320 | } 321 | for (uint32_t i = 0; i < wifiGateway.GetN (); ++i) 322 | { 323 | anim.UpdateNodeDescription (wifiGateway.Get (i), "Gateway"); // Optional 324 | anim.UpdateNodeColor (wifiGateway.Get (i), 0, 255, 0); // Optional 325 | } 326 | //anim.EnablePacketMetadata (); // Optional/ 327 | anim.EnableWifiMacCounters (Seconds (0), Seconds (10)); //Optional 328 | anim.EnableWifiPhyCounters (Seconds (0), Seconds (10)); //Optional 329 | } 330 | 331 | 332 | 333 | 334 | //Populate routing table 335 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 336 | 337 | // setting up simulator 338 | 339 | 340 | Ptr flowMonitor; 341 | FlowMonitorHelper flowHelper; 342 | flowMonitor = flowHelper.InstallAll(); 343 | 344 | Simulator::Stop (Seconds (stopTime+1)); 345 | Simulator::Run (); 346 | Simulator::Destroy (); 347 | flowMonitor->SerializeToXmlFile(stringbuilder(saveFilePrefix,(char*)"_flowMonitor.xml"), false, false); 348 | 349 | 350 | 351 | 352 | uint32_t bytes_received = 0; 353 | 354 | for (uint32_t i = 0; i < serverAppContainer.GetN (); ++i){ 355 | char nodename[30+sizeof(serverAppContainer.GetN ())*8]; 356 | uint32_t expected = 1; 357 | uint32_t totalPacketsThrough = DynamicCast (serverAppContainer.Get (i))->GetReceived ();; 358 | if (i==0){ 359 | snprintf(nodename, sizeof(nodename), "gateway\t"); 360 | expected = M2;//martDeviceNodes * mobileUserNodes; 361 | bytes_received += expected; 362 | }else if (i (serverAppContainer.Get (i))->GetLost (); 375 | //uint32_t window = DynamicCast (serverAppContainer.Get (i))->GetPacketWindowSize (); 376 | //std::cout <<"\t. Packets lost: " << lost <<"( "< 11 | #include 12 | #include "ns3/core-module.h" 13 | #include "ns3/point-to-point-module.h" 14 | #include "ns3/csma-module.h" 15 | #include "ns3/network-module.h" 16 | #include "ns3/applications-module.h" 17 | #include "ns3/olsr-helper.h" 18 | #include "ns3/wifi-module.h" 19 | #include "ns3/mobility-module.h" 20 | #include "ns3/internet-module.h" 21 | #include "ns3/netanim-module.h" 22 | #include "ns3/basic-energy-source.h" 23 | #include "ns3/flow-monitor-helper.h" 24 | 25 | using namespace ns3; 26 | 27 | 28 | 29 | 30 | char * stringbuilder( char* prefix, char* sufix){ 31 | char* buf = (char*)malloc(50); 32 | snprintf(buf, 50, "%s%s", prefix, sufix); 33 | return buf; 34 | } 35 | 36 | 37 | ApplicationContainer sendMessage(ApplicationContainer apps, double time, Ptrsource,Ptrsink, uint32_t packetSize){ 38 | Ipv4Address remoteAddress = sink->GetObject ()->GetAddress (1, 0).GetLocal (); 39 | 40 | uint16_t port = 9; // well-known echo port number 41 | uint32_t maxPacketCount = 1; 42 | Time interPacketInterval = Seconds (20.); 43 | UdpClientHelper client (remoteAddress, port); 44 | client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 45 | client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 46 | client.SetAttribute ("PacketSize", UintegerValue (packetSize+12)); 47 | client.SetAttribute ("StartTime", TimeValue (Seconds (time))); 48 | 49 | apps.Add(client.Install (source)); 50 | return apps; 51 | } 52 | 53 | ApplicationContainer authenticateA(ApplicationContainer appContainer, double time, Ptr user, Ptr gateway ){ 54 | 55 | uint32_t M1 = 84, M2=64, M3=1, M4 =1; 56 | appContainer = sendMessage(appContainer, time, user, gateway , M1); 57 | appContainer = sendMessage(appContainer, time, gateway, user, M2); 58 | appContainer = sendMessage(appContainer, time, user, gateway , M3); 59 | appContainer = sendMessage(appContainer, time, gateway, user, M4); 60 | 61 | return appContainer; 62 | } 63 | 64 | ApplicationContainer authenticateB(ApplicationContainer appContainer, double time, Ptr user, Ptr gateway , Ptr gateway2 ){ 65 | 66 | uint32_t M1 = 84, M2=64, M3=1, M4=1; 67 | appContainer = sendMessage(appContainer, time, user, gateway , M1); 68 | appContainer = sendMessage(appContainer, time, gateway, gateway2, M1); 69 | appContainer = sendMessage(appContainer, time, gateway2, user, M2); 70 | appContainer = sendMessage(appContainer, time, user, gateway2 , M3); 71 | appContainer = sendMessage(appContainer, time, gateway2, user, M4); 72 | 73 | return appContainer; 74 | } 75 | 76 | ApplicationContainer authenticateC(ApplicationContainer appContainer, double time, Ptr BS, Ptr CH , Ptr SN ){ 77 | 78 | uint32_t M1 = 124, M2=108, M3=44, M4=64; 79 | appContainer = sendMessage(appContainer, time, BS, CH, M1); 80 | appContainer = sendMessage(appContainer, time, CH, SN, M2); 81 | appContainer = sendMessage(appContainer, time, SN, CH, M3); 82 | appContainer = sendMessage(appContainer, time, CH, BS, M4); 83 | 84 | return appContainer; 85 | } 86 | 87 | 88 | 89 | 90 | int 91 | main (int argc, char *argv[]) 92 | { 93 | 94 | // 95 | // First, we declare and initialize a few local variables that control some 96 | 97 | // simulation parameters. 98 | uint32_t mobileUserNodes = 3; 99 | uint32_t baseStations = 2; 100 | uint32_t sensorNodes = 4; 101 | uint32_t clusterHeads = 2; 102 | uint32_t stopTime = 2400; 103 | bool skipApplications =0; 104 | bool verbose = 0; 105 | char saveFilePrefix[50] ; 106 | 107 | 108 | 109 | CommandLine cmd; 110 | cmd.AddValue ("MU", "number of user nodes", mobileUserNodes); 111 | cmd.AddValue ("BS", "number of base stations", baseStations); 112 | cmd.AddValue ("SN", "number of sensors nodes", sensorNodes); 113 | cmd.AddValue ("CH", "number of cluster heads", clusterHeads); 114 | cmd.AddValue ("t", "simulation stop time (seconds)", stopTime); 115 | cmd.AddValue ("na", "Dont install applications", skipApplications); 116 | cmd.AddValue ("v", "Verbose mode.", verbose); 117 | cmd.AddValue ("s", "Define the prefix for .pcap anf .xml files. Default: WSN ", saveFilePrefix); 118 | 119 | 120 | 121 | 122 | 123 | 124 | // 125 | // The system global variables and the local values added to the argument 126 | // system can be overridden by command line arguments by using this call. 127 | // 128 | cmd.Parse (argc, argv); 129 | 130 | if (stopTime < 2) 131 | { 132 | std::cout << "Use a simulation stop time >= 2 seconds" << std::endl; 133 | exit (1); 134 | } 135 | 136 | std::cout << "MU="<< mobileUserNodes <<", BS="<< baseStations <<", SN="<< sensorNodes << ", CH="<< clusterHeads < subnetAlloc = CreateObject (); 236 | 237 | subnetAlloc->Add (Vector (0.0, 0.0, 0.0)); //for BS_0 238 | BS_pos[0][0]=0.0; 239 | BS_pos[0][1]=0.0; 240 | 241 | for (uint32_t j = 0; j < baseStations-1; ++j){ 242 | double theta = (j)*360/(baseStations-1); 243 | uint32_t r = 200; 244 | BS_pos[j+1][0]=sin(theta)*r; 245 | BS_pos[j+1][1]=cos(theta)*r; 246 | subnetAlloc->Add (Vector (sin(theta)*r, cos(theta)*r, 0.0)); 247 | } 248 | 249 | 250 | uint32_t rBS; 251 | 252 | for (uint32_t j = 0; j < wifiNode_CH.GetN (); ++j){ 253 | double theta = (j)*360/wifiNode_CH.GetN(); 254 | uint32_t r =((double)rand() / (RAND_MAX))*90 ; 255 | rBS = j % baseStations; 256 | CH_pos[j][0]= BS_pos[rBS][0] + sin(theta)*r; 257 | CH_pos[j][1]= BS_pos[rBS][1] + cos(theta)*r; 258 | subnetAlloc->Add (Vector (CH_pos[j][0], CH_pos[j][1], 0.0)); 259 | nearestBS[j] = 0; 260 | double minval = 1000; 261 | for(uint32_t k = 0; k < baseStations; ++k){ 262 | double d = sqrt( pow(BS_pos[k][0]-CH_pos[j][0] ,2) + pow(BS_pos[k][1]-CH_pos[j][1],2) ); 263 | if (d < minval){ 264 | nearestBS[j] = k; 265 | minval = d; 266 | } 267 | } 268 | } 269 | 270 | double x, y; 271 | for (uint32_t j = 0; j < wifiNode_SN.GetN (); ++j){ 272 | double theta = (j)*360/wifiNode_SN.GetN(); 273 | uint32_t r =((double)rand() / (RAND_MAX))*20 ; 274 | rBS = j % clusterHeads; 275 | x = CH_pos[rBS][0] + (sin(theta)*r); 276 | y = CH_pos[rBS][1] + (cos(theta)*r); 277 | subnetAlloc->Add (Vector (x , y, 0.0)); 278 | 279 | nearestCH[j] = 0; 280 | double minval = 1000; 281 | for(uint32_t k = 0; k < baseStations; ++k){ 282 | double d = sqrt( pow(CH_pos[k][0]-sin(theta)*r ,2) + pow(CH_pos[k][1]-cos(theta)*r ,2) ); 283 | if (d < minval){ 284 | nearestCH[j] = k; 285 | minval = d; 286 | } 287 | } 288 | } 289 | 290 | mobility.SetPositionAllocator (subnetAlloc); 291 | mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); 292 | mobility.Install (wifiGateway); 293 | mobility.Install (wifiNode_CH); 294 | mobility.Install (wifiNode_SN); 295 | 296 | 297 | // Installing internet stack 298 | 299 | InternetStackHelper stack; 300 | OlsrHelper olsr; 301 | stack.SetRoutingHelper (olsr); 302 | stack.Install (wifiUserNodes); 303 | stack.Install (wifiNode_CH); 304 | stack.Install (wifiNode_SN); 305 | stack.Install (wifiGateway); 306 | 307 | 308 | // Install Ipv4 addresses 309 | Ipv4AddressHelper address; 310 | address.SetBase ("10.1.1.0", "255.255.255.0"); 311 | Ipv4InterfaceContainer apInterface; 312 | apInterface = address.Assign (apDevices); 313 | apInterface = address.Assign (UserDevices); 314 | apInterface = address.Assign (SmartDevices_CH); 315 | apInterface = address.Assign (SmartDevices_SN); 316 | 317 | 318 | 319 | // Crating applications 320 | 321 | ApplicationContainer serverAppContainer, clientAppContainer; 322 | 323 | if (skipApplications){ 324 | std::cout <<"Setup skipping applications."< user = wifiUserNodes.Get (i); 343 | for (uint32_t j = i; j < sensorNodes; j+=mobileUserNodes){ 344 | 345 | Ptr CH = wifiNode_CH.Get (nearestCH[j] ); 346 | Ptr BS = wifiGateway.Get (nearestBS[ nearestCH[j] ] ); 347 | Ptr SN = wifiNode_SN.Get (j); 348 | 349 | if(nearestBS[ nearestCH[j] ] == 0){ 350 | clientAppContainer = authenticateA(clientAppContainer, time , user, BS); 351 | }else{ 352 | clientAppContainer = authenticateB(clientAppContainer, time , user, wifiGateway.Get (0), BS); 353 | } 354 | clientAppContainer = authenticateC(clientAppContainer, time , BS, CH, SN); 355 | //time = time +.2; 356 | } 357 | serverAppContainer.Add(server.Install (user)); 358 | //time = time +.1; 359 | } 360 | 361 | 362 | 363 | 364 | 365 | serverAppContainer.Start (Seconds (0.0)); 366 | serverAppContainer.Stop (Seconds (stopTime+1)); 367 | 368 | 369 | 370 | //clientAppContainer.Start (Seconds (1.0)); //started individually 371 | clientAppContainer.Stop (Seconds (stopTime+1)); 372 | 373 | } 374 | 375 | 376 | 377 | std::cout <<"Setup Compete."< flowMonitor; 396 | FlowMonitorHelper flowHelper; 397 | flowMonitor = flowHelper.InstallAll(); 398 | 399 | Simulator::Stop (Seconds (stopTime+1)); 400 | Simulator::Run (); 401 | Simulator::Destroy (); 402 | 403 | // Generating output 404 | 405 | flowMonitor->SerializeToXmlFile(stringbuilder(saveFilePrefix,(char*)"_flowMonitor.xml"), false, false); 406 | if (!skipApplications){ 407 | uint32_t bytes_received = 0, totalPacketsThrough; 408 | for (uint32_t i = 0; i < serverAppContainer.GetN (); ++i){ 409 | totalPacketsThrough = DynamicCast (serverAppContainer.Get (i))->GetReceived ();; 410 | bytes_received += totalPacketsThrough ; 411 | } 412 | std::cout <<"Total packets received ("<< "MU="<< mobileUserNodes <<", BS="<< baseStations <<", SN="<< sensorNodes << ", CH="<< clusterHeads <<") : "<< bytes_received << std::endl; 413 | } 414 | return 0; 415 | } 416 | 417 | 418 | 419 | 420 | -------------------------------------------------------------------------------- /iot_MasterCopy.cc: -------------------------------------------------------------------------------- 1 | /* GNU General Public License for more details. 2 | * 3 | * You should have received a copy of the GNU General Public License 4 | * along with this program; if not, write to the Free Software 5 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | * 7 | * Author: Soumya Banerjee (Adapted from wireless-animation.cc) 8 | */ 9 | 10 | #include /* srand, rand */ 11 | #include 12 | #include "ns3/core-module.h" 13 | #include "ns3/point-to-point-module.h" 14 | #include "ns3/csma-module.h" 15 | #include "ns3/network-module.h" 16 | #include "ns3/applications-module.h" 17 | #include "ns3/olsr-helper.h" 18 | #include "ns3/wifi-module.h" 19 | #include "ns3/mobility-module.h" 20 | #include "ns3/internet-module.h" 21 | #include "ns3/netanim-module.h" 22 | #include "ns3/basic-energy-source.h" 23 | #include "ns3/flow-monitor-helper.h" 24 | //#include "ns3/simple-device-energy-model.h" 25 | //#include "ns3/v4ping-helper.h" 26 | //#include "ns3/v4ping.h" 27 | 28 | using namespace ns3; 29 | 30 | 31 | static bool verbose = 0; 32 | 33 | char * stringbuilder( char* prefix, char* sufix){ 34 | char* buf = (char*)malloc(50); 35 | snprintf(buf, 50, "%s%s", prefix, sufix); 36 | return buf; 37 | } 38 | 39 | 40 | ApplicationContainer sendMessage(ApplicationContainer apps, double time, Ptrsource,Ptrsink, uint32_t packetSize){ 41 | Ipv4Address remoteAddress = sink->GetObject ()->GetAddress (1, 0).GetLocal (); 42 | /*V4PingHelper ping4(remoteAddress); // remote address 43 | //ping4.SetAttribute ("MaxPackets", UintegerValue (1)); 44 | ping4.SetAttribute ("Interval", TimeValue (Seconds (25.))); 45 | ping4.SetAttribute ("Size", UintegerValue (packetSize)); 46 | ApplicationContainer apps = ping4.Install (source); 47 | apps.Start (Seconds (3.0)); 48 | apps.Stop (Seconds (15.0)); 49 | */ 50 | //std::cout <<"message at "<>"; 61 | apps.Add(client.Install (source)); 62 | return apps; 63 | } 64 | 65 | ApplicationContainer authenticate(ApplicationContainer appContainer, double time, Ptr user, Ptr gateway , Ptr device ){ 66 | 67 | if (verbose){ 68 | std::cout<<"user : "<< user->GetObject ()->GetAddress (1, 0).GetLocal (); 69 | std::cout<<" gateway : "<< gateway->GetObject ()->GetAddress (1, 0).GetLocal (); 70 | std::cout<<" device : "<< device->GetObject ()->GetAddress (1, 0).GetLocal ()<= 2 seconds" << std::endl; 135 | exit (1); 136 | } 137 | 138 | if (verbose) 139 | { 140 | //LogComponentEnable("UdpClient", LOG_LEVEL_INFO); 141 | //LogComponentEnable("UdpServer", LOG_LEVEL_INFO); 142 | LogComponentEnable("Simulator", LOG_LEVEL_INFO); 143 | } 144 | //Since default reference loss is defined for 5 GHz, it needs to be changed when operating at 2.4 GHz 145 | Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046)); 146 | 147 | // creating nodes 148 | //NodeContainer allNodes; 149 | NodeContainer wifiUserNodes; 150 | wifiUserNodes.Create (mobileUserNodes); 151 | //allNodes.Add (wifiUserNodes); 152 | NodeContainer wifiDeviceNodes; 153 | wifiDeviceNodes.Create (smartDeviceNodes); 154 | //allNodes.Add (wifiDeviceNodes); 155 | NodeContainer wifiGateway ; 156 | wifiGateway.Create (1); 157 | //allNodes.Add (wifiGateway); 158 | 159 | // creating wireless channel 160 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 161 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 162 | phy.SetChannel (channel.Create ()); 163 | 164 | WifiHelper wifi; 165 | wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); 166 | 167 | WifiMacHelper mac; 168 | Ssid ssid = Ssid ("ns-3-ssid"); 169 | mac.SetType ("ns3::StaWifiMac", 170 | "Ssid", SsidValue (ssid), 171 | "ActiveProbing", BooleanValue (false)); 172 | 173 | NetDeviceContainer UserDevices; 174 | UserDevices = wifi.Install (phy, mac, wifiUserNodes); 175 | mac.SetType ("ns3::StaWifiMac", 176 | "Ssid", SsidValue (ssid)); 177 | 178 | NetDeviceContainer SmartDevices; 179 | SmartDevices = wifi.Install (phy, mac, wifiDeviceNodes); 180 | mac.SetType ("ns3::ApWifiMac", 181 | "Ssid", SsidValue (ssid)); 182 | 183 | 184 | NetDeviceContainer apDevices; 185 | apDevices = wifi.Install (phy, mac, wifiGateway); 186 | 187 | 188 | // defining Mobility 189 | 190 | MobilityHelper mobility; 191 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 192 | "MinX", DoubleValue (-10.0), 193 | "MinY", DoubleValue (-10.0), 194 | "DeltaX", DoubleValue (5.0), 195 | "DeltaY", DoubleValue (5.0), 196 | "GridWidth", UintegerValue (5), 197 | "LayoutType", StringValue ("RowFirst")); 198 | //mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 199 | // "Bounds", RectangleValue (Rectangle (-50, 50, -25, 50))); 200 | 201 | mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", 202 | "Bounds", RectangleValue (Rectangle (-150, 150, -150, 150)), 203 | "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=3]"), 204 | "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.4]")); 205 | mobility.Install (wifiUserNodes); 206 | 207 | /*MobilityModel mobilityModel = MobilityModel (); 208 | mobilityModel.SetAttribute ("Bounds", RectangleValue (Rectangle (-150, 150, -150, 150))); 209 | mobilityModel.SetAttribute ("Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.4]")); 210 | 211 | for (uint32_t j = 0; j < wifiUserNodes.GetN (); ++j){ 212 | if(j==0) 213 | mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); 214 | else if(j==1) 215 | mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=2]")); 216 | else 217 | mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=15]")); 218 | mobility.Install (wifiUserNodes.Get(j)); 219 | //std::cout< subnetAlloc = CreateObject (); 226 | subnetAlloc->Add (Vector (0.0, 0.0, 0.0)); //for gateway 227 | for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){ 228 | double theta = (j)*360/wifiDeviceNodes.GetN(); 229 | uint32_t r =((double)rand() / (RAND_MAX))*80 +20; 230 | subnetAlloc->Add (Vector (sin(theta)*r, cos(theta)*r, 0.0)); 231 | //std::cout <<"[ "< gateway = wifiGateway.Get (0); 266 | UdpServerHelper server(port); 267 | serverAppContainer.Add(server.Install (gateway)); 268 | 269 | 270 | double time = 1; 271 | for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i){ 272 | Ptr user = wifiUserNodes.Get (i); 273 | for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){ 274 | Ptr device = wifiDeviceNodes.Get (j); 275 | if(i==0){ 276 | serverAppContainer.Add(server.Install (device)); 277 | //std::cout <<"device "< stream = ascii.CreateFileStream (stringbuilder(saveFilePrefix,(char*)"_trace.tr")); 321 | //phy.EnableAsciiAll (stream); 322 | //stack.EnableAsciiIpv4All (stream); 323 | 324 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_users"), UserDevices, 0); 325 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_devices"), SmartDevices, 0); 326 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_gateway"), apDevices, 0); 327 | 328 | } 329 | 330 | 331 | if(enableAnim) { 332 | AnimationInterface anim (stringbuilder(saveFilePrefix,(char*)"-animation.xml")); // Mandatory 333 | for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i) 334 | { 335 | anim.UpdateNodeDescription (wifiUserNodes.Get (i), "MU"); // Optional 336 | anim.UpdateNodeColor (wifiUserNodes.Get (i), 255, 0, 0); // Optional 337 | } 338 | for (uint32_t i = 0; i < wifiDeviceNodes.GetN (); ++i) 339 | { 340 | anim.UpdateNodeDescription (wifiDeviceNodes.Get (i), "SD"); // Optional 341 | anim.UpdateNodeColor (wifiDeviceNodes.Get (i), 255, 255, 0); // Optional 342 | } 343 | for (uint32_t i = 0; i < wifiGateway.GetN (); ++i) 344 | { 345 | anim.UpdateNodeDescription (wifiGateway.Get (i), "Gateway"); // Optional 346 | anim.UpdateNodeColor (wifiGateway.Get (i), 0, 255, 0); // Optional 347 | } 348 | //anim.EnablePacketMetadata (); // Optional/ 349 | anim.EnableWifiMacCounters (Seconds (0), Seconds (10)); //Optional 350 | anim.EnableWifiPhyCounters (Seconds (0), Seconds (10)); //Optional 351 | } 352 | 353 | 354 | 355 | 356 | //Populate routing table 357 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 358 | 359 | // setting up simulator 360 | 361 | 362 | Ptr flowMonitor; 363 | FlowMonitorHelper flowHelper; 364 | flowMonitor = flowHelper.InstallAll(); 365 | 366 | Simulator::Stop (Seconds (stopTime+1)); 367 | Simulator::Run (); 368 | Simulator::Destroy (); 369 | flowMonitor->SerializeToXmlFile(stringbuilder(saveFilePrefix,(char*)"_flowMonitor.xml"), false, false); 370 | 371 | 372 | 373 | 374 | uint32_t bytes_received = 0; 375 | 376 | for (uint32_t i = 0; i < serverAppContainer.GetN (); ++i){ 377 | char nodename[30+sizeof(serverAppContainer.GetN ())*8]; 378 | uint32_t expected=1; 379 | uint32_t totalPacketsThrough = DynamicCast (serverAppContainer.Get (i))->GetReceived ();; 380 | if (i==0){ 381 | snprintf(nodename, sizeof(nodename), "gateway\t"); 382 | expected = smartDeviceNodes * mobileUserNodes; 383 | bytes_received += expected*84; 384 | }else if (i (serverAppContainer.Get (i))->GetLost (); 397 | //uint32_t window = DynamicCast (serverAppContainer.Get (i))->GetPacketWindowSize (); 398 | //std::cout <<"\t. Packets lost: " << lost <<"( "< /* srand, rand */ 11 | #include 12 | #include "ns3/core-module.h" 13 | #include "ns3/point-to-point-module.h" 14 | #include "ns3/csma-module.h" 15 | #include "ns3/network-module.h" 16 | #include "ns3/applications-module.h" 17 | #include "ns3/olsr-helper.h" 18 | #include "ns3/wifi-module.h" 19 | #include "ns3/mobility-module.h" 20 | #include "ns3/internet-module.h" 21 | #include "ns3/netanim-module.h" 22 | #include "ns3/basic-energy-source.h" 23 | #include "ns3/flow-monitor-helper.h" 24 | //#include "ns3/simple-device-energy-model.h" 25 | //#include "ns3/v4ping-helper.h" 26 | //#include "ns3/v4ping.h" 27 | 28 | using namespace ns3; 29 | 30 | 31 | static bool verbose = 0; 32 | uint32_t M1 = 84, M3=144;//, M3 = 64; 33 | 34 | char * stringbuilder( char* prefix, char* sufix){ 35 | char* buf = (char*)malloc(50); 36 | snprintf(buf, 50, "%s%s", prefix, sufix); 37 | return buf; 38 | } 39 | 40 | 41 | ApplicationContainer sendMessage(ApplicationContainer apps, double time, Ptrsource,Ptrsink, uint32_t packetSize){ 42 | Ipv4Address remoteAddress = sink->GetObject ()->GetAddress (1, 0).GetLocal (); 43 | /*V4PingHelper ping4(remoteAddress); // remote address 44 | //ping4.SetAttribute ("MaxPackets", UintegerValue (1)); 45 | ping4.SetAttribute ("Interval", TimeValue (Seconds (25.))); 46 | ping4.SetAttribute ("Size", UintegerValue (packetSize)); 47 | ApplicationContainer apps = ping4.Install (source); 48 | apps.Start (Seconds (3.0)); 49 | apps.Stop (Seconds (15.0)); 50 | */ 51 | //std::cout <<"message at "<>"; 62 | apps.Add(client.Install (source)); 63 | return apps; 64 | } 65 | 66 | ApplicationContainer authenticate(ApplicationContainer appContainer, double time, Ptr user, Ptr gateway , Ptr device ){ 67 | 68 | if (verbose){ 69 | std::cout<<"user : "<< user->GetObject ()->GetAddress (1, 0).GetLocal (); 70 | std::cout<<" gateway : "<< gateway->GetObject ()->GetAddress (1, 0).GetLocal (); 71 | std::cout<<" device : "<< device->GetObject ()->GetAddress (1, 0).GetLocal ()<= 2 seconds" << std::endl; 139 | exit (1); 140 | } 141 | 142 | if (verbose) 143 | { 144 | //LogComponentEnable("UdpClient", LOG_LEVEL_INFO); 145 | //LogComponentEnable("UdpServer", LOG_LEVEL_INFO); 146 | LogComponentEnable("Simulator", LOG_LEVEL_INFO); 147 | } 148 | //Since default reference loss is defined for 5 GHz, it needs to be changed when operating at 2.4 GHz 149 | Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046)); 150 | 151 | // creating nodes 152 | //NodeContainer allNodes; 153 | NodeContainer wifiUserNodes; 154 | wifiUserNodes.Create (mobileUserNodes); 155 | //allNodes.Add (wifiUserNodes); 156 | NodeContainer wifiDeviceNodes; 157 | wifiDeviceNodes.Create (smartDeviceNodes); 158 | //allNodes.Add (wifiDeviceNodes); 159 | NodeContainer wifiGateway ; 160 | wifiGateway.Create (1); 161 | //allNodes.Add (wifiGateway); 162 | 163 | // creating wireless channel 164 | YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); 165 | YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); 166 | phy.SetChannel (channel.Create ()); 167 | 168 | WifiHelper wifi; 169 | wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); 170 | 171 | WifiMacHelper mac; 172 | Ssid ssid = Ssid ("ns-3-ssid"); 173 | mac.SetType ("ns3::StaWifiMac", 174 | "Ssid", SsidValue (ssid), 175 | "ActiveProbing", BooleanValue (false)); 176 | 177 | NetDeviceContainer UserDevices; 178 | UserDevices = wifi.Install (phy, mac, wifiUserNodes); 179 | mac.SetType ("ns3::StaWifiMac", 180 | "Ssid", SsidValue (ssid)); 181 | 182 | NetDeviceContainer SmartDevices; 183 | SmartDevices = wifi.Install (phy, mac, wifiDeviceNodes); 184 | mac.SetType ("ns3::ApWifiMac", 185 | "Ssid", SsidValue (ssid)); 186 | 187 | 188 | NetDeviceContainer apDevices; 189 | apDevices = wifi.Install (phy, mac, wifiGateway); 190 | 191 | 192 | // defining Mobility 193 | 194 | MobilityHelper mobility; 195 | mobility.SetPositionAllocator ("ns3::GridPositionAllocator", 196 | "MinX", DoubleValue (-10.0), 197 | "MinY", DoubleValue (-10.0), 198 | "DeltaX", DoubleValue (5.0), 199 | "DeltaY", DoubleValue (5.0), 200 | "GridWidth", UintegerValue (5), 201 | "LayoutType", StringValue ("RowFirst")); 202 | //mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", 203 | // "Bounds", RectangleValue (Rectangle (-50, 50, -25, 50))); 204 | 205 | mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", 206 | "Bounds", RectangleValue (Rectangle (-150, 150, -150, 150)), 207 | "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=3]"), 208 | "Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.4]")); 209 | mobility.Install (wifiUserNodes); 210 | 211 | /*MobilityModel mobilityModel = MobilityModel (); 212 | mobilityModel.SetAttribute ("Bounds", RectangleValue (Rectangle (-150, 150, -150, 150))); 213 | mobilityModel.SetAttribute ("Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.4]")); 214 | 215 | for (uint32_t j = 0; j < wifiUserNodes.GetN (); ++j){ 216 | if(j==0) 217 | mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); 218 | else if(j==1) 219 | mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=2]")); 220 | else 221 | mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=15]")); 222 | mobility.Install (wifiUserNodes.Get(j)); 223 | //std::cout< subnetAlloc = CreateObject (); 230 | subnetAlloc->Add (Vector (0.0, 0.0, 0.0)); //for gateway 231 | for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){ 232 | double theta = (j)*360/wifiDeviceNodes.GetN(); 233 | uint32_t r =((double)rand() / (RAND_MAX))*80 +20; 234 | subnetAlloc->Add (Vector (sin(theta)*r, cos(theta)*r, 0.0)); 235 | //std::cout <<"[ "< gateway = wifiGateway.Get (0); 270 | UdpServerHelper server(port); 271 | serverAppContainer.Add(server.Install (gateway)); 272 | 273 | 274 | double time = 1; 275 | for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i){ 276 | Ptr user = wifiUserNodes.Get (i); 277 | for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){ 278 | Ptr device = wifiDeviceNodes.Get (j); 279 | if(i==0){ 280 | serverAppContainer.Add(server.Install (device)); 281 | //std::cout <<"device "< stream = ascii.CreateFileStream (stringbuilder(saveFilePrefix,(char*)"_trace.tr")); 325 | //phy.EnableAsciiAll (stream); 326 | //stack.EnableAsciiIpv4All (stream); 327 | 328 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_users"), UserDevices, 0); 329 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_devices"), SmartDevices, 0); 330 | phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_gateway"), apDevices, 0); 331 | 332 | } 333 | 334 | 335 | if(enableAnim) { 336 | AnimationInterface anim (stringbuilder(saveFilePrefix,(char*)"-animation.xml")); // Mandatory 337 | for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i) 338 | { 339 | anim.UpdateNodeDescription (wifiUserNodes.Get (i), "MU"); // Optional 340 | anim.UpdateNodeColor (wifiUserNodes.Get (i), 255, 0, 0); // Optional 341 | } 342 | for (uint32_t i = 0; i < wifiDeviceNodes.GetN (); ++i) 343 | { 344 | anim.UpdateNodeDescription (wifiDeviceNodes.Get (i), "SD"); // Optional 345 | anim.UpdateNodeColor (wifiDeviceNodes.Get (i), 255, 255, 0); // Optional 346 | } 347 | for (uint32_t i = 0; i < wifiGateway.GetN (); ++i) 348 | { 349 | anim.UpdateNodeDescription (wifiGateway.Get (i), "Gateway"); // Optional 350 | anim.UpdateNodeColor (wifiGateway.Get (i), 0, 255, 0); // Optional 351 | } 352 | //anim.EnablePacketMetadata (); // Optional/ 353 | anim.EnableWifiMacCounters (Seconds (0), Seconds (10)); //Optional 354 | anim.EnableWifiPhyCounters (Seconds (0), Seconds (10)); //Optional 355 | } 356 | 357 | 358 | 359 | 360 | //Populate routing table 361 | Ipv4GlobalRoutingHelper::PopulateRoutingTables (); 362 | 363 | // setting up simulator 364 | 365 | 366 | Ptr flowMonitor; 367 | FlowMonitorHelper flowHelper; 368 | flowMonitor = flowHelper.InstallAll(); 369 | 370 | Simulator::Stop (Seconds (stopTime+1)); 371 | Simulator::Run (); 372 | Simulator::Destroy (); 373 | flowMonitor->SerializeToXmlFile(stringbuilder(saveFilePrefix,(char*)"_flowMonitor.xml"), false, false); 374 | 375 | 376 | 377 | 378 | uint32_t bytes_received = 0; 379 | 380 | for (uint32_t i = 0; i < serverAppContainer.GetN (); ++i){ 381 | char nodename[30+sizeof(serverAppContainer.GetN ())*8]; 382 | uint32_t expected = 1; 383 | uint32_t totalPacketsThrough = DynamicCast (serverAppContainer.Get (i))->GetReceived ();; 384 | if (i==0){ 385 | snprintf(nodename, sizeof(nodename), "gateway\t"); 386 | expected = 0;//martDeviceNodes * mobileUserNodes; 387 | bytes_received += expected; 388 | }else if (i (serverAppContainer.Get (i))->GetLost (); 401 | //uint32_t window = DynamicCast (serverAppContainer.Get (i))->GetPacketWindowSize (); 402 | //std::cout <<"\t. Packets lost: " << lost <<"( "< -------------------------------------------------------------------------------- /iot.cc: -------------------------------------------------------------------------------- 1 | /* GNU General Public License for more details. 2 | * 3 | * You should have received a copy of the GNU General Public License 4 | * along with this program; if not, write to the Free Software 5 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | * 7 | * Author: Soumya Banerjee (Adapted from wireless-animation.cc) 8 | */ 9 | 10 | #include /* srand, rand */ 11 | #include 12 | #include "ns3/core-module.h" 13 | #include "ns3/point-to-point-module.h" 14 | #include "ns3/csma-module.h" 15 | #include "ns3/network-module.h" 16 | #include "ns3/applications-module.h" 17 | #include "ns3/olsr-helper.h" 18 | #include "ns3/wifi-module.h" 19 | #include "ns3/mobility-module.h" 20 | #include "ns3/internet-module.h" 21 | #include "ns3/netanim-module.h" 22 | #include "ns3/basic-energy-source.h" 23 | #include "ns3/flow-monitor-helper.h" 24 | //#include "ns3/simple-device-energy-model.h" 25 | //#include "ns3/v4ping-helper.h" 26 | //#include "ns3/v4ping.h" 27 | 28 | using namespace ns3; 29 | 30 | 31 | static bool verbose = 0; 32 | 33 | char * stringbuilder( char* prefix, char* sufix){ 34 | char* buf = (char*)malloc(50); 35 | snprintf(buf, 50, "%s%s", prefix, sufix); 36 | return buf; 37 | } 38 | 39 | 40 | ApplicationContainer sendMessage(ApplicationContainer apps, double time, Ptrsource,Ptrsink, uint32_t packetSize){ 41 | Ipv4Address remoteAddress = sink->GetObject ()->GetAddress (1, 0).GetLocal (); 42 | /*V4PingHelper ping4(remoteAddress); // remote address 43 | //ping4.SetAttribute ("MaxPackets", UintegerValue (1)); 44 | ping4.SetAttribute ("Interval", TimeValue (Seconds (25.))); 45 | ping4.SetAttribute ("Size", UintegerValue (packetSize)); 46 | ApplicationContainer apps = ping4.Install (source); 47 | apps.Start (Seconds (3.0)); 48 | apps.Stop (Seconds (15.0)); 49 | */ 50 | //std::cout <<"message at "<>"; 61 | apps.Add(client.Install (source)); 62 | return apps; 63 | } 64 | 65 | ApplicationContainer authenticateA(ApplicationContainer appContainer, double time, Ptr user, Ptr gateway ){ 66 | 67 | if (verbose){ 68 | std::cout<<"user : "<< user->GetObject ()->GetAddress (1, 0).GetLocal (); 69 | std::cout<<" gateway : "<< gateway->GetObject ()->GetAddress (1, 0).GetLocal (); 70 | } 71 | uint32_t M1 = 84, M2=64, M3=1, M4 =1; 72 | appContainer = sendMessage(appContainer, time, user, gateway , M1); 73 | appContainer = sendMessage(appContainer, time, gateway, user, M2); 74 | appContainer = sendMessage(appContainer, time, user, gateway , M3); 75 | appContainer = sendMessage(appContainer, time, gateway, user, M4); 76 | 77 | //appContainer = sendMessage(appContainer, time, user, device , M1); 78 | //appContainer = sendMessage(appContainer, time, device, user, M2); 79 | return appContainer; 80 | } 81 | 82 | ApplicationContainer authenticateB(ApplicationContainer appContainer, double time, Ptr user, Ptr gateway , Ptr gateway2 ){ 83 | 84 | if (verbose){ 85 | std::cout<<"user : "<< user->GetObject ()->GetAddress (1, 0).GetLocal (); 86 | std::cout<<" gateway : "<< gateway->GetObject ()->GetAddress (1, 0).GetLocal (); 87 | std::cout<<" gateway2 : "<< gateway2->GetObject ()->GetAddress (1, 0).GetLocal ()< BS, Ptr CH , Ptr SN ){ 102 | 103 | if (verbose){ 104 | std::cout<<"BS : "<< BS->GetObject ()->GetAddress (1, 0).GetLocal (); 105 | std::cout<<" CH : "<< CH->GetObject ()->GetAddress (1, 0).GetLocal (); 106 | std::cout<<" SN : "<< SN->GetObject ()->GetAddress (1, 0).GetLocal ()<= 2 seconds" << std::endl; 180 | exit (1); 181 | } 182 | 183 | std::cout << "MU="<< mobileUserNodes <<", BS="<< baseStations <<", SN="<< sensorNodes << ", CH="<< clusterHeads <