--- trunk/lib/Frey/Server.pm 2008/06/30 07:13:35 36 +++ trunk/lib/Frey/Server.pm 2008/11/29 22:43:13 628 @@ -1,148 +1,173 @@ package Frey::Server; -use strict; -use warnings; +use Moose; +extends 'Frey::Editor'; +with 'Frey::Config'; -use Continuity; -#use Continuity::REPL; use Data::Dump qw/dump/; -use base 'Frey'; -use Frey::HTML; +use Frey::Run; -my @messages; # Global (shared) list of messages -my $got_message; # Flag to indicate that there is a new message to display - -use vars qw( $repl $server ); +has 'port' => ( + documentation => 'port on which server listen', + is => 'ro', + isa => 'Int', + default => sub { + my $self = shift; + $ENV{FREY_PORT} || $self->config->{port} || 16001 + }, +); -#$repl = Continuity::REPL->new; -$server = Continuity->new( - port => 16001, - path_session => 1, - cookie_session => 'sid', - callback => \&main, +has 'editor' => ( + is => 'ro', + isa => 'Frey::Editor', + lazy => 1, + default => sub { + Frey::Editor->new; + }, ); -$server->debug_level( 2 ); -sub run { - $server->loop; -} +=head2 request -my @callbacks; -my $callback_count; +This is simple dispatcher for our server. Currently it's in flux and +documented only in source code. -sub gen_link { - my ($text, $code) = @_; - $callbacks[$callback_count++] = $code; - return qq{$text}; -} +=cut -sub process_links { - my $request = shift; - my $cb = $request->param('cb'); - if (exists $callbacks[$cb]) { - $callbacks[$cb]->($request); - delete $callbacks[$cb]; - } +sub print { + my $self = shift; + warn "# print ", join(' ', map { length $_ } @_ ); + $self->{_print}->( @_ ); } -# This is the main entrypoint. We are looking for one of three things -- a -# pushstream, a sent message, or a request for the main HTML. We delegate each -# of these cases, none of which will return (they all loop forever). -sub main { - my ($req) = @_; - - my $path = $req->request->url->path; - warn "REQUEST: $path\n"; - - warn $req->request->header('User_Agent'); -#warn dump( $req ); - - # If this is a request for the pushtream, then give them that - if($path =~ /pushstream/) { - pushstream($req); - } - - # If they are sending us a message, we give them a thread for that too - if($path =~ /sendmessage/) { - send_message($req); - } +sub request { + my ( $self, $url, $params ) = @_; - # Otherwise, lets give them page - send_page($req); -} + if ( my $ref = ref($url) ) { + die "url not URI but ", dump( $url ) unless $ref =~ m{^URI}; + $url = URI->new($url); + } -# Here we accept a connection to the browser, and keep it open. Meanwhile we -# watch the global $got_message variable, and when it gets touched we send off -# the list of messages through the held-open connection. Then we let the -# browser open a new connection and begin again. -sub pushstream { - my ($req) = @_; - # Set up watch event -- this will be triggered when $got_message is written - my $w = Coro::Event->var(var => \$got_message, poll => 'w'); - while(1) { - print STDERR "**** GOT MESSAGE, SENDING ****\n"; - my $log = join "
", @messages; - $req->print($log); - $req->next; - print STDERR "**** Waiting for got_message indicator ****\n"; - $w->next; - } -} + my $path = $url->path; +# eval { + { -# Watch for the user to send us a message. As soon as we get it, we add it to -# our list of messages and touch the $got_message flag to let all the -# pushstreams know. -sub send_message { - my ($req) = @_; - while(1) { - my $msg = $req->param('message'); - my $name = $req->param('username'); - if($msg) { - unshift @messages, "$name: $msg"; - pop @messages if $#messages > 15; # Only keep the recent 15 messages - } - $got_message = 1; - $req->print("Got it!"); - $req->next; - } -} + if ( $path =~ m{/reload(.*)} ) { -# This isn't a pushstream, nor a new message. It is just the main page. We loop -# in case they ask for it multiple times :) -sub send_page { - my ($req) = @_; - my $templates = Template::Declare->templates; - while(1) { - warn "param = ",dump($req->param); - my $path = $req->request->url->path; + $ENV{FREY_NO_LOG} = 1; + my $cmd = "perl -c $0"; + warn "# check syntax with $cmd"; + if ( system($cmd) == 0 ) { + my $server = Frey::Server->new; + $self->load_config; + Module::Reload->check; + warn "# reload done"; + $self->print( refresh( $1, 1 ) ); + return; + } else { + warn "ERROR: $?"; + } + $ENV{FREY_NO_LOG} = 0; + + } elsif ( $path =~ m{/exit(.*)} ) { + # FIXME do we need some kind of check here for production? :-) + # ./bin/dev.sh will restart us during development + $self->print( refresh( $1, 2 ) ); + exit; + } my $html; - if ( $path =~ m/::/ ) { - my ( undef, $module, $method ) = split(m!/!, $path, 3); + sub rest2class { + my $class = shift; + $class =~ s/-/::/; # sigh! + return $class; + } + - if ( ! defined( $templates->{$module} ) ) { - $req->conn->send_status_line( 404, "$module" ); - $html = "Package $module not found"; - } elsif ( ! $method ) { - $html = Frey::HTML->page( 'package-methods', $req, $module ); - } elsif ( grep(/^\Q$method\E$/, @{ $templates->{$module} }) ) { - $html = Frey::HTML->page( $method, $req ); + my $f; + + # shared run params + my $run = { + request_url => $url, +# debug => 1, + }; + + if ( + $path =~ m{/Frey[:-]+ObjectBrowser} + ) { + $f = Frey::ObjectBrowser->new( fey_class => $params->{class} ); +# $f->request( $req ); + } elsif ( + $path =~ m{/Frey[:-]+ObjectDesigner} + ) { + $f = Frey::ObjectDesigner->new( fey_class => $params->{class} ); +# $f->request( $req ); + } elsif ( $path =~ $self->editor->url_regex ) { + $self->print( $self->editor->command( $path ) ); + return; + } elsif ( + $path =~ m{/([^/]+)/(\w*as_\w+)/?([^/]+)?} + ) { + my $class = rest2class $1; + warn "# run $path -> $class $2"; + $run->{format} = $3 if $3; + $params->{request_url} = $url, + $run->{$_} = $params->{$_} foreach keys %$params; + $f = Frey::Run->new( class => $class, params => $run, run => $2, request_url => $url ); + } elsif ( + $path =~ m{/([^/]+)/?$} + ) { + my $class = rest2class $1; + warn "# introspect $class"; + $run->{class} ||= $class; + $f = Frey::Run->new( class => 'Frey::Introspect', params => $run, request_url => $url ); + } else { + $f = Frey::Run->new( class => 'Frey::ClassBrowser', params => $run, request_url => $url ); + } + + if ( $f ) { + $f->clean_status; +# $f->add_status( { request => $req } ); + $f->status_parts; + if ( my $html = $f->html ) { + warn "## html ",length($html)," bytes"; + $self->print( $html ); } else { - $req->conn->send_status_line( 404, "$module $method" ); - $html = "Package $module doesn't have $method"; + confess "no output from $f"; } } else { - warn "fallback to status page\n"; - $html = Frey::HTML->page( 'status' ); + confess "# can't call request on nothing!"; } - $req->print( $html ); - warn ">> ",length( $html ), " bytes\n"; - $req->next; + }; + + if ( $@ ) { + warn "SERVER ERROR: $@"; +# $req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it + $self->print( qq{
$@
\r\n\r\n} );
+#		Carp::REPL::repl;
 	}
+
+}
+
+sub refresh {
+	my ( $url, $time ) = @_;
+	$url  ||= '/';
+	$time ||= 1;
+	warn "# refresh $url";
+	qq|
+		
+		
+			
+		
+		
+			Refresh $url in $time sec
+		
+		
+		\n\r\n\r
+	|; # XXX newlines at end are important to flush content to browser
 }
 
 1;