Skip to content
Newer
Older
100644 161 lines (133 sloc) 3.95 KB
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
1 #!/usr/bin/env perl
5b0127b @rcaputo Remove emit() from roles.
authored
2 # vim: ts=2 sw=2 noexpandtab
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
3
4 use warnings;
5 use strict;
6eae87c @rcaputo Add "emits" and "observes" attribute declarations.
authored
6 use lib qw(../lib);
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
7
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
8 my $rot13_server_port = 12345;
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
9
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
10 ### Bot::BasicBot bot. Bot bot bot bot bot!
11 #
12 # Create a classing Bot::BasicBot, but use Reflex::Client within it to
13 # talk to a server asynchronously.
14
15 {
16 package MyBot;
17 use Moose;
18 use MooseX::NonMoose;
19 extends 'Bot::BasicBot', 'Reflex::Base';
20 use Reflex::Collection;
21 use Reflex::Client;
22
134c7e0 @rcaputo Add simpler has_many syntax for Reflex::Collection.
authored
23 has_many clients => ( handles => { remember_client => "remember" } );
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
24
25 sub said {
26 my ($mybot, $bot_event) = @_;
27
28 my $said = $bot_event->{body};
29 return unless $said =~ /^\s*rot13\s*(\S.*?)\s*$/;
30 my $to_rot_13 = $1;
31
32 $mybot->remember_client(
33 Reflex::Client->new(
34 port => $rot13_server_port,
35 on_connected => sub {
36 my $client = shift;
37 $client->put($to_rot_13);
38 },
39 on_data => sub {
40 my ($client, $response) = @_;
41 $mybot->say(
42 channel => $bot_event->{channel},
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
43 body => $response->octets(),
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
44 );
45 $client->stop();
46 },
47 ),
48 );
49
50 # Say nothing now.
51 return;
52 }
53 }
54
55 ### Reflex rot13 server.
56 #
57 # We're embedding a small server within the bot for testing.
58 # The idea of a rot13 server is pretty silly, but it serves (har) as a
59 # good, small example.
60 #
61 # It also illustrates that isolated Reflexy things still work.
62
63 {
64 # A stream that echoes back whatever it receives after rot13
65 # encrypting it.
66
67 {
68 package Rot13EchoStream;
69 use Moose;
70 extends 'Reflex::Stream';
71
72 sub on_data {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
73 my ($self, $data) = @_;
74 my $text = $data->octets();
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
75 warn "Server has been asked to rot13('$text')...\n";
76 $text =~ tr[a-zA-Z][n-za-mN-ZA-M];
77 $self->put($text);
78 }
79 }
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
80
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
81 # The rot13 server itself.
82 # It's your basic Reflex server using Rot13EchoStream to handle
83 # client connections.
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
84
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
85 {
86 package Rot13Server;
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
87
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
88 use Moose;
89 extends 'Reflex::Acceptor';
90 use Reflex::Collection;
91
134c7e0 @rcaputo Add simpler has_many syntax for Reflex::Collection.
authored
92 has_many clients => ( handles => { remember_client => "remember" } );
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
93
94 sub on_accept {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
95 my ($self, $socket) = @_;
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
96 $self->remember_client(
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
97 Rot13EchoStream->new( handle => $socket->handle() )
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
98 );
99 }
100
101 sub on_error {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
102 my ($self, $error) = @_;
103 warn $error->formatted(), "\n";
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
104 $self->stop();
105 }
106 }
107 }
108
109 ### Main program.
110 #
111 # Start the rot13 server so it's there for the bot.
112 # Start the bot.
113 # Run it all, forever, via the bot's main loop.
114
115 my $server = Rot13Server->new(
116 listener => IO::Socket::INET->new(
117 LocalAddr => '127.0.0.1',
118 LocalPort => $rot13_server_port,
119 Listen => 5,
120 Reuse => 1,
121 ),
122 );
123
124 my $bot = MyBot->new(
125 server => "irc.perl.org",
126 channels => ["#bots"],
127
128 nick => "reflex-eg-42",
129 username => "bot",
130 name => "Reflex Example 42 Bot",
131
132 charset => "utf-8",
133 )->run();
134
135 __END__
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
136
137 Inspiration:
138
139 17:18 kthakore : awnstudio_: yeah me
140 17:18 kthakore : dngor: right ..
253508c @rcaputo New example using Reflex within Bot::BasicBot. Incomplete, per TODO n…
authored
141 17:18 kthakore : but how do I plug it into Bot::BasicBot
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
142 17:19 dngor : When you get the trigger from Bot::BasicBot, open a
4dcae46 @rcaputo Whitespace fixes.
authored
143 socket, send a request, and wait for a response.
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
144 17:19 dngor : You could use IO::Socket::INET and
145 $poe_kernel->select_read(), or something higher level.
146 17:19 kthakore : http://github.com/PerlGameDev/SDL/blob/master/tools/SDLBot.pl
147 17:20 kthakore : but where do I get $poe_kernel from?
148 17:20 dngor : If the server is localhost, the connect() will tend to
149 pass or fail pretty quickly... unless your firewall is
150 interfering with localhost. So blocking is generally not
4dcae46 @rcaputo Whitespace fixes.
authored
151 a problem.
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
152 17:20 kthakore : I mean I know BasicBot uses Poe
153 17:20 dngor : Presumably Bot::BasicBot passes it to you, in the
4dcae46 @rcaputo Whitespace fixes.
authored
154 standard POE way. If not, POE::Kernel exports it.
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
155 17:21 dngor : Hm. There's an excellent exercise. Using Reflex INSIDE
4dcae46 @rcaputo Whitespace fixes.
authored
156 POE components.
c3ae474 @rcaputo Document a new example idea: Using Reflex from POE programs.
authored
157 17:21 dngor : Bot::BasicBot + Reflex::Client
158 17:22 kthakore : dngor: whut is reflex ... and how do I use it?
159 17:23 dngor : It's neither here nor there. I'm just brainstorming aloud.
160 17:23 kthakore : dngor: oh ok
Something went wrong with that request. Please try again.