forked from ts2/ts2-sim-server
/
main.go
87 lines (75 loc) · 2.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Copyright (C) 2008-2016 by Nicolas Piganeau and the TS2 team
* (See AUTHORS file)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/ts2/ts2-sim-server/server"
"github.com/ts2/ts2-sim-server/simulation"
"io/ioutil"
"log"
"os"
"os/signal"
)
func main() {
// Command line arguments
port := flag.String("port", "22222", "The port on which the server will listen")
addr := flag.String("addr", "0.0.0.0", "The address on which the server will listen. Set to 0.0.0.0 to listen on all addresses.")
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)
// Load the simulation
var sim simulation.Simulation
if len(flag.Args()) > 0 {
simFile := flag.Arg(0)
log.Printf("Loading simulation: %s\n", simFile)
data, err := ioutil.ReadFile(simFile)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(data, &sim)
log.Printf("Simulation loaded: %s\n", sim.Options.Title)
} else {
fmt.Fprintf(os.Stderr, "Error: Please specify a simulation file\n\n")
flag.Usage()
os.Exit(1)
}
go server.Run(&sim, *addr, *port)
// Route all messages
for {
select {
case <-killChan:
// TODO gracefully shutdown things maybe
log.Println("Server killed, exiting...")
os.Exit(0)
}
}
}