-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRPCServer.go
57 lines (49 loc) · 1.2 KB
/
RPCServer.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
// RPC from http://www.artima.com/weblogs/viewpost.jsp?thread=333589
// Sample calls, for use with rpc_client.py
package main
import (
"net/rpc/jsonrpc"
"net/rpc"
"net"
"log"
"strconv"
)
type RPCFunc struct {} // Callable by JSON-RPC
// must have key type string
func (*RPCFunc) DictLen(arg *map[string]int, result *int) error {
for k, v := range *arg {
log.Print("Arg passed: "+string(k)+" "+strconv.Itoa(v))
}
*result = len(*arg)
return nil
}
func (*RPCFunc) Add(arg *int, result *int) error {
log.Print("Arg passed: " + strconv.Itoa(*arg))
*result = *arg + 1
return nil
}
func (*RPCFunc) Echo(arg *string, result *string) error {
log.Print("Arg passed: " + *arg)
*result = ">" + *arg + "<"
return nil
}
func main() {
log.Print("Starting Server...")
l, err := net.Listen("tcp", "localhost:1234")
if err != nil {
log.Fatal(err)
}
defer l.Close()
log.Print("listening on: ", l.Addr())
rpc.Register(new (RPCFunc))
for {
log.Print("waiting for connections...")
conn, err := l.Accept()
if err != nil {
log.Printf("accept error: %s", conn)
continue
}
log.Printf("connection started: %v", conn.RemoteAddr())
go jsonrpc.ServeConn(conn)
}
}