parameters: ()
docstring: Start the session server.
docstring_summary: Start the session server.
docstring_tokens: Start the session server .
function: def main():
      """Start the session server."""
      # the following config variables read from the config.json file
      # are described here:
      #
      # port:               local port on which the server is listening.
      # portRewrite:        true if local ports are computed from 443 https/wss URLs (apache rewrite rule).
      # server:             host where this session script is running.
      # sslKey:             private key for a SSL enabled server.
      # sslCertificate:     certificate for a SSL enabled server.
      # simulationServers:  lists all the available simulation servers.
      # administrator:      email address of administrator that will receive the notifications.
      # mailServer:         SMTP mail server host from which the notifications are sent.
      # mailServerPort:     SMTP mail server port.
      # mailSender:         email address used to send the notifications.
      # mailSenderUser:     user name to authenticate on the SMTP server.
      # mailSenderPassword: password to authenticate on the SMTP server with the mailSenderUser.
      # logDir:             directory where the log file is written.
      # debug:              debug mode (output to stdout).
      #
      global config
      global simulation_server_loads
  
      # logging system
      root_logger = logging.getLogger()
      root_logger.setLevel(logging.DEBUG)
      if 'logDir' not in config:
          config['logDir'] = 'log'
      else:
          config['logDir'] = expand_path(config['logDir'])
      if 'portRewrite' not in config:
          config['portRewrite'] = False
      if 'debug' not in config:
          config['debug'] = False
      sessionLogDir = os.path.join(config['logDir'], 'session')
      logFile = os.path.join(sessionLogDir, 'output.log')
      try:
          if not os.path.exists(sessionLogDir):
              os.makedirs(sessionLogDir)
          if config['debug']:
              file_handler = logging.StreamHandler(sys.stdout)
          else:
              file_handler = logging.handlers.RotatingFileHandler(logFile, maxBytes=500000, backupCount=10)
          formatter = logging.Formatter('%(asctime)-15s [%(levelname)-7s]  %(message)s')
          file_handler.setFormatter(formatter)
          file_handler.setLevel(logging.INFO)
          root_logger.addHandler(file_handler)
      except (OSError, IOError) as e:
          sys.exit("Log file '" + logFile + "' cannot be created: " + str(e))
      # disable tornado.access INFO logs
      tornado_access_log = logging.getLogger('tornado.access')
      tornado_access_log.setLevel(logging.WARNING)
  
      simulation_server_loads = [0] * len(config['simulationServers'])
      if 'administrator' in config:
          if 'mailServer' not in config:
              logging.info("No mail server defined in configuration, disabling e-mail notifications to " +
                           config['administrator'] + ".")
              del config['administrator']
          elif 'mailSender' not in config:
              logging.info("No mail sender defined in configuration, disabling e-mail notifications to " +
                           config['administrator'] + ".")
              del config['administrator']
      handlers = []
      handlers.append((r'/session', SessionHandler))
      handlers.append((r'/', ClientWebSocketHandler))
      handlers.append((r'/monitor', MonitorHandler))
      application = tornado.web.Application(handlers)
      if 'server' not in config:
          config['server'] = 'localhost'
      if 'sslCertificate' in config and 'sslKey' in config:
          config['ssl'] = True
          ssl_certificate = os.path.abspath(expand_path(config['sslCertificate']))
          ssl_key = os.path.abspath(expand_path(config['sslKey']))
          ssl_options = {"certfile": ssl_certificate, "keyfile": ssl_key}
          http_server = tornado.httpserver.HTTPServer(application, ssl_options=ssl_options)
      else:
          config['ssl'] = False
          http_server = tornado.httpserver.HTTPServer(application)
      update_load()
      http_server.listen(config['port'])
      message = "Session server running on port %d (" % config['port']
      if not config['ssl']:
          message += 'no '
      message += 'SSL)'
      logging.info(message)
  
      try:
          tornado.ioloop.IOLoop.current().start()
      except Exception:
          logging.error(traceback.format_exc() + "\n")
          for client in ClientWebSocketHandler.clients:
              del client
function_tokens: def main ( ) : # the following config variables read from the config.json file # are described here: # # port:               local port on which the server is listening. # portRewrite:        true if local ports are computed from 443 https/wss URLs (apache rewrite rule). # server:             host where this session script is running. # sslKey:             private key for a SSL enabled server. # sslCertificate:     certificate for a SSL enabled server. # simulationServers:  lists all the available simulation servers. # administrator:      email address of administrator that will receive the notifications. # mailServer:         SMTP mail server host from which the notifications are sent. # mailServerPort:     SMTP mail server port. # mailSender:         email address used to send the notifications. # mailSenderUser:     user name to authenticate on the SMTP server. # mailSenderPassword: password to authenticate on the SMTP server with the mailSenderUser. # logDir:             directory where the log file is written. # debug:              debug mode (output to stdout). # global config global simulation_server_loads # logging system root_logger = logging . getLogger ( ) root_logger . setLevel ( logging . DEBUG ) if 'logDir' not in config : config [ 'logDir' ] = 'log' else : config [ 'logDir' ] = expand_path ( config [ 'logDir' ] ) if 'portRewrite' not in config : config [ 'portRewrite' ] = False if 'debug' not in config : config [ 'debug' ] = False sessionLogDir = os . path . join ( config [ 'logDir' ] , 'session' ) logFile = os . path . join ( sessionLogDir , 'output.log' ) try : if not os . path . exists ( sessionLogDir ) : os . makedirs ( sessionLogDir ) if config [ 'debug' ] : file_handler = logging . StreamHandler ( sys . stdout ) else : file_handler = logging . handlers . RotatingFileHandler ( logFile , maxBytes = 500000 , backupCount = 10 ) formatter = logging . Formatter ( '%(asctime)-15s [%(levelname)-7s]  %(message)s' ) file_handler . setFormatter ( formatter ) file_handler . setLevel ( logging . INFO ) root_logger . addHandler ( file_handler ) except ( OSError , IOError ) as e : sys . exit ( "Log file '" + logFile + "' cannot be created: " + str ( e ) ) # disable tornado.access INFO logs tornado_access_log = logging . getLogger ( 'tornado.access' ) tornado_access_log . setLevel ( logging . WARNING ) simulation_server_loads = [ 0 ] * len ( config [ 'simulationServers' ] ) if 'administrator' in config : if 'mailServer' not in config : logging . info ( "No mail server defined in configuration, disabling e-mail notifications to " + config [ 'administrator' ] + "." ) del config [ 'administrator' ] elif 'mailSender' not in config : logging . info ( "No mail sender defined in configuration, disabling e-mail notifications to " + config [ 'administrator' ] + "." ) del config [ 'administrator' ] handlers = [ ] handlers . append ( ( r'/session' , SessionHandler ) ) handlers . append ( ( r'/' , ClientWebSocketHandler ) ) handlers . append ( ( r'/monitor' , MonitorHandler ) ) application = tornado . web . Application ( handlers ) if 'server' not in config : config [ 'server' ] = 'localhost' if 'sslCertificate' in config and 'sslKey' in config : config [ 'ssl' ] = True ssl_certificate = os . path . abspath ( expand_path ( config [ 'sslCertificate' ] ) ) ssl_key = os . path . abspath ( expand_path ( config [ 'sslKey' ] ) ) ssl_options = { "certfile" : ssl_certificate , "keyfile" : ssl_key } http_server = tornado . httpserver . HTTPServer ( application , ssl_options = ssl_options ) else : config [ 'ssl' ] = False http_server = tornado . httpserver . HTTPServer ( application ) update_load ( ) http_server . listen ( config [ 'port' ] ) message = "Session server running on port %d (" % config [ 'port' ] if not config [ 'ssl' ] : message += 'no ' message += 'SSL)' logging . info ( message ) try : tornado . ioloop . IOLoop . current ( ) . start ( ) except Exception : logging . error ( traceback . format_exc ( ) + "\n" ) for client in ClientWebSocketHandler . clients : del client