Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dual control of Tcl program? Help!

3 views
Skip to first unread message

Sam Harrison

unread,
Oct 22, 1996, 3:00:00 AM10/22/96
to

I have a large(ish) Tcl/Tk program that is mouse driven.
(I am using Tcl7.5, Tk4.1, Solaris2)

I wish to extend the program so that is can be driven by commands
arriving on a UNIX socket. This must happen concurrently with
mouse control.

I guess that I will call fork() to create a child process. This
child process will block awaiting input from the socket. When
input arrives, the child will interpret it and call the apropriate
procedure in the Tcl/Tk script. When the Tcl procedure completes,
it will return to the child C code which will block on the socket
again, awaiting the next input. All this must happen concurrently
and independantly of user-driven mouse control.

I have thought of two possible solutions, but am not happy with
either:
1. I could have the child process run as a self contained,
separate Tcl application, and use the Tcl send command to invoke
the appropriate procedures in the parent Tcl application. I do
not want to do this as I guess it would be inefficient.
2. I could make the child process provide a "new event source", as
described in the Tcl_CreateEventSource manual page (no need for
two processes if this method is used?). I am not keen on this
approach as it will involve much hacking about of Tcl itself which
I guess will be difficult.

If anybody can suggest a simpler/cleaner/more efficient way of
solving my problem than the above please let me know.
Thanks in advance,

Sam Harrison.
S.Har...@aivru.shef.ac.uk
A.I. Vision Research Unit, Sheffield University

Jeffrey Hobbs

unread,
Oct 22, 1996, 3:00:00 AM10/22/96
to

In article <326D0D...@aivru.shef.ac.uk>,

Sam Harrison <S.Har...@aivru.shef.ac.uk> wrote:
>I wish to extend the program so that is can be driven by commands
>arriving on a UNIX socket. This must happen concurrently with
>mouse control.

No forking necessary! The socket facilities in Tcl7.5+ provide an
easy way to send commands to a Tcl program via sockets that is still
being UI event driven. You can see an example of this in Tetris
(http://www.cs.uoregon.edu/research/tcl/code/tclet/tetris.html),
which accepts commands off the socket (in multi-player mode) while
still functioning just fine.

Anyway, it's a common trick that has a base example at:
http://www.sco.com/Technology/tcl/SocketExample.html

--
Jeffrey Hobbs office: 541.683.7891
Nomad of the 'Net email: jho...@cs.uoregon.edu
URL: http://www.cs.uoregon.edu/~jhobbs/

Marcia Ann Perry

unread,
Oct 29, 1996, 3:00:00 AM10/29/96
to

I wrote a client-server app in C and Tcl/Tk with the server and
client as one program/one process, no threads, no children. I
build my network code on top of TCP (ie., using socket(),bind(),
connect(),listen(), and whatever is appropriate for client or
server functionality). What I did was this: the "server"
part got a "main_socket" by calling socket(), then used
fcntl(main_socket, F_SETFL, O_NDELAY) to make socket non-
blocking, then I used Tcl_CreateFileHandler() with a mask of
TK_READABLE and the name of a C-coded procedure to invoke to
accept "client" connections. When a connection was accepted,
via call to accept(), I did the same thing: accept() returned
"new_sock" which I called fcntl on to make non-blocking, then
called Tcl_CreateFileHandler on new_sock with again a mask of
TK_READABLE and a procedure to invoke that reads/processes the
message, and writes reply. No one blocks and my GUI/mouse
activity can go on simultaneously.

Marcia Perry
map...@orion.sfsu.edu


0 new messages