/[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 182 - (hide annotations)
Sun Sep 7 19:44:08 2008 UTC (15 years, 8 months ago) by dpavlin
File size: 3537 byte(s)
added some pod and cleanup
1 dpavlin 19 package Frey::Server;
2 dpavlin 2
3 dpavlin 55 use Moose;
4 dpavlin 10
5 dpavlin 100 with 'Frey::Web';
6    
7 dpavlin 2 use Continuity;
8 dpavlin 36 #use Continuity::REPL;
9 dpavlin 2 use Data::Dump qw/dump/;
10    
11 dpavlin 121 use Carp::REPL;
12 dpavlin 101 use Frey::ClassLoader;
13 dpavlin 23
14 dpavlin 2 my @messages; # Global (shared) list of messages
15     my $got_message; # Flag to indicate that there is a new message to display
16    
17 dpavlin 10 use vars qw( $repl $server );
18 dpavlin 2
19 dpavlin 34 #$repl = Continuity::REPL->new;
20 dpavlin 37
21 dpavlin 182 =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 dpavlin 25 sub run {
36 dpavlin 103 my ( $self, $port ) = @_;
37 dpavlin 64 $server = Continuity->new(
38 dpavlin 103 port => $port || 16001,
39 dpavlin 64 path_session => 1,
40     cookie_session => 'sid',
41     callback => \&main,
42     debug_level => 1,
43 dpavlin 162 staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?|xml|json|ya?ml)(\?.*)?$/ },
44 dpavlin 64 );
45 dpavlin 114 $Module::Reload::Debug = 1;
46 dpavlin 101 Frey::ClassLoader->new->load_all_classes();
47 dpavlin 25 $server->loop;
48     }
49 dpavlin 2
50 dpavlin 182 =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 dpavlin 2 sub main {
58 dpavlin 37 my ($req) = @_;
59 dpavlin 2
60 dpavlin 37 my $path = $req->request->url->path;
61 dpavlin 142 warn "REQUEST: $path ",dump( $req->params ),"\n";
62 dpavlin 2
63 dpavlin 142 Module::Reload->check if $path =~ m!reload! || $req->param('reload');
64 dpavlin 120
65 dpavlin 142 # warn $req->request->header('User_Agent');
66 dpavlin 2
67 dpavlin 142 # eval {
68     {
69 dpavlin 66
70 dpavlin 114 my $f;
71 dpavlin 66
72 dpavlin 114 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 dpavlin 158 } elsif ( $path =~ m!/markup/([^/]+)(.*)! ) {
79 dpavlin 180 $f = Frey::Run->new( class => $1 );
80 dpavlin 121 } else {
81     $f = Frey::ClassBrowser->new;
82 dpavlin 66 }
83 dpavlin 114 $f->html( $req ) if $f;
84 dpavlin 66
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 dpavlin 121 Carp::REPL::repl; # FIXME if $self->debug
92 dpavlin 67
93 dpavlin 53 }
94    
95 dpavlin 37 # If this is a request for the pushtream, then give them that
96     if($path =~ /pushstream/) {
97     pushstream($req);
98     }
99 dpavlin 121
100     if ( $path =~ m/die/ ) {
101     Carp::REPL::repl; # FIXME if $self->debug
102     }
103    
104 dpavlin 37 # 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 dpavlin 2
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 dpavlin 25 1;

  ViewVC Help
Powered by ViewVC 1.1.26