1 |
package Frey::Server; |
2 |
|
3 |
use Moose; |
4 |
|
5 |
use Continuity; |
6 |
#use Continuity::REPL; |
7 |
use Continuity::Widget::DomNode; |
8 |
use Data::Dump qw/dump/; |
9 |
|
10 |
use Frey; |
11 |
use Frey::Introspect; |
12 |
use Frey::ObjectBrowser; |
13 |
|
14 |
my @messages; # Global (shared) list of messages |
15 |
my $got_message; # Flag to indicate that there is a new message to display |
16 |
|
17 |
use vars qw( $repl $server ); |
18 |
|
19 |
#$repl = Continuity::REPL->new; |
20 |
|
21 |
sub run { |
22 |
$server = Continuity->new( |
23 |
port => 16001, |
24 |
path_session => 1, |
25 |
cookie_session => 'sid', |
26 |
callback => \&main, |
27 |
debug_level => 1, |
28 |
staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?)$/ }, |
29 |
); |
30 |
$server->loop; |
31 |
} |
32 |
|
33 |
# This is the main entrypoint. We are looking for one of three things -- a |
34 |
# pushstream, a sent message, or a request for the main HTML. We delegate each |
35 |
# of these cases, none of which will return (they all loop forever). |
36 |
sub main { |
37 |
my ($req) = @_; |
38 |
|
39 |
my $path = $req->request->url->path; |
40 |
warn "REQUEST: $path\n"; |
41 |
|
42 |
warn $req->request->header('User_Agent'); |
43 |
#warn dump( $req ); |
44 |
|
45 |
if ( $path =~ m!/~/([^/]+)(?:/([^/]*))?! ) { |
46 |
my $f = Frey::Introspect->new( package => $1 ); |
47 |
$f->html( $req ); |
48 |
} |
49 |
|
50 |
if ( $path =~ m!/ob/([^/]+)(?:/([^/]*))?! ) { |
51 |
my $f = Frey::ObjectBrowser->new; |
52 |
$f->html( $req ); |
53 |
} |
54 |
# If this is a request for the pushtream, then give them that |
55 |
if($path =~ /pushstream/) { |
56 |
pushstream($req); |
57 |
} |
58 |
|
59 |
# If they are sending us a message, we give them a thread for that too |
60 |
if($path =~ /sendmessage/) { |
61 |
send_message($req); |
62 |
} |
63 |
|
64 |
my $f = Frey->new; |
65 |
my $classes = Continuity::Widget::DomNode->create( |
66 |
ul => [ |
67 |
map { |
68 |
warn dump( $_ ); |
69 |
my ( $package, $path ) = %$_; |
70 |
( li => [ |
71 |
a => { href => '/~/' . $package . '/' } => [ $package ], |
72 |
' ', |
73 |
a => { href => '/ob/' . $package } => [ 'browse' ], |
74 |
" <tt>$path</tt>" |
75 |
] ) |
76 |
} @{ $f->classes } |
77 |
], |
78 |
)->to_string; |
79 |
$req->print( $classes ); |
80 |
} |
81 |
|
82 |
# Here we accept a connection to the browser, and keep it open. Meanwhile we |
83 |
# watch the global $got_message variable, and when it gets touched we send off |
84 |
# the list of messages through the held-open connection. Then we let the |
85 |
# browser open a new connection and begin again. |
86 |
sub pushstream { |
87 |
my ($req) = @_; |
88 |
# Set up watch event -- this will be triggered when $got_message is written |
89 |
my $w = Coro::Event->var(var => \$got_message, poll => 'w'); |
90 |
while(1) { |
91 |
print STDERR "**** GOT MESSAGE, SENDING ****\n"; |
92 |
my $log = join "<br>", @messages; |
93 |
$req->print($log); |
94 |
$req->next; |
95 |
print STDERR "**** Waiting for got_message indicator ****\n"; |
96 |
$w->next; |
97 |
} |
98 |
} |
99 |
|
100 |
|
101 |
# Watch for the user to send us a message. As soon as we get it, we add it to |
102 |
# our list of messages and touch the $got_message flag to let all the |
103 |
# pushstreams know. |
104 |
sub send_message { |
105 |
my ($req) = @_; |
106 |
while(1) { |
107 |
my $msg = $req->param('message'); |
108 |
my $name = $req->param('username'); |
109 |
if($msg) { |
110 |
unshift @messages, "$name: $msg"; |
111 |
pop @messages if $#messages > 15; # Only keep the recent 15 messages |
112 |
} |
113 |
$got_message = 1; |
114 |
$req->print("Got it!"); |
115 |
$req->next; |
116 |
} |
117 |
} |
118 |
|
119 |
1; |