public inbox for guile-gtk@sourceware.org
 help / color / mirror / Atom feed
* select+read on socket in gtk,guile-gtk
@ 2000-06-19 20:17 Steve Tell
  2000-06-20 21:25 ` select+read on socket in guile,guile-gtk Steve Tell
  2000-06-21 15:27 ` select+read on socket in gtk,guile-gtk Gary Houston
  0 siblings, 2 replies; 5+ messages in thread
From: Steve Tell @ 2000-06-19 20:17 UTC (permalink / raw)
  To: Guile-GTK List, Guile Mailing List

Does guile-gtk provide a way to hook (file descriptors from) guile ports
into the Gtk+ select loop?   Glib's GIOChannel's perhaps?
I know I could fall back on gtk-idle-add, but it seems less clean.

Next question then is, how do I do a non-blocking read on a guile port,
returning as many characters as are available right now?  Is there an
interface to the raw read(2) system call, like perl's sysread()?

What I'm ultimately trying to do is arrange for a procedure to get called
with complete lines read from a connected TCP socket, so that information
recieved from the server at the other end can be used to update a
items in a guile-gtk interface.

In C its easy:
	fd = socket()
	conect(fd  ... )
	/* use my select, or hook into fd_set of another select() caller */
	
	n = read(fd, rbuf, sizeof(rbuf))
	/* scan buffer, looking for newlines.  When a whole line is
	found, dispatch to line-parsing function.  Save partial
	lines for next time around */

I've already built a version of this in C hooked to guile for an app that
already has extensive C routines linked in, but I'd rather do a little
desk-widget companion in pure guile-gtk if possible.  
	Guile already has all of the the socket operations, so I suspect
I'm just overlooking somthing.


thanks,
Steve
steve@telltronics.org






^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: select+read on socket in guile,guile-gtk
  2000-06-19 20:17 select+read on socket in gtk,guile-gtk Steve Tell
@ 2000-06-20 21:25 ` Steve Tell
  2000-06-21 21:50   ` Steve Tell
  2000-06-21 15:27 ` select+read on socket in gtk,guile-gtk Gary Houston
  1 sibling, 1 reply; 5+ messages in thread
From: Steve Tell @ 2000-06-20 21:25 UTC (permalink / raw)
  To: Guile-GTK List

Its probably bad form to follow up my own question, but I realized that
there might be another way to do what I'm trying to do...

if guile's cooperative threads play nice with guile-gtk -
Can I create and maniuplate Gtk+ GUIs from any thread?

thanks again,
Steve

On Mon, 19 Jun 2000, Steve Tell wrote:

> 
> Does guile-gtk provide a way to hook (file descriptors from) guile ports
> into the Gtk+ select loop?   Glib's GIOChannel's perhaps?
> I know I could fall back on gtk-idle-add, but it seems less clean.
> 
> Next question then is, how do I do a non-blocking read on a guile port,
> returning as many characters as are available right now?  Is there an
> interface to the raw read(2) system call, like perl's sysread()?
> 
> What I'm ultimately trying to do is arrange for a procedure to get called
> with complete lines read from a connected TCP socket, so that information
> recieved from the server at the other end can be used to update a
> items in a guile-gtk interface.
> 
> In C its easy:
> 	fd = socket()
> 	conect(fd  ... )
> 	/* use my select, or hook into fd_set of another select() caller */
> 	
> 	n = read(fd, rbuf, sizeof(rbuf))
> 	/* scan buffer, looking for newlines.  When a whole line is
> 	found, dispatch to line-parsing function.  Save partial
> 	lines for next time around */
> 
> I've already built a version of this in C hooked to guile for an app that
> already has extensive C routines linked in, but I'd rather do a little
> desk-widget companion in pure guile-gtk if possible.  
> 	Guile already has all of the the socket operations, so I suspect
> I'm just overlooking somthing.
> 
> 
> thanks,
> Steve
> steve@telltronics.org
> 
> 
> 
> 
> 
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: select+read on socket in gtk,guile-gtk
  2000-06-19 20:17 select+read on socket in gtk,guile-gtk Steve Tell
  2000-06-20 21:25 ` select+read on socket in guile,guile-gtk Steve Tell
@ 2000-06-21 15:27 ` Gary Houston
  1 sibling, 0 replies; 5+ messages in thread
From: Gary Houston @ 2000-06-21 15:27 UTC (permalink / raw)
  To: tell; +Cc: guile-gtk, guile

> Date: Mon, 19 Jun 2000 23:16:50 -0400 (EDT)
> From: Steve Tell <tell@telltronics.org>
> 
> Does guile-gtk provide a way to hook (file descriptors from) guile ports
> into the Gtk+ select loop?   Glib's GIOChannel's perhaps?
> I know I could fall back on gtk-idle-add, but it seems less clean.

I don't know anything about this.

> Next question then is, how do I do a non-blocking read on a guile port,
> returning as many characters as are available right now?  Is there an
> interface to the raw read(2) system call, like perl's sysread()?

There's no sysread procedure for ports in general, but recv! and 
recvfrom! can be used on socket ports.

A general sysread seems like a reasonable thing to want.  It probably
needs to be implemented as a member of the ptob structure, so that it
can work as expected for unbuffered ports.

To avoid blocking when no bytes are available at all, you'd also need
to put the port into non-blocking mode:

(define p (pipe))
(define pr (car p))
(fcntl pr F_SETFL (logior O_NONBLOCK (fcntl pr F_GETFL)))

(read-char pr) =>

standard input:7:1: In procedure read-char in expression (read-char (car p)):
standard input:7:1: Resource temporarily unavailable
ABORT: (system-error)

> What I'm ultimately trying to do is arrange for a procedure to get called
> with complete lines read from a connected TCP socket, so that information
> recieved from the server at the other end can be used to update a
> items in a guile-gtk interface.

read-line or read-delimited could probably do it, but would block.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: select+read on socket in guile,guile-gtk
  2000-06-20 21:25 ` select+read on socket in guile,guile-gtk Steve Tell
@ 2000-06-21 21:50   ` Steve Tell
  2000-06-22  2:19     ` Mikael Djurfeldt
  0 siblings, 1 reply; 5+ messages in thread
From: Steve Tell @ 2000-06-21 21:50 UTC (permalink / raw)
  To: Guile-GTK List, Guile Mailing List

While mulling over the comments from Chris C. and Gary H. (thanks!), and
poking about the source code preparing to write some guile port
extensions, I found a couple of read-made ways to accomplish what I need
for now.

On Wed, 21 Jun 2000, I wrote:

> if guile's cooperative threads play nice with guile-gtk -
> Can I create and maniuplate Gtk+ GUIs from any thread?

Turns out that with a little fiddling, this works after a fashion;
example program attached.

> On Mon, 19 Jun 2000, I wrote:
> > Does guile-gtk provide a way to hook (file descriptors from) guile ports
> > into the Gtk+ select loop?   Glib's GIOChannel's perhaps?

Turns out that Gtk+ has such a facility, and guile-gtk already exposes it:  
gtk-input-add

> > What I'm ultimately trying to do is arrange for a procedure to get called
> > with complete lines read from a connected TCP socket, so that information
> > recieved from the server at the other end can be used to update
> > items in a guile-gtk interface.

Two short example programs in this direction are attached, tested with a
freshly-compiled guile-1.4 and guile-gtk-0.18 (with Greg Badros' patch for
smob creation)

The first one coaxes guile cooperative threads and guile-gtk into running
together.  It ends up burning a lot of CPU time; probably both gtk and
guile are doing nonblocking reads and yielding back and forth.  
The second uses gtk-input-add.
Along the way, both illustrate creating a simple TCP client.

Steve


#!/tmp/ggtest/bin/guile-gtk -s
!#
;
; Test 3 using guile to write a simple network client
; that displays results in a Gtk+ window.
; This one uses guile cooperative threads.
;
(use-modules (ice-9 threads))
(use-modules (gtk gtk))

(read-set! keywords 'prefix)

(define (print-to-port port . l)
    (for-each (lambda (elem) (display elem port)) l))
(define (print . l)
    (apply print-to-port (cons (current-output-port) l)))

; Use any TCP server that periodicly spits back lines of text.
(define IRMP3_PORT 9232)
(define IRMP3_HOST "localhost")

(define mysock (socket AF_INET SOCK_STREAM 0))
(connect mysock AF_INET (car (hostent:addr-list 
			      (gethostbyname IRMP3_HOST)))
	 IRMP3_PORT)

(define (process-lines p lab) ; loop forever reading and processing lines
  (do ((line (read-line mysock) (read-line mysock)))
      (#f)
    (print "line=\"" line "\"\n")
    (gtk-label-set-text lab line)))

(define (make-button parent txt func) 
  (let* ((btn (gtk-button-new-with-label txt)))
    (gtk-container-add parent btn)
    (gtk-widget-show btn)
    (gtk-signal-connect btn "clicked" func)))

(define (make-label parent txt) 
  (let* ((lab (gtk-label-new txt)))
    (gtk-container-add parent lab)
    (gtk-widget-show lab)
    lab))

(let* ((window (gtk-widget-new 'GtkWindow
			       :type         'toplevel
			       :title        "hello world"
			       :GtkContainer::border_width 10))
       (vbox (gtk-vbox-new #f 10))
       (statlabel (make-label vbox "(no status yet)")))

  (make-button vbox "test me" (lambda () (display "tested.") (newline)))
  (make-button vbox "quit" (lambda () (gtk-widget-destroy window)))
  (gtk-container-add window vbox)
  (gtk-widget-show vbox)
  (gtk-widget-show window)

  (call-with-new-thread (lambda ()
			  (print "thread started\n")
			  (process-lines mysock statlabel)
			  (print "process-lines returns\n")
			  (close-port mysock))
			(lambda (e) (print "thread error " e "\n")))

; Unless we call guile's thread-yield in the gtk idle loop,
; our thread doesn't run.    And unless we call (gtk-update) inside the
; idle procedure, the gtk stuff never gets drawn properly.
; Somthing tells me this isn't the way things are supposed to work...
  (gtk-idle-add (lambda ()
		  (yield)
		  (gtk-update)
		  ))
  (gtk-standalone-main window))

; end of net3g.scm


#!/tmp/ggtest/bin/guile-gtk -s
!#
;
; Test 4 using guile to write a simple network client
; that displays results in a Gtk+ window
; This one uses gtk-input-add
;

(use-modules (gtk gtk))
(use-modules (gtk gdk))
(read-set! keywords 'prefix)

; Use any TCP server that periodicly spits back lines of text.
(define IRMP3_PORT 9232)
(define IRMP3_HOST "localhost")

(define mysock (socket AF_INET SOCK_STREAM 0))

(connect mysock AF_INET (car (hostent:addr-list 
			      (gethostbyname IRMP3_HOST)))
	 IRMP3_PORT)

(define (read-process-line p lab) ; process single line recieved over socket
  (let ((line (read-line p)))
    (gtk-label-set-text lab line)
    ))

(define (make-button parent txt func) 	; gtk helpers
  (let* ((btn (gtk-button-new-with-label txt)))
    (gtk-container-add parent btn)
    (gtk-widget-show btn)
    (gtk-signal-connect btn "clicked" func)))

(define (make-label parent txt) 
  (let* ((lab (gtk-label-new txt)))
    (gtk-container-add parent lab)
    (gtk-widget-show lab)
    lab))

(let* ((window (gtk-widget-new 'GtkWindow
			       :type         'toplevel
			       :title        "hello world"
			       :GtkContainer::border_width 10))
       (vbox (gtk-vbox-new #f 10))
       (statlabel (make-label vbox "(no status yet)")))

  (make-button vbox "test me" (lambda () (display "tested.") (newline)))
  (make-button vbox "quit" (lambda () (gtk-widget-destroy window)))
  (gtk-container-add window vbox)
  (gtk-widget-show vbox)
  (gtk-widget-show window)
  
  (gtk-input-add mysock
		 1	; should be 'read  (for GDK_INPUT_READ)
			; but that causes errors  
		 (lambda (source condition)
		   (read-process-line mysock statlabel)
		   ))

  (gtk-standalone-main window))

; end of net4g.scm


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: select+read on socket in guile,guile-gtk
  2000-06-21 21:50   ` Steve Tell
@ 2000-06-22  2:19     ` Mikael Djurfeldt
  0 siblings, 0 replies; 5+ messages in thread
From: Mikael Djurfeldt @ 2000-06-22  2:19 UTC (permalink / raw)
  To: Steve Tell; +Cc: djurfeldt

(Sorry for not having read your messages in detail yet.)

If you want to use GTk in a COOP-threaded application, probably the
best way to do it is to plug in COOP-threads into GLIB.  Then you can
run the GTk handler in one thread, but still freely accees GTk from
any thread:

  cvs -d :pserver:anoncvs@anoncvs.cygnus.com:/cvs/guile login
  cvs -z 9 -d :pserver:anoncvs@anoncvs.cygnus.com:/cvs/guile \
    checkout guile/guile-gtkthreads

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2000-06-22  2:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-06-19 20:17 select+read on socket in gtk,guile-gtk Steve Tell
2000-06-20 21:25 ` select+read on socket in guile,guile-gtk Steve Tell
2000-06-21 21:50   ` Steve Tell
2000-06-22  2:19     ` Mikael Djurfeldt
2000-06-21 15:27 ` select+read on socket in gtk,guile-gtk Gary Houston

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).