-
Notifications
You must be signed in to change notification settings - Fork 35
/
RpcClientServer.go
88 lines (75 loc) · 1.79 KB
/
RpcClientServer.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
package main
import (
"errors"
"fmt"
"log"
"net"
"net/rpc"
"time"
"github.com/DistributedClocks/GoVector/govec"
"github.com/DistributedClocks/GoVector/govec/vrpc"
)
var done chan int = make(chan int, 1)
// Args are mathematical arguments for the rpc operations
type Args struct {
A, B int
}
// Quotient is the result of a Divide RPC
type Quotient struct {
Quo, Rem int
}
// Arith is an RPC math server type
type Arith int
// Multiply performs multiplication on two integers
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
// Divide divides a by b and returns a quotient with a remainder
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
func rpcServer() {
fmt.Println("Starting server")
logger := govec.InitGoVector("server", "serverlogfile", govec.GetDefaultConfig())
arith := new(Arith)
server := rpc.NewServer()
server.Register(arith)
l, e := net.Listen("tcp", ":8080")
if e != nil {
log.Fatal("listen error:", e)
}
options := govec.GetDefaultLogOptions()
vrpc.ServeRPCConn(server, l, logger, options)
}
func rpcClient() {
fmt.Println("Starting client")
logger := govec.InitGoVector("client", "clientlogfile", govec.GetDefaultConfig())
options := govec.GetDefaultLogOptions()
client, err := vrpc.RPCDial("tcp", "127.0.0.1:8080", logger, options)
if err != nil {
log.Fatal(err)
}
var result int
err = client.Call("Arith.Multiply", Args{5, 6}, &result)
if err != nil {
log.Fatal(err)
}
var qresult Quotient
err = client.Call("Arith.Divide", Args{4, 2}, &qresult)
if err != nil {
log.Fatal(err)
}
done <- 1
}
func main() {
go rpcServer()
time.Sleep(time.Millisecond)
go rpcClient()
<-done
}