Re: first poe program - request for review

Rocco Caputo <[email protected]>
Newsgroups gmane.comp.lang.perl.poe
Message-ID <[email protected]>
If CLIENTS held Client objects, you could avoid passing $client_id to  
everything.  process_client_command() could be a Client method, as  
generate_response().  Perhaps:

sub handle_requests {
   my $c = shift;
   my $mud_client = get_mud_client($c);

   $mud_client->start() if (
     not $client->error()
     and not $client->connected()
   );

   $mud_client->process_command($c);
   $mud_client->get_response($c);
}

Since it's working, I recommend making a lot of connections through it  
to see whether it leaks memory.  Try to get some concurrency going on  
the server side.  See how the proxy handles server downtime.

Here's a simple concurrent server to get you started:

use POE qw(Component::Server::TCP);
POE::Component::Server::TCP->new(
   Port => 9000,
   ClientConnected => sub {
     $_[HEAP]{client}->put("hi!");
   },
   ClientInput => sub {
     $_[HEAP]{client}->put("you said: $_[ARG0]");
   },
);

POE::Kernel->run();
exit;

-- 
Rocco Caputo - [email protected]


On Apr 7, 2009, at 19:16, hubert depesz lubaczewski wrote:

> hi,
> i just finished writing my first poe program, and since i learn best  
> by
> hearing/reading about what did i do wrong, or what can be done  
> better -
> i would like to ask you for a review, and pointing about everything  
> that
> can be done better/nicer/more "poe-ish".
>
> program itself can be found in here:
> http://svn.depesz.com/svn/mud-proxy/mud-proxy.pl
> if you'd like to run it to test what it does, you will also need test
> server: http://svn.depesz.com/svn/mud-proxy/test-server.pl (this is  
> not
> poe based, it's just a simple tcp server).
>
> mud-proxy.pl is supposed to work as a proxy between http client, which
> sends request every-so-often, and some remote server which uses
> stateful connections.
>
> when new client connects using http, mud-proxy starts new connection  
> to
> server, arranges things in such a way, that any input from server will
> be put in buffers associated with http-client cookie.
>
> Example usage:
> in 1st terminal, you run ./test-server.pl
> in 2nd terminal, you run ./mud-proxy.pl
> in 3rd terminal, you run:
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud -O -
> it will print something like this:
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : null
> }
>
> data: null means that there was no data from server. which is fine.
>
> now you run the command again:
>
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud -O -
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : "Give me smaller number: "
> }
>
> and it returned some data from server. server asks for smaller  
> number, so i pass: cmd=2:
>
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud?cmd=2 -O -
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : null
> }
>
> and i ask for new data again:
>
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud -O -
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : "And larger: "
> }
>
> now it asks for larger integer, so:
>
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud?cmd=10 -O -
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : null
> }
>
> no, I check the status every so often, and get something like this:
>
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud -O -
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : "XX: 2\nXX: 3\nXX: 4\nXX: 5\nXX: 6\n"
> }
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud -O -
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : "XX: 7\nXX: 8\nXX: 9\n"
> }
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud -O -
> {
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : null
> }
> => wget --keep-session-cookies --load-cookies=/tmp/cook --save- 
> cookies=/tmp/cook -q http://127.0.0.1:8181/mud -O -
> {
>   "error" : "Disconnected",
>   "id" : "fKrV4QQgz8EczKlOxgFMQgGQfHJCCGGTnL1nYWwb1JaUbBbIVm",
>   "data" : "XX: 10\r\n"
> }
>
> As you can see stateless http client was able to communicate with  
> stateful tcp
> server, exchange data and basically "just work with it".
>
> of course - since my test-server.pl is is synchronous and
> single-threaded, we can't really see the asynchroneous nature of poe,
> but this is just simplistic example - theoretically you can make
> mud-proxy.pl connect to any tcp server, and it should work.
>
> So, back to my original request - if you have some spare time,  
> please check the
> sources of mud-proxy.pl, and tell me what, and why, should i have  
> done differently.
>
> Best regards,
>
> depesz
>
> -- 
> Linkedin: http://www.linkedin.com/in/depesz  /  blog: http://www.depesz.com/
> jid/gtalk: [email protected] / aim:depeszhdl / skype:depesz_hdl / gg: 
> 6749007
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.