/[Frey]/branches/mojo/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 /branches/mojo/lib/Frey/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 60 - (hide annotations)
Tue Jul 8 12:20:08 2008 UTC (15 years, 10 months ago) by dpavlin
Original Path: trunk/lib/Frey/Server.pm
File size: 3022 byte(s)
skeleton of server-side object browser
1 dpavlin 19 package Frey::Server;
2 dpavlin 2
3 dpavlin 55 use Moose;
4 dpavlin 10
5 dpavlin 2 use Continuity;
6 dpavlin 36 #use Continuity::REPL;
7 dpavlin 53 use Continuity::Widget::DomNode;
8 dpavlin 2 use Data::Dump qw/dump/;
9    
10 dpavlin 55 use Frey;
11     use Frey::Introspect;
12 dpavlin 60 use Frey::ObjectBrowser;
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     $server = Continuity->new(
21 dpavlin 37 port => 16001,
22     path_session => 1,
23     cookie_session => 'sid',
24     callback => \&main,
25 dpavlin 42 debug_level => 1,
26 dpavlin 48 staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?)$/ },
27 dpavlin 34 );
28 dpavlin 37
29 dpavlin 25 sub run {
30     $server->loop;
31     }
32 dpavlin 2
33     # This is the main entrypoint. We are looking for one of three things -- a
34     # pushstream, a sent message, or a request for the main HTML. We delegate each
35     # of these cases, none of which will return (they all loop forever).
36     sub main {
37 dpavlin 37 my ($req) = @_;
38 dpavlin 2
39 dpavlin 37 my $path = $req->request->url->path;
40     warn "REQUEST: $path\n";
41 dpavlin 2
42     warn $req->request->header('User_Agent');
43     #warn dump( $req );
44    
45 dpavlin 53 if ( $path =~ m!/~/([^/]+)(?:/([^/]*))?! ) {
46     my $f = Frey::Introspect->new( package => $1 );
47     $f->html( $req );
48     }
49    
50 dpavlin 60 if ( $path =~ m!/ob/([^/]+)(?:/([^/]*))?! ) {
51     my $f = Frey::ObjectBrowser->new;
52     $f->html( $req );
53     }
54 dpavlin 37 # If this is a request for the pushtream, then give them that
55     if($path =~ /pushstream/) {
56     pushstream($req);
57     }
58 dpavlin 2
59 dpavlin 37 # If they are sending us a message, we give them a thread for that too
60     if($path =~ /sendmessage/) {
61     send_message($req);
62     }
63 dpavlin 2
64 dpavlin 55 while ( 1 ) {
65     my $f = Frey->new;
66 dpavlin 56 my $classes = Continuity::Widget::DomNode->create(
67     ul => [
68     map {
69     warn dump( $_ );
70     my ( $package, $path ) = %$_;
71     ( li => [ a => { href => '/~/' . $package . '/' } => [ $package ], " <tt>$path</tt>" ] )
72     } @{ $f->classes }
73     ],
74     )->to_string;
75     $req->print( $classes );
76 dpavlin 55 $req->next;
77 dpavlin 53 }
78 dpavlin 2 }
79    
80     # Here we accept a connection to the browser, and keep it open. Meanwhile we
81     # watch the global $got_message variable, and when it gets touched we send off
82     # the list of messages through the held-open connection. Then we let the
83     # browser open a new connection and begin again.
84     sub pushstream {
85     my ($req) = @_;
86     # Set up watch event -- this will be triggered when $got_message is written
87     my $w = Coro::Event->var(var => \$got_message, poll => 'w');
88     while(1) {
89     print STDERR "**** GOT MESSAGE, SENDING ****\n";
90     my $log = join "<br>", @messages;
91     $req->print($log);
92     $req->next;
93     print STDERR "**** Waiting for got_message indicator ****\n";
94     $w->next;
95     }
96     }
97    
98    
99     # Watch for the user to send us a message. As soon as we get it, we add it to
100     # our list of messages and touch the $got_message flag to let all the
101     # pushstreams know.
102     sub send_message {
103     my ($req) = @_;
104     while(1) {
105     my $msg = $req->param('message');
106     my $name = $req->param('username');
107     if($msg) {
108     unshift @messages, "$name: $msg";
109     pop @messages if $#messages > 15; # Only keep the recent 15 messages
110     }
111     $got_message = 1;
112     $req->print("Got it!");
113     $req->next;
114     }
115     }
116    
117 dpavlin 25 1;

  ViewVC Help
Powered by ViewVC 1.1.26