-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (77 loc) · 2.01 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/rpc"
)
// Mode ...
type Mode int
// Process running mode options
const (
NONE Mode = iota
CLIENT
SERVER
)
func (m *Mode) String() string {
if *m == CLIENT {
return "client"
} else if *m == SERVER {
return "server"
}
return "unknown"
}
// Set value of flag
func (m *Mode) Set(value string) error {
if value == "client" {
*m = CLIENT
} else if value == "server" {
*m = SERVER
}
return nil
}
// CmdFlags defines a structure holding command line arguments
type CmdFlags struct {
RunMode Mode
}
var (
cmdFlags CmdFlags
)
func init() {
flag.Var(&cmdFlags.RunMode, "mode", "Running mode server/client")
flag.Parse()
}
func main() {
fmt.Printf("Hello %s\n", cmdFlags.RunMode.String())
if cmdFlags.RunMode == CLIENT {
// Tries to connect to localhost:1234 (The port on which rpc server is listening)
conn, err := net.Dial("tcp", "localhost:1234")
if err != nil {
log.Fatal("Connectiong:", err)
}
defer conn.Close()
// Create a struct, that mimics all methods provided by interface.
// It is not compulsory, we are doing it here, just to simulate a traditional method call.
arith := &ArithC{client: rpc.NewClient(conn)}
fmt.Println(arith.Multiply(5, 6))
fmt.Println(arith.Divide(500, 11))
} else if cmdFlags.RunMode == SERVER {
//Creating an instance of struct which implement Arithmetic interface
arith := new(ArithS)
// Register a new rpc server (In most cases, you will use default server only)
// And register struct we created above by name "Arith"
// The wrapper method here ensures that only structs which implement Arith interface
// are allowed to register themselves.
server := rpc.NewServer()
registerArith(server, arith)
// Listen for incoming tcp packets on specified port.
l, e := net.Listen("tcp", ":1234")
if e != nil {
log.Fatal("listen error:", e)
}
// This statement links rpc server to the socket, and allows rpc server to accept
// rpc request coming from that socket.
server.Accept(l)
}
}