initializer.py 5.13 KB
Newer Older
Nicolas Seydoux's avatar
Nicolas Seydoux committed
1 2 3 4
import glob
from subprocess import Popen
import subprocess
import sys
5
import os
Nicolas Seydoux's avatar
Nicolas Seydoux committed
6
import os.path
7
import time
8
import re
Nicolas Seydoux's avatar
Nicolas Seydoux committed
9
import argparse
10 11

def get_process_id(config_path):
Nicolas Seydoux's avatar
Nicolas Seydoux committed
12 13
    #if(re.match(".*\d{3}\.\d{3}\.\d+\.\d+.*", config_path) != None):
    if len(config_path.split("/")) > 4:
14 15 16
        return config_path.split("/")[5]
    else:
        return config_path.split("/")[3]
Nicolas Seydoux's avatar
Nicolas Seydoux committed
17

Nicolas Seydoux's avatar
Nicolas Seydoux committed
18 19 20 21
parser = argparse.ArgumentParser(description='Starts an edr simulation')
parser.add_argument('-t', '--topology', help="The folder containing the generated topology", type=str)
parser.add_argument('-a', '--applications', help="the folder containing the applications, or no-app", type=str)
parser.add_argument('-o', '--output', help="The folder to which results are sent", type=str)
22
#parser.add_argument('-d', '--data', help="Data to be replayed by simulated sensors", type=str)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
23
parser.add_argument('-r', '--rate', help="Rate of active sensors (one in r)", type=str)
24
parser.add_argument('-d', '--duration', help="Time between the start of applications and the end of the simulation", type=int)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
25 26

args = parser.parse_args()
27 28 29
SIM_TIME = 930
if args.duration != None:
    SIM_TIME = args.duration
30
    print(SIM_TIME)
31

Nicolas Seydoux's avatar
Nicolas Seydoux committed
32 33 34 35 36 37 38 39 40 41 42 43
# if len(sys.argv) != 3:
#     exit("usage : "+sys.argv[0]+" <topology folder> <applications folder | no-app>")

if args.topology == None or args.applications == None or args.output == None:
    exit(args.help)

topo_folder = args.topology
app_folder = args.applications
out_folder = args.output

if not os.path.exists(out_folder):
    os.makedirs(out_folder)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
44 45 46

processes = []

Nicolas Seydoux's avatar
Nicolas Seydoux committed
47
for log in glob.glob(out_folder+"/*"):
48
    os.remove(log)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
49

50
############ STARTING SENSORS
51
#for sensor in glob.glob(topo_folder+'sensor*'):
Nicolas Seydoux's avatar
Nicolas Seydoux committed
52 53
with open(out_folder+"/sensors.log", "w") as logfile:
    cmd = ["java", "-jar", "sensor.jar", topo_folder+'sensor*.json']
Nicolas Seydoux's avatar
Nicolas Seydoux committed
54 55 56 57 58 59 60 61 62
    if args.rate != None:
        # To apply the sensor rate, only one in rate sensors are left with the right pattern
        sensors = glob.glob(topo_folder+'sensor*.json')
        sensors.sort()
        index = 0
        for sensor in sensors:
            if index % int(args.rate) != 0:
                os.rename(sensor, os.path.dirname(sensor)+"/"+os.path.basename(sensor)[1:])
            index += 1
Nicolas Seydoux's avatar
Nicolas Seydoux committed
63
    processes.append(Popen(cmd, stdout=logfile, stderr=logfile))
Nicolas Seydoux's avatar
Nicolas Seydoux committed
64

65 66
print("Sensors started")

67

68
########### STARTING NODES
69
os.chdir("node")
70

71 72 73
buildings = glob.glob("../"+topo_folder+'building*')
buildings.sort()
for building in buildings:
74
    print("Launching "+building)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
75 76
    with open("../"+out_folder+"/"+get_process_id(building)+".log", "w") as logfile:
        processes.append(Popen(["mvn", "exec:java", "-Dconfig="+building, "-Dfilename=../"+out_folder+"/"+get_process_id(building)+".ttl",  "-Dtracename=../"+out_folder+"/"+get_process_id(building)+".trace"], stdout=logfile, stderr=logfile))
77
        time.sleep(1)
78
    print("Done")
79
time.sleep(3)
80 81 82 83

floors = glob.glob("../"+topo_folder+'floor*')
floors.sort()
for floor in floors:
84
    print("Launching "+floor)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
85 86
    with open("../"+out_folder+"/"+get_process_id(floor)+".log", "w") as logfile:
        processes.append(Popen(["mvn", "exec:java", "-Dconfig="+floor, "-Dfilename=../"+out_folder+"/"+get_process_id(floor)+".ttl", "-Dtracename=../"+out_folder+"/"+get_process_id(floor)+".trace"], stdout=logfile, stderr=logfile))
87
        time.sleep(1)
88
    print("Done")
89
time.sleep(3)
90 91 92 93

galleries = glob.glob("../"+topo_folder+'gallery*')
galleries.sort()
for gallery in galleries:
94
    print("Launching "+gallery)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
95 96
    with open("../"+out_folder+"/"+get_process_id(gallery)+".log", "w") as logfile:
        processes.append(Popen(["mvn", "exec:java", "-Dconfig="+gallery, "-Dfilename=../"+out_folder+"/"+get_process_id(gallery)+".ttl", "-Dtracename=../"+out_folder+"/"+get_process_id(gallery)+".trace"], stdout=logfile, stderr=logfile))
97
        time.sleep(1)
98
    print("Done")
99
time.sleep(3)
100

101 102 103 104 105 106 107 108
rooms = glob.glob("../"+topo_folder+'room*')
rooms.sort()
for room in rooms:
    print("Launching "+room)
    with open("../"+out_folder+"/"+get_process_id(room)+".log", "w") as logfile:
        processes.append(Popen(["mvn", "exec:java", "-Dconfig="+room, "-Dfilename=../"+out_folder+"/"+get_process_id(room)+".ttl", "-Dtracename=../"+out_folder+"/"+get_process_id(room)+".trace"], stdout=logfile, stderr=logfile))
        time.sleep(1)
    print("Done")
Nicolas Seydoux's avatar
Nicolas Seydoux committed
109

110
####################### WAITING FOR NODES TO START...
111
time.sleep(2*len(processes))
Nicolas Seydoux's avatar
Nicolas Seydoux committed
112
#time.sleep(45)
Nicolas Seydoux's avatar
Nicolas Seydoux committed
113 114 115 116 117 118
####################### RESTORING SENSOR NAMES...
os.chdir("../")
sensors = glob.glob(topo_folder+'ensor*.json')
for sensor in sensors:
    os.rename(sensor, os.path.dirname(sensor)+"/s"+os.path.basename(sensor))

Nicolas Seydoux's avatar
Nicolas Seydoux committed
119 120
###################### ... AND LAUNCHING THE APPLICATIONS
if app_folder != "no-app":
Nicolas Seydoux's avatar
Nicolas Seydoux committed
121
    os.chdir("application-mockup")
122
    for application in glob.glob("../"+app_folder+'*.json'):
Nicolas Seydoux's avatar
Nicolas Seydoux committed
123 124 125 126 127 128 129
        time.sleep(2)
        print("Launching "+application)
        with open("../"+out_folder+"/"+application.split("/")[3]+".log", "w") as logfile:
            processes.append(Popen(["mvn", "exec:java", "-Dconfig="+application, "-Dfilename=../"+out_folder+"/"+application.split("/")[3]+".ttl"], stdout=logfile, stderr=logfile))
        print("Done")
    # os.chdir("../node")

130
time.sleep(SIM_TIME)
131

Nicolas Seydoux's avatar
Nicolas Seydoux committed
132
for p in processes:
133
    p.terminate()