/[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 184 - (show annotations)
Tue Sep 9 23:15:46 2008 UTC (15 years, 7 months ago) by dpavlin
File size: 3544 byte(s)
added html call to class browser
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;
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 if ( $@ ) {
88 warn $@;
89 #$req->conn->send_error( 404 ); # FIXME this should probably be 500, but we can't ship page with it
90 $req->print( qq{<pre class="error">$@<pre>} );
91 Carp::REPL::repl; # FIXME if $self->debug
92
93 }
94
95 # If this is a request for the pushtream, then give them that
96 if($path =~ /pushstream/) {
97 pushstream($req);
98 }
99
100 if ( $path =~ m/die/ ) {
101 Carp::REPL::repl; # FIXME if $self->debug
102 }
103
104 # If they are sending us a message, we give them a thread for that too
105 if($path =~ /sendmessage/) {
106 send_message($req);
107 }
108
109 }
110
111 # Here we accept a connection to the browser, and keep it open. Meanwhile we
112 # watch the global $got_message variable, and when it gets touched we send off
113 # the list of messages through the held-open connection. Then we let the
114 # browser open a new connection and begin again.
115 sub pushstream {
116 my ($req) = @_;
117 # Set up watch event -- this will be triggered when $got_message is written
118 my $w = Coro::Event->var(var => \$got_message, poll => 'w');
119 while(1) {
120 print STDERR "**** GOT MESSAGE, SENDING ****\n";
121 my $log = join "<br>", @messages;
122 $req->print($log);
123 $req->next;
124 print STDERR "**** Waiting for got_message indicator ****\n";
125 $w->next;
126 }
127 }
128
129
130 # Watch for the user to send us a message. As soon as we get it, we add it to
131 # our list of messages and touch the $got_message flag to let all the
132 # pushstreams know.
133 sub send_message {
134 my ($req) = @_;
135 while(1) {
136 my $msg = $req->param('message');
137 my $name = $req->param('username');
138 if($msg) {
139 unshift @messages, "$name: $msg";
140 pop @messages if $#messages > 15; # Only keep the recent 15 messages
141 }
142 $got_message = 1;
143 $req->print("Got it!");
144 $req->next;
145 }
146 }
147
148 1;

  ViewVC Help
Powered by ViewVC 1.1.26