triggering callback from "outside"

Discussion in 'Perl Misc' started by Hannes, Mar 6, 2004.

  1. Hannes

    Hannes Guest

    Hi,

    does anybody know if it is possible to trigger a callback from the "outside"
    (another process).
    The only method I know of, are (unix) signals, which won't work for me
    (since signal handling is delayed in Perl/Tk MainLoop, and won't work on
    win32 platforms).

    Hannes
     
    Hannes, Mar 6, 2004
    #1
    1. Advertising

  2. Hannes

    Ben Morrow Guest

    Hannes <> wrote:
    > does anybody know if it is possible to trigger a callback from the "outside"
    > (another process).
    > The only method I know of, are (unix) signals, which won't work for me
    > (since signal handling is delayed in Perl/Tk MainLoop, and won't work on
    > win32 platforms).


    It depends on what the receiving process is waiting for... you can use
    sockets (probably best for portablility), fifos... read perldoc perlipc
    for some idea of the possible mechanisms.

    Ben

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces molit animos, tristesque mentes erigit. |
    Musica vel ipsas arbores et horridas movet feras. |
     
    Ben Morrow, Mar 6, 2004
    #2
    1. Advertising

  3. Hannes

    Guest

    Hannes <> wrote:
    > Hi,
    >
    > does anybody know if it is possible to trigger a callback from the
    > "outside" (another process).


    Isn't the point of a callback that the outside process decides when *it*
    wants to trigger it? If you want to tell the outside process what to do,
    isn't that just an ordinary call?

    Maybe I don't understand the question...

    > The only method I know of, are (unix) signals, which won't work for me
    > (since signal handling is delayed in Perl/Tk MainLoop, and won't work on
    > win32 platforms).


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 6, 2004
    #3
  4. Hannes

    Ala Qumsieh Guest

    Hannes wrote:

    > The only method I know of, are (unix) signals, which won't work for me
    > (since signal handling is delayed in Perl/Tk MainLoop, and won't work on
    > win32 platforms).


    As I suggested in comp.lang.perl.tk, why don't you use sockets? Did you
    try that approach?

    I would make the Tk process the server, and listen (in a non-blocking
    manner) at a certain port and wait for data to be read. Then any other
    app can connect to that port and trigger the callback by sending the
    correct data.

    --Ala
     
    Ala Qumsieh, Mar 7, 2004
    #4
  5. Hannes

    Hannes Guest


    > I would make the Tk process the server, and listen (in a non-blocking
    > manner) at a certain port and wait for data to be read. Then any other
    > app can connect to that port and trigger the callback by sending the
    > correct data.


    Is there a way in Perl to get a callback triggered by incoming data?
    Keeping the process busy by polling the non-blocking socket in an
    idle-callback wouldn't be very good...

    Hannes
     
    Hannes, Mar 7, 2004
    #5
  6. Hannes

    Ala Qumsieh Guest

    Hannes wrote:

    > Is there a way in Perl to get a callback triggered by incoming data?
    > Keeping the process busy by polling the non-blocking socket in an
    > idle-callback wouldn't be very good...


    Why not? That's very similar to what Tk::MainLoop() is doing. You have
    to poll to find something (unless you're using signals, but in your case
    you can't).

    For your purposes, I would replace the call to Tk::MainLoop with
    something like the following [untested - adapted from example in perldoc
    IO::Select]:

    use Tk qw/:eventtypes/;
    use IO::Select;
    use IO::Socket;

    my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 12345);
    my $sel = new IO::Select($lsn);

    while (1) { # this is your mainloop
    # process any Tk events like mouse moves/button presses/etc
    1 while $MW->DoOneEvent(ALL_EVENTS|DONT_WAIT);

    # poll for any data in the sockets in a non-blocking way
    if (my @ready = $sel->can_read(0.1)) {
    for my $fh (@ready) {
    if ($fh == $lsn) { # new connection
    my $new = $lsn->accept;
    $sel->add($new);
    } else {
    # somebody sent something
    my $data = <$fh>;
    if (defined $data) {
    #
    # trigger your callback based on what $data contains.
    #
    } else { # hung up
    $sel->remove($fh);
    $fh->close;
    }
    }
    }
    }
    }

    Now your clients can connect to port 12345 on the local machine and send
    whatever data is needed to trigger the callbacks.

    --Ala
     
    Ala Qumsieh, Mar 7, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric B
    Replies:
    1
    Views:
    1,936
    Eric B
    Sep 3, 2003
  2. Karim
    Replies:
    3
    Views:
    460
    Karim
    Nov 26, 2003
  3. Veeresh
    Replies:
    1
    Views:
    1,215
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2004
  4. Nathan Sokalski
    Replies:
    1
    Views:
    610
    miher
    Jun 15, 2009
  5. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    305
    Victor Bazarov
    Feb 7, 2012
Loading...

Share This Page