/[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 48 - (show annotations)
Wed Jul 2 12:36:59 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3504 byte(s)
support .htm? as static file format
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 use Frey::HTML;
12
13 my @messages; # Global (shared) list of messages
14 my $got_message; # Flag to indicate that there is a new message to display
15
16 use vars qw( $repl $server );
17
18 #$repl = Continuity::REPL->new;
19 $server = Continuity->new(
20 port => 16001,
21 path_session => 1,
22 cookie_session => 'sid',
23 callback => \&main,
24 debug_level => 1,
25 staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?)$/ },
26 );
27
28 sub run {
29 $server->loop;
30 }
31
32 # This is the main entrypoint. We are looking for one of three things -- a
33 # pushstream, a sent message, or a request for the main HTML. We delegate each
34 # of these cases, none of which will return (they all loop forever).
35 sub main {
36 my ($req) = @_;
37
38 my $path = $req->request->url->path;
39 warn "REQUEST: $path\n";
40
41 warn $req->request->header('User_Agent');
42 #warn dump( $req );
43
44 # If this is a request for the pushtream, then give them that
45 if($path =~ /pushstream/) {
46 pushstream($req);
47 }
48
49 # If they are sending us a message, we give them a thread for that too
50 if($path =~ /sendmessage/) {
51 send_message($req);
52 }
53
54 # Otherwise, lets give them page
55 send_page($req);
56 }
57
58 # Here we accept a connection to the browser, and keep it open. Meanwhile we
59 # watch the global $got_message variable, and when it gets touched we send off
60 # the list of messages through the held-open connection. Then we let the
61 # browser open a new connection and begin again.
62 sub pushstream {
63 my ($req) = @_;
64 # Set up watch event -- this will be triggered when $got_message is written
65 my $w = Coro::Event->var(var => \$got_message, poll => 'w');
66 while(1) {
67 print STDERR "**** GOT MESSAGE, SENDING ****\n";
68 my $log = join "<br>", @messages;
69 $req->print($log);
70 $req->next;
71 print STDERR "**** Waiting for got_message indicator ****\n";
72 $w->next;
73 }
74 }
75
76
77 # Watch for the user to send us a message. As soon as we get it, we add it to
78 # our list of messages and touch the $got_message flag to let all the
79 # pushstreams know.
80 sub send_message {
81 my ($req) = @_;
82 while(1) {
83 my $msg = $req->param('message');
84 my $name = $req->param('username');
85 if($msg) {
86 unshift @messages, "$name: $msg";
87 pop @messages if $#messages > 15; # Only keep the recent 15 messages
88 }
89 $got_message = 1;
90 $req->print("Got it!");
91 $req->next;
92 }
93 }
94
95 # This isn't a pushstream, nor a new message. It is just the main page. We loop
96 # in case they ask for it multiple times :)
97 sub send_page {
98 my ($req) = @_;
99 my $templates = Template::Declare->templates;
100 while(1) {
101 warn "param = ",dump($req->param);
102 my $path = $req->request->url->path;
103
104 my $html;
105
106 if ( $path =~ m/::/ ) {
107 my ( undef, $module, $method ) = split(m!/!, $path, 3);
108
109 if ( ! defined( $templates->{$module} ) ) {
110 $req->conn->send_status_line( 404, "$module" );
111 $html = "Package $module not found";
112 } elsif ( ! $method ) {
113 $html = Frey::HTML->page( 'package-methods', $req, $module );
114 } elsif ( grep(/^\Q$method\E$/, @{ $templates->{$module} }) ) {
115 $html = Frey::HTML->page( $method, $req );
116 } else {
117 $req->conn->send_status_line( 404, "$module $method" );
118 $html = "Package $module doesn't have $method";
119 }
120 } else {
121 warn "fallback to status page\n";
122 $html = Frey::HTML->page( 'status' );
123 }
124
125 $req->print( $html );
126 warn ">> ",length( $html ), " bytes\n";
127 $req->next;
128 }
129 }
130
131 1;

  ViewVC Help
Powered by ViewVC 1.1.26