-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
173 lines (139 loc) · 3.83 KB
/
system.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package quacktors
import (
"errors"
"fmt"
"github.com/Azer0s/qpmd"
"net"
"sync"
)
const handler = "handler"
const pidVal = "pid"
//The System struct represents a logical actor system (i.e. a
//collection of PIDs that have been assigned a handler
//name so that remote machines can look them up).
//Furthermore, the System struct also keeps track of the
//connection to the local qpmd and keeps track of the
//system server status (the server which is used to
//look up PIDs by handler names, etc).
type System struct {
name string
handlers map[string]*Pid
handlersMu *sync.RWMutex
quitChan chan bool
heartbeatQuitChan chan bool
closed bool
}
//HandleRemote associates a PID with a handler name.
func (s *System) HandleRemote(name string, process *Pid) {
s.handlersMu.Lock()
defer s.handlersMu.Unlock()
s.handlers[name] = process
}
//IsClosed returns true if the connection to the
//local qpmd or the system server were closed.
func (s *System) IsClosed() bool {
return s.closed
}
//Close closes the connection to the local qpmd
//and quits the system server.
func (s *System) Close() {
s.closed = true
s.quitChan <- true
s.heartbeatQuitChan <- true
}
func (s *System) startServer() (uint16, error) {
logger.Debug("starting system server",
"system_name", s.name)
return startServer(func(portChan chan int, errorChan chan error) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
errorChan <- errors.New("couldn't start system server on random port")
return
}
port := listener.Addr().(*net.TCPAddr).Port
portChan <- port
logger.Debug("started system server successfully",
"system_name", s.name)
for {
select {
case <-s.quitChan:
logger.Info("quitting system server",
"system_name", s.name)
return
default:
conn, err := listener.Accept()
if err != nil {
logger.Warn("there was an error while accepting an incoming client for system",
"system_name", s.name,
"error", err)
continue
}
logger.Info("handling incoming client for system",
"system_name", s.name,
"client", conn.RemoteAddr().String())
go s.handleClient(conn)
}
}
})
}
func (s *System) handleClient(conn net.Conn) {
c := conn.RemoteAddr().String()
defer func() {
recover()
logger.Debug("closing system server connection to client",
"system_name", s.name,
"client", c)
err := conn.Close()
if err != nil {
return
}
}()
req, err := readRequest(conn)
if err != nil {
return
}
switch req.RequestType {
case qpmd.REQUEST_HELLO:
//I'll leave the hello message for now. Maybe it'll be useful in the future
//(plus it's more consistent to machine to machine communication)
logger.Debug("handling system server hello request",
"system_name", s.name,
"client", c)
err = writeOk(conn, map[string]interface{}{})
if err != nil {
logger.Warn("there was an error while sending ok message to client",
"client", c,
"error", err)
}
case qpmd.REQUEST_LOOKUP:
s.handlersMu.RLock()
defer s.handlersMu.RUnlock()
handlerName := req.Data[handler].(string)
logger.Debug("handling system server lookup request",
"system_name", s.name,
"client", c,
"handler_name", handlerName)
h, ok := s.handlers[handlerName]
if ok {
err = writeOk(conn, map[string]interface{}{
pidVal: h,
})
if err != nil {
logger.Warn("there was an error while sending ok message to client",
"client", c,
"error", err)
}
return
}
logger.Warn("couldn't find handler for system server lookup request",
"system_name", s.name,
"client", c,
"handler_name", handlerName)
err = writeError(conn, fmt.Errorf("couldn't find handler %s", handlerName))
if err != nil {
logger.Warn("there was an error while sending error message to client",
"client", c,
"error", err)
}
}
}