-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
85 lines (70 loc) · 1.55 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
crypto_rand "crypto/rand"
"encoding/gob"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net"
"time"
"github.com/dradtke/distchan"
)
type AdderInput struct {
ID string
A, B int
}
type AdderOutput struct {
ID string
Answer int
}
func producer(out chan<- AdderInput) {
for {
input := AdderInput{
ID: newUUID(),
A: rand.Intn(100),
B: rand.Intn(100),
}
fmt.Printf("[%s] requesting answer to %d + %d\n", input.ID, input.A, input.B)
out <- input
}
}
// ID generation method graciously borrowed from https://play.golang.org/p/4FkNSiUDMg
func newUUID() string {
uuid := make([]byte, 16)
n, err := io.ReadFull(crypto_rand.Reader, uuid)
if n != len(uuid) || err != nil {
panic(err)
}
uuid[8] = uuid[8]&^0xc0 | 0x80
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
}
func main() {
bindAddr := flag.String("bind", "", "address to bind to")
flag.Parse()
if *bindAddr == "" {
log.Fatal("no bind address specified")
}
ln, err := net.Listen("tcp", *bindAddr)
if err != nil {
panic(err)
}
rand.Seed(time.Now().Unix())
gob.Register(AdderInput{})
gob.Register(AdderOutput{})
var (
out = make(chan AdderInput)
in = make(chan AdderOutput)
server, _ = distchan.NewServer(ln, out, in)
)
server.Start()
fmt.Println("waiting for clients to connect...")
server.WaitUntilReady()
fmt.Println("...and go!")
go producer(out)
for result := range in {
fmt.Printf("[%s] received answer: %d\n", result.ID, result.Answer)
}
}