-
Notifications
You must be signed in to change notification settings - Fork 11
/
client.go
56 lines (45 loc) · 873 Bytes
/
client.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
package main
import (
"encoding/gob"
"flag"
"fmt"
"log"
"net"
"github.com/dradtke/distchan"
)
type AdderInput struct {
ID string
A, B int
}
type AdderOutput struct {
ID string
Answer int
}
func main() {
addr := flag.String("addr", "", "address to connect to")
flag.Parse()
if *addr == "" {
log.Fatal("no server address specified")
}
gob.Register(AdderInput{})
gob.Register(AdderOutput{})
conn, err := net.Dial("tcp", *addr)
if err != nil {
panic(err)
}
var (
out = make(chan AdderOutput)
in = make(chan AdderInput)
client, _ = distchan.NewClient(conn, out, in)
)
client.Start()
fmt.Println("waiting for input...")
for input := range in {
fmt.Printf("[%s] processing %d + %d\n", input.ID, input.A, input.B)
answer := input.A + input.B
out <- AdderOutput{
ID: input.ID,
Answer: answer,
}
}
}