Last active
July 4, 2018 00:07
-
-
Save anxiousmodernman/606a6fd66aeca4d258a49521bed6568d to your computer and use it in GitHub Desktop.
server.tcl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env jimsh | |
set SOCKET /tmp/ipc.sock | |
set client [socket unix $SOCKET] | |
set msg [list db put foo baz] | |
variable msg2 { | |
db put blah 1 | |
db put blah 99 | |
db get blah | |
} | |
puts [list $msg2] | |
$client puts $msg2 | |
$client flush | |
$client gets reply | |
puts $reply | |
$client gets reply | |
puts $reply | |
$client gets reply | |
puts $reply | |
$client close |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env jimsh | |
package require sled | |
sled db /tmp/sled-jimtcl.db | |
set SOCKET /tmp/ipc.sock | |
if {[file exists $SOCKET]} { | |
puts "Error: socket exists. Removing..." | |
file delete $SOCKET | |
} | |
proc serve {path} { | |
variable f {} | |
set f [socket unix.server $path] | |
$f readable { | |
set client [$f accept] | |
puts "accepting conn..." | |
set cmds [list] | |
while {[$client gets buf] > -1} { | |
if {[string length [string trim $buf]] > 0} { | |
puts "received: $buf" | |
set computed [eval $buf] | |
puts "computed: $computed" | |
$client puts $computed | |
} | |
} | |
$client close | |
} | |
# We block here. | |
# A call to `vwait` enters the eventloop. `vwait` processes | |
# events until the named (global) variable changes or all | |
# event handlers are removed. The variable need not exist | |
# beforehand. If there are no event handlers defined, `vwait` | |
# returns immediately. | |
vwait done | |
} | |
signal handle SIGINT | |
try -signal { | |
serve $SOCKET | |
vwait done | |
} on signal {sig} { | |
# we aren't checking what $sig is, since we only handle SIGINT | |
file delete $SOCKET | |
puts "\nGoodbye." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment