-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
128 lines (113 loc) · 4.13 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
package main
import (
"flag"
"fmt"
"google.golang.org/grpc"
"log"
"net"
//"net/http"
_ "net/http/pprof"
"os"
"rloop/Go-Ground-Station/datastore"
"rloop/Go-Ground-Station/gsgrpc"
"rloop/Go-Ground-Station/gstypes"
"rloop/Go-Ground-Station/helpers"
"rloop/Go-Ground-Station/logging"
"rloop/Go-Ground-Station/server"
"strconv"
)
func main() {
/*
go func() {
log.Println(http.ListenAndServe("localhost:8080", nil))
}()
*/
fmt.Println("Backend version 13-04-2018")
//errors
var networkConfigError error
var grpcError error
//networking
var networkConfig gstypes.Networking
var hostsTolisten []gstypes.Host
var hostsToCommand []gstypes.Host
var GrpcPort int
var nodesMap map[int]gstypes.Host
var nodesPorts []int
var grpcConn net.Listener
//services
var serviceManager *server.ServiceManager
var simController *server.SimController
var gsLogger *logging.Gslogger
var dataStoreManager *datastore.DataStoreManager
var udpBroadCasterServer *server.UDPBroadcasterServer
var udpListenerServers []*server.UDPListenerServer
var grpcServer *grpc.Server
//channels
var serviceChannel chan<- gstypes.ServerControlWithTimeout
var simCommandChannel chan<- *gstypes.SimulatorCommandWithResponse
var simInitChannel chan<- *gstypes.SimulatorInitWithResponse
var loggerChannel chan<- gstypes.PacketStoreElement
var grpcChannelsHolder *gsgrpc.ChannelsHolder
var dataStoreChannel chan<- gstypes.PacketStoreElement
var commandChannel chan<- gstypes.Command
networkConfig, networkConfigError = helpers.DecodeNetworkingFile("./config/networking.json")
if networkConfigError != nil {
log.Fatalf("No config is defined, please define a config file: %v \n", networkConfigError)
os.Exit(1)
}
hostsTolisten = networkConfig.HostsToListen
hostsToCommand = networkConfig.HostsToCommand
GrpcPort = networkConfig.Grpc
nodesMap = map[int]gstypes.Host{}
//create the nodes map, for efficiency
arrlength := len(hostsTolisten)
nodesPorts = make([]int, arrlength)
for idx := 0; idx < arrlength; idx++ {
currPort := hostsTolisten[idx].Port
nodesMap[currPort] = hostsTolisten[idx]
nodesPorts[idx] = currPort
}
//PARSE FLAGS, IF FLAGS ARE GIVEN DETERMINE WHICH PORTS THE SERVER WILL LISTEN TO ELSE DO NOTHING AND USE PORTS FROM CONFIG FILE
//the array that will hold the port numbers for the UDP listeners
flag.Parse()
flags := flag.Args()
flagLength := len(flags)
if flagLength > 0 {
nodesPorts = make([]int, flagLength)
for i, p := range flags {
port, err := strconv.Atoi(p)
if err == nil {
nodesPorts[i] = port
}
}
}
//create a servicemanager and get the channel where control commands will be issued
serviceManager, serviceChannel = server.NewServiceManager()
if networkConfig.WithSim && networkConfig.PySim != "" {
simController, simCommandChannel, simInitChannel = server.NewSimController()
simController.Connect(networkConfig.PySim)
serviceManager.SetSimController(simController)
}
gsLogger, loggerChannel = logging.New()
//struct that will contain the channels that will be used to communicate between the datastoremanager and stream server
grpcChannelsHolder = gsgrpc.GetChannelsHolder()
//Create the datastoremanager server
dataStoreManager, dataStoreChannel = datastore.New(grpcChannelsHolder)
//create the broadcasting server that will send the commands to the rpod
udpBroadCasterServer, commandChannel = server.CreateNewUDPCommandServer(hostsToCommand)
//Create the UDPListenerServers that will listen to the packets sent by the rpod
udpListenerServers = server.CreateNewUDPListenerServers(dataStoreChannel, loggerChannel, nodesPorts, nodesMap)
//Create the gsgrpc stream server
grpcConn, grpcServer, grpcError = gsgrpc.NewGoGrpcServer(GrpcPort, grpcChannelsHolder, commandChannel, simCommandChannel, simInitChannel, serviceChannel, serviceManager)
serviceManager.SetDatastoreManager(dataStoreManager)
serviceManager.SetGsLogger(gsLogger)
serviceManager.SetUDPListenerServers(udpListenerServers)
serviceManager.SetUDPBroadcaster(udpBroadCasterServer)
if grpcError != nil {
panic("unable to start gsgrpc server")
} else {
serviceManager.SetGrpcServer(grpcServer, grpcConn)
}
serviceManager.RunAll()
select {}
}