On Tue, Nov 26, 2002 at 10:09:19AM -0800, Jason W. Martinez wrote:
> Hello,
> 
> I'm new to POE, so please excuse me if I have missed out on the obvious.....
> 
> My goal (eventually) is to write a client application that would send 
> information to a server as well as handle information sent to other clients 
> on a network. I'm involved in a social psych experiment, where interactions 
> will be done through soley through computers.....
> 
> I have managed to figure out most of what I need done, however, I am having 
> trouble with using Tk with the TCP component. In order to test an application 
> I have tinkered around with a sample file which I picked up from Matt 
> Sergeant's slide show paper..... His sample works great. My sample, 
> however.... Could someone please tell me what's wrong with my code? Or, how I 
> may improve upon it.
> 
> I'm using WinXp (reluctantly) but also couldn't get it to run in redhat 7.1

I've attached a revised version of your program.  Here's a list of
changes I made.

- I added warnings and strict, just to weed out common problems.  To
  your credit, there were none.

- "inline_states" was not correct.  I replaced it with InlineStates.

- "_start" was not correct; that overwrites Client::TCP's own
  initialization routines.  Instead I used the Started callback, which
  was committed earlier this morning.  You will need to update POE to
  the latest CVS version to use this change.  See:
  http://poe.perl.org/?Where_to_get_POE

- I made the client connect to an echo server on localhost and send a
  stream of messages instead of just one.

- I renamed callbacks to be a little more consistent.

Now for the caveats:

The biggest one first.  Tk 800.023 file watchers do not work under
ActiveState Perl.  Unless they have been fixed recently, your Tk
client won't work as you've written it.  People have worked around it
in the past by polling for socket activity.  Every 1/10 second, they
check the socket for activity and process it manually.

In the off chance that you're using cygwin, its sockets are not
compatible with Tk's file watchers.  Cygwin indicates connect failures
through the exception status of a socket.  Tk versions through 800.023
do not support watching for exceptions, so the two are incompatible.

Non-blocking connects aren't supported directly by ActiveState Perl,
but Garrett Goebel's patch to the CVS version of POE seems to work
around it.  The patch is not extensively tested, though, so please
report any problems you encounter.

-- Rocco Caputo - [EMAIL PROTECTED] - http://poe.perl.org/
#!/usr/bin/perl

use warnings;
use strict;

use Tk;
use POE;
use POE::Component::Client::TCP;

# A portion of this code was taken from Matt Sergeant's slide show:
# "Programming POE" http://axkit.org/docs/presentations/tpc2002/ This
# is modified in an attempt to figure out how to use Tk with the TCP
# Component.

POE::Component::Client::TCP->new
  ( RemoteAddress => "localhost",
    RemotePort => "echo",

    Started   => \&ui_start,
    Connected => \&client_count,

    ServerInput => sub  {
      my ($heap, $input) = @_[HEAP, ARG0];
      print "Socket input: $input\n";

      ## call client_count() with all our parameters
      client_count(@_);
    },

    ## Not "inline_states"
    InlineStates => { enable_ui => \&startui,
                      ev_count => \&ui_count,
                      ev_clear => \&ui_clear,
                     },
  );

$poe_kernel->run();
exit 0; ## added (not necessary, though)

## Renamed.
sub ui_start {
  my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];

  $heap->{send_count} = 0;

  $poe_main_window->Label( -text => 'Counter' )->pack;

  $heap->{counter_widget} =
    $poe_main_window->Label(-textvariable => \$heap->{counter} )->pack;

  $poe_main_window->Button(-text => 'Clear',
                           -command => $session->postback('ev_clear')
                          )->pack;

  $kernel->yield("ev_count");
}

sub ui_count {
  $_[HEAP]->{counter}++;
  $_[KERNEL]->yield("ev_count");
}

sub ui_clear {
  $_[HEAP]->{counter} = 0;
}

sub client_count {
  my $heap = $_[HEAP];
  $heap->{send_count}++;
  $heap->{server}->put("test $heap->{send_count}");
}

Reply via email to