forked from maniacchallenge/2013
/
main.go
137 lines (111 loc) · 2.78 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"fmt"
"master/betternode"
"master/database"
"master/log"
"master/profile"
"master/rounds"
"master/server"
"os"
"runtime"
"strings"
"time"
)
func main() {
defer func() {
log.Close() // Close log
server.Close() // Close server
betternode.Close() // Close nodes
profile.StopProfile() // Close profile
time.Sleep(time.Second) // Wait for everything to close
}()
// Make Go run parallel
runtime.GOMAXPROCS(runtime.NumCPU())
log.Message("ManiacMaster starting.")
// Initialize database
log.Message("Connecting to database.")
err := database.Connect("localhost:27018", "maniac", "", "")
if err != nil {
log.Error(err)
fmt.Println("Error:", err.Error())
}
// Create TCP server
log.Message("Starting TCP server.")
err = server.Start(":6789", betternode.NewNode)
if err != nil {
log.Error(err)
fmt.Println("Error:", err.Error())
}
// Take commands
log.Message("Ready for command line input.")
fmt.Println("MANIAC Master ready.")
var command string
s := bufio.NewScanner(os.Stdin)
quit:
for {
// Get next command
fmt.Print("M> ")
if !s.Scan() {
fmt.Println("Input ended!")
break
}
command = s.Text()
// to trimmed lower case
command = strings.ToLower(strings.TrimSpace(command))
switch command {
case "help": // show helpful text
printHelp()
case "quit", "exit": // quits the master
break quit
case "next": // starts new round
rounds.NewRound()
case "send": // sends new message
rounds.NewTransaction()
case "info": // show info about the master
printInfo()
case "load":
fmt.Print("Profile file: ")
fmt.Scanln(&command)
profile.OpenProfile(command)
case "run":
fmt.Print("Profile name: ")
fmt.Scanln(&command)
go profile.RunProfile(command)
case "kill":
profile.StopProfile()
case "list":
profile.ListProfiles()
case "close":
fmt.Print("Profile name: ")
fmt.Scanln(&command)
profile.CloseProfile(command)
default:
fmt.Println("Invalid command: Type \"help\" to get help.")
}
}
}
func printHelp() {
fmt.Println("Commands:\n" +
"\thelp - shows this help message\n" +
"\tquit - shuts down the master\n" +
"\tnext - starts a new round\n" +
"\tsend - generates and sends a new message\n" +
"\tload - Loads a new profile\n" +
"\tlist - Lists all loaded profiles\n" +
"\trun - Runs a profile\n" +
"\tkill - Kills a running profile\n" +
"\tclose - Closes a loaded profile\n" +
"\tinfo - prints info about the current status of the master")
}
func printInfo() {
gos := runtime.NumGoroutine()
ths := runtime.GOMAXPROCS(0)
cpu := runtime.NumCPU()
nds := betternode.CountNodes()
fmt.Printf("Go Routines:\t%d\n"+
"Threads:\t%d\n"+
"Cores:\t\t%d\n"+
"Connected:\t%d\n", gos, ths, cpu, nds)
}