/[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 55 - (show annotations)
Sat Jul 5 19:00:10 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 2632 byte(s)
remove all usage of Template::Declare [0.05]

- die gracefully if class can't be loaded
1 package Frey::Server;
2
3 use Moose;
4
5 use Continuity;
6 #use Continuity::REPL;
7 use Continuity::Widget::DomNode;
8 use Data::Dump qw/dump/;
9
10 use Frey;
11 use Frey::Introspect;
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 staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?)$/ },
26 );
27
28 sub run {
29 $server->loop;
30 }
31
32 # This is the main entrypoint. We are looking for one of three things -- a
33 # pushstream, a sent message, or a request for the main HTML. We delegate each
34 # of these cases, none of which will return (they all loop forever).
35 sub main {
36 my ($req) = @_;
37
38 my $path = $req->request->url->path;
39 warn "REQUEST: $path\n";
40
41 warn $req->request->header('User_Agent');
42 #warn dump( $req );
43
44 if ( $path =~ m!/~/([^/]+)(?:/([^/]*))?! ) {
45 my $f = Frey::Introspect->new( package => $1 );
46 $f->html( $req );
47 }
48
49 # If this is a request for the pushtream, then give them that
50 if($path =~ /pushstream/) {
51 pushstream($req);
52 }
53
54 # If they are sending us a message, we give them a thread for that too
55 if($path =~ /sendmessage/) {
56 send_message($req);
57 }
58
59 while ( 1 ) {
60 my $f = Frey->new;
61 $req->print( dump( $f->classes ) );
62 $req->next;
63 }
64 }
65
66 # Here we accept a connection to the browser, and keep it open. Meanwhile we
67 # watch the global $got_message variable, and when it gets touched we send off
68 # the list of messages through the held-open connection. Then we let the
69 # browser open a new connection and begin again.
70 sub pushstream {
71 my ($req) = @_;
72 # Set up watch event -- this will be triggered when $got_message is written
73 my $w = Coro::Event->var(var => \$got_message, poll => 'w');
74 while(1) {
75 print STDERR "**** GOT MESSAGE, SENDING ****\n";
76 my $log = join "<br>", @messages;
77 $req->print($log);
78 $req->next;
79 print STDERR "**** Waiting for got_message indicator ****\n";
80 $w->next;
81 }
82 }
83
84
85 # Watch for the user to send us a message. As soon as we get it, we add it to
86 # our list of messages and touch the $got_message flag to let all the
87 # pushstreams know.
88 sub send_message {
89 my ($req) = @_;
90 while(1) {
91 my $msg = $req->param('message');
92 my $name = $req->param('username');
93 if($msg) {
94 unshift @messages, "$name: $msg";
95 pop @messages if $#messages > 15; # Only keep the recent 15 messages
96 }
97 $got_message = 1;
98 $req->print("Got it!");
99 $req->next;
100 }
101 }
102
103 1;

  ViewVC Help
Powered by ViewVC 1.1.26