Created
April 7, 2014 22:40
-
-
Save clarle/10069899 to your computer and use it in GitHub Desktop.
Client/Server management script for COMP 512
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
import os | |
from time import sleep | |
from optparse import OptionParser | |
# Remember to add the trailing slash! | |
SERVER_FOLDER = "/home/2010/cleung24/comp512/a3/servercode/" | |
CLIENT_FOLDER = "/home/2010/cleung24/comp512/a3/clientsrc/" | |
MIDDLEWARE_PORT = 5678 | |
def compile_target(target): | |
if target == 'interface': | |
classpath = SERVER_FOLDER | |
os.putenv("CLASSPATH", classpath) | |
os.system("javac " + SERVER_FOLDER + "ResInterface/ResourceManager.java") | |
os.chdir(SERVER_FOLDER) | |
os.system("jar cvf ResInterface.jar ResInterface/*.class") | |
elif target == 'client': | |
classpath = CLIENT_FOLDER + ":" + SERVER_FOLDER + "ResInterface.jar" | |
os.putenv("CLASSPATH", classpath) | |
os.system("cp -f " + SERVER_FOLDER + "ResInterface.jar " + CLIENT_FOLDER) | |
os.system("javac " + CLIENT_FOLDER + "client.java") | |
elif target == 'middle': | |
classpath = SERVER_FOLDER | |
os.putenv("CLASSPATH", classpath) | |
os.system("javac " + SERVER_FOLDER + "ResImpl/MiddlewareImpl.java") | |
elif target == 'server': | |
classpath = SERVER_FOLDER | |
os.putenv("CLASSPATH", classpath) | |
for rm_type in ['Car', 'Flight', 'Hotel']: | |
os.system("javac " + SERVER_FOLDER + "ResImpl/" + rm_type + "ManagerImpl.java") | |
def remove_classes(): | |
os.system("rm " + CLIENT_FOLDER + "*.class") | |
os.system("rm " + SERVER_FOLDER + "ResImpl/*.class") | |
os.system("rm " + SERVER_FOLDER + "ResInterface/*.class") | |
def start_client(middleware): | |
classpath = CLIENT_FOLDER + ":" + SERVER_FOLDER + "ResInterface.jar" | |
os.putenv("CLASSPATH", classpath) | |
cmd = "java -Djava.security.policy=file:" + CLIENT_FOLDER + "java.policy client " + middleware | |
os.system(cmd) | |
def start_middle(servers): | |
classpath = SERVER_FOLDER | |
os.putenv("CLASSPATH", classpath) | |
os.system("kill $(ps aux | grep 'rmiregistry " + str(MIDDLEWARE_PORT) + "' | awk '{print $2}')") | |
os.system("kill $(ps aux | grep 'MiddlewareImpl' | awk {'print $2}')") | |
# Fix for delay between the actual kill command and killing the process | |
sleep(1) | |
cmd = "java -Djava.rmi.server.codebase=file:" + SERVER_FOLDER + " -Djava.security.policy=file:" + SERVER_FOLDER + "java.policy ResImpl.MiddlewareImpl " | |
for server in servers: | |
cmd += server | |
cmd += " " | |
os.system("rmiregistry " + str(MIDDLEWARE_PORT) + " &") | |
cmd += "&" | |
os.system(cmd) | |
def start_rms(ports): | |
classpath = SERVER_FOLDER | |
os.putenv("CLASSPATH", classpath) | |
rm_types = ['Car', 'Flight', 'Hotel'] | |
for i in range(3): | |
os.system("kill $(ps aux | grep 'rmiregistry " + str(ports[i]) + "' | awk '{print $2}')") | |
os.system("kill $(ps aux | grep '" + rm_types[i] + "ManagerImpl' | awk '{print $2}')") | |
# Fix for delay between kill command and actual killing of process | |
sleep(1) | |
os.system("rmiregistry " + str(ports[i]) + " &") | |
os.system("java -Djava.rmi.server.codebase=file:" + SERVER_FOLDER + " -Djava.security.policy=file:" + SERVER_FOLDER + "java.policy ResImpl." + rm_types[i] + "ManagerImpl " + str(ports[i]) + " &") | |
# os.system("xterm -e \"bash -c \\\"java ResImpl." + rm_types[i] + "ManagerImpl " + str(ports[i]) + "; exec bash\\\"\" &") | |
def start_tcp_middle(servers): | |
classpath = SERVER_FOLDER | |
os.putenv("CLASSPATH", classpath) | |
os.system("kill $(ps aux | grep 'MiddlewareImpl' | awk {'print $2}')") | |
# Fix for delay between the actual kill command and killing the process | |
sleep(1) | |
cmd = "java ResImpl.MiddlewareImpl " | |
for server in servers: | |
cmd += server | |
cmd += " " | |
os.system(cmd) | |
def start_tcp(ports): | |
classpath = SERVER_FOLDER | |
os.putenv("CLASSPATH", classpath) | |
rm_types = ['Car', 'Flight', 'Hotel'] | |
for i in range(3): | |
os.system("kill $(ps aux | grep '" + rm_types[i] + "ManagerImpl' | awk '{print $2}')") | |
# Fix for delay between kill command and actual killing of process | |
sleep(1) | |
os.system("xterm -e \"bash -c \\\"java ResImpl." + rm_types[i] + "ManagerImpl " + str(ports[i]) + "; exec bash\\\"\" &") | |
parser = OptionParser() | |
parser.add_option("-b", "--build", action="store", dest="target", help="Compile the target program. (choices: interface, client, middleware, server)") | |
parser.add_option("-c", "--client", action="store", dest="middleware", help="Start the client and connect it to the middleware.") | |
parser.add_option("-m", "--middle", action="store", type="string", nargs=3, dest="servers", help="Start the middleware server and connect it to the RMs.") | |
parser.add_option("-s", "--server", action="store", type="int", nargs=3, dest="ports", help="Start the Resource Managers on different ports.") | |
parser.add_option("-t", "--tcp-middle", action="store", type="string", nargs=3, dest="tcp_servers", help="Start the TCP middleware server") | |
parser.add_option("-q", "--tcp-server", action="store", type="int", nargs=3, dest="tcp_ports", help="Start the TCP resource managers.") | |
parser.add_option("-r", "--remove", action="store_true", dest="remove", help="Remove any old class files.") | |
(options, args) = parser.parse_args() | |
if options.target: | |
compile_target(options.target) | |
elif options.remove: | |
remove_classes() | |
elif options.middleware: | |
start_client(options.middleware) | |
elif options.servers: | |
start_middle(options.servers) | |
elif options.ports: | |
start_rms(options.ports) | |
elif options.tcp_servers: | |
start_tcp_middle(options.tcp_servers) | |
elif options.tcp_ports: | |
start_tcp(options.tcp_ports) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment