Re: Need help for passing data back and forth data over a persistent SSL TCP socket

[email protected] (Gunnar Strand) Fri, 21 Feb 2014 11:14:00 +0100
Newsgroups perl.poe
Message-ID <CAEsF1LtSFvgfvo_L-9zjLZ=Y5DLwQTWS0OzuynGOFs2TRe44oQ@mail.gmail.com>
Hi,



2014-02-21 1:48 GMT+01:00  <[email protected]>:
> Hi there,
>
> I'm trying to write a POE based server/client combo that uses SSL authed
> persistent connections for comunication between a client and server.
>
> Basically I have a server running that listens on port 2001. A client
> connects to the server. Sets up an SSL connection with client certificate
> auth and then what I _want_ to happen is that the client then (every 10
> seconds) asks for new temperature data from the server and is served the
> current value. I have the client asking in a loop every 10 seconds but the
> server isn't triggering and responding past the first connection event. I'm
> not sure I'm doing this right (tm) can anyone help me? I have some inline
> comments in the code.

You need to have a look at timers for POE:

http://poe.perl.org/?POE_Cookbook/Recurring_Alarms

>
> client :
>
> #!/usr/bin/perl
>
> ### very simple connect to server with auth certs and when connected sends
> the "temp" command.
> ### then when it receives input it fires off Server input and wait 10 then
> sends again. But
> ### should it trigger a input event again? I think I'm looping in POE
> incorrectly.
>
>     ServerInput   => sub {
>         my $command = "temp";
>       while(1) {
>         print "from server: ".$_[ARG0]."\n";
>         sleep (10);
>         print "Sending to server : $command\n";
>         $_[HEAP]{server}->put($command);
>       }

You are correct that you are "looping" incorrectly. POE is
event-driven and uses run-to-completion scheduling
which means that each subroutine must exit before the POE Kernel can
schedule the next event.

You need to 1) set up a recurring alarm and corresponding event
handler which sends "$command" to the server,
and 2) handle data from the server in "ServerInput" and then return
from the subroutine.

Add an alarm to "Connected":

      $_[HEAP]->{next_alarm_time} = int(time());   # Immediately
trigger an alarm
      $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});

Add an inline state "tick" event:

    tick => sub {
      my $command = "temp";
      print "Sending to server : $command\n";
      $_[HEAP]{server}->put($command);
      $_[HEAP]->{next_alarm_time}+=10;
      $_[KERNEL]->alarm(tick => $_[HEAP]->{next_alarm_time});
    },

See http://search.cpan.org/dist/POE/lib/POE/Component/Client/TCP.pm#InlineStates

Remove *everything* from the "while" loop except the "print".

I have never dabbled in SSL, so I can't verify the server function, but perhaps
POE::Component::Server::TCP could relieve you of some of the socket handling
in the server part.

http://search.cpan.org/~rcaputo/POE/lib/POE/Component/Server/TCP.pm

BR
Gunnar