if { [llength $argv] == 0 || ! [string is integer [lindex $argv 0]] } {
puts stderr \
- "Script should be called with one argument, server's port number:\n\
- ./exec_server 2547"
+ "Script should be called with at least one argument, server's port number:\
+ \n ./exec_server 2547 \[debug\]"
exit 2
}
-set server_port [lindex $argv 0]
-
set debug_output false
-if { [llength $argv] == 2 || [lindex $argv 1] == "debug" } {
+if { [llength $argv] == 2 && [lindex $argv 1] == "debug" } {
set debug_output true
}
+set spremam_conf_fajl false
-#######
-# Exec_Server --
-# Open the server listening socket and enter the Tcl event loop
-#
-# Arguments:
-# port The server's port number
-
-proc Exec_Server {port} {
- set s [socket -server ExecAccept $port]
- vwait forever
-}
#######
-# Exec_Accept --
-# Accept a connection from a new client.
-# This is called after a new socket connection
-# has been created by Tcl.
+# Exec_Accept -- Accept a connection from a new client.
+# This is called after a new socket connection has been created by Tcl.
#
# Arguments:
# sock The new socket connection to the client
#######
# Exec_command --
-# This procedure is called when the server
-# can read data from the client
+# This procedure is called when the server can read data from the client
#
# Arguments:
# sock The socket connection to the client
proc Exec_command {sock} {
- global client server_port debug_output
+ global client server_port debug_output fileId spremam_conf_fajl
# Check end of file or abnormal connection drop,
# then send result of exec command back to the client.
close $sock
puts "Close $client(addr,$sock)"
unset client(addr,$sock)
- } else {
- if { $line == "Kraj" } {
- puts "Kraj rada."
- puts $sock "Kraj"
- close $sock
- exit 0
+ return
+ }
+
+ if { $debug_output } { puts "$server_port/K: >$line<" }
+
+ if { $line == "Kraj" } {
+ puts "Kraj rada."
+ puts $sock "Kraj"
+ close $sock
+ exit 0
+ }
+
+ set command [lindex $line 0]
+ switch -exact $command {
+
+ "create_conf_file" {
+ set conf_fajl [lindex $line 1]
+ if { $debug_output} { puts "Spremam conf fajl: $conf_fajl" }
+ set fileId [open "$conf_fajl" w]
+ ;# dodati provjeru!!!
+ set spremam_conf_fajl true
+ }
+
+ "close_conf_file" {
+ if { $debug_output} { puts "Zatvaram conf fajl" }
+ close $fileId
+ set spremam_conf_fajl false
+ }
+
+ default {
+ if { $spremam_conf_fajl } {
+ catch {eval puts $fileId "$line"} odg
+ if { $debug_output && ($odg != "") } {
+ puts "$server_port/puts greska: $odg"
+ }
+ } else {
+ if { [llength $line] == 0 } { return}
+ catch {eval exec $line} odg
+ if { $debug_output } { puts "$server_port/O: $odg" }
+ puts $sock $odg
}
- if { $debug_output } { puts "$server_port/Kom >$line<" }
- if { [llength $line] == 0 } {return}
- catch {eval exec $line} odg
- if { $debug_output } { puts "$server_port/Odg: $odg" }
- puts $sock $odg
- puts $sock "Kraj"
- ;# prije: flush $sock
- catch {flush $sock} rez
- if { $rez != "" } {
- puts "Socket closed on remote.\n$rez"
- }
+ }
+ }
+ puts $sock "Kraj"
+ catch {flush $sock} rez
+ if { $rez != "" } {
+ puts "Socket closed on remote.\n$rez"
}
}
-Exec_Server $server_port
+#######
+# Open the server listening socket and enter the Tcl event loop
+
+set server_port [lindex $argv 0]
+set s [socket -server ExecAccept $server_port]
+vwait forever