1 |
dpavlin |
19 |
package Frey::Server; |
2 |
dpavlin |
2 |
|
3 |
dpavlin |
19 |
use strict; |
4 |
dpavlin |
10 |
use warnings; |
5 |
|
|
|
6 |
dpavlin |
2 |
use Continuity; |
7 |
|
|
use Continuity::REPL; |
8 |
|
|
use Data::Dump qw/dump/; |
9 |
|
|
|
10 |
dpavlin |
23 |
use base 'Frey'; |
11 |
|
|
use Frey::HTML; |
12 |
|
|
|
13 |
dpavlin |
2 |
my @messages; # Global (shared) list of messages |
14 |
|
|
my $got_message; # Flag to indicate that there is a new message to display |
15 |
|
|
|
16 |
dpavlin |
10 |
use vars qw( $repl $server ); |
17 |
dpavlin |
2 |
|
18 |
dpavlin |
25 |
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 |
dpavlin |
2 |
|
28 |
dpavlin |
25 |
$server->loop; |
29 |
|
|
} |
30 |
dpavlin |
2 |
|
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 |
dpavlin |
19 |
warn "REQUEST: $path\n"; |
39 |
dpavlin |
2 |
|
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 |
dpavlin |
19 |
pushstream($req); |
46 |
dpavlin |
2 |
} |
47 |
|
|
|
48 |
|
|
# If they are sending us a message, we give them a thread for that too |
49 |
|
|
if($path =~ /sendmessage/) { |
50 |
dpavlin |
19 |
send_message($req); |
51 |
dpavlin |
2 |
} |
52 |
|
|
|
53 |
dpavlin |
19 |
# Otherwise, lets give them page |
54 |
|
|
send_page($req); |
55 |
dpavlin |
2 |
} |
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 |
dpavlin |
19 |
sub send_page { |
97 |
dpavlin |
23 |
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 ( grep(/^\Q$method\E$/, @{ $templates->{$module} }) ) { |
110 |
|
|
$req->print( Frey::HTML->page( $method, $req ) ); |
111 |
|
|
} else { |
112 |
|
|
$req->conn->send_status_line( 404, "$module $method" ); |
113 |
|
|
$req->print("Package $module doesn't have $method"); |
114 |
|
|
} |
115 |
|
|
} else { |
116 |
|
|
my $html = Frey::HTML->page( 'status' ); |
117 |
|
|
$req->print( $html ); |
118 |
dpavlin |
26 |
warn ">> ",length( $html ), " bytes | ", substr($html, 0, 80),$/; |
119 |
dpavlin |
23 |
} |
120 |
|
|
$req->next; |
121 |
|
|
} |
122 |
dpavlin |
2 |
} |
123 |
|
|
|
124 |
dpavlin |
25 |
1; |