-
Notifications
You must be signed in to change notification settings - Fork 8
/
socket.go
162 lines (127 loc) · 3.28 KB
/
socket.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Package zerorpc provides a client/server Golang library for the ZeroRPC protocol,
//
// for additional info see http://zerorpc.dotcloud.com
package zerorpc
import (
zmq "github.com/pebbe/zmq4"
"log"
"sync"
)
// ZeroRPC socket representation
type socket struct {
zmqSocket *zmq.Socket
Channels []*channel
server *Server
socketErrors chan error
mu sync.Mutex
}
// Connects to a ZeroMQ endpoint and returns a pointer to a new znq.DEALER socket,
// a listener for incoming messages is invoked on the new socket
func connect(endpoint string) (*socket, error) {
zmqSocket, err := zmq.NewSocket(zmq.DEALER)
if err != nil {
return nil, err
}
s := socket{
zmqSocket: zmqSocket,
Channels: make([]*channel, 0),
socketErrors: make(chan error),
}
if err := s.zmqSocket.Connect(endpoint); err != nil {
return nil, err
}
log.Printf("ZeroRPC socket connected to %s", endpoint)
go s.listen()
return &s, nil
}
// Binds to a ZeroMQ endpoint and returns a pointer to a new znq.ROUTER socket,
// a listener for incoming messages is invoked on the new socket
func bind(endpoint string) (*socket, error) {
zmqSocket, err := zmq.NewSocket(zmq.ROUTER)
if err != nil {
return nil, err
}
s := socket{
zmqSocket: zmqSocket,
Channels: make([]*channel, 0),
}
if err := s.zmqSocket.Bind(endpoint); err != nil {
return nil, err
}
log.Printf("ZeroRPC socket bound to %s", endpoint)
go s.listen()
return &s, nil
}
// Close the socket,
// it closes all the channels first
func (s *socket) close() error {
for _, c := range s.Channels {
c.close()
s.removeChannel(c)
}
log.Printf("ZeroRPC socket closed")
return s.zmqSocket.Close()
}
// Removes a channel from the socket's array of channels
func (s *socket) removeChannel(c *channel) {
s.mu.Lock()
defer s.mu.Unlock()
channels := make([]*channel, 0)
for _, t := range s.Channels {
if t != c {
channels = append(channels, t)
}
}
s.Channels = channels
}
// Sends an event on the ZeroMQ socket
func (s *socket) sendEvent(e *Event, identity string) error {
b, err := e.packBytes()
if err != nil {
return err
}
log.Printf("ZeroRPC socket sent event %s", e.Header["message_id"].(string))
i, err := s.zmqSocket.SendMessage(identity, "", b)
if err != nil {
return err
}
log.Printf("ZeroRPC socket sent %d bytes", i)
return nil
}
func (s *socket) listen() {
log.Printf("ZeroRPC socket listening for incoming data")
for {
barr, err := s.zmqSocket.RecvMessageBytes(0)
if err != nil {
s.socketErrors <- err
}
t := 0
for _, k := range barr {
t += len(k)
}
log.Printf("ZeroRPC socket received %d bytes", t)
ev, err := unPackBytes(barr[len(barr)-1])
if err != nil {
s.socketErrors <- err
}
log.Printf("ZeroRPC socket recieved event %s", ev.Header["message_id"].(string))
var ch *channel
if _, ok := ev.Header["response_to"]; !ok {
ch = s.newChannel(ev.Header["message_id"].(string))
go ch.sendHeartbeats()
if len(barr) > 1 {
ch.identity = string(barr[0])
}
} else {
for _, c := range s.Channels {
if c.Id == ev.Header["response_to"].(string) {
ch = c
}
}
}
if ch != nil && ch.state == open {
log.Printf("ZeroRPC socket routing event %s to channel %s", ev.Header["message_id"].(string), ch.Id)
ch.socketInput <- ev
}
}
}