/[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 53 - (hide annotations)
Sat Jul 5 15:19:55 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3761 byte(s)
huge wapping changes all over the place [0.05]

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

  ViewVC Help
Powered by ViewVC 1.1.26