1 |
package Frey::Server; |
2 |
|
3 |
use Moose; |
4 |
|
5 |
with 'Frey::Web'; |
6 |
|
7 |
use Continuity; |
8 |
#use Continuity::REPL; |
9 |
use Data::Dump qw/dump/; |
10 |
|
11 |
use Carp::REPL; |
12 |
use Frey::ClassLoader; |
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 |
my ( $self, $port ) = @_; |
23 |
$server = Continuity->new( |
24 |
port => $port || 16001, |
25 |
path_session => 1, |
26 |
cookie_session => 'sid', |
27 |
callback => \&main, |
28 |
debug_level => 1, |
29 |
staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?|xml|json|ya?ml)(\?.*)?$/ }, |
30 |
); |
31 |
$Module::Reload::Debug = 1; |
32 |
Frey::ClassLoader->new->load_all_classes(); |
33 |
$server->loop; |
34 |
} |
35 |
|
36 |
# This is the main entrypoint. We are looking for one of three things -- a |
37 |
# pushstream, a sent message, or a request for the main HTML. We delegate each |
38 |
# of these cases, none of which will return (they all loop forever). |
39 |
sub main { |
40 |
my ($req) = @_; |
41 |
|
42 |
my $path = $req->request->url->path; |
43 |
warn "REQUEST: $path ",dump( $req->params ),"\n"; |
44 |
|
45 |
Module::Reload->check if $path =~ m!reload! || $req->param('reload'); |
46 |
|
47 |
# warn $req->request->header('User_Agent'); |
48 |
|
49 |
sub run_markup { |
50 |
my ( $req, $class ) = @_; |
51 |
|
52 |
my %params = $req->params; |
53 |
|
54 |
my @required = |
55 |
grep { |
56 |
defined $_ && !defined( $params{$_} ) |
57 |
} |
58 |
map { |
59 |
my $attr = $class->meta->get_attribute($_); |
60 |
$attr->is_required && $_ |
61 |
} $class->meta->get_attribute_list; |
62 |
|
63 |
warn "## required = ",dump( @required ); |
64 |
warn "## params = ",dump( %params ); |
65 |
|
66 |
my $html; |
67 |
|
68 |
if ( @required ) { |
69 |
$html = qq|<form method="post">|; |
70 |
$html .= qq|<input type="text" name="$_">| foreach @required; |
71 |
$html .= qq|<input type="submit" value="Run $class"></form>|; |
72 |
} else { |
73 |
my $o = $class->new( %params ); |
74 |
$o->depends if $o->can('depends'); |
75 |
$html = $o->markup; |
76 |
} |
77 |
|
78 |
warn ">>> markup $class ",length( $html ), " bytes\n"; |
79 |
return $html; |
80 |
} |
81 |
|
82 |
|
83 |
# eval { |
84 |
{ |
85 |
|
86 |
my $f; |
87 |
|
88 |
if ( $path =~ m!/~/([^/]+)(.*)! ) { |
89 |
$f = Frey::Introspect->new( package => $1 ); |
90 |
} elsif ( $path =~ m!/ob/([^/]+)(.*)! ) { |
91 |
$f = Frey::ObjectBrowser->new( fey_class => $1 ); |
92 |
} elsif ( $path =~ m!/od/([^/]+)(.*)! ) { |
93 |
$f = Frey::ObjectDesigner->new( fey_class => $1 ); |
94 |
} elsif ( $path =~ m!/markup/([^/]+)(.*)! ) { |
95 |
$req->print( run_markup( $req, $1 ) ); |
96 |
} else { |
97 |
$f = Frey::ClassBrowser->new; |
98 |
} |
99 |
$f->html( $req ) if $f; |
100 |
|
101 |
}; |
102 |
|
103 |
if ( $@ ) { |
104 |
warn $@; |
105 |
#$req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it |
106 |
$req->print( qq{<pre class="error">$@<pre>} ); |
107 |
Carp::REPL::repl; # FIXME if $self->debug |
108 |
|
109 |
} |
110 |
|
111 |
# If this is a request for the pushtream, then give them that |
112 |
if($path =~ /pushstream/) { |
113 |
pushstream($req); |
114 |
} |
115 |
|
116 |
if ( $path =~ m/die/ ) { |
117 |
Carp::REPL::repl; # FIXME if $self->debug |
118 |
} |
119 |
|
120 |
# If they are sending us a message, we give them a thread for that too |
121 |
if($path =~ /sendmessage/) { |
122 |
send_message($req); |
123 |
} |
124 |
|
125 |
} |
126 |
|
127 |
# Here we accept a connection to the browser, and keep it open. Meanwhile we |
128 |
# watch the global $got_message variable, and when it gets touched we send off |
129 |
# the list of messages through the held-open connection. Then we let the |
130 |
# browser open a new connection and begin again. |
131 |
sub pushstream { |
132 |
my ($req) = @_; |
133 |
# Set up watch event -- this will be triggered when $got_message is written |
134 |
my $w = Coro::Event->var(var => \$got_message, poll => 'w'); |
135 |
while(1) { |
136 |
print STDERR "**** GOT MESSAGE, SENDING ****\n"; |
137 |
my $log = join "<br>", @messages; |
138 |
$req->print($log); |
139 |
$req->next; |
140 |
print STDERR "**** Waiting for got_message indicator ****\n"; |
141 |
$w->next; |
142 |
} |
143 |
} |
144 |
|
145 |
|
146 |
# Watch for the user to send us a message. As soon as we get it, we add it to |
147 |
# our list of messages and touch the $got_message flag to let all the |
148 |
# pushstreams know. |
149 |
sub send_message { |
150 |
my ($req) = @_; |
151 |
while(1) { |
152 |
my $msg = $req->param('message'); |
153 |
my $name = $req->param('username'); |
154 |
if($msg) { |
155 |
unshift @messages, "$name: $msg"; |
156 |
pop @messages if $#messages > 15; # Only keep the recent 15 messages |
157 |
} |
158 |
$got_message = 1; |
159 |
$req->print("Got it!"); |
160 |
$req->next; |
161 |
} |
162 |
} |
163 |
|
164 |
1; |