Last active
October 21, 2019 15:03
-
-
Save ryankshah/c610bf247d565a3d23e05feec6d55277 to your computer and use it in GitHub Desktop.
This file contains 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 characters
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 | |
time_reached = False | |
def thread_client(popens, client, args): | |
while not time_reached: | |
popens[client] = client.popen(args) | |
time.sleep(9) | |
popens[client].send_signal(SIGINT) | |
def runExperiment( | |
policy, | |
port, | |
device_id, | |
level, | |
): | |
'''Run experiments with n clients and m servers''' | |
global time_reached | |
#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) | |
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 | |
client_threads = {} | |
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 | |
client_threads[client] = threading.Thread(target=thread_client, args=(popens, client, args))#, daemon=True) | |
for threads in client_threads.values(): | |
threads.start(); | |
time.sleep(10) | |
time_reached = True | |
for threads in client_threads.values(): | |
threads.join() | |
#endTime = time.time() + 10 # Run for 60 seconds | |
for (h, line) in pmonitor(popens):#, timeoutms=500): | |
if h: | |
with open('times.txt', 'a') as myfile: | |
myfile.write(line) | |
# | |
# if time.time() >= endTime: | |
# for p in popens.values(): | |
# p.send_signal(SIGINT) | |
net.stop() | |
# network with n hosts connected to one switch | |
class NetworkTopology(Topo): | |
clients = 10 | |
servers = 1 | |
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 = '4320' | |
device_id = '0000acd2-65af-4931-8d62-cf9705e451ff' | |
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