Monday, 21 October 2019

For loop thread mininet popen

In my Python program, using mininet, I have a for loop where each client runs a Java program where the output is a time (integer) which is captured in a popen written to a text file by the Python script.

However, each client only runs once, and needs to reconnect to the server program to get another time. The way this is intended to work is have each client continuously connect and receive a time from the server until the program is finished.

Adding a while True loop within the client for loop would not work as the rest of the script would never finish until closed manually.

So how can I make it such that each client can continuously connect to the server using the following:

args = 'java -jar client.jar ' + str(server[0].IP()) + ' ' + server[1] + ' report ' + device_id + ' ' + level
popens[client] = \
    client.popen(args)

Here is the code I am using:

#!/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 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
        popens[client] = \
            client.popen(args)

    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 = 2

    def __init__(self):
        # Initialize topology
        Topo.__init__(self)

        controller = self.addController('c0')
        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)

note: I have tried the following, but receive no times:

def thread_client(popens, client, args):
    popens[client] = \
        client.popen(args)

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,))


from For loop thread mininet popen

No comments:

Post a Comment