-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
68 lines (43 loc) · 1.14 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
//implementation of the server logic for sending RPC messages in raft
// implemetation is similar to that used by eli.
// Tried my best to make sure I understood what was happening here
//
package main
import (
"fmt"
"net"
"net/rpc"
"sync"
)
type Server struct{
mux sync.Mutex
serverId int
peerIds []int
node *NodeModel
rpcServer *rpc.Server
listener net.Listener
peerClients map[int]*rpc.Client
ready <- chan interface{}
quit chan interface{}
wg sync.WaitGroup
}
func NewServer(serverId int, peerIds []int, ready <-chan interface{}) *Server {
s := new(Server)
s.serverId = serverId
s.peerIds = peerIds
s.peerClients = make(map[int]*rpc.Client)
s.ready = ready
s.quit = make(chan interface{})
return s
}
//sends RPC calls nodes in the cluster
func (s *Server) Call(id int, serviceMethod string, args interface{}, reply interface{}) error {
s.mux.Lock()
peer := s.peerClients[id]
s.mux.Unlock()
if peer == nil {
return fmt.Errorf("Client %d is most likely closed partitioned or dead", id)
}else {
return peer.Call(serviceMethod, args, reply)
}
}