-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
68 lines (62 loc) · 1.49 KB
/
server.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
package main
import (
"bytes"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"github.com/ttacon/glorious/agent"
"github.com/ttacon/glorious/context"
"github.com/ttacon/glorious/tailer"
)
func runServer(agnt *agent.Agent, addr string, lgr context.Logger) error {
server := rpc.NewServer()
server.Register(agnt)
listener, e := net.Listen("tcp", addr)
if e != nil {
log.Fatal("listen error:", e)
}
for {
if conn, err := listener.Accept(); err != nil {
lgr.Fatal("accept error: " + err.Error())
} else {
lgr.Infof("new connection established\n")
handleNewConnection(
conn,
server,
tailer.NewTailer(agnt),
lgr,
)
}
}
}
func handleNewConnection(
conn net.Conn,
server *rpc.Server,
tailr tailer.Tailer,
lgr context.Logger,
) {
lgr.Debug("parsing magic cookie")
var buf = make([]byte, 4)
if n, err := conn.Read(buf); err != nil {
lgr.Error("failed to read magic cookie from connection, closing, err: ", err)
_ = conn.Close()
return
} else if n != 4 {
lgr.Error("did not read entire cookie frame, exiting (read %d of 4 bytes)\n", n)
_ = conn.Close()
return
}
if bytes.Equal(buf, MAGIC_COOKIE_V1) {
lgr.Debug("creating agent based rpc connection")
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
return
} else if bytes.Equal(buf, MAGIC_COOKIE_V1_LOGS) {
lgr.Debug("creating stream based connection")
// Stream logs back.
go tailr.Handle(conn)
return
}
lgr.Error("unknown MAGIC_COOKIE received, closing, got: ", string(buf))
_ = conn.Close()
}