/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations)
Sun Jun 29 16:24:41 2008 UTC (15 years, 10 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 package Frey::Server;
2
3 use strict;
4 use warnings;
5
6 use Continuity;
7 use Continuity::REPL;
8 use Data::Dump qw/dump/;
9
10 use base 'Frey';
11
12 use Frey::HTML;
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 $repl = Continuity::REPL->new;
19
20 $server = Continuity->new(
21 port => 16001,
22 path_session => 1,
23 cookie_session => 'sid',
24 callback => \&main,
25 );
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 warn "REQUEST: $path\n";
38
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 pushstream($req);
45 }
46
47 # If they are sending us a message, we give them a thread for that too
48 if($path =~ /sendmessage/) {
49 send_message($req);
50 }
51
52 # Otherwise, lets give them page
53 send_page($req);
54 }
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 sub send_page {
96 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 }
123

  ViewVC Help
Powered by ViewVC 1.1.26