Created
April 24, 2017 12:20
-
-
Save Horkyze/8ce163bf2bc2905b7b198e8c233fe3cf to your computer and use it in GitHub Desktop.
Revisions
-
Horkyze created this gist
Apr 24, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,192 @@ #!/usr/bin/python """ Setting the position of Nodes (only for Stations and Access Points) and providing mobility. """ from mininet.net import Mininet from mininet.node import Controller, RemoteController, OVSKernelAP, OVSSwitch from mininet.link import TCLink from mininet.cli import CLI from mininet.log import setLogLevel import threading import subprocess import time import socket import re def topology(): "Create a network." net = Mininet( controller=Controller, link=TCLink, accessPoint=OVSKernelAP, switch=OVSSwitch, useWmediumd=True, enable_interference=True) print "*** Creating nodes" h30 = net.addHost( 'h30', mac='00:00:00:00:00:30', ip='10.0.0.30/8' ) h31 = net.addHost( 'h31', mac='00:00:00:00:00:31', ip='10.0.0.31/8' ) h32 = net.addHost( 'h32', mac='00:00:00:00:00:32', ip='10.0.0.32/8' ) sta1 = net.addStation( 'sta1', mac='00:00:00:00:00:02', ip='10.0.0.1/8' """,position='50,50,0'""") sta2 = net.addStation( 'sta2', mac='00:00:00:00:00:03', ip='10.0.0.2/8' """, position='90,10,0'""") sta3 = net.addStation( 'sta3', mac='00:00:00:00:00:04', ip='10.0.0.3/8' """, position='20,20,0'""") ap20 = net.addAccessPoint( 'ap20', ssid= 'new-ssid', mode= 'g', channel= '5', position='25,50,0', range='35' ) ap21 = net.addAccessPoint( 'ap21', ssid= 'new-ssid', mode= 'g', channel= '5', position='75,70,0', range='35' ) ap22 = net.addAccessPoint( 'ap22', ssid= 'new-ssid', mode= 'g', channel= '5', position='99,0,0', range='35' ) s1 = net.addSwitch('s1') s2 = net.addSwitch('s2') s3 = net.addSwitch('s3') s4 = net.addSwitch('s4') s5 = net.addSwitch('s5') c1 = net.addController( 'c1', controller=RemoteController ) print "*** Configuring wifi nodes" net.configureWifiNodes() print "*** Associating and Creating links" net.addLink(s1, s2) net.addLink(s2, s3) net.addLink(s3, s5) net.addLink(s4, s5) net.addLink(ap20, s1) net.addLink(ap21, s4) net.addLink(ap22, s3) net.addLink(s1, h30) net.addLink(s3, h31) net.addLink(s4, h32) net.addLink(ap20, sta1) net.addLink(ap21, sta2) net.addLink(ap22, sta3) print "*** Starting network" net.build() c1.start() ap20.start( [c1] ) ap21.start( [c1] ) ap22.start( [c1] ) s1.start( [c1] ) s2.start( [c1] ) s3.start( [c1] ) s4.start( [c1] ) s5.start( [c1] ) """uncomment to plot graph""" net.plotGraph(max_x=100, max_y=100) net.seed(1234) # "*** Available models: RandomWalk, TruncatedLevyWalk, RandomDirection, RandomWayPoint, GaussMarkov, ReferencePoint, TimeVariantCommunity ***" net.startMobility(startTime=0, model='GaussMarkov', max_x=100, max_y=100, min_v=3, max_v=4) #net.startMobility(startTime=1, model='RandomWalk', max_x=100, max_y=100, min_v=0.7, max_v=1) # net.startMobility(startTime=0) # net.mobility(sta1, 'start', time=1, position='0.0,50.0,0.0') # net.mobility(sta1, 'stop', time=30, position='100.0,50.0,0.0') # net.stopMobility(stopTime=31) """Starting background tasks""" initStats(net) t = threading.Thread(target=worker, args=(net, )) t.start() print "*** Running CLI" CLI( net ) print "*** Stopping network" net.stop() def getWlanInt(ap): for i in ap.intfs: if 'wlan' in ap.intfs[i].name: return ap.intfs[i].name return 0 def initStats(net): for ap in net.accessPoints: ap.RxStats = Stats("RX", ap) ap.TxStats = Stats("TX", ap) def worker(net): while True: for ap in net.accessPoints: intf = getWlanInt(ap) ap.RxStats.update(intf) ap.TxStats.update(intf) ap.RxStats.send() time.sleep(1) class Stats: """ curr - estimated live traffic, per second prev - previous bytes transfared in total direction - RX or TX """ def __init__(self, direction, ap): self.ap = ap self.direction = direction self.curr = 0 self.prev = 0 self.avg = 0 self.max = 0 self.tick = 0 try: self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.s.connect(('127.0.0.1', 12345)) except Exception as e: print e def echo(self): f = open("stats.txt", "a") f.write("%s-%s:\t cur: %d \t\t avg: %d \t\t max:%d\n" % (self.ap.name, self.direction, self.curr, self.avg, self.max)) f.close() def update(self, intf): cmd = subprocess.Popen( 'ifconfig ' + intf + " |grep -oP '(?<="+self.direction+" bytes:)[0-9]*' ", shell=True, stdout=subprocess.PIPE) for line in cmd.stdout: current = int(line) if self.prev == 0: self.prev = current self.tick = self.tick + 1 self.curr = current - self.prev self.prev = current if (self.curr > self.max): self.max = self.curr self.avg = (self.avg + self.curr) / self.tick self.echo() def packStats(self): self.ap.params['rx'] = self.ap.RxStats.curr self.ap.params['tx'] = self.ap.TxStats.curr s = str(self.ap.params).replace("'", '"').replace('>', '"').replace('<', '"') s = re.sub(r'Station ', '', s) s = re.sub(r': sta[0-9]-wlan.:\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b\spid=[0-9]*', '', s) return s # send data to the constroller application def send(self): try: self.s.send(self.packStats()) except Exception as e: print e # data = s.recv(BUFFER_SIZE) if __name__ == '__main__': setLogLevel( 'info' ) topology()