<commit_before>#
#  Titanic service
#
#  Implements server side of http://rfc.zeromq.org/spec:9

lappend auto_path .
package require MDClient 1.0
package require MDWorker 1.0
package require uuid

if {[llength $argv] == 0} {
    set argv [list driver]
} elseif {[llength $argv] != 1} {
    puts "Usage: titanic.tcl <driver|request|reply|close>"
    exit 1
}

set tclsh [info nameofexecutable]
expr {srand([pid])}
set verbose 0

lassign $argv what

set TITANIC_DIR ".titanic"

#  Return a new UUID as a printable character string
proc s_generate_uuid {} {
    return [uuid::uuid generate]
}

#  Returns freshly allocated request filename for given UUID
proc s_request_filename {uuid} {
    return [file join $::TITANIC_DIR $uuid.req]
}

#  Returns freshly allocated reply filename for given UUID
proc s_reply_filename {uuid} {
    return [file join $::TITANIC_DIR $uuid.rep]
}

#  Titanic request service
proc titanic_request {} {

    zmq context context 1
    set pipe [zmq socket pipe context PAIR]
    pipe connect "ipc://titanicpipe.ipc"

    set worker [MDWorker new "tcp://localhost:5555" "titanic.request" $::verbose]
    set reply {}

    while {1} {
        #  Send reply if it's not null
        #  And then get next request from broker
	set request [$worker recv $reply]
	if {[llength $request] == 0} {
	    break ;# Interrupted, exit
	}

        #  Ensure message directory exists
	file mkdir $::TITANIC_DIR

	#  Generate UUID and save message to disk
	set uuid [s_generate_uuid]
	set filename [s_request_filename $uuid]
	set file [open $filename "w"]
	puts -nonewline $file [join $request \n]
	close $file

	#  Send UUID through to message queue
	set reply [list]
	set reply [zmq zmsg_add $reply $uuid]
	zmq zmsg_send $pipe $reply

        #  Now send UUID back to client
        #  Done by the mdwrk_recv() at the top of the loop
	set reply [list]
	puts "I: titanic.request to $uuid / $reply"
	set reply [zmq zmsg_add $reply "200"]
	puts "I: titanic.request to $uuid / $reply"
	set reply [zmq zmsg_add $reply $uuid]
	puts "I: titanic.request to $uuid / $reply"
	puts [join [zmq zmsg_dump $reply] \n]
    }
    $worker destroy
}

#  Titanic reply service
proc titanic_reply {} {
    set worker [MDWorker new "tcp://localhost:5555" "titanic.reply" $::verbose]
    set reply {}

    while {1} {
	set request [$worker recv $reply]
	if {[llength $request] == 0} {
	    break
	}
	set uuid [zmq zmsg_pop request]
	set req_filename [s_request_filename $uuid]
	set rep_filename [s_reply_filename $uuid]
	if {[file exists $rep_filename]} {
	    set file [open $rep_filename r]
	    set reply [split [read $file] \n]
	    set reply [zmq zmsg_push $reply "200"]
	    puts "I: titanic.reply to $uuid"
	    puts [join [zmq zmsg_dump $reply] \n]
	    close $file
	} else {
	    if {[file exists req_filename]} {
		set reply "300"
	    } else {<commit_msg>add forgotten $<commit_after>#
#  Titanic service
#
#  Implements server side of http://rfc.zeromq.org/spec:9

lappend auto_path .
package require MDClient 1.0
package require MDWorker 1.0
package require uuid

if {[llength $argv] == 0} {
    set argv [list driver]
} elseif {[llength $argv] != 1} {
    puts "Usage: titanic.tcl <driver|request|reply|close>"
    exit 1
}

set tclsh [info nameofexecutable]
expr {srand([pid])}
set verbose 0

lassign $argv what

set TITANIC_DIR ".titanic"

#  Return a new UUID as a printable character string
proc s_generate_uuid {} {
    return [uuid::uuid generate]
}

#  Returns freshly allocated request filename for given UUID
proc s_request_filename {uuid} {
    return [file join $::TITANIC_DIR $uuid.req]
}

#  Returns freshly allocated reply filename for given UUID
proc s_reply_filename {uuid} {
    return [file join $::TITANIC_DIR $uuid.rep]
}

#  Titanic request service
proc titanic_request {} {

    zmq context context 1
    set pipe [zmq socket pipe context PAIR]
    pipe connect "ipc://titanicpipe.ipc"

    set worker [MDWorker new "tcp://localhost:5555" "titanic.request" $::verbose]
    set reply {}

    while {1} {
        #  Send reply if it's not null
        #  And then get next request from broker
	set request [$worker recv $reply]
	if {[llength $request] == 0} {
	    break ;# Interrupted, exit
	}

        #  Ensure message directory exists
	file mkdir $::TITANIC_DIR

	#  Generate UUID and save message to disk
	set uuid [s_generate_uuid]
	set filename [s_request_filename $uuid]
	set file [open $filename "w"]
	puts -nonewline $file [join $request \n]
	close $file

	#  Send UUID through to message queue
	set reply [list]
	set reply [zmq zmsg_add $reply $uuid]
	zmq zmsg_send $pipe $reply

        #  Now send UUID back to client
        #  Done by the mdwrk_recv() at the top of the loop
	set reply [list]
	puts "I: titanic.request to $uuid / $reply"
	set reply [zmq zmsg_add $reply "200"]
	puts "I: titanic.request to $uuid / $reply"
	set reply [zmq zmsg_add $reply $uuid]
	puts "I: titanic.request to $uuid / $reply"
	puts [join [zmq zmsg_dump $reply] \n]
    }
    $worker destroy
}

#  Titanic reply service
proc titanic_reply {} {
    set worker [MDWorker new "tcp://localhost:5555" "titanic.reply" $::verbose]
    set reply {}

    while {1} {
	set request [$worker recv $reply]
	if {[llength $request] == 0} {
	    break
	}
	set uuid [zmq zmsg_pop request]
	set req_filename [s_request_filename $uuid]
	set rep_filename [s_reply_filename $uuid]
	if {[file exists $rep_filename]} {
	    set file [open $rep_filename r]
	    set reply [split [read $file] \n]
	    set reply [zmq zmsg_push $reply "200"]
	    puts "I: titanic.reply to $uuid"
	    puts [join [zmq zmsg_dump $reply] \n]
	    close $file
	} else {
	    if {[file exists $req_filename]} {
		set reply "300"
	    } else {
{'fraction_non_alphanumeric': 0.1068114165645246, 'fraction_numerical': 0.010856242339345124, 'mean_word_length': 3.3503427265803505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6773628', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2004, 'n_words': 696}