Skip to content

Instantly share code, notes, and snippets.

@ryankshah
Created Oct 21, 2019
Embed
What would you like to do?
#!/usr/bin/python
# -*- coding: utf-8 -*-
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections, pmonitor
from mininet.log import setLogLevel
from mininet.node import Controller, OVSSwitch
#from mininet.node import OVSController
from signal import SIGINT
import math
import random
import time
import threading
def thread_client(popens, client, args):
while True:
popens[client] = \
client.popen(args)
def runExperiment(
policy,
port,
device_id,
level,
):
'''Run experiments with n clients and m servers'''
#controller = RemoteController()
topo = NetworkTopology()
net = Mininet(topo, switch=OVSSwitch)
net.start()
# Get hosts
hosts = net.hosts
# Separate hosts into clients and servers
clients = []
servers = {}
i = 0
for host in hosts:
# If its a server add to server list
# else add to client list
if 'server' in host.name:
servers[host] = str(int(port)+i)
i += 1
else:
clients.append(host)
#info("*** Testing connections\n")
#net.pingAll()
popens = {}
for server in servers:
popens[server] = \
server.popen('java -jar server.jar ' + port + ' ' + policy)
time.sleep(15) # Wait 10 seconds for servers to start
for client in clients:
# Get random server from servers list
server = random.choice(servers.items())
args = 'java -jar client.jar ' + str(server[0].IP()) + ' ' + server[1] + ' report ' + device_id + ' ' + level
c = threading.Thread(target=thread_client, args=(popens, client, args))#, daemon=True)
c.start()
endTime = time.time() + 10 # Run for 60 seconds
for (h, line) in pmonitor(popens):#, timeoutms=500):
if time.time() >= endTime:
for p in popens.values():
p.send_signal(SIGINT)
if h:
with open('times.txt', 'a') as myfile:
myfile.write(line)
net.stop()
# network with n hosts connected to one switch
class NetworkTopology(Topo):
clients = 10
servers = 2
def __init__(self):
# Initialize topology
Topo.__init__(self)
switch = self.addSwitch('s1')
for s in range(self.servers):
server = self.addHost('server%s' % (s + 1))
self.addLink(server, switch)
for c in range(self.clients):
client = self.addHost('client%s' % (c + 1))
self.addLink(client, switch)
topos = {'mytopo': lambda : NetworkTopology()}
if __name__ == '__main__':
setLogLevel('info')
policy = 'RBIBA'
port = '6540'
device_id = 'ec5ae996-2b1f-4f85-a168-b3f8e2abf897'
level = '2'
runExperiment(policy, port, device_id, level)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment