/[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 106 - (show annotations)
Sun Jul 13 12:22:14 2008 UTC (15 years, 9 months ago) by dpavlin
File size: 3379 byte(s)
added Frey::Collection role to database models
1 package Frey::Server;
2
3 use Moose;
4
5 with 'Frey::Web';
6
7 use Continuity;
8 #use Continuity::REPL;
9 use Data::Dump qw/dump/;
10
11 use Frey::ClassLoader;
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
20 sub run {
21 my ( $self, $port ) = @_;
22 $server = Continuity->new(
23 port => $port || 16001,
24 path_session => 1,
25 cookie_session => 'sid',
26 callback => \&main,
27 debug_level => 1,
28 staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?)$/ },
29 );
30 Frey::ClassLoader->new->load_all_classes();
31 $server->loop;
32 }
33
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 my ($req) = @_;
39
40 my $path = $req->request->url->path;
41 warn "REQUEST: $path\n";
42
43 warn $req->request->header('User_Agent');
44 #warn dump( $req );
45
46 eval {
47
48 if ( $path =~ m!/~/([^/]+)(?:/([^/]*))?! ) {
49 my $f = Frey::Introspect->new( package => $1 );
50 $f->html( $req );
51 }
52
53 if ( $path =~ m!/ob/([^/]+)(?:/([^/]*))?! ) {
54 my $f = Frey::ObjectBrowser->new( fey_class => $1 );
55 $f->html( $req );
56 }
57
58 };
59
60 if ( $@ ) {
61 warn $@;
62 #$req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it
63 $req->print( qq{<pre class="error">$@<pre>} );
64
65 } else {
66
67 my $f = Frey::ClassLoader->new;
68 my $classes = dom2html(
69 ul => [
70 map {
71 warn dump( $_ );
72 my $package = $_;
73 ( li => [
74 a => { href => '/~/' . $package . '/' } => [ $package ],
75 ' <tt>', $f->package_path( $package ), '</tt> ',
76 $package->can('collection') ?
77 ( a => { href => '/ob/' . $package } => [ 'collection' ] ) : '',
78 ] )
79 } $f->classes
80 ],
81 );
82 $req->print( $classes );
83
84 }
85
86 # If this is a request for the pushtream, then give them that
87 if($path =~ /pushstream/) {
88 pushstream($req);
89 }
90
91 # If they are sending us a message, we give them a thread for that too
92 if($path =~ /sendmessage/) {
93 send_message($req);
94 }
95
96 }
97
98 # Here we accept a connection to the browser, and keep it open. Meanwhile we
99 # watch the global $got_message variable, and when it gets touched we send off
100 # the list of messages through the held-open connection. Then we let the
101 # browser open a new connection and begin again.
102 sub pushstream {
103 my ($req) = @_;
104 # Set up watch event -- this will be triggered when $got_message is written
105 my $w = Coro::Event->var(var => \$got_message, poll => 'w');
106 while(1) {
107 print STDERR "**** GOT MESSAGE, SENDING ****\n";
108 my $log = join "<br>", @messages;
109 $req->print($log);
110 $req->next;
111 print STDERR "**** Waiting for got_message indicator ****\n";
112 $w->next;
113 }
114 }
115
116
117 # Watch for the user to send us a message. As soon as we get it, we add it to
118 # our list of messages and touch the $got_message flag to let all the
119 # pushstreams know.
120 sub send_message {
121 my ($req) = @_;
122 while(1) {
123 my $msg = $req->param('message');
124 my $name = $req->param('username');
125 if($msg) {
126 unshift @messages, "$name: $msg";
127 pop @messages if $#messages > 15; # Only keep the recent 15 messages
128 }
129 $got_message = 1;
130 $req->print("Got it!");
131 $req->next;
132 }
133 }
134
135 1;

  ViewVC Help
Powered by ViewVC 1.1.26