1#! /bin/sh
2# -*- tcl -*- \
3exec tclsh "$0" ${1+"$@"}
4
5# @@ Meta Begin
6# Application nnsd 1.0.1
7# Meta platform     tcl
8# Meta summary      Nano Name Service Demon
9# Meta description  This application is a simple demon on top
10# Meta description  of the nano name service facilities
11# Meta subject      {name service} server demon
12# Meta require      {Tcl 8.4}
13# Meta require      comm
14# Meta require      logger
15# Meta require      interp
16# Meta require      nameserv::common
17# Meta require      nameserv::server
18# Meta author       Andreas Kupries
19# Meta license      BSD
20# @@ Meta End
21
22package provide nnsd 1.0.1
23
24# nnsd - Nano Name Service Demon
25# ==== = =======================
26#
27# Use cases
28# ---------
29# 
30# (1)	Run a simple trusted name service on some host.
31#	
32# Command syntax
33# --------------
34#
35# Ad 1) nnsd ?-localonly BOOL? ?-port PORT?
36#
37#       Run the server. If no port is specified the default port 38573
38#       is used to listen for client. The option -localonly determines
39#       what connections are acceptable, local only (default), or
40#       remote connections as well. Local connections are whose
41#       originating from the same host which is running the server.
42#       Remote connections come from other hosts.
43
44lappend auto_path [file join [file dirname [file dirname [file normalize [info script]]]] modules]
45
46package require nameserv::server
47
48namespace eval ::nnsd {}
49
50proc ::nnsd::ProcessCommandLine {} {
51    global argv
52
53    # Process the options, perform basic validation.
54
55    while {[llength $argv]} {
56	set opt [lindex $argv 0]
57	if {![string match "-*" $opt]} break
58
59	switch -exact -- $opt {
60	    -localonly {
61		if {[llength $argv] % 2 == 1} Usage
62
63		# Todo: Check boolean 
64		set local [lindex $argv 1]
65		set argv [lrange $argv 2 end]
66
67		nameserv::server::configure -localonly $local
68	    }
69	    -port {
70		if {[llength $argv] % 2 == 1} Usage
71
72		# Todo: Check non-zero unsigned short integer
73		set port [lindex $argv 1]
74		set argv [lrange $argv 2 end]
75
76		nameserv::server::configure -port $port
77	    }
78	    -debug {
79		# Undocumented. Activate the logger services provided
80		# by various packages.
81		logger::setlevel debug
82		set argv [lrange $argv 1 end]
83	    }
84	    default {
85		Usage
86	    }
87	}
88    }
89
90    # Additional validation, and extraction of the non-option
91    # arguments. Of which this application has none.
92
93    if {[llength $argv]} Usage
94
95    return
96}
97
98proc ::nnsd::Usage {} {
99    global argv0
100    puts stderr "$argv0 wrong#args, expected:\
101	    ?-localonly BOOL? ?-port PORT?"
102    exit 1
103}
104
105proc ::nnsd::ArgError {text} {
106    global argv0
107    puts stderr "$argv0: $text"
108    exit 1
109}
110
111proc bgerror {args} {
112    puts stderr $args
113    puts stderr $::errorInfo
114    return
115}
116
117# ### ### ### ######### ######### #########
118## Main
119
120proc ::nnsd::Headline {} {
121    global argv0 
122    set p        [nameserv::server::cget -port]
123    set l [expr {[nameserv::server::cget -localonly]
124		 ? "local only"
125		 : "local & remote"}]
126
127    puts "$argv0 [package require nnsd], listening on $p ($l)"
128    return
129}
130
131proc ::nnsd::Do {} {
132    global argv0 
133
134    ProcessCommandLine
135
136    nameserv::server::start
137    Headline
138
139    vwait forever
140    return
141}
142
143# ### ### ### ######### ######### #########
144## Invoking the functionality.
145
146if {[catch {
147    ::nnsd::Do
148} msg]} {
149    puts $::errorInfo
150    #::nnsd::ArgError $msg
151}
152
153# ### ### ### ######### ######### #########
154exit
155