/[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 190 - (show annotations)
Sun Sep 14 00:45:47 2008 UTC (15 years, 7 months ago) by dpavlin
File size: 3529 byte(s)
remove Carp::REPL because it really screws with error reporting.
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 Carp::REPL; ## XXX it would be nice, but it breaks error reporting too much
12 use Frey::ClassLoader;
13
14 my @messages; # Global (shared) list of messages
15 my $got_message; # Flag to indicate that there is a new message to display
16
17 use vars qw( $repl $server );
18
19 #$repl = Continuity::REPL->new;
20
21 =head1 NAME
22
23 Frey::Server - Continuity based server for Frey
24
25 =head2 DESCRIPTION
26
27 This is one of pissible server implementations for Frey. In it's current stage, it's also most complete one.
28
29 =head2 run
30
31 $o->run( $optional_port );
32
33 =cut
34
35 sub run {
36 my ( $self, $port ) = @_;
37 $server = Continuity->new(
38 port => $port || 16001,
39 path_session => 1,
40 cookie_session => 'sid',
41 callback => \&main,
42 debug_level => 1,
43 staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?|xml|json|ya?ml)(\?.*)?$/ },
44 );
45 $Module::Reload::Debug = 1;
46 Frey::ClassLoader->new->load_all_classes();
47 $server->loop;
48 }
49
50 =head2 main
51
52 This is simple dispatcher for our server. Currently it's in flux and
53 documented only in source code.
54
55 =cut
56
57 sub main {
58 my ($req) = @_;
59
60 my $path = $req->request->url->path;
61 warn "REQUEST: $path ",dump( $req->params ),"\n";
62
63 Module::Reload->check if $path =~ m!reload! || $req->param('reload');
64
65 # warn $req->request->header('User_Agent');
66
67 # eval {
68 {
69
70 my $f;
71
72 if ( $path =~ m!/~/([^/]+)(.*)! ) {
73 $f = Frey::Introspect->new( package => $1 );
74 } elsif ( $path =~ m!/ob/([^/]+)(.*)! ) {
75 $f = Frey::ObjectBrowser->new( fey_class => $1 );
76 } elsif ( $path =~ m!/od/([^/]+)(.*)! ) {
77 $f = Frey::ObjectDesigner->new( fey_class => $1 );
78 } elsif ( $path =~ m!/(markup|html)/([^/]+)(.*)! ) {
79 $f = Frey::Run->new( class => $2 );
80 } else {
81 $f = Frey::ClassBrowser->new;
82 }
83 $f->html( $req ) if $f;
84
85 };
86
87 my $self = $req;
88
89 if ( $@ ) {
90 warn $@;
91 #$req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it
92 $req->print( qq{<pre class="error">$@<pre>} );
93 # Carp::REPL::repl;
94
95 }
96
97 # If this is a request for the pushtream, then give them that
98 if($path =~ /pushstream/) {
99 pushstream($req);
100 }
101
102 # If they are sending us a message, we give them a thread for that too
103 if($path =~ /sendmessage/) {
104 send_message($req);
105 }
106
107 }
108
109 # Here we accept a connection to the browser, and keep it open. Meanwhile we
110 # watch the global $got_message variable, and when it gets touched we send off
111 # the list of messages through the held-open connection. Then we let the
112 # browser open a new connection and begin again.
113 sub pushstream {
114 my ($req) = @_;
115 # Set up watch event -- this will be triggered when $got_message is written
116 my $w = Coro::Event->var(var => \$got_message, poll => 'w');
117 while(1) {
118 print STDERR "**** GOT MESSAGE, SENDING ****\n";
119 my $log = join "<br>", @messages;
120 $req->print($log);
121 $req->next;
122 print STDERR "**** Waiting for got_message indicator ****\n";
123 $w->next;
124 }
125 }
126
127
128 # Watch for the user to send us a message. As soon as we get it, we add it to
129 # our list of messages and touch the $got_message flag to let all the
130 # pushstreams know.
131 sub send_message {
132 my ($req) = @_;
133 while(1) {
134 my $msg = $req->param('message');
135 my $name = $req->param('username');
136 if($msg) {
137 unshift @messages, "$name: $msg";
138 pop @messages if $#messages > 15; # Only keep the recent 15 messages
139 }
140 $got_message = 1;
141 $req->print("Got it!");
142 $req->next;
143 }
144 }
145
146 1;

  ViewVC Help
Powered by ViewVC 1.1.26