blockchain-access-control

git clone git://git.codymlewis.com/blockchain-access-control.git
Log | Files | Refs | README

create_network.py (1335B)


      1 #!/usr/bin/python
      2 # -*- coding: utf-8 -*-
      3 
      4 '''
      5 Create a mininet based network for the IoT
      6 '''
      7 
      8 from mininet.topo import Topo
      9 from mininet.net import Mininet
     10 from mininet.node import OVSSwitch, RemoteController
     11 from mininet.log import setLogLevel, info
     12 from mininet.cli import CLI
     13 from mininet.link import TCLink
     14 
     15 class IoTTopo(Topo):
     16     "A star shaped network"
     17     def build(self, **opts):
     18         switch = self.addSwitch(f"s1")
     19         for i in range(opts["n"]):
     20             self.addLink(
     21                 self.addHost(f"h{i}"),
     22                 switch,
     23                 **{
     24                     "bw": 0.1,
     25                     "delay": "5ms",
     26                     "loss": 0,
     27                     "max_queue_size": 1000,
     28                     "use_htb": True
     29                 }
     30             )
     31 
     32 def run_network(n=5):
     33     '''
     34     Start up and run the network
     35     :param n: Number of hosts
     36     '''
     37     topo = IoTTopo(n=n)
     38     net = Mininet(
     39         topo=topo,
     40         link=TCLink,
     41         switch=OVSSwitch,
     42         controller=RemoteController
     43     )
     44     net.start()
     45     info("*** Starting a server on all hosts")
     46     for host in net.hosts:
     47         host.cmd(
     48             "export FLASK_APP=Server.py && flask run --host=0.0.0.0 --port=80 &"
     49         )
     50     CLI(net)
     51     net.stop()
     52 
     53 
     54 if __name__ == '__main__':
     55     setLogLevel('info')
     56     run_network()