1 |
package Frey::Server; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Continuity; |
7 |
use Continuity::REPL; |
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
use HTML; |
11 |
|
12 |
my @messages; # Global (shared) list of messages |
13 |
my $got_message; # Flag to indicate that there is a new message to display |
14 |
|
15 |
use vars qw( $repl $server ); |
16 |
$repl = Continuity::REPL->new; |
17 |
|
18 |
$server = Continuity->new( |
19 |
port => 16001, |
20 |
path_session => 1, |
21 |
cookie_session => 'sid', |
22 |
callback => \&main, |
23 |
); |
24 |
$server->debug_level( 2 ); |
25 |
|
26 |
$server->loop; |
27 |
|
28 |
# This is the main entrypoint. We are looking for one of three things -- a |
29 |
# pushstream, a sent message, or a request for the main HTML. We delegate each |
30 |
# of these cases, none of which will return (they all loop forever). |
31 |
sub main { |
32 |
my ($req) = @_; |
33 |
|
34 |
my $path = $req->request->url->path; |
35 |
warn "REQUEST: $path\n"; |
36 |
|
37 |
warn $req->request->header('User_Agent'); |
38 |
#warn dump( $req ); |
39 |
|
40 |
# If this is a request for the pushtream, then give them that |
41 |
if($path =~ /pushstream/) { |
42 |
pushstream($req); |
43 |
} |
44 |
|
45 |
# If they are sending us a message, we give them a thread for that too |
46 |
if($path =~ /sendmessage/) { |
47 |
send_message($req); |
48 |
} |
49 |
|
50 |
# Otherwise, lets give them page |
51 |
send_page($req); |
52 |
} |
53 |
|
54 |
# Here we accept a connection to the browser, and keep it open. Meanwhile we |
55 |
# watch the global $got_message variable, and when it gets touched we send off |
56 |
# the list of messages through the held-open connection. Then we let the |
57 |
# browser open a new connection and begin again. |
58 |
sub pushstream { |
59 |
my ($req) = @_; |
60 |
# Set up watch event -- this will be triggered when $got_message is written |
61 |
my $w = Coro::Event->var(var => \$got_message, poll => 'w'); |
62 |
while(1) { |
63 |
print STDERR "**** GOT MESSAGE, SENDING ****\n"; |
64 |
my $log = join "<br>", @messages; |
65 |
$req->print($log); |
66 |
$req->next; |
67 |
print STDERR "**** Waiting for got_message indicator ****\n"; |
68 |
$w->next; |
69 |
} |
70 |
} |
71 |
|
72 |
|
73 |
# Watch for the user to send us a message. As soon as we get it, we add it to |
74 |
# our list of messages and touch the $got_message flag to let all the |
75 |
# pushstreams know. |
76 |
sub send_message { |
77 |
my ($req) = @_; |
78 |
while(1) { |
79 |
my $msg = $req->param('message'); |
80 |
my $name = $req->param('username'); |
81 |
if($msg) { |
82 |
unshift @messages, "$name: $msg"; |
83 |
pop @messages if $#messages > 15; # Only keep the recent 15 messages |
84 |
} |
85 |
$got_message = 1; |
86 |
$req->print("Got it!"); |
87 |
$req->next; |
88 |
} |
89 |
} |
90 |
|
91 |
# This isn't a pushstream, nor a new message. It is just the main page. We loop |
92 |
# in case they ask for it multiple times :) |
93 |
sub send_page { |
94 |
my ($req) = @_; |
95 |
while(1) { |
96 |
warn "param = ",dump($req->param); |
97 |
$req->print( HTML->view( 'status' ) ); |
98 |
$req->next; |
99 |
Module::Refresh->refresh; |
100 |
} |
101 |
} |
102 |
|