-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
156 lines (123 loc) · 2.84 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
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
package rpcx
import (
"log"
"net"
"net/rpc"
"time"
)
type ServerDelegate interface {
PING(args *PingArgs, reply *PingReply) error
TICK(args *TickArgs, reply *TickReply) error
}
type ServerStatus string
const (
StatusStopped = ServerStatus("stopped")
StatusPaused = ServerStatus("paused")
StatusRunning = ServerStatus("running")
)
// Server rpc server
type Server struct {
Delegate ServerDelegate
rpcServer *rpc.Server
status ServerStatus
statusSwitch chan ServerStatus
listener net.Listener
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func (s *Server) RPCServer() *rpc.Server {
if nil == s.rpcServer {
s.rpcServer = rpc.NewServer()
s.statusSwitch = make(chan ServerStatus)
}
return s.rpcServer
}
func (s *Server) RegisterInternalServices() {
server := &internalServer{
Delegate: s.Delegate,
}
s.RegisterServices("RPCX", server)
}
func (s *Server) RegisterServices(name string, rcvr interface{}) {
if "" == name {
s.RPCServer().Register(rcvr)
} else {
s.RPCServer().RegisterName(name, rcvr)
}
}
func (s *Server) ServeConn(conn net.Conn) {
//log.Printf("[RPCX SERVER] New Client Connected: %v", conn.RemoteAddr())
go s.RPCServer().ServeConn(conn)
}
func (s *Server) Stop() {
if s.status == StatusStopped {
return
}
//log.Println("[RPCX SERVER] will Stop")
// this will bring server from accepting connection
s.listener.Close()
s.status = StatusStopped
}
func (s *Server) Pause() {
if s.status == StatusPaused {
return
}
//log.Println("[RPCX SERVER] will pause")
s.statusSwitch <- StatusPaused
s.status = StatusPaused
}
// Run run
func (s *Server) Run(address string) {
if s.status == StatusRunning {
return
}
tcpAddr, err := net.ResolveTCPAddr("tcp", address)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
s.listener = listener
//log.Printf("[RPCX SERVER] Running on %s\r\n", address)
for {
if s.status == StatusStopped {
//log.Printf("[RPCX SERVER] %s stopped\r\n", address)
return
}
if s.status == StatusPaused {
//log.Printf("[RPCX SERVER] %s paused\r\n", address)
status := <-s.statusSwitch
if StatusRunning != status {
continue
}
}
conn, err := listener.Accept()
if err != nil {
//log.Printf("[RPCX] Listener Accept Error: %v", err)
s.Stop()
//s.Delegate.Disconnected()
continue
}
s.ServeConn(conn)
}
}
type internalServer struct {
Delegate ServerDelegate
}
func (t *internalServer) PING(args *PingArgs, reply *PingReply) error {
reply.ReceivedAt = time.Now()
reply.DeliveredAt = time.Now()
if nil != t.Delegate {
return t.Delegate.PING(args, reply)
}
return nil
}
func (t *internalServer) TICK(args *TickArgs, reply *TickReply) error {
reply.ReceivedAt = time.Now()
reply.DeliveredAt = time.Now()
if nil != t.Delegate {
return t.Delegate.TICK(args, reply)
}
return nil
}