#!/user/bin/python3

import sys
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.net import Mininet
from mininet.topo import Topo
from mininet.node import RemoteController, OVSSwitch
from mininet.clean import Cleanup
from mininet.link import TCLink

from time import sleep
import json
import requests

class test_topo( Topo ):
    def build(self):
        s1 = self.addSwitch('s1', port=6653)
        s2 = self.addSwitch("s2", port=6653)
        for i in range(0, 10):
            name = 'h' + str(i)
            h_buf = self.addHost(name)
            self.addLink(name, 's1')

        #for i in range(5, 10):
            #name = "h" + str(i)
            #h_buf = self.addHost(name)
            #self.addLink(name, "s2")

        #self.addLink("s2", "s1", cls = TCLink, bw = 500)
        #print("bandwidth: {bandwidth}".format(bandwidth = int(sys.argv[2])))

def run_test_topo():
    "Bootstrap a Mininet network using the Minimal Topology"

    # Cleanup.cleanup()

    # Create an instance of our topology
    topo = test_topo()

    # Create a network based on the topology using OVS and controlled by
    # a remote controller.
    controller_ip = '0.0.0.0'
    controller_port = 6653

    net = Mininet(
        topo=topo, xterms=False,
        controller=lambda name: RemoteController( name, ip=controller_ip, port=controller_port),
        switch=OVSSwitch,
        autoSetMacs=True )

    # Actually start the network
    net.start()

    print("loading the net, 30 sec count down...")
    sleep(5)
    print("net is completed")

    s1 = net.get('s1')
    #s2 = net.get('s2')
    s1.cmd('ovs-vsctl set bridge s1 protocols=OpenFlow10,OpenFlow11,OpenFlow12,OpenFlow13,OpenFlow14,OpenFlow15')
    #s1.cmd('ovs-vsctl set bridge s1 datapath_type=netdev')
    s1.cmd('ovs-vsctl meter-features s1 -O OpenFlow13')
    #s2.cmd('ovs-vsctl set bridge s2 protocols=OpenFlow10,OpenFlow11,OpenFlow12,OpenFlow13,OpenFlow14,OpenFlow15')
    #s2.cmd('ovs-vsctl set bridge s2 datapath_type=netdev')
    #s2.cmd('ovs-vsctl meter-features s2 -O OpenFlow13')
    sleep(5)
    
    print("conducting ping and starting up all servers")
    #cntr = 0
    for i in range(0, 5):
        #if cntr == 20:
           # cntr = 0
        #print("wait...")
        #sleep(30)
        #print("end...")
        time = 15
        name_ser = "h" + str(i)
        name_cli = "h" + str(i + 5)
        host_buf = net.get(name_ser)
        cli_buf = net.get(name_cli)
        sampling_time = 2
        print(i+1)
        host_buf.cmd("ping 10.0.0.{host_name})".format(host_name = i + 5 + 1 ))
    #host_buf.cmd("iperf3 -s -i {sampling_time} -J > ./log_dir/{log_name}.txt &".format(sampling_time = sampling_time, log_name = name_ser))
        host_buf.cmd("iperf3 -s -i {sampling_time} -J > ./log_dir_reno/normal_seed_{log_name}.txt &".format(sampling_time = sampling_time,log_name = name_ser))
        sleep(5) 
        ip = "10.0.0" + str(i+1)
        cli_buf.cmd("iperf3 -c {ip} -t {time} &".format(ip = ip, time = time))
        #cntr = cntr + 1
    print("ping is completed")

    print("let the packets fly...")
    #sleep(15)

    #print("seed: {argv}".format(argv = sys.argv[1]))

   
    
    #cntr = 0
    #flow_block = 20
    #print("first round start")
   # for i in range(0, 5):
       # if cntr == 20:
        #    cntr = 0
        #    time = time + 20
        
        #host_buf = net.get("h" + str(i))
        #print(i)
        #ip = "10.0.0" + str((i + 5) )
        #host_buf.cmd("iperf3 -c {ip} -t {time} &".format(ip = ip, time = time))
        #cntr = cntr + 1
        #sleep(5)


    #print("first round end")

    #start = end
    #end = start + flow_block
    #time = 100
    #for i in range(0, 10):
       # print(start, " ~ ", end)
       # for j in range(start, end):
           # host_buf = net.get("h" + str(j))
           # ip = "10.0." + str((int)((j + 120 + 1) / 256)) + "." + str((j + 120 + 1) % 256)
            #host_buf.cmd("iperf3 -c {ip} -t {time} &".format(ip = ip, time = time))
        
       # start = end
        #end = end + flow_block

        #if start >= 120:
         #   start = start % 120

       # if end > 120:
           # end = end % 120

      #  sleep(20)

    #CLI(net)
    sleep(60)
    net.stop()
    print("Finish")
    CLI.do_quit

if __name__ == '__main__':
    run_test_topo()
    sleep(120)
