This repository has been archived by the owner on Dec 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
82 lines (70 loc) · 1.86 KB
/
request.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
package main
import (
"bitbucket.org/evolutek/cellaserv2-protobuf"
"net"
"time"
)
type RequestTracking struct {
sender net.Conn
timer *time.Timer
spies []net.Conn
}
func handleRequest(conn net.Conn, msgRaw []byte, req *cellaserv.Request) {
log.Info("[Request] Incoming from %s", conn.RemoteAddr())
// Runtime checks in Get*() functions are useless
name := req.ServiceName
method := req.Method
id := req.Id
var ident *string
if req.ServiceIdentification != nil {
ident = req.ServiceIdentification
log.Debug("[Request] id:%d %s[%s].%s", *id, *name, *ident, *method)
} else {
log.Debug("[Request] id:%d %s.%s", *id, *name, *method)
}
if *name == "cellaserv" {
cellaservRequest(conn, req)
return
}
idents, ok := services[*name]
if !ok || len(idents) == 0 {
log.Warning("[Request] id:%d No such service: %s", *id, *name)
sendReplyError(conn, req, cellaserv.Reply_Error_NoSuchService)
return
}
var srvc *Service
if ident != nil {
srvc, ok = idents[*ident]
if !ok {
log.Warning("[Request] id:%d No such identification for service %s: %s",
*id, *name, *ident)
}
} else {
srvc, ok = idents[""]
if !ok {
log.Warning("[Request] id:%d Must use identification for service %s",
*id, *name)
}
}
if !ok {
sendReplyError(conn, req, cellaserv.Reply_Error_InvalidIdentification)
return
}
// Handle timeouts
handleTimeout := func() {
_, ok := reqIds[*id]
if ok {
log.Error("[Request] id:%d Timeout of %s", *id, srvc)
sendReplyError(conn, req, cellaserv.Reply_Error_Timeout)
}
}
timer := time.AfterFunc(5*time.Second, handleTimeout)
// The ID is used to track the sender of the request
reqIds[*id] = &RequestTracking{conn, timer, srvc.Spies}
srvc.sendMessage(msgRaw)
// Forward message to the spies of this service
for _, spy := range srvc.Spies {
sendRawMessage(spy, msgRaw)
}
}
// vim: set nowrap tw=100 noet sw=8: