How to integrate ruby tk/gtk event loop with another kind event loop such as socket server?

Hello,

I needed to write a test/monitoring program for an application that is
written in C++ which sends and receives messages on sockets. Normally I
would write such a test application in most likely C++, java because I need
gui. I have been tying to learn ruby for some time, and it is very much easy
to write all other stuff except the gui part in ruby. I am trying to use tk
as well as gtk for learning purposes.

I am using ruby 1.8.15 (one click installation for windows) about the tk
library that comes with it. I have downloaded and installed
Ruby-GNOME2-0.14.1.for
windows.
<http://ruby-gnome2.sourceforge.jp/#Ruby-GNOME2-0.14.1+released.>
I have perl tk book, and trying to translate it to ruby and use the features
that are explained there. I have noticed that very crucial notebook widget
is not available. I have also noticed that some of the other features such
as binding external events and geometry managers except pack manager that
are available in perl tk are not available in ruby binding. Even such a
small application, this makes tk almost unusable as gui toolkit. Besides I
could not find a way to integrate an external event loop or process to Tk
eventloop. There are some explanations about filevent and tk send interface
which iseemed not available in windows and in ruby.

I find gtk much more convenient especially with the use of glade ide and
libglade ruby binding. Even if know almost nothing about gtk, by using glade
interface designer and libglade binding, I was able to create quite
sophisticated well looking window with necessary events handled without
writing a line of ruby code. On the other hand, I have not been able to
figure out how to integrate a socket server to Gtk event loop.

To make to long story short, I need help about integration of a socket
server process/thread to Gtk and Tk event loop. How can I hook a callback to
these event loops so that when a new data is available the callback is
called automatically.

I expect a high volume of network traffic so I need to decouple the gui
event loop and the server process.

Any help much appreciated and thanks in advance.

TaO

Maybe, the followings are not answers which you want.

Tcl/Tk libraries with One-Click Ruby Installer 182-15 is
only standard libraries of Tcl/Tk.
Perl/Tk uses the 'ptk' library which includes some mega widgets
from some Tcl/Tk extensions.
Ruby/Tk supports such extensions by libraries on "tkextlib/*".
But, to use such extionstions, those are available on Tcl/Tk.

I recommend you to get ActiveTcl binary package from ActiveState
site (http://www.activestate.com/\), and use One-Click Ruby
Installer 184-16 rc1.
ActiveTcl binary package inlcudes much extensions than 'ptk'.
Of course, it includes some extensions with a notebook widget.

# Current latest release of Tcl/Tk is 8.4.13.
# Although I don't test it yet, probably, it will work with Ruby/Tk.
# In the near future, I'll check the changes between 8.4.12 and
# 8.4.13, and new features included in ActiveTcl8.5.0.0 Beta5.
# Before commiting supports for such changes, you may need
# Tk.tk_call() method to use new features.

By the way, I cannot understand the following part of your mail.

···

From: "Talha Oktay" <toktay@gmail.com>
Subject: how to integrate ruby tk/gtk event loop with another kind event loop such as socket server?
Date: Mon, 24 Apr 2006 17:40:02 +0900
Message-ID: <aaeca74d0604240139u748ccc96o50086cad74442729@mail.gmail.com>

                  I have also noticed that some of the other features such
as binding external events and geometry managers except pack manager that
are available in perl tk are not available in ruby binding.

I think those are supported on Ruby/Tk.
Please tell me the case which you have troubles.
Did you see the Ruby/Tk examples on Ruby source tree?
Those are also included in One-Click Ruby Installer.

                                                                 Besides I
could not find a way to integrate an external event loop or process to Tk
eventloop. There are some explanations about filevent and tk send interface
which iseemed not available in windows and in ruby.

Old Ruby/Tk had some troubles when running an eventloop on non-main
thread. But current Ruby/Tk is safe for Ruby's threads.
There is no problem, even if an eventloop works on non-main thread
or the other threads controll the Tk interpreter.
So, it is better to create a thread for I/O operations.
However, on Windows, I heard that there are some I/O operations
blocks thread-switching.

# That is the reason of why the sample 'irbtk.rb' ('wish'-like(?) IRB)
# doesn't work properly on Windows.

Do you say about such case?
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

I expect a high volume of network traffic so I need to decouple the gui
event loop and the server process.

Any help much appreciated and thanks in advance.

The following is probably insufficient for your high performance
situation, but for more forgiving situations:

Tk.after_idle {...}

is an easy way to have your own code run during the event loop. If Tk
has nothing to do then it runs your block. It only does this once,
but:

p = proc {print "Hi!\n";Tk.after_idle(&p)}
Tk.after_idle(p)

···

--

All the geometry managers from Tk are available in Ruby/Tk. What
problems are you running into?

obj.pack
obj.place
obj.grid

as a backend. Similar to how Tk originally used X11 as a backend.

Good luck,
c.

as a backend. Similar to how Tk originally used X11 as a backend.

Woops, sorry about this. It slipped in from an earlier draft. Please ignore.

Message-ID: <b40ff0a0604240845m5c89dad5j1f53f6eaa3a38b20@mail.gmail.com>

p = proc {print "Hi!\n";Tk.after_idle(&p)}
Tk.after_idle(p)

Or, TkTimer.new(:idle, -1, proc{print "Hi!\n"}).start

···

From: "Chris Alfeld" <chris.alfeld@gmail.com>
Subject: Re: how to integrate ruby tk/gtk event loop with another kind event loop such as socket server?
Date: Tue, 25 Apr 2006 00:45:46 +0900
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

Thank you all for all yours posts. I really appreciate. I was wrong about
geometry managers. I guess the form geometry manager is not in standard Tk
distribution.

However, I have stopped my parallel development on Tk, and I will try
information you provided some other time for learning purposes. I continue
my development using Gtk binding. So far, I am quite pleased with the
documentation, tutorials and available GTK tools existing. However my
question is still pending about integration to Gtk main loop.
I had following observations in windows platform about using threads.
In windows platform, threads are not being scheduled against gtk main loop(
this is also valid for tk main loop as well). When I open a socket and bind
it in the thread, it sits there. Gtk main loop does not leave control to the
thread. As fork is not available in windows platforms I have downloaded the
win32-utils process module and tying to use that but could not be successful
so far. Mostly due to my lack of understanding the parallel programming
using processes I guess. I am much accustomed to using threads.

Again thank you all.

···

On 4/24/06, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:

From: "Chris Alfeld" <chris.alfeld@gmail.com>
Subject: Re: how to integrate ruby tk/gtk event loop with another kind
event loop such as socket server?
Date: Tue, 25 Apr 2006 00:45:46 +0900
Message-ID: <b40ff0a0604240845m5c89dad5j1f53f6eaa3a38b20@mail.gmail.com>
> p = proc {print "Hi!\n";Tk.after_idle(&p)}
> Tk.after_idle(p)

Or, TkTimer.new(:idle, -1, proc{print "Hi!\n"}).start
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Hi,

Hi,

Thank you all for all yours posts. I really appreciate. I was wrong about
geometry managers. I guess the form geometry manager is not in standard Tk
distribution.

However, I have stopped my parallel development on Tk, and I will try
information you provided some other time for learning purposes. I continue
my development using Gtk binding. So far, I am quite pleased with the
documentation, tutorials and available GTK tools existing. However my
question is still pending about integration to Gtk main loop.
I had following observations in windows platform about using threads.
In windows platform, threads are not being scheduled against gtk main loop(
this is also valid for tk main loop as well). When I open a socket and bind
it in the thread, it sits there. Gtk main loop does not leave control to the
thread. As fork is not available in windows platforms I have downloaded the
win32-utils process module and tying to use that but could not be successful
so far. Mostly due to my lack of understanding the parallel programming
using processes I guess. I am much accustomed to using threads.

Again thank you all.

How about Gtk.idle_add ?

require 'gtk2'

Gtk.init
w = Gtk::Window.new("Test")
w.signal_connect("destroy"){Gtk.main_quit}
w.show_all
Gtk.idle_add do
  p Time.now
  true
end
Gtk.main

···

On Tue, 25 Apr 2006 18:03:35 +0900 "Talha Oktay" <toktay@gmail.com> wrote:

--
.:% Masao Mutoh<mutoh@highway.ne.jp>