/[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 42 - (hide annotations)
Mon Jun 30 20:02:16 2008 UTC (14 years, 7 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 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 dpavlin 36 #use Continuity::REPL;
8 dpavlin 2 use Data::Dump qw/dump/;
9    
10 dpavlin 23 use base 'Frey';
11     use Frey::HTML;
12    
13 dpavlin 2 my @messages; # Global (shared) list of messages
14     my $got_message; # Flag to indicate that there is a new message to display
15    
16 dpavlin 10 use vars qw( $repl $server );
17 dpavlin 2
18 dpavlin 34 #$repl = Continuity::REPL->new;
19     $server = Continuity->new(
20 dpavlin 37 port => 16001,
21     path_session => 1,
22     cookie_session => 'sid',
23     callback => \&main,
24 dpavlin 42 debug_level => 1,
25 dpavlin 34 );
26 dpavlin 37
27 dpavlin 25 sub run {
28     $server->loop;
29     }
30 dpavlin 2
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 dpavlin 37 my ($req) = @_;
36 dpavlin 2
37 dpavlin 37 my $path = $req->request->url->path;
38     warn "REQUEST: $path\n";
39 dpavlin 2
40     warn $req->request->header('User_Agent');
41     #warn dump( $req );
42    
43 dpavlin 37 # If this is a request for the pushtream, then give them that
44     if($path =~ /pushstream/) {
45     pushstream($req);
46     }
47 dpavlin 2
48 dpavlin 37 # 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 dpavlin 2
53 dpavlin 37 # Otherwise, lets give them page
54     send_page($req);
55 dpavlin 2 }
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 dpavlin 19 sub send_page {
97 dpavlin 23 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 dpavlin 30 my $html;
104    
105 dpavlin 23 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 dpavlin 30 $html = "Package $module not found";
111 dpavlin 28 } elsif ( ! $method ) {
112 dpavlin 30 $html = Frey::HTML->page( 'package-methods', $req, $module );
113 dpavlin 23 } elsif ( grep(/^\Q$method\E$/, @{ $templates->{$module} }) ) {
114 dpavlin 30 $html = Frey::HTML->page( $method, $req );
115 dpavlin 23 } else {
116     $req->conn->send_status_line( 404, "$module $method" );
117 dpavlin 30 $html = "Package $module doesn't have $method";
118 dpavlin 23 }
119     } else {
120 dpavlin 30 warn "fallback to status page\n";
121     $html = Frey::HTML->page( 'status' );
122 dpavlin 23 }
123 dpavlin 30
124     $req->print( $html );
125     warn ">> ",length( $html ), " bytes\n";
126 dpavlin 23 $req->next;
127     }
128 dpavlin 2 }
129    
130 dpavlin 25 1;

  ViewVC Help
Powered by ViewVC 1.1.26