/[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 66 - (hide annotations)
Wed Jul 9 12:08:07 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3284 byte(s)
moved error catching into Frey::Server
1 dpavlin 19 package Frey::Server;
2 dpavlin 2
3 dpavlin 55 use Moose;
4 dpavlin 10
5 dpavlin 2 use Continuity;
6 dpavlin 36 #use Continuity::REPL;
7 dpavlin 53 use Continuity::Widget::DomNode;
8 dpavlin 2 use Data::Dump qw/dump/;
9    
10 dpavlin 55 use Frey;
11     use Frey::Introspect;
12 dpavlin 60 use Frey::ObjectBrowser;
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 25 sub run {
22 dpavlin 64 $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 dpavlin 25 $server->loop;
31     }
32 dpavlin 2
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 dpavlin 37 my ($req) = @_;
38 dpavlin 2
39 dpavlin 37 my $path = $req->request->url->path;
40     warn "REQUEST: $path\n";
41 dpavlin 2
42     warn $req->request->header('User_Agent');
43     #warn dump( $req );
44    
45 dpavlin 66 eval {
46    
47     if ( $path =~ m!/~/([^/]+)(?:/([^/]*))?! ) {
48     my $f = Frey::Introspect->new( package => $1 );
49     $f->html( $req );
50     }
51    
52     if ( $path =~ m!/ob/([^/]+)(?:/([^/]*))?! ) {
53     my $f = Frey::ObjectBrowser->new;
54     $f->html( $req );
55     }
56    
57     };
58    
59     if ( $@ ) {
60     warn $@;
61     #$req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it
62     $req->print( qq{<pre class="error">$@<pre>} );
63     $req->next;
64 dpavlin 53 }
65    
66 dpavlin 37 # If this is a request for the pushtream, then give them that
67     if($path =~ /pushstream/) {
68     pushstream($req);
69     }
70 dpavlin 2
71 dpavlin 37 # If they are sending us a message, we give them a thread for that too
72     if($path =~ /sendmessage/) {
73     send_message($req);
74     }
75 dpavlin 2
76 dpavlin 64 my $f = Frey->new;
77     my $classes = Continuity::Widget::DomNode->create(
78     ul => [
79     map {
80     warn dump( $_ );
81     my ( $package, $path ) = %$_;
82     ( li => [
83     a => { href => '/~/' . $package . '/' } => [ $package ],
84     ' ',
85     a => { href => '/ob/' . $package } => [ 'browse' ],
86     " <tt>$path</tt>"
87     ] )
88     } @{ $f->classes }
89     ],
90     )->to_string;
91     $req->print( $classes );
92 dpavlin 2 }
93    
94     # Here we accept a connection to the browser, and keep it open. Meanwhile we
95     # watch the global $got_message variable, and when it gets touched we send off
96     # the list of messages through the held-open connection. Then we let the
97     # browser open a new connection and begin again.
98     sub pushstream {
99     my ($req) = @_;
100     # Set up watch event -- this will be triggered when $got_message is written
101     my $w = Coro::Event->var(var => \$got_message, poll => 'w');
102     while(1) {
103     print STDERR "**** GOT MESSAGE, SENDING ****\n";
104     my $log = join "<br>", @messages;
105     $req->print($log);
106     $req->next;
107     print STDERR "**** Waiting for got_message indicator ****\n";
108     $w->next;
109     }
110     }
111    
112    
113     # Watch for the user to send us a message. As soon as we get it, we add it to
114     # our list of messages and touch the $got_message flag to let all the
115     # pushstreams know.
116     sub send_message {
117     my ($req) = @_;
118     while(1) {
119     my $msg = $req->param('message');
120     my $name = $req->param('username');
121     if($msg) {
122     unshift @messages, "$name: $msg";
123     pop @messages if $#messages > 15; # Only keep the recent 15 messages
124     }
125     $got_message = 1;
126     $req->print("Got it!");
127     $req->next;
128     }
129     }
130    
131 dpavlin 25 1;

  ViewVC Help
Powered by ViewVC 1.1.26