/[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 42 - (show annotations)
Mon Jun 30 20:02:16 2008 UTC (14 years, 5 months ago) by dpavlin
File size: 3427 byte(s)
 r44@eeepy:  dpavlin | 2008-06-30 18:08:33 +0200
 Change logic of generating html page using Frey::HTML->page
 because Template::Declare around_template is doing it around
 *all* templates (including those used with show).

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

  ViewVC Help
Powered by ViewVC 1.1.26