Skip to content

Commit 1cb245f

Browse files
committed
updates
1 parent e42e83b commit 1cb245f

File tree

13 files changed

+261
-138
lines changed

13 files changed

+261
-138
lines changed

.DS_Store

6 KB
Binary file not shown.

classes/cluster/cluster-rx.tcl

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,37 @@
88
# Get information about the requester from the protocol
99
set proto [ my protocol $protocol ]
1010
set descriptor [ $proto descriptor $chanID ]
11-
12-
if { [dict get $CONFIG remote] == 0 } {
13-
# When we have defined that we only wish to work with local scripts, we will
11+
12+
if { [string is false -strict [dict get $CONFIG remote]] } {
13+
# When we have defined that we only wish to work with local scripts, we will
1414
# check and immediately ignore any data received from outside the localhost
1515
if { ! [dict exists $descriptor local] } {
16-
if { ! [my is_local [dict get $descriptor address]] } {
17-
return
18-
} else { dict set descriptor local 1 }
16+
if { ! [my is_local [dict get $descriptor address]] } {
17+
puts "Received from Non Local Source: $descriptor"
18+
puts [::cluster::packet::decode $packet [self]]
19+
return
20+
} else {
21+
dict set descriptor local 1
22+
}
1923
} else {
20-
if { ! [dict get $descriptor local] } { return }
24+
if { ! [dict get $descriptor local] } {
25+
puts "Received from Non Local Source: $descriptor"
26+
puts [::cluster::packet::decode $packet [self]]
27+
return
28+
}
2129
}
2230
}
23-
24-
# Attempt to decode the received packet.
31+
32+
# Attempt to decode the received packet.
2533
# An empty payload will be returned if we fail to decode the packet for any reason.
2634
set payloads [::cluster::packet::decode $packet [self]]
2735
set payloads_remaining [llength $payloads]
28-
36+
2937
foreach payload $payloads {
3038
if { $payload eq {} || ! [dict exists $payload sid] || [dict get $payload sid] eq $SERVICE_ID } {
3139
# Ignore empty payloads or payloads that we receive from ourselves.
3240
return
3341
}
34-
#puts "rx | $protocol"
3542
# Are we currently listening to the channel that the communication was
3643
# received on?
3744
if { [dict get $payload channel] ni $COMM_CHANNELS } {
@@ -41,17 +48,17 @@
4148
# Called before anything is done with the received payload but after it is
4249
# decoded. $payload may be modified if necessary before it is further evaluated.
4350
try {my run_hook evaluate receive} on error {r} { return }
44-
51+
4552
try {my run_hook channel [dict get $payload channel] receive} on error {r} { return }
46-
53+
4754
#lassign $payload type rchan op ruid system_id service_id protocols flags data
48-
55+
4956
# Provide the data to the matching service to handle and parse. Create the
50-
# service if it does not exist.
57+
# service if it does not exist.
5158
# - If we receive an empty value in return, the received data has been rejected.
5259
set service [my service $proto $chanID $payload $descriptor]
5360
if { $service eq {} } { return }
54-
61+
5562
set protocol [$proto proto]
5663
if { $protocol ne "c" } {
5764
my event channel receive $proto $chanID $service
@@ -66,7 +73,7 @@
6673
}
6774

6875
# A filter is a list of services which should parse / receive the given payload.
69-
# It is used as an insecure way of routing broadcasted data to specific clients
76+
# It is used as an insecure way of routing broadcasted data to specific clients
7077
# When we have not yet created a channel. For example, it can be useful to request
7178
# a group of clients to join a specific channel.
7279
::oo::define ::cluster::cluster method check_filter { filter } {
@@ -78,39 +85,39 @@
7885
}
7986

8087
# Whenever we receive data, we will check to see if the service already exists
81-
# within our cache. If it does, we will return a reference to the service. If
82-
# it doesn't, we will create it then return its reference.
88+
# within our cache. If it does, we will return a reference to the service. If
89+
# it doesn't, we will create it then return its reference.
8390
#
84-
# If a service should not be allowed to communicate with us, we will return an
91+
# If a service should not be allowed to communicate with us, we will return an
8592
# empty string at which point the command should cease to parse the received
8693
# payload immediately.
8794
::oo::define ::cluster::cluster method service { proto chanID payload descriptor } {
8895
set system_id [dict get $payload hid]
8996
set service_id [dict get $payload sid]
90-
97+
9198
# Added Security - if the system id does not match, we dont parse it when only
9299
# accepting local.
93100
if { [dict get $CONFIG remote] == 0 && $system_id ne $SYSTEM_ID } { return }
94-
101+
95102
set uuid ${service_id}@${system_id}
96-
103+
97104
set service ${NS}::services::$uuid
98-
105+
99106
set serviceExists [expr { [info commands $service] ne {} }]
100-
107+
101108
# Call our service eval hook
102109
try {my run_hook evaluate service} on error {r} { return }
103-
110+
104111
if { [string is true $serviceExists] } {
105112
# If our service already exists, we will validate it against the
106113
# received data to determine if we want to allow communication
107-
# with the service. If we validate, then we will return the
114+
# with the service. If we validate, then we will return the
108115
# reference to the service to our handler.
109116
try {my run_hook service validate} on error {r} { return }
110117
if { [$service validate $proto $chanID $payload $descriptor] } { return $service }
111118
} else {
112119
# If we have never seen this service, we will create it. We will check
113-
# it against our security policies and retain it if it is a service we
120+
# it against our security policies and retain it if it is a service we
114121
# are allowed to communicate with. Otherwise it will be destroyed.
115122
try {
116123
set service [::cluster::service create $service [self] $proto $chanID $payload $descriptor]

classes/cluster/cluster-tx.tcl

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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
@@ -23,7 +23,7 @@
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]
@@ -33,7 +33,7 @@
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 {
@@ -46,7 +46,7 @@
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 {
@@ -81,39 +81,39 @@
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
@@ -136,29 +136,29 @@
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
@@ -169,9 +169,9 @@
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
@@ -181,36 +181,37 @@
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 {
@@ -225,4 +226,4 @@
225226
}
226227
}
227228
return [list $success $failed]
228-
}
229+
}

0 commit comments

Comments
 (0)