-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
151 lines (123 loc) · 5.1 KB
/
helpers.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
150
151
#!/usr/bin/env python
# coding: utf-8
from ns.core import *
from ns.internet import *
from ns.mobility import *
from ns.network import *
from ns.wifi import *
from ns.csma import *
from ns.applications import *
from common import *
def connectToCloudServer(host, cmd):
"""
Calculates if host needs to connect to cloud server instead of local office's server
Every N-th host should connect to the cluod
"""
return host % cmd.connectNthNodeToCloud == 0
def installUdpEchoWifiClients(cloudClient, localServerClient, localHosts, skipList, cmd):
udpEchoClientInstaller("Wifi", cloudClient, localServerClient, localHosts, skipList, cmd)
def installUdpEchoLanClients(cloudClient, localServerClient, localLANs, skipList, cmd):
udpEchoClientInstaller("LAN", cloudClient, localServerClient, localLANs, skipList, cmd)
def udpEchoClientInstaller(hostType, cloudClient, localServerClient, localNodes, skipList, cmd):
for host in range(localNodes.GetN()):
node = localNodes.Get(host)
if host in skipList:
logDebug("Installing UdpEchoClient to "+hostType+" host: skipping node #", node.GetId())
continue
if connectToCloudServer(host, cmd):
logDebug("Installing cloud server UdpEchoClient to "+hostType+" host: node #", node.GetId())
installUdpEchoClient(cloudClient, node, cmd.clientStart, cmd.stopTime)
else:
logDebug("Installing local server UdpEchoClient to "+hostType+" host: node #", node.GetId())
installUdpEchoClient(localServerClient, node, cmd.clientStart, cmd.stopTime)
def setUpWifi(wifiPhy, wifiMac, nodes):
wifiHelper = WifiHelper()
wifiHelper.SetRemoteStationManager(
"ns3::ConstantRateWifiManager",
"DataMode", StringValue("OfdmRate54Mbps")
)
return wifiHelper.Install(wifiPhy, wifiMac, nodes)
def setUpWifiPhy():
wifiChannel = YansWifiChannelHelper.Default()
wifiPhy = YansWifiPhyHelper.Default()
wifiPhy.SetChannel(wifiChannel.Create())
return wifiPhy
def setUpWifiMac(wifiType, ssid):
wifiMac = NqosWifiMacHelper.Default()
wifiMac.SetType(str(wifiType), "Ssid", SsidValue(ssid))
return wifiMac
def createNodes(count):
nodeContainer = NodeContainer()
nodeContainer.Create(int(count))
return nodeContainer
def createNodesWithStack(count):
nodes = createNodes(count)
installInternetStack(nodes)
return nodes
def setUpIPAddresses(baseAddress, mask, devices):
ipAddrs = Ipv4AddressHelper()
ipAddrs.SetBase(Ipv4Address(baseAddress), Ipv4Mask(mask))
return ipAddrs.Assign(devices)
def installInternetStack(nodes):
internet = InternetStackHelper()
internet.Install(nodes)
def setUpUdpEchoClient(address, port, interval, packetSize):
client = UdpEchoClientHelper(address, port)
client.SetAttribute("Interval", TimeValue(Seconds(interval)))
client.SetAttribute("PacketSize", UintegerValue(packetSize))
return client
def installUdpEchoClient(echoServer, host, start, end):
clientApps = echoServer.Install(host)
clientApps.Start(Seconds(start))
clientApps.Stop(Seconds(end))
def installUdpEchoServer(node, port, start, end):
server = UdpEchoServerHelper(port)
serverApps = server.Install(node)
serverApps.Start(Seconds(start))
serverApps.Stop(Seconds(end))
def installCSMA(dataRate, delay, node):
csma = CsmaHelper()
csma.SetChannelAttribute("DataRate", StringValue(dataRate))
csma.SetChannelAttribute("Delay", StringValue(delay))
return csma.Install(node)
def addToContainer(collector, *nodes):
allNodes = NodeContainer()
allNodes.Add(collector)
for node in nodes:
allNodes.Add(node)
return allNodes
def setUpWifiHostsMobility(nodes, wifiDistance):
log("Installing static mobility; distance " + str(wifiDistance))
mobility = MobilityHelper()
mobility.SetPositionAllocator(
"ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (wifiDistance),
"DeltaY", DoubleValue (wifiDistance*2),
"LayoutType", StringValue ("RowFirst")
)
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel")
mobility.Install(nodes)
return mobility
def doWifiTracing(infix, wifiPhy, wifiNodes, cmd):
wifiLogPrefix = cmd.outputFolder+infix+"-"+cmd.wifiFileName
wifiPhy.EnablePcap(wifiLogPrefix, wifiNodes)
def doCSMATracing(infix, csmaNodes, cmd):
csmaLogPrefix = cmd.outputFolder+infix+"-"+cmd.csmaFileName
CsmaHelper().EnablePcap(csmaLogPrefix, csmaNodes)
def logNodes(msg, cloudNodes):
logDebug(msg, cloudNodes.GetN())
for node in range(cloudNodes.GetN()):
logDebug(" - Node id: ", cloudNodes.Get(node).GetId())
def logAssignIPs(nodes, nodeInterfaces):
logDebug("Assigning IP addresses to nodes")
for interface in range(nodeInterfaces.GetN()):
logDebug(" - #", nodes.Get(interface).GetId(), "\t",
nodeInterfaces.GetAddress(interface))
def setUpCloudUdpEchoClient(cmd):
cloudServerAddress = Ipv4Address(cmd.cloudServerIp)
return setUpUdpEchoClient(cloudServerAddress, cmd.discardPort, cmd.packetInterval, cmd.packetSize)
def setUpOfficeServerClient(serverIp, cmd):
serverAddress = Ipv4Address(serverIp)
return setUpUdpEchoClient(serverAddress, cmd.discardPort, cmd.packetInterval, cmd.packetSize)