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

  ViewVC Help
Powered by ViewVC 1.1.26