/[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 19 - (hide annotations)
Sun Jun 29 12:55:39 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 2656 byte(s)
- create slim server caller in bin for Frey::Server
- restructure view packages
- beginning of rest dispatcher based on Template::Declare
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     use Continuity::REPL;
8     use Data::Dump qw/dump/;
9    
10 dpavlin 10 use HTML;
11 dpavlin 2
12     my @messages; # Global (shared) list of messages
13     my $got_message; # Flag to indicate that there is a new message to display
14    
15 dpavlin 10 use vars qw( $repl $server );
16 dpavlin 2 $repl = Continuity::REPL->new;
17    
18     $server = Continuity->new(
19     port => 16001,
20     path_session => 1,
21     cookie_session => 'sid',
22 dpavlin 19 callback => \&main,
23 dpavlin 2 );
24     $server->debug_level( 2 );
25    
26     $server->loop;
27    
28     # This is the main entrypoint. We are looking for one of three things -- a
29     # pushstream, a sent message, or a request for the main HTML. We delegate each
30     # of these cases, none of which will return (they all loop forever).
31     sub main {
32     my ($req) = @_;
33    
34     my $path = $req->request->url->path;
35 dpavlin 19 warn "REQUEST: $path\n";
36 dpavlin 2
37     warn $req->request->header('User_Agent');
38     #warn dump( $req );
39    
40     # If this is a request for the pushtream, then give them that
41     if($path =~ /pushstream/) {
42 dpavlin 19 pushstream($req);
43 dpavlin 2 }
44    
45     # If they are sending us a message, we give them a thread for that too
46     if($path =~ /sendmessage/) {
47 dpavlin 19 send_message($req);
48 dpavlin 2 }
49    
50 dpavlin 19 # Otherwise, lets give them page
51     send_page($req);
52 dpavlin 2 }
53    
54     # Here we accept a connection to the browser, and keep it open. Meanwhile we
55     # watch the global $got_message variable, and when it gets touched we send off
56     # the list of messages through the held-open connection. Then we let the
57     # browser open a new connection and begin again.
58     sub pushstream {
59     my ($req) = @_;
60     # Set up watch event -- this will be triggered when $got_message is written
61     my $w = Coro::Event->var(var => \$got_message, poll => 'w');
62     while(1) {
63     print STDERR "**** GOT MESSAGE, SENDING ****\n";
64     my $log = join "<br>", @messages;
65     $req->print($log);
66     $req->next;
67     print STDERR "**** Waiting for got_message indicator ****\n";
68     $w->next;
69     }
70     }
71    
72    
73     # Watch for the user to send us a message. As soon as we get it, we add it to
74     # our list of messages and touch the $got_message flag to let all the
75     # pushstreams know.
76     sub send_message {
77     my ($req) = @_;
78     while(1) {
79     my $msg = $req->param('message');
80     my $name = $req->param('username');
81     if($msg) {
82     unshift @messages, "$name: $msg";
83     pop @messages if $#messages > 15; # Only keep the recent 15 messages
84     }
85     $got_message = 1;
86     $req->print("Got it!");
87     $req->next;
88     }
89     }
90    
91     # This isn't a pushstream, nor a new message. It is just the main page. We loop
92     # in case they ask for it multiple times :)
93 dpavlin 19 sub send_page {
94 dpavlin 2 my ($req) = @_;
95     while(1) {
96 dpavlin 10 warn "param = ",dump($req->param);
97 dpavlin 19 $req->print( HTML->view( 'status' ) );
98 dpavlin 2 $req->next;
99 dpavlin 13 Module::Refresh->refresh;
100 dpavlin 2 }
101     }
102    

  ViewVC Help
Powered by ViewVC 1.1.26