11# Broadcast to the cluster. This is a shortcut to send to the cluster protocol.
22::oo::define ::cluster::cluster method broadcast { payload } {
33 try {
4- set proto [dict get $PROTOCOLS c]
4+ set proto [dict get $PROTOCOLS c]
55 try {my run_hook broadcast} on error {r} { return 0 }
6- try {my run_hook channel [dict get $payload channel] send} on error {r} { return 0 }
6+ try {my run_hook channel [dict get $payload channel] send} on error {r} { return 0 }
77 return [ $proto send [::cluster::packet::encode $payload ] ]
88 } on error {result options} {
99 ::onError $result $options " While Broadcasting a Cluster Payload" $payload
2323 set props [lsort -unique [concat $UPDATED_PROPS $props ]]
2424 set UPDATED_PROPS [list ]
2525 my CheckServices
26- my CheckProtocols
26+ my CheckProtocols
2727 }
2828 if { " tags" in $props } { set tags 1 }
2929 my broadcast [my heartbeat_payload $props $tags $channel ]
3333}
3434
3535# Reschedule the heartbeat so that it will occur after the given time. This is useful
36- # for when we want to cause a heartbeat to occur sooner than the normal time but dont
36+ # for when we want to cause a heartbeat to occur sooner than the normal time but dont
3737# want to accidentally dispatch tons of heartbeats when multiple services call it, etc.
3838::oo::define ::cluster::cluster method heartbeat_after { ms } {
3939 try {
4646
4747
4848# Send a discovery probe to the cluster. Each service will send its response
49- # based on the best protocol it can find.
49+ # based on the best protocol it can find.
5050::oo::define ::cluster::cluster method discover { {ruid {}} {channel 0} } {
5151 my variable LAST_DISCOVERY
5252 if { ! [info exists LAST_DISCOVERY] } { set LAST_DISCOVERY [clock seconds] } else {
8181 lappend services {*}[my resolver [dict get $args -resolver]]
8282 }
8383 if { [dict exists $args -resolve] } {
84- # Resolve the given list.
84+ # Resolve the given list.
8585 lappend services {*}[my resolve [dict get $args -resolve]]
8686 }
87-
87+
8888 # puts "Send to services [llength $services]"
8989 # puts $services
9090 set allow_broadcast 0
91-
91+
9292 if { [dict exists $args -broadcast] } {
9393 # 1 / 0 - Indicates if we want to broadcast the message or not.
9494 # If empty we will try to decide automatically.
9595 set broadcast [dict get $args -broadcast]
9696 if { $broadcast eq {} } { set allow_broadcast 1 }
97- } else {
97+ } else {
9898 # When broadcast is empty we are indicating that we are not yet
9999 # sure if we want to broadcast the message.
100- set broadcast {}
101- # When we have not explicitly turned broadcasting off then we
100+ set broadcast {}
101+ # When we have not explicitly turned broadcasting off then we
102102 # will allow it to be used as a last resort if all other protocols
103103 # fail. The broadcast will be filtered so only the given service
104104 # will respond.
105105 set allow_broadcast 1
106106 }
107107
108108 if { ! [string is true -strict $broadcast ] } {
109- if { $services eq {} } { return }
109+ if { $services eq {} } { return }
110110 }
111-
111+
112112 if { [dict exists $args -protocols] } {
113113 set protocols [dict get $args -protocols]
114114 } else {
115115 # If protocols isnt provided - we need to determine the
116- # best way to send our message based on the number of
116+ # best way to send our message based on the number of
117117 # services that have been resolved.
118118 #
119119 # Right now we keep it simple - more than 3 we use cluster
136136 set broadcast 0
137137 }
138138 }
139-
139+
140140 if { [dict exists $args -channel] } {
141- set channel [dict get $args -channel]
141+ set channel [dict get $args -channel]
142142 } else { set channel 0 }
143-
143+
144144 if { [dict exists $args -ruid] } {
145- dict set request ruid [dict get $args -ruid]
145+ dict set request ruid [dict get $args -ruid]
146146 }
147-
147+
148148 if { [dict exists $args -data] } {
149- dict set request data [dict get $args -data]
149+ dict set request data [dict get $args -data]
150150 }
151-
151+
152152 # Make sure we only have one of each service
153153 set services [lsort -unique $services ]
154-
154+
155155 if { [string is true -strict $broadcast ] && $services ne {} } {
156156 # If we are using the cluster protocol to transmit, we use broadcast
157157 # to send our payload. This means that we also need to add a filter
158- # to the request so that only our desired services will handle the
159- # message being sent.
158+ # to the request so that only our desired services will handle the
159+ # message being sent.
160160 #
161- # A filter will be automatically applied when sending directly to
161+ # A filter will be automatically applied when sending directly to
162162 # a service otherwise.
163163 #
164164 # This allows us to use the multicast as a p2p connection to aid in
169169 }
170170 dict set request filter $filter
171171 }
172-
172+
173173 set payload [my event_payload $request $channel ]
174-
174+
175175 if { [string is true -strict $broadcast ] } {
176176 # We have indicated that we want to broadcast the message.
177177 my broadcast $payload
181181 # as we will attempt to handle it ourselves if required.
182182 set response [ my send_payload $services $payload $protocols 0 ]
183183 if { $response eq {} } {
184- # We will receive an empty response when a hook has cancelled the
184+ # We will receive an empty response when a hook has cancelled the
185185 # transmission
186186 } else {
187187 lassign $response success failed
188188 # puts stderr "Success: $success"
189-
189+
190190 if { $failed ne {} } {
191191 # puts stderr "Failed: $failed"
192192 # If we failed to send to any services we will determine how we should
193193 # proceed.
194194 # ::utils::flog "Failed to Send To Cluster Services \n $args \n $failed"
195195 # ~! "Cluster Warning" "Failed to send to Cluster Services" \
196196 # -context "
197- # Success:
197+ # Success:
198198 # $success
199199 # ------------
200- # Failed:
200+ # Failed:
201201 # $failed
202202 # "
203203 }
204204 }
205205 }
206- # Return a resolved list of the services and payload being used. This way
206+ # Return a resolved list of the services and payload being used. This way
207207 # multiple requests can simply call send_payload directly without running
208208 # through this process.
209209 return [list $services $payload $protocols ]
210210}
211211
212212::oo::define ::cluster::cluster method send_payload { services payload protocols {allow_broadcast 0} } {
213213 set success [dict create] ; set failed [dict create]
214+
214215 try {my run_hook channel [dict get $payload channel] send} on error { r } { return }
215216 foreach service $services {
216217 try {
225226 }
226227 }
227228 return [list $success $failed ]
228- }
229+ }
0 commit comments