/[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 23 - (hide annotations)
Sun Jun 29 16:24:41 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3337 byte(s)
another major refactor and version bump [0.01]

- return 404 errors for resources which doesn't exist
- move HTML page wrapper is now in Fray::HTML->page
- dispatch to defined Template::Declare templates
- display available templates on status screen
- NOTE very clean and unobfuscated mapping
- a bits of documentation all over the place
1 dpavlin 19 package Frey::Server;
2 dpavlin 2
3 dpavlin 19 use strict;
4 dpavlin 10 use warnings;
5    
6 dpavlin 2 use Continuity;
7     use Continuity::REPL;
8     use Data::Dump qw/dump/;
9    
10 dpavlin 23 use base 'Frey';
11 dpavlin 2
12 dpavlin 23 use Frey::HTML;
13    
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 $repl = Continuity::REPL->new;
19    
20     $server = Continuity->new(
21     port => 16001,
22     path_session => 1,
23     cookie_session => 'sid',
24 dpavlin 19 callback => \&main,
25 dpavlin 2 );
26     $server->debug_level( 2 );
27    
28     $server->loop;
29    
30     # This is the main entrypoint. We are looking for one of three things -- a
31     # pushstream, a sent message, or a request for the main HTML. We delegate each
32     # of these cases, none of which will return (they all loop forever).
33     sub main {
34     my ($req) = @_;
35    
36     my $path = $req->request->url->path;
37 dpavlin 19 warn "REQUEST: $path\n";
38 dpavlin 2
39     warn $req->request->header('User_Agent');
40     #warn dump( $req );
41    
42     # If this is a request for the pushtream, then give them that
43     if($path =~ /pushstream/) {
44 dpavlin 19 pushstream($req);
45 dpavlin 2 }
46    
47     # If they are sending us a message, we give them a thread for that too
48     if($path =~ /sendmessage/) {
49 dpavlin 19 send_message($req);
50 dpavlin 2 }
51    
52 dpavlin 19 # Otherwise, lets give them page
53     send_page($req);
54 dpavlin 2 }
55    
56     # Here we accept a connection to the browser, and keep it open. Meanwhile we
57     # watch the global $got_message variable, and when it gets touched we send off
58     # the list of messages through the held-open connection. Then we let the
59     # browser open a new connection and begin again.
60     sub pushstream {
61     my ($req) = @_;
62     # Set up watch event -- this will be triggered when $got_message is written
63     my $w = Coro::Event->var(var => \$got_message, poll => 'w');
64     while(1) {
65     print STDERR "**** GOT MESSAGE, SENDING ****\n";
66     my $log = join "<br>", @messages;
67     $req->print($log);
68     $req->next;
69     print STDERR "**** Waiting for got_message indicator ****\n";
70     $w->next;
71     }
72     }
73    
74    
75     # Watch for the user to send us a message. As soon as we get it, we add it to
76     # our list of messages and touch the $got_message flag to let all the
77     # pushstreams know.
78     sub send_message {
79     my ($req) = @_;
80     while(1) {
81     my $msg = $req->param('message');
82     my $name = $req->param('username');
83     if($msg) {
84     unshift @messages, "$name: $msg";
85     pop @messages if $#messages > 15; # Only keep the recent 15 messages
86     }
87     $got_message = 1;
88     $req->print("Got it!");
89     $req->next;
90     }
91     }
92    
93     # This isn't a pushstream, nor a new message. It is just the main page. We loop
94     # in case they ask for it multiple times :)
95 dpavlin 19 sub send_page {
96 dpavlin 23 my ($req) = @_;
97     my $templates = Template::Declare->templates;
98     while(1) {
99     warn "param = ",dump($req->param);
100     my $path = $req->request->url->path;
101    
102     if ( $path =~ m/::/ ) {
103     my ( undef, $module, $method ) = split(m!/!, $path, 3);
104    
105     if ( ! defined( $templates->{$module} ) ) {
106     $req->conn->send_status_line( 404, "$module" );
107     $req->print("Package $module not found");
108     } elsif ( grep(/^\Q$method\E$/, @{ $templates->{$module} }) ) {
109     $req->print( Frey::HTML->page( $method, $req ) );
110     } else {
111     $req->conn->send_status_line( 404, "$module $method" );
112     $req->print("Package $module doesn't have $method");
113     }
114     } else {
115     my $html = Frey::HTML->page( 'status' );
116     $req->print( $html );
117     warn ">> ",length( $html ), " bytes\n";
118     }
119     $req->next;
120     Module::Refresh->refresh;
121     }
122 dpavlin 2 }
123    

  ViewVC Help
Powered by ViewVC 1.1.26