+++ /dev/null
-# $Id: exec_server.tcl,v 1.6 2007/07/19 03:14:13 marko Exp $
-#! /usr/local/bin/tclsh8.4
-
-#######
-# Script should be called with at least one argument: port number
-# The second (optional) argument is used for debugging
-# ./exec_server port_number [debug]
-# ./exec_server 2547
-
-if { [llength $argv] == 0 || ! [string is integer [lindex $argv 0]] } {
- puts stderr \
- "Script should be called with at least one argument, server's port number:\
- \n ./exec_server 2547 \[debug\]"
- exit 2
-}
-
-set debug_output false
-if { [llength $argv] == 2 && [lindex $argv 1] == "debug" } {
- set debug_output true
-}
-
-set spremam_conf_fajl false
-
-
-#######
-# 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
-# addr The client's IP address
-# port The client's port number
-
-proc ExecAccept {sock addr port} {
- global client
-
- # Record the client's information
- puts "Accept $sock from $addr port $port"
- set client(addr,$sock) [list $addr $port]
-
- fconfigure $sock -buffering full
-
- # Set up a callback for when the client sends data
- fileevent $sock readable [list Exec_command $sock]
-}
-
-#######
-# Exec_command --
-# 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 fileId spremam_conf_fajl
-
- # Check end of file or abnormal connection drop,
- # then send result of exec command back to the client.
-
- if {[eof $sock] || [catch {gets $sock line}]} {
- close $sock
- puts "Close $client(addr,$sock)"
- unset client(addr,$sock)
- return
- }
-
- if { $debug_output } { puts "$server_port/K: >$line<" }
-
- if { $line == "Kraj" } {
- puts "Kraj rada."
- puts $sock "Kraj"
- close $sock
- exit 0
- }
-
-
- if {$spremam_conf_fajl} {
- if { $line == "close_conf_file" } {
- if { $debug_output} { puts "Zatvaram conf fajl" }
- close $fileId
- set spremam_conf_fajl false
- puts $sock "Kraj"
- } else {
- catch {puts $fileId $line} odg
- if { $debug_output && ($odg != "") } {
- puts "$server_port/puts greska: $odg"
- }
- }
- } else {
- if { [lindex $line 0] == "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
-
- } else {
- if { [llength $line] == 0 } { return}
- catch {eval exec $line} odg
- if { $debug_output } { puts "$server_port/O: $odg" }
- puts $sock $odg
- }
-
- puts $sock "Kraj"
- }
- catch {flush $sock} rez
- if { $rez != "" } {
- puts "Socket closed on remote.\n$rez"
- }
-}
-
-#######
-# 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
-