<commit_before>#
# Ctable Server 
#
# $Id$
#

package require Tclx
package require ctable_net

namespace eval ::ctable_server {
  variable registeredCtables
  variable evalEnabled 1

#
# Variables for "shutdown" command
#
  variable shuttingDown 0
  variable clientList {}

#
# register - register a table for remote access
#
proc register {ctableUrl localTableName {type ""}} {
    variable registeredCtables

    lassign [::ctable_net::split_ctable_url $ctableUrl] host port dir exportedTableName options

    serverlog "register $ctableUrl $exportedTableName $type"
    set registeredCtables($exportedTableName) $localTableName

    start_server $port
}

#
# register_instantiator - register a ctable creator
#
proc register_instantiator {cTable} {
    variable registeredCtableCreators

    set registeredCtableCreators($cTable) ""
}

proc register_redirect {ctableUrl redirectedToCtableUrl} {
    variable registeredCtableRedirects

    lassign [::ctable_net::split_ctable_url $ctableUrl] host port dir table options
    start_server $port

    serverlog "register redirect table $table to $redirectedToCtableUrl"
    set registeredCtableRedirects($table:$port) $redirectedToCtableUrl
}

#
# start_server - setup  server socket
#
proc start_server {{port 11111}} {
    variable portSockets

    if {[info exists portSockets($port)]} {
	return
    }
    set serverSock [socket -server ::ctable_server::accept_connection $port]

    set portSockets($port) $serverSock
}

#
# shutdown_servers - close server sockets and flag the system to shut down
#
proc shutdown_servers {} {
    variable portSockets
    variable shuttingDown

    foreach {port sock} [array get portSockets] {
	close $sock
	unset portSockets($port)
    }
    set shuttingDown 1
}

#
# accept_connection - accept a client connection
#
proc accept_connection {sock ip port} {
    variable clientList
    lappend clientList $sock $ip $port
    serverlog "connect from $sock $ip $port"

    set theirPort [lindex [fconfigure $sock -sockname] 2]

    fconfigure $sock -blocking 0 -translation auto
    fileevent $sock readable [list ::ctable_server::remote_receive $sock $theirPort]

    puts $sock [list ctable_server 1.0 ready]
    flush $sock
}

#
# remote_receive - receive data from the remote side
#
proc remote_receive {sock myPort} {
    global errorCode errorInfo
    variable registeredCtableRedirects
    variable ctableUrlCache

    if {[eof $sock]} {
	variable clientList
	variable shuttingDown
	set i [lsearch $clientList $sock]
	if {$i >= 0} {
	    set j [expr $i + 2]
	    set clientList [lreplace $clientList $i $j {}]
	}
	serverlog "EOF on $sock, closing"
	close $sock
	if {$shuttingDown && [llength $clientList] == 0} {
	    exit 0
	}
	return
    }

    if {[gets $sock line] >= 0} {
	lassign $line ctableUrl line 

	if {![info exists ctableUrlCache($ctableUrl)]} {
	    lassign [::ctable_net::split_ctable_url $ctableUrl] host port dir table options

	    if {[info exists registeredCtableRedirects($table:$myPort)]} {
#puts "sending redirect to $sock, $ctableUrl -> $registeredCtableRedirects($table:$myPort)"<commit_msg>Fix bugs in "$ctable shutdown".<commit_after>#
# Ctable Server 
#
# $Id$
#

package require Tclx
package require ctable_net

namespace eval ::ctable_server {
  variable registeredCtables
  variable evalEnabled 1

#
# Variables for "shutdown" command
#
  variable shuttingDown 0
  variable clientList {}

#
# register - register a table for remote access
#
proc register {ctableUrl localTableName {type ""}} {
    variable registeredCtables

    lassign [::ctable_net::split_ctable_url $ctableUrl] host port dir exportedTableName options

    serverlog "register $ctableUrl $exportedTableName $type"
    set registeredCtables($exportedTableName) $localTableName

    start_server $port
}

#
# register_instantiator - register a ctable creator
#
proc register_instantiator {cTable} {
    variable registeredCtableCreators

    set registeredCtableCreators($cTable) ""
}

proc register_redirect {ctableUrl redirectedToCtableUrl} {
    variable registeredCtableRedirects

    lassign [::ctable_net::split_ctable_url $ctableUrl] host port dir table options
    start_server $port

    serverlog "register redirect table $table to $redirectedToCtableUrl"
    set registeredCtableRedirects($table:$port) $redirectedToCtableUrl
}

#
# start_server - setup  server socket
#
proc start_server {{port 11111}} {
    variable portSockets

    if {[info exists portSockets($port)]} {
	return
    }
    set serverSock [socket -server ::ctable_server::accept_connection $port]

    set portSockets($port) $serverSock
}

#
# shutdown_servers - close server sockets and flag the system to shut down
#
proc shutdown_servers {} {
    variable portSockets
    variable shuttingDown

    foreach {port sock} [array get portSockets] {
	close $sock
	unset portSockets($port)
    }
    set shuttingDown 1
}

#
# accept_connection - accept a client connection
#
proc accept_connection {sock ip port} {
    variable clientList
    lappend clientList $sock $ip $port
    serverlog "connect from $sock $ip $port"

    set theirPort [lindex [fconfigure $sock -sockname] 2]

    fconfigure $sock -blocking 0 -translation auto
    fileevent $sock readable [list ::ctable_server::remote_receive $sock $theirPort]

    puts $sock [list ctable_server 1.0 ready]
    flush $sock
}

#
# remote_receive - receive data from the remote side
#
proc remote_receive {sock myPort} {
    global errorCode errorInfo
    variable registeredCtableRedirects
    variable ctableUrlCache

    if {[eof $sock]} {
	variable clientList
	variable shuttingDown
	set i [lsearch $clientList $sock]
	if {$i >= 0} {
	    set j [expr $i + 2]
	    set clientList [lreplace $clientList $i $j]
	}
	serverlog "EOF on $sock, closing"
	close $sock
	if {$shuttingDown} {
	    if {[llength $clientList] == 0} {
	        serverlog "All client sockets closed, shutting down"
	        exit 0
	    } else {
		serverlog "Still waiting on [lrange $clientList 0 2] ([llength $clientList])..."
	    }
	}
	return
    }

    if {[gets $sock line] >= 0} {
	lassign $line ctableUrl line 

	if {![info exists ctableUrlCache($ctableUrl)]} {
	    if [catch {
	      lassign [::ctable_net::split_ctable_url $ctableUrl] host port dir table options
	    } err] {
	      serverlog "$ctableUrl: $err"; # $::errorInfo
	      puts $sock [list e $err "" $::errorCode]
	      flush $sock
	      return
	    }

	    if {[info exists registeredCtableRedirects($table:$myPort)]} {
#puts "sending redirect to $sock, $ctableUrl -> $registeredCtableRedirects($table:$myPort)"
{'fraction_non_alphanumeric': 0.09050873196659073, 'fraction_numerical': 0.005466970387243736, 'mean_word_length': 4.008365019011407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '101832', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2000, 'n_words': 624}