@@ -30,9 +30,8 @@ type eventCallback func(string, []PeerInfo)
30
30
31
31
// Multicast servent main structure
32
32
type Multicast struct {
33
- Period int // multicast period (in secs, 10 by default)
34
- Payload string // Will be appended to the messages
35
-
33
+ period int // multicast period (in secs, 10 by default)
34
+ payload string // Will be appended to the messages
36
35
conn * net.UDPConn
37
36
addr * net.UDPAddr
38
37
failedTime int // timeout for peers' hello messages, 0 means no timeout
@@ -85,17 +84,33 @@ func JoinMulticast(addPeerCallback eventCallback, removePeerCallback eventCallba
85
84
}
86
85
87
86
return & Multicast {
88
- Period : defaultPeriod ,
87
+ period : defaultPeriod ,
89
88
conn : c ,
90
89
addr : udpAddr ,
91
90
failedTime : defaultFailedTime ,
92
91
addPeerCallback : aCb ,
93
92
removePeerCallback : rCb ,
93
+ helloTicker : time .NewTicker (time .Duration (defaultPeriod ) * time .Second ),
94
94
quit : make (chan bool , 1 ),
95
95
peers : make (map [string ]PeerInfo ),
96
96
}
97
97
}
98
98
99
+ // SetPeriod will define a different interval for the periodic multicasts. Currently setting the
100
+ // period once StartMulticast has been called is not supported, so this function must be called
101
+ // always before StartMulticast()
102
+ func (mc * Multicast ) SetPeriod (period int ) {
103
+ mc .period = period
104
+ mc .helloTicker = time .NewTicker (time .Duration (period ) * time .Second )
105
+ }
106
+
107
+ // SetPayload will define a payload to carry with multicast messages. Currently setting the
108
+ // payload once StartMulticast has been called is not supported, so this function must be called
109
+ // always before StartMulticast()
110
+ func (mc * Multicast ) SetPayload (payload string ) {
111
+ mc .payload = payload
112
+ }
113
+
99
114
// StartMulticast initiates advertising ourselves through multicasting
100
115
func (mc * Multicast ) StartMulticast () {
101
116
// Periodically announce ourselves to the network
@@ -120,11 +135,10 @@ func (mc *Multicast) ListenPeers() {
120
135
// users of this library when the program exits or the discovery service is disabled by
121
136
// the end user
122
137
func (mc * Multicast ) LeaveMulticast () {
123
- // Stop sending hello
124
- if mc .helloTicker != nil {
125
- mc .helloTicker .Stop ()
126
- }
127
- msg , e := makeByeMessage (mc .Payload ).serialize ()
138
+ // Stop sending 'hello'
139
+ mc .helloTicker .Stop ()
140
+
141
+ msg , e := makeByeMessage (mc .payload ).serialize ()
128
142
if e != nil {
129
143
log .Error (e )
130
144
}
@@ -158,8 +172,7 @@ func (mc *Multicast) read(b []byte) (int, *net.UDPAddr, error) {
158
172
// sendHellos returns an error if failing to set up the message, otherwise it handles its own
159
173
// errors (i.e. it will keep sending hellos after a failure).
160
174
func (mc * Multicast ) sendHellos () error {
161
- mc .helloTicker = time .NewTicker (time .Duration (mc .Period ) * time .Second )
162
- msg , e := makeHelloMessage (mc .Payload ).serialize ()
175
+ msg , e := makeHelloMessage (mc .payload ).serialize ()
163
176
if e != nil {
164
177
return e
165
178
}
@@ -191,7 +204,7 @@ func (mc *Multicast) listenPeers() error {
191
204
}
192
205
193
206
// Set a deadline to avoid blocking on a read forever
194
- e := mc .conn .SetReadDeadline (time .Now ().Add (time .Duration (mc .Period ) * time .Second ))
207
+ e := mc .conn .SetReadDeadline (time .Now ().Add (time .Duration (mc .period ) * time .Second ))
195
208
if e != nil {
196
209
log .Error (e )
197
210
}
0 commit comments