-
Notifications
You must be signed in to change notification settings - Fork 2
/
jasperserver.tcl
103 lines (72 loc) · 2.32 KB
/
jasperserver.tcl
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
set jg_server ""
set jg_server_semaphore ""
set jg_server_debug 1
proc debug_puts {msg} {
global jg_server_debug
if {$jg_server_debug eq 1} {
puts $msg
}
}
proc handle_client {sock addr port} {
global jg_server
global jg_server_semaphore
puts "Accepted connection from [fconfigure $sock -peername] on port $port"
global env; puts $env(PATH)
fconfigure $sock -buffering line -encoding utf-8
while {[gets $sock action] != -1} {
puts "Received action: $action"
if {$action eq "CLOSE"} {
# Close current connection
close $sock
puts "Connection closed."
return
} elseif {$action eq "SHUTDOWN"} {
# Close the current connection and server
close $sock
puts "Connection closed."
jg_stop_server
return
} elseif {$action eq "EVAL"} {
puts "Getting command."
if {[gets $sock command] != -1} {
puts "Received command: $command"
set error [catch {eval $command} result]
puts "Result: $result"
puts "Error: $error"
if {$error == 0} {
# Send non-error return code
set errorstr "0"
} else {
# Send error code
set errorstr "1"
}
set reslen [string length $result]
set lenstr [format "%08x" $reslen]
puts "Sending result length: $lenstr"
puts "Sending result: $result"
puts $sock $errorstr$lenstr$result
} else {
puts "Error reading command."
}
}
}
close $sock
puts "Connection closed."
}
proc jg_start_server {port} {
global jg_server
global jg_server_semaphore
# Create a server socket
set jg_server [socket -server handle_client $port]
puts "Server started on port $port"
# fconfigure $server_socket -blocking 1 ;# Set the socket to blocking mode
# Wait for the server to be closed
vwait jg_server_semaphore
close $jg_server
puts "Server closed."
}
proc jg_stop_server {} {
global jg_server_semaphore
puts "Setting semaphore."
set jg_server_semaphore "closed"
}