/
identity.go
62 lines (52 loc) · 1.25 KB
/
identity.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
//
// Demonstrate identities as used by the request-reply pattern.
// Run this program by itself.
//
package main
import (
zmq "github.com/pebbe/zmq4"
"fmt"
"regexp"
)
var (
all_char = regexp.MustCompile("^[^[:cntrl:]]*$")
)
func main() {
sink, _ := zmq.NewSocket(zmq.ROUTER)
defer sink.Close()
sink.Bind("inproc://example")
// First allow 0MQ to set the identity
anonymous, _ := zmq.NewSocket(zmq.REQ)
defer anonymous.Close()
anonymous.Connect("inproc://example")
anonymous.Send("ROUTER uses a generated UUID", 0)
dump(sink)
// Then set the identity ourselves
identified, _ := zmq.NewSocket(zmq.REQ)
defer identified.Close()
identified.SetIdentity("PEER2")
identified.Connect("inproc://example")
identified.Send("ROUTER socket uses REQ's socket identity", 0)
dump(sink)
}
func dump(soc *zmq.Socket) {
fmt.Println("----------------------------------------")
for {
// Process all parts of the message
message, _ := soc.Recv(0)
// Dump the message as text or binary
fmt.Printf("[%03d] ", len(message))
if all_char.MatchString(message) {
fmt.Print(message)
} else {
for i := 0; i < len(message); i++ {
fmt.Printf("%02X ", message[i])
}
}
fmt.Println()
more, _ := soc.GetRcvmore()
if !more {
break
}
}
}