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