forked from zeromq/goczmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
91 lines (72 loc) · 2.05 KB
/
monitor.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
package goczmq
/*
#include "czmq.h"
zactor_t *Monitor_new (zsock_t *sock) {
zactor_t *monitor = zactor_new(zmonitor, sock);
return monitor;
}
int Monitor_verbose (zactor_t *monitor) {
return zstr_sendx(monitor, "VERBOSE", NULL);
}
int Monitor_listen (zactor_t *monitor, const char *type) {
return zstr_sendx(monitor, "LISTEN", type, NULL);
}
void Monitor_destroy (zactor_t *monitor) {
zactor_destroy(&monitor);
}
*/
import "C"
import (
"unsafe"
)
// Monitor provides an API for obtaining socket events
type Monitor struct {
zactorT *C.struct__zactor_t
}
// NewMonitor creates new Monitor actor.
func NewMonitor(socket *Sock) *Monitor {
m := &Monitor{}
m.zactorT = C.Monitor_new((*C.struct__zsock_t)(unsafe.Pointer(socket.zsockT)))
return m
}
// Listen specifies which events to listen for. "ALL" is also supported.
func (m *Monitor) Listen(event string) error {
cmd := C.CString("LISTEN")
defer C.free(unsafe.Pointer(cmd))
eventStr := C.CString(event)
defer C.free(unsafe.Pointer(eventStr))
rc := C.Monitor_listen((*C.struct__zactor_t)(unsafe.Pointer(m.zactorT)), eventStr)
if rc == -1 {
return ErrActorCmd
}
return nil
}
// Start activates the socket monitoring. Additional Listen() calls will not have an effect after this.
func (m *Monitor) Start() error {
cmd := C.CString("START")
defer C.free(unsafe.Pointer(cmd))
rc := C.zstr_send(unsafe.Pointer(m.zactorT), cmd)
if rc == -1 {
return ErrActorCmd
}
C.zsock_wait(unsafe.Pointer(m.zactorT))
return nil
}
// Verbose enables verbose mode, logging activity to stdout
func (m *Monitor) Verbose() error {
rc := C.Monitor_verbose((*C.struct__zactor_t)(unsafe.Pointer(m.zactorT)))
if rc == -1 {
return ErrActorCmd
}
return nil
}
// Socket returns the actor as a Sock instance, useful and necessary for being able to receive messages
func (m *Monitor) Socket() *Sock {
s := &Sock{}
s.zsockT = (*C.struct__zsock_t)(unsafe.Pointer(m.zactorT))
return s
}
// Destroy destroys the monitor instance
func (m *Monitor) Destroy() {
C.Monitor_destroy((*C.struct__zactor_t)(unsafe.Pointer(m.zactorT)))
}