forked from wcong/ants-go
/
server.go
126 lines (112 loc) · 3.08 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
package rpc
import (
"github.com/wcong/ants-go/ants/action"
"github.com/wcong/ants-go/ants/node"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"strconv"
)
const (
RPC_TYPE = "tcp"
)
type RpcServer struct {
node *node.Node
port int
rpcClient action.RpcClientAnts
reporter action.Watcher
distributer action.Watcher
}
func NewRpcServer(node *node.Node, port int, rpcClient action.RpcClientAnts, reporter, distributer action.Watcher) *RpcServer {
rpcServer := &RpcServer{node, port, rpcClient, reporter, distributer}
rpcServer.start()
return rpcServer
}
// start a rpc server
func (this *RpcServer) server() {
rpc.Register(this)
listener, e := net.Listen(RPC_TYPE, ":"+strconv.Itoa(this.port))
if e != nil {
log.Println(e)
return
}
for {
if conn, err := listener.Accept(); err != nil {
log.Println(err)
} else {
log.Println("new connection")
go jsonrpc.ServeConn(conn)
}
}
}
// start a dead loop for server
func (this *RpcServer) start() {
go this.server()
}
// is server alive
func (this *RpcServer) IsAlive(request *action.RpcBase, response *action.RpcBase) error {
return nil
}
// handle the join request,
// if this is the master node ,let it join and connect to server
// else send it master node ,let it talk to master
func (this *RpcServer) LetMeIn(request *action.LeftMeInRequest, response *action.LeftMeInResponse) error {
if this.node.IsMasterNode() {
this.node.Join()
response.Result = true
response.NodeInfo = this.node.NodeInfo
this.rpcClient.Connect(request.NodeInfo.Ip, request.NodeInfo.Port)
this.node.Ready()
} else {
response.Result = false
response.NodeInfo = this.node.Cluster.GetMasterNode()
}
return nil
}
// just tell who i am
func (this *RpcServer) Connect(request *action.RpcBase, response *action.RpcBase) error {
response.Result = true
response.NodeInfo = this.node.NodeInfo
return nil
}
func (this *RpcServer) StopNode(request *action.StopRequest, response *action.StopResponse) error {
this.stopNode()
return nil
}
// expose method ,for accept method
func (this *RpcServer) AcceptRequest(request *action.DistributeRequest, response *action.DistributeReqponse) error {
this.node.AcceptRequest(request.Request)
return nil
}
// start spider for slave
// for now just start reporter
func (this *RpcServer) StartSpider(request *action.DistributeRequest, response *action.DistributeReqponse) error {
this.reporter.Start()
return nil
}
// for master accept crawl result
// if no more request to crawl stop it
// notice:
// * local request do not go through this way
// * close action also start by reporter
func (this *RpcServer) AcceptResult(request *action.ReportRequest, response *action.ReportResponse) error {
this.node.AcceptResult(request.ScrapeResult)
if this.node.IsStop() {
for _, nodeInfo := range this.node.GetAllNode() {
if this.node.IsMe(nodeInfo.Name) {
this.stopNode()
} else {
this.rpcClient.StopNode(nodeInfo.Name)
}
}
}
return nil
}
// stop the node
// crawler ,reporter and distributer
func (this *RpcServer) stopNode() {
this.node.StopCrawl()
this.reporter.Stop()
this.distributer.Stop()
}