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