multithreading - Do I need to use Threads in TCL for my application? -


the application trying build follows.

java client <---> tcl server (say server.tcl) <---> other tcl script (say abc.tcl)

i using socket programming communication between java client , tcl server. requirement call abc.tcl server.tcl @ runtime. abc.tcl while executing send questions java client through server.tcl. user sitting @ java client should send reply questions asked , answer should come abc.tcl. while reply comes abc.tcl should wait. can't have busy waiting loop in abc.tcl control not come server.tcl once talk java client.

abc.tcl using procedures defined in server.tcl. design can changed though if required.

i thinking of using threads in tcl. planning call abc.tcl separate thread, , keep waiting replies. way server.tcl can execute separately abc.tcl.

wanted opinion on better way ? if threads better way, challenges might encounter , how can take care of them ?

edited :

i not using socket communication between server.tcl , abc.tcl. in server.tcl, sourcing abc.tcl , when communication request comes java client, calling procedure (say proc someproc{}) of abc.tcl. in someproc procedure, processing done , question (say "do want continue ?") sent java client through server.tcl. in abc.tcl want procedure someproc wait till user enters something. , when user enters answer (either yes/no), want procedure continue execution point waiting user input.

now happening not able correctly set waiting condition required. tried using flag variable updated when user inputs answer, , tried keeping busy waiting loop in someproc, based on flag variable. when user input comes, not resume execution waiting loop onwards. starts normal request , goes procedure registered in server.tcl handle incoming data.

i have "vwait events" call , have " fileevent $sock readable [list svchandler $sock]" in server.tcl.

i not able get, how can wait in procedure someproc in abc.tcl , resume processing same busy wait, based on user input.

heading ##server.tcl file :

source "<path>/servertest.tcl"   set svcport [lindex $argv 0] set filepath <path related files kept> set replyready 0 set packet ""  proc writetofile {filename data mode} { set fileid [open $filename $mode] puts -nonewline $fileid $data close $fileid }  proc writejavautf {sock msg} {   set date [exec date]   set msg $date$msg   set data [encoding convertto utf-8 $msg]   puts -nonewline $sock [binary format "s" [string length $data]]$data }  proc readjavautf {sock} {   binary scan [read $sock 2] "s" len   set data [read $sock [expr {$len & 0xffff}]]   return [encoding convertfrom utf-8 $data] }  proc sendmessagetouser {sock msg} {       writetofile [exec pwd]/cts/log "\nsending message client" "a"      writejavautf $sock $msg\n }  proc setreplyready {} {         global replyready         set replyready 1         writetofile [exec pwd]/cts/log "\nsetreplyready" "a"  }  proc resetreplyready {} {         global replyready         set replyready 0         writetofile [exec pwd]/cts/log "\nresetreplyready" "a" }  proc getreplyreadystatus {} {         global replyready        writetofile [exec pwd]/cts/log "\nreply ready : $replyready" "a"     return $replyready }   proc executecommand {sock msg} {     writetofile [exec pwd]/cts/log "\nexecutecommand" "a"     runservertest $sock }  # handles input client ,  client shutdown proc  svchandler {sock} {    global packet replyready   set packet [readjavautf $sock] ;# client packet   writetofile [exec pwd]/cts/log "\nthe packet client $packet" "w"  set packet [string range $packet 0 [expr {[string first "\n" $packet] - 1}]]  set endchar "eoc"    if {[eof $sock] || ![string compare -nocase $packet $endchar]} {    ;# client gone or finished       writetofile [exec pwd]/cts/log "closing socket" "a"       writetofile [exec pwd]/cts/flag "0" "w"      close $sock        ;# release servers client channel      exit   } else {     #doservice $sock $ipacket      set typereply "reply"      set typeexecute "exec"      set type [string range $packet 0 [expr {[string first ":" $packet] - 1}]]      writetofile [exec pwd]/cts/log "\nthe type : $type" "a"      # if not reply request      if {![string compare -nocase $type $typeexecute]} {        executecommand $sock $packet      } else {       writetofile [exec pwd]/cts/log "\nexecuting else" "a"        setreplyready      }    } }  proc accept {sock addr port}  {    # setup handler future communication on client socket   fileevent $sock readable [list svchandler $sock]    # read client input in lines, disable blocking i/o   fconfigure $sock -buffering line -blocking 0 -translation binary    return $sock }  set sock [socket -server accept $svcport] vwait events    ;# handle events till variable events set 

heading ## servertest.tcl (as abc.tcl)

proc runservertest {sock} { global replyready writetofile [exec pwd]/cts/log "\ninside servertest.tcl." "a"  resetreplyready sendmessagetouser $sock "from server : socket working. want continue ?" writetofile [exec pwd]/cts/log "\nwaiting user input" "a" #loop untill reply read socket while {![getreplyreadystatus]} { after 1000 }  set retmessage [getpacket] resetreplyready writetofile [exec pwd]/cts/log "\nthe reply user : $retmessage" "a" } 

heading ## challenge in code

i send new request "exec:hello" , send reply "reply:yes"

as can see in proc svchandler{}, if new request execute executecommand{} proc. else, set replyready flag. when execute executecommand{} proc, proc runservertest{} called in servertest.tcl. in proc, send question user, able see on java ui screen on client. , when try send reply, proc setreplyready{} in svchandler{}, not executed @ all. when saw log file, see output of proc getreplyreadystatus{}, being called while loop in proc runservertest{}, after every second. means while loop executing indefinitely. , proc setreplyready{} not getting called svchandler{} may while loop not exiting. don't know process waiting. workaround ?

thanks, peeyush

don't use threads. use event loop.

read on fileevent: http://www.tcl.tk/man/tcl8.6/tclcmd/fileevent.htm

in general set event handlers both java client socket , abc script socket trigger when readable , need when data arrives.

if need implement timed operations, example watchdog timer, check out after command.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -