/usr/share/tcltk/tcllib1.14/nns/nns.tcl is in tcllib 1.14-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | # -*- tcl -*-
# ### ### ### ######### ######### #########
## Name Service - Client side access
# ### ### ### ######### ######### #########
## Requirements
package require Tcl 8.4
package require comm ; # Generic message transport
package require interp ; # Interpreter helpers.
package require logger ; # Tracing internal activity
package require nameserv::common ; # Common/shared utilities
package require snit ; # OO support, for streaming search class
package require uevent ; # Generate events for connection-loss
namespace eval ::nameserv {}
# ### ### ### ######### ######### #########
## API: Write, Read, Search
proc ::nameserv::bind {name data} {
# Registers this application at the configured name service under
# the specified name, and provides a value.
#
# Note: The application is allowed register multiple names.
#
# Note: A registered name is automatically removed by the server
# when the connection to it collapses.
DO Bind $name $data
return
}
proc ::nameserv::release {} {
# Releases all names the application has registered at the
# configured name service.
DO Release
return
}
proc ::nameserv::search {args} {
# Searches the configured name service for applications whose name
# matches the given pattern. Returns a dictionary mapping from the
# names to the data they provided at 'bind' time.
# In continuous and async modes it returns an object whose
# contents reflect the current set of matching entries.
array set a [search-parseargs $args]
upvar 0 a(oneshot) oneshot
upvar 0 a(continuous) continuous
upvar 0 a(pattern) pattern
if {$continuous} {
variable search
# This client uses the receiver object as tag for the search
# in the service. This is easily unique, and makes dispatch of
# incoming results later easy too.
set receiver [receiver %AUTO% $oneshot]
if {[catch {
ASYNC Search/Continuous/Start $receiver $pattern
} err]} {
# Release the allocated object to prevent a leak, then
# rethrow the error.
$receiver destroy
return -code error $err
}
set search($receiver) .
return $receiver
} else {
return [DO Search $pattern]
}
}
proc ::nameserv::protocol {} {
return 1
}
proc ::nameserv::server_protocol {} {
return [DO ProtocolVersion]
}
proc ::nameserv::server_features {} {
return [DO ProtocolFeatures]
}
# ### ### ### ######### ######### #########
## semi-INT: search argument processing.
proc ::nameserv::search-parseargs {arguments} {
# This command is semi-public. It is not documented for public
# use, however the package nameserv::auto uses as helper in its
# implementation of the search command.
switch -exact [llength $arguments] {
0 {
set oneshot 0
set continuous 0
set pattern *
}
1 {
set opt [lindex $arguments 0]
if {$opt eq "-continuous"} {
set oneshot 0
set continuous 1
set pattern *
} elseif {$opt eq "-async"} {
set oneshot 1
set continuous 1
set pattern *
} else {
set oneshot 0
set continuous 0
set pattern $opt
}
}
2 {
set opt [lindex $arguments 0]
if {$opt eq "-continuous"} {
set oneshot 0
set continuous 1
set pattern [lindex $arguments 1]
} elseif {$opt eq "-async"} {
set oneshot 1
set continuous 1
set pattern [lindex $arguments 1]
} else {
return -code error "wrong\#args: Expected ?-continuous|-async? ?pattern?"
}
}
default {
return -code error "wrong\#args: Expected ?-continuous|-async? ?pattern?"
}
}
return [list oneshot $oneshot continuous $continuous pattern $pattern]
}
# ### ### ### ######### ######### #########
## INT: Communication setup / teardown / use
proc ::nameserv::DO {args} {
variable sid
log::debug [linsert $args end @ $sid]
if {[catch {
[SERV] send $sid $args
#eval [linsert $args 0 [SERV] send $sid] ;# $args
} msg]} {
if {[string match "*refused*" $msg]} {
return -code error "No name server present @ $sid"
} else {
return -code error $msg
}
}
# Result of the call
return $msg
}
proc ::nameserv::ASYNC {args} {
variable sid
log::debug [linsert $args end @ $sid]
if {[catch {
[SERV] send -async $sid $args
#eval [linsert $args 0 [SERV] send $sid] ;# $args
} msg]} {
if {[string match "*refused*" $msg]} {
return -code error "No name server present @ $sid"
} else {
return -code error $msg
}
}
# No result to return
return
}
proc ::nameserv::SERV {} {
variable comm
variable sid
variable host
variable port
if {$comm ne ""} {return $comm}
# NOTE
# -local 1 means that clients can only talk to a local
# name service. Might make sense to auto-force
# -local 0 for host ne "localhost".
set interp [interp::createEmpty]
foreach msg {
Search/Continuous/Change
} {
interp alias $interp $msg {} ::nameserv::$msg
}
set sid [list $port $host]
set comm [comm::comm new ::nameserv::CSERV \
-interp $interp \
-local 1 \
-listen 1]
$comm hook lost ::nameserv::LOST
log::debug [list SERV @ $sid : $comm]
return $comm
}
proc ::nameserv::LOST {args} {
upvar 1 id id chan chan reason reason
variable comm
variable sid
variable search
log::debug [list LOST @ $sid - $reason]
$comm destroy
set comm {}
set sid {}
# Notify async/cont search of the loss.
foreach r [array names search] {
$r DATA stop
unset search($r)
}
uevent::generate nameserv lost-connection [list reason $reason]
return
}
# ### ### ### ######### ######### #########
## Initialization - System state
namespace eval ::nameserv {
# Object command of the communication channel to the server.
# If present re-configuration is not possible. Also the comm
# id of the server.
variable comm {}
variable sid {}
# Table of active async/cont searches
variable search ; array set search {}
}
# ### ### ### ######### ######### #########
## API: Configuration management (host, port)
proc ::nameserv::cget {option} {
return [configure $option]
}
proc ::nameserv::configure {args} {
variable host
variable port
variable comm
if {![llength $args]} {
return [list -host $host -port $port]
}
if {[llength $args] == 1} {
# cget
set opt [lindex $args 0]
switch -exact -- $opt {
-host { return $host }
-port { return $port }
default {
return -code error "bad option \"$opt\", expected -host, or -port"
}
}
}
if {$comm ne ""} {
return -code error "Unable to configure an active connection"
}
# Note: Should -port/-host be made configurable after
# communication has started it will be necessary to provide code
# which retracts everything from the old server and re-initializes
# the new one.
while {[llength $args]} {
set opt [lindex $args 0]
switch -exact -- $opt {
-host {
if {[llength $args] < 2} {
return -code error "value for \"$opt\" is missing"
}
set host [lindex $args 1]
set args [lrange $args 2 end]
}
-port {
if {[llength $args] < 2} {
return -code error "value for \"$opt\" is missing"
}
set port [lindex $args 1]
# Todo: Check non-zero unsigned short integer
set args [lrange $args 2 end]
}
default {
return -code error "bad option \"$opt\", expected -host, or -port"
}
}
}
return
}
# ### ### ### ######### ######### #########
## Receiver for continuous and async searches
proc ::nameserv::Search/Continuous/Change {tag type response} {
# Ignore messages for searches which were canceled already.
#
# Due to the async nature of the messages for cont/async search
# the client may have canceled the receiver object already, sent
# the stop message already, but still has to process search
# results which were already in flight. We ignore them.
if {![llength [info commands $tag]]} return
# This client uses the receiver object as tag, dispatch the
# received notification to it.
$tag DATA $type $response
return
}
snit::type ::nameserv::receiver {
option -command -default {}
constructor {{once 0}} {
set singleshot $once
return
}
destructor {
if {$singleshot} return
::nameserv::ASYNC Search/Continuous/Stop $self
Callback stop {}
return
}
method get {k} {
if {![info exists current($k)]} {return -code error "Unknown key \"$k\""}
return $current($k)
}
method names {} {
return [array names current]
}
method size {} {
return [array size current]
}
method getall {{pattern *}} {
return [array get current $pattern]
}
method filled {} {
return $filled
}
method {DATA stop} {} {
if {$filled && $singleshot} return
set singleshot 1 ; # Prevent 'stop' again during destruction.
Callback stop {}
return
}
method {DATA add} {response} {
set filled 1
if {$singleshot} {
ASYNC Search/Continuous/Stop $self
}
array set current $response
Callback add $response
if {$singleshot} {
Callback stop {}
}
return
}
method {DATA remove} {response} {
set filled 1
foreach {k v} $response {
unset -nocomplain current($k)
}
Callback remove $response
return
}
proc Callback {type response} {
upvar 1 options options
if {$options(-command) eq ""} return
# Defer execution to event loop
after 0 [linsert $options(-command) end $type $response]
return
}
variable singleshot 0
variable current -array {}
variable filled 0
}
# ### ### ### ######### ######### #########
## Initialization - Tracing, Configuration
logger::initNamespace ::nameserv
namespace eval ::nameserv {
# Host and port to connect to, to get access to the nameservice.
variable host localhost
variable port [nameserv::common::port]
namespace export bind release search protocol \
server_protocol server_features configure cget
}
# ### ### ### ######### ######### #########
## Ready
package provide nameserv 0.4.2
##
# ### ### ### ######### ######### #########
|