diff --git a/LaunchScript/VirtualAcousticStarterConfig.json b/LaunchScript/VirtualAcousticStarterConfig.json index 321ac46e5d2b402c82eab2fa62ca8200567967e4..870539ce565e1c35f2ce7d12d8e625374803cdb3 100644 --- a/LaunchScript/VirtualAcousticStarterConfig.json +++ b/LaunchScript/VirtualAcousticStarterConfig.json @@ -1,7 +1,9 @@ { "sLocalIP": "0.0.0.0", - "nLocalPort": 41578, + "nLauncherPort": 41578, + "nVAServerPort": 12340, "nDefaultSleep": 3, + "tReproductionModules": ("TalkthroughHP", "CTC"), "tVirtualAcousticVersions": { "2018.a": { "file": "bin/VAServer.exe", "dir": "../v2018.a", "params": "localhost:12340 conf/VACore.ini" }, diff --git a/LaunchScript/VirtualAcousticsStarterServer.py b/LaunchScript/VirtualAcousticsStarterServer.py index 3e49abd556f49b86b7935b03fc4e483929361478..4f60d0cfa70778c52d06e9ed275ae9209d913f0b 100644 --- a/LaunchScript/VirtualAcousticsStarterServer.py +++ b/LaunchScript/VirtualAcousticsStarterServer.py @@ -9,9 +9,27 @@ class ErrorCodes(Enum): ERROR_BINDING_SOCKET = 3 ERROR_CONNECTING_SOCKET = 4 +# Class representing the VA-Launcher config (.json) file +class LauncherConfig: + def __init__(conf, sConfigFile): + with open( sConfigFile ) as json_file: + json_config = json.load(json_file) + + try: + conf.dVirtualAcousticVersions = json_config["tVirtualAcousticVersions"] + conf.sLocalIP = json_config["sLocalIP"] + conf.nLauncherPort = json_config["nLauncherPort"] + conf.nVAServerPort = json_config["nVAServerPort"] + conf.nDefaultSleep = json_config["nDefaultSleep"] + conf.tReproductionModules = json_config["tReproductionModules"] + except KeyError as e: + print( "ERROR reading the json config. Missing " + str(e.args[0]) ) + sys.exit( ErrorCodes.ERROR_INCOMPLETE_CONFIG ) + class VirtualAcousticsLauncher: def __init__(self): print("init") + self.oConfig = None self.oVAProcess = None self.oLauncherServerSocket = None self.oLauncherConnection = None @@ -20,7 +38,6 @@ class VirtualAcousticsLauncher: self.start() - def start(self): print( "VirtualAcoustics Starter script - press ctrl+c to quit" ) self.readConfig() @@ -28,14 +45,12 @@ class VirtualAcousticsLauncher: self.runLauncher() - def readConfig(self): sHostConfigurationFile = "VirtualAcousticStarterConfig." + socket.gethostname() + ".json" sGeneralConfigurationFile = "VirtualAcousticStarterConfig.json" - sUsedConfigFile = "" - #check which config file exists and load it + sUsedConfigFile = "" if os.path.isfile( sHostConfigurationFile ): sUsedConfigFile = sHostConfigurationFile elif os.path.isfile( self.sCurrentScriptsDirectory + "/" + sHostConfigurationFile ): @@ -49,27 +64,17 @@ class VirtualAcousticsLauncher: sys.exit( ErrorCodes.ERROR_NO_CONFIG_FOUND ) print("Using config: " + sUsedConfigFile) - with open( sUsedConfigFile) as json_file: - json_config = json.load(json_file) - - try: - self.tVirtualAcousticVersions = json_config["tVirtualAcousticVersions"] - self.sLocalIP = json_config["sLocalIP"] - self.nLocalPort = json_config["nLocalPort"] - self.nDefaultSleep = json_config["nDefaultSleep"] - except KeyError as e: - print( "ERROR reading the json config. Missing " + str(e.args[0]) ) - sys.exit( ErrorCodes.ERROR_INCOMPLETE_CONFIG ) + self.oConfig = LauncherConfig( sUsedConfigFile ) def openLauncherServerSocket(self): - print( "Creating server socket at " + self.sLocalIP + ":" + str( self.nLocalPort ) ) + print( "Creating server socket at " + self.oConfig.sLocalIP + ":" + str( self.oConfig.nLauncherPort ) ) self.oLauncherServerSocket = socket.socket() - if self.sLocalIP == "": - self.sLocalIP = socket.gethostname() + if self.oConfig.sLocalIP == "": + self.oConfig.sLocalIP = socket.gethostname() try: - self.oLauncherServerSocket.bind( ( self.sLocalIP, self.nLocalPort ) ) + self.oLauncherServerSocket.bind( ( self.oConfig.sLocalIP, self.oConfig.nLauncherPort ) ) self.oLauncherServerSocket.listen( 3 ) except socket.error: print( "Error on binding socket" ) @@ -77,7 +82,6 @@ class VirtualAcousticsLauncher: self.oLauncherServerSocket.settimeout( 1.0 ) - def runLauncher(self): try: while True: @@ -103,11 +107,11 @@ class VirtualAcousticsLauncher: self.oVAProcess.kill() self.oVAProcess = None - tInstanceToStart = self.ReadVariantToStart() - if not tInstanceToStart: + dVAServerToStart = self.ReadVAServerIDToStart() + if not dVAServerToStart: continue - self.StartRequestedVAServer(tInstanceToStart) + self.StartRequestedVAServer(dVAServerToStart) except KeyboardInterrupt: print( "Caught keyboard interrupt, quitting" ) @@ -115,29 +119,29 @@ class VirtualAcousticsLauncher: - def ReadVariantToStart(self): + def ReadVAServerIDToStart(self): try: - self.sVariantName = self.oLauncherConnection.recv( 512 ) - if type( self.sVariantName ) is bytes: - self.sVariantName = self.sVariantName.decode( 'utf-8' ) - print( "Received launch request for variant: " + self.sVariantName ) + self.sVAServerID = self.oLauncherConnection.recv( 512 ) + if type( self.sVAServerID ) is bytes: + self.sVAServerID = self.sVAServerID.decode( 'utf-8' ) + print( "Received launch request for variant: " + self.sVAServerID ) except socket.error: print( "Error while reading variant" ) self.oLauncherConnection.close() return None else: try: - tInstance = self.tVirtualAcousticVersions[self.sVariantName] + dVAServerInstance = self.oConfig.dVirtualAcousticVersions[self.sVAServerID] except KeyError: - tInstance = None + dVAServerInstance = None - if not tInstance: - print( 'Requested VA Instance "' + self.sVariantName + '" not available' ) + if not dVAServerInstance: + print( 'Requested VA Instance "' + self.sVAServerID + '" not available' ) self.oLauncherConnection.send( b'f' ) #answer 'requested version not available self.oLauncherConnection.close() return None - return tInstance + return dVAServerInstance def StartRequestedVAServer(self, tInstance): try: @@ -151,14 +155,14 @@ class VirtualAcousticsLauncher: if sWorkingDir and os.path.isfile( sWorkingDir + "/" + self.sVAExecutableFile ): self.sVAExecutableFile = sWorkingDir + "/" + self.sVAExecutableFile else: - print( "ERROR: Invalid config for " + self.sVariantName + " -- file " + self.sVAExecutableFile + " does not exist" ) + print( "ERROR: Invalid config for " + self.sVAServerID + " -- file " + self.sVAExecutableFile + " does not exist" ) self.oLauncherConnection.send( b'n' ) #answer 'binary file cannot be found or invalid' return elif sWorkingDir == None: sWorkingDir = os.path.dirname( self.sVAExecutableFile ) except KeyError: self.sVAExecutableFile = None - print( "ERROR: config for " + self.sVariantName + " has no valid \"file\" entry" ) + print( "ERROR: config for " + self.sVAServerID + " has no valid \"file\" entry" ) self.oLauncherConnection.send( b'i' ) #answer 'invalid file entry in the config' self.oLauncherConnection.close() return @@ -176,8 +180,8 @@ class VirtualAcousticsLauncher: try: nSleep = tInstance["sleep"] except KeyError: - nSleep = self.nDefaultSleep - + nSleep = self.oConfig.nDefaultSleep + # start instance print( 'executing "' + sCommand + '"' )