/[Frey]/trunk/lib/Frey/Server.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Frey/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 220 - (hide annotations)
Fri Oct 31 23:07:46 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 3580 byte(s)
hush request dump output
1 dpavlin 19 package Frey::Server;
2 dpavlin 2
3 dpavlin 55 use Moose;
4 dpavlin 10
5 dpavlin 100 with 'Frey::Web';
6    
7 dpavlin 2 use Continuity;
8 dpavlin 36 #use Continuity::REPL;
9 dpavlin 2 use Data::Dump qw/dump/;
10    
11 dpavlin 190 #use Carp::REPL; ## XXX it would be nice, but it breaks error reporting too much
12 dpavlin 101 use Frey::ClassLoader;
13 dpavlin 23
14 dpavlin 2 my @messages; # Global (shared) list of messages
15     my $got_message; # Flag to indicate that there is a new message to display
16    
17 dpavlin 10 use vars qw( $repl $server );
18 dpavlin 2
19 dpavlin 34 #$repl = Continuity::REPL->new;
20 dpavlin 37
21 dpavlin 182 =head1 NAME
22    
23     Frey::Server - Continuity based server for Frey
24    
25     =head2 DESCRIPTION
26    
27     This is one of pissible server implementations for Frey. In it's current stage, it's also most complete one.
28    
29     =head2 run
30    
31     $o->run( $optional_port );
32    
33     =cut
34    
35 dpavlin 25 sub run {
36 dpavlin 103 my ( $self, $port ) = @_;
37 dpavlin 64 $server = Continuity->new(
38 dpavlin 103 port => $port || 16001,
39 dpavlin 64 path_session => 1,
40     cookie_session => 'sid',
41     callback => \&main,
42 dpavlin 205 debug_level => 2,
43 dpavlin 162 staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?|xml|json|ya?ml)(\?.*)?$/ },
44 dpavlin 64 );
45 dpavlin 205 $Module::Reload::Debug = 1; # auto if debug_level > 1
46 dpavlin 101 Frey::ClassLoader->new->load_all_classes();
47 dpavlin 25 $server->loop;
48     }
49 dpavlin 2
50 dpavlin 182 =head2 main
51    
52     This is simple dispatcher for our server. Currently it's in flux and
53     documented only in source code.
54    
55     =cut
56    
57 dpavlin 2 sub main {
58 dpavlin 37 my ($req) = @_;
59 dpavlin 190
60 dpavlin 37 my $path = $req->request->url->path;
61 dpavlin 220 #warn "REQUEST: $path ",dump( $req->params );
62 dpavlin 2
63 dpavlin 142 Module::Reload->check if $path =~ m!reload! || $req->param('reload');
64 dpavlin 120
65 dpavlin 142 # warn $req->request->header('User_Agent');
66 dpavlin 2
67 dpavlin 142 # eval {
68     {
69 dpavlin 66
70 dpavlin 114 my $f;
71 dpavlin 66
72 dpavlin 114 if ( $path =~ m!/~/([^/]+)(.*)! ) {
73     $f = Frey::Introspect->new( package => $1 );
74     } elsif ( $path =~ m!/ob/([^/]+)(.*)! ) {
75     $f = Frey::ObjectBrowser->new( fey_class => $1 );
76     } elsif ( $path =~ m!/od/([^/]+)(.*)! ) {
77     $f = Frey::ObjectDesigner->new( fey_class => $1 );
78 dpavlin 213 } elsif ( $path =~ m!/(markup|request)/([^/]+)(.*)! ) {
79 dpavlin 184 $f = Frey::Run->new( class => $2 );
80 dpavlin 121 } else {
81 dpavlin 216 $f = Frey::Run->new( class => 'Frey::ClassBrowser' );
82 dpavlin 66 }
83 dpavlin 213 $f->request( $req ) if $f;
84 dpavlin 66
85     };
86    
87 dpavlin 190 my $self = $req;
88    
89 dpavlin 66 if ( $@ ) {
90     warn $@;
91 dpavlin 220 $req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it
92 dpavlin 66 $req->print( qq{<pre class="error">$@<pre>} );
93 dpavlin 190 # Carp::REPL::repl;
94 dpavlin 67
95 dpavlin 53 }
96    
97 dpavlin 37 # If this is a request for the pushtream, then give them that
98     if($path =~ /pushstream/) {
99     pushstream($req);
100     }
101 dpavlin 121
102 dpavlin 37 # If they are sending us a message, we give them a thread for that too
103     if($path =~ /sendmessage/) {
104     send_message($req);
105     }
106 dpavlin 2
107     }
108    
109     # Here we accept a connection to the browser, and keep it open. Meanwhile we
110     # watch the global $got_message variable, and when it gets touched we send off
111     # the list of messages through the held-open connection. Then we let the
112     # browser open a new connection and begin again.
113     sub pushstream {
114     my ($req) = @_;
115     # Set up watch event -- this will be triggered when $got_message is written
116     my $w = Coro::Event->var(var => \$got_message, poll => 'w');
117     while(1) {
118     print STDERR "**** GOT MESSAGE, SENDING ****\n";
119     my $log = join "<br>", @messages;
120     $req->print($log);
121     $req->next;
122     print STDERR "**** Waiting for got_message indicator ****\n";
123     $w->next;
124     }
125     }
126    
127    
128     # Watch for the user to send us a message. As soon as we get it, we add it to
129     # our list of messages and touch the $got_message flag to let all the
130     # pushstreams know.
131     sub send_message {
132     my ($req) = @_;
133     while(1) {
134     my $msg = $req->param('message');
135     my $name = $req->param('username');
136     if($msg) {
137     unshift @messages, "$name: $msg";
138     pop @messages if $#messages > 15; # Only keep the recent 15 messages
139     }
140     $got_message = 1;
141     $req->print("Got it!");
142     $req->next;
143     }
144     }
145    
146 dpavlin 25 1;

  ViewVC Help
Powered by ViewVC 1.1.26