-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
83 lines (68 loc) · 1.95 KB
/
init.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
package quacktors
import (
"fmt"
"github.com/Azer0s/qpmd"
"github.com/Azer0s/quacktors/config"
"github.com/Azer0s/quacktors/logging"
"github.com/Azer0s/quacktors/typeregister"
"github.com/vmihailenco/msgpack/v5"
"net"
)
var messageGatewayPort = uint16(0)
var gpGatewayPort = uint16(0)
var logger logging.Logger
var qpmdPort uint16
func initQuacktorSystems() {
logger = config.GetLogger()
qpmdPort = config.GetQpmdPort()
initializeGateways()
initializeQpmdConnection()
initializeBuiltInMessages()
}
func initializeGateways() {
var err error
messageGatewayPort, err = startMessageGateway()
if err != nil {
logger.Fatal("there was an error while starting the message gateway",
"error", err)
}
gpGatewayPort, err = startGeneralPurposeGateway()
if err != nil {
logger.Fatal("there was an error while starting the general purpose gateway",
"error", err)
}
}
func initializeQpmdConnection() {
failIfConnectionError := func(err error) {
if err != nil {
panic("Couldn't connect to qpmd! Is qpmd running?")
}
}
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", qpmdPort))
failIfConnectionError(err)
b, err := msgpack.Marshal(qpmd.Request{
RequestType: qpmd.REQUEST_HELLO,
Data: map[string]interface{}{
qpmd.MACHINE_ID: machineId,
qpmd.MESSAGE_GATEWAY_PORT: messageGatewayPort,
qpmd.GP_GATEWAY_PORT: gpGatewayPort,
},
})
try(err)
_, err = conn.Write(b)
failIfConnectionError(err)
buf := make([]byte, 4096)
_, err = conn.Read(buf)
failIfConnectionError(err)
res := qpmd.Response{}
err = msgpack.Unmarshal(buf, &res)
try(err)
}
func initializeBuiltInMessages() {
typeregister.Store(Pid{}.Type(), Pid{})
typeregister.Store(DownMessage{}.Type(), DownMessage{})
typeregister.Store(PoisonPill{}.Type(), PoisonPill{})
typeregister.Store(GenericMessage{}.Type(), GenericMessage{})
typeregister.Store(DisconnectMessage{}.Type(), DisconnectMessage{})
typeregister.Store(KillMessage{}.Type(), KillMessage{})
}