Ejemplo n.º 1
0
func TestMain(m *testing.M) {
	data, _ := ioutil.ReadFile("../simulation/test_data/demo.json")
	var s simulation.Simulation
	json.Unmarshal(data, &s)
	mainLogger := log.New()
	InitializeLogger(mainLogger)
	simulation.InitializeLogger(mainLogger)
	go Run(&s, "0.0.0.0", "22222")
	os.Exit(m.Run())
}
Ejemplo n.º 2
0
func main() {
	// Command line arguments
	port := flag.String("port", server.DEFAULT_PORT, "The port on which the server will listen")
	addr := flag.String("addr", server.DEFAULT_ADDR, "The address on which the server will listen. Set to 0.0.0.0 to listen on all addresses.")
	logFile := flag.String("logfile", "", "The filename in which to save the logs. If not specified, the logs are sent to stderr.")
	logLevel := flag.String("loglevel", "info", "The minimum level of log to be written. Possible values are 'crit', 'error', 'warn', 'info' and 'debug'.")

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, `Usage of ts2-sim-server:
  ts2-sim-server [options...] file

ARGUMENTS:
  file
		The JSON simulation file to load

OPTIONS:
`)
		flag.PrintDefaults()
	}

	flag.Parse()

	// Handle ctrl+c to kill on terminal
	killChan := make(chan os.Signal, 1)
	signal.Notify(killChan, os.Interrupt)

	// Setup logging system
	logger = log.New()
	var outputHandler log.Handler
	if *logFile != "" {
		outputHandler = log.Must.FileHandler(*logFile, log.LogfmtFormat())
	} else {
		outputHandler = log.StderrHandler
	}
	logLvl, err_level := log.LvlFromString(*logLevel)
	if err_level != nil {
		fmt.Fprintf(os.Stderr, "Error: Unknown loglevel\n\n")
		flag.Usage()
		os.Exit(1)
	}
	logger.SetHandler(log.LvlFilterHandler(
		logLvl,
		outputHandler,
	))
	simulation.InitializeLogger(logger)
	server.InitializeLogger(logger)

	// Load the simulation
	if len(flag.Args()) == 0 {
		fmt.Fprintf(os.Stderr, "Error: Please specify a simulation file\n\n")
		flag.Usage()
		os.Exit(1)
	}
	simFile := flag.Arg(0)
	logger.Info("Loading simulation", "file", simFile)

	data, err := ioutil.ReadFile(simFile)
	if err != nil {
		logger.Crit("Unable to read file", "file", simFile, "error", err)
		os.Exit(1)
	}

	var sim simulation.Simulation
	errload := json.Unmarshal(data, &sim)

	if errload != nil {
		logger.Error("Load Error", "file", simFile, "error", errload)
		return
	}
	logger.Info("Simulation loaded", "sim", sim.Options.Title)

	go server.Run(&sim, *addr, *port)

	// Route all messages
	for {
		select {

		case <-killChan:
			// TODO gracefully shutdown things maybe
			logger.Info("Server killed, exiting...")
			os.Exit(0)
		}
	}
}