/[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

Diff of /trunk/lib/Frey/Server.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/server.pl revision 2 by dpavlin, Sat Jun 28 11:49:35 2008 UTC trunk/lib/Frey/Server.pm revision 53 by dpavlin, Sat Jul 5 15:19:55 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  package Frey::Server;
   
 # "HTTP Push" is not readily attainable, so instead we will simulate it using a  
 # long-pull, aka "Comet". The client browser simply opens an HTTP connection to  
 # the server and waits for a response. The server doesn't respond until there  
 # is some event (here a new message), giving the appearance of HTTP-push.  
 #  
 # Each user gets three continuations for these three cases:  
 #  
 #   - Initial load or reload of the page  
 #   - Sending a message (uses AJAX on the client)  
 #   - Recieving messages (uses COMET on the client)  
2    
3  use strict;  use strict;
4  use lib 'lib';  use warnings;
5    
6  use Continuity;  use Continuity;
7  use Continuity::REPL;  #use Continuity::REPL;
8    use Continuity::Widget::DomNode;
9  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
10    
11  use View;  use base 'Frey';
12    use Frey::HTML;
13  use Template::Declare;  use Frey::ObjectBrowser;
 use Template::Declare::Tags; # defaults to 'HTML'  
 Template::Declare->init( roots => ['View']);  
 warn "templates = ",dump( Template::Declare->templates );  
14    
15  my @messages;    # Global (shared) list of messages  my @messages;    # Global (shared) list of messages
16  my $got_message; # Flag to indicate that there is a new message to display  my $got_message; # Flag to indicate that there is a new message to display
17    
18  use vars qw( $repl $server @messages );  use vars qw( $repl $server );
 $repl = Continuity::REPL->new;  
19    
20    #$repl = Continuity::REPL->new;
21  $server = Continuity->new(  $server = Continuity->new(
22    port => 16001,          port => 16001,
23    path_session => 1,          path_session => 1,
24    cookie_session => 'sid',          cookie_session => 'sid',
25            callback => \&main,
26            debug_level => 1,
27            staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|html?)$/ },
28  );  );
 $server->debug_level( 2 );  
29    
30  $server->loop;  sub run {
31            $server->loop;
32    }
33    
34  # This is the main entrypoint. We are looking for one of three things -- a  # 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  # 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).  # of these cases, none of which will return (they all loop forever).
37  sub main {  sub main {
38    my ($req) = @_;          my ($req) = @_;
39        
40    my $path = $req->request->url->path;          my $path = $req->request->url->path;
41    print STDERR "Path: '$path'\n";          warn "REQUEST: $path\n";
42    
43          warn $req->request->header('User_Agent');          warn $req->request->header('User_Agent');
44  #warn dump( $req );  #warn dump( $req );
45    
46    # If this is a request for the pushtream, then give them that          if ( $path =~ m!/~/([^/]+)(?:/([^/]*))?! ) {
47    if($path =~ /pushstream/) {                  my $f = Frey::Introspect->new( package => $1 );
48      pushstream($req);                  $f->html( $req );
49    }          }
50    
51            # If this is a request for the pushtream, then give them that
52            if($path =~ /pushstream/) {
53                    pushstream($req);
54            }
55        
56    # If they are sending us a message, we give them a thread for that too          # If they are sending us a message, we give them a thread for that too
57    if($path =~ /sendmessage/) {          if($path =~ /sendmessage/) {
58      send_message($req);                  send_message($req);
59    }          }
60    
61            if($path =~ m!^/ob!) {
62                    Frey::ObjectBrowser->new( req => $req )->html;
63            }
64    
65    # Otherwise, lets give them the base page          # Otherwise, lets give them page
66    send_base_page($req);          send_page($req);
67  }  }
68    
69  # Here we accept a connection to the browser, and keep it open. Meanwhile we  # Here we accept a connection to the browser, and keep it open. Meanwhile we
# Line 104  sub send_message { Line 105  sub send_message {
105    
106  # This isn't a pushstream, nor a new message. It is just the main page. We loop  # This isn't a pushstream, nor a new message. It is just the main page. We loop
107  # in case they ask for it multiple times :)  # in case they ask for it multiple times :)
108  sub send_base_page {  sub send_page {
109    my ($req) = @_;          my ($req) = @_;
110    while(1) {          my $templates = Template::Declare->templates;
111          warn "base page";          while(1) {
112          $req->out( Template::Declare->show( 'user' ) );                  warn "param = ",dump($req->param);
113  =for later                  my $path = $req->request->url->path;
114      $req->print(qq{  
115        <html>                  my $html;
116          <head>  
117            <title>Chat!</title>                  if ( $path =~ m/::/ ) {
118            <script src="jquery.js" type="text/javascript"></script>                          my ( undef, $module, $method ) = split(m!/!, $path, 3);
119            <script src="chat-ajax-push.js" type="text/javascript"></script>  
120          </head>                          if ( ! defined( $templates->{$module} ) ) {
121          <body>                                  $req->conn->send_status_line( 404, "$module" );
122            <form id=f>                                  $html = "Package $module not found";
123            <input type=text id=username name=usernamename size=10>                          } elsif ( ! $method ) {
124            <input type=text id=message name=message size=50>                                  $html = Frey::HTML->page( 'package-templates', $req, $module );
125            <input type=submit name="sendbutton" value="Send" id="sendbutton">                          } elsif ( grep(/^\Q$method\E$/, @{ $templates->{$module} }) ) {
126            <span id=status></span>                                  $html = Frey::HTML->page( $method, $req );
127            </form>                          } else {
128            <br>                                  $req->conn->send_status_line( 404, "$module $method" );
129            <div id=log>-- no messages yet --</div>                                  $html = "Package $module doesn't have $method";
130          </body>                          }
131        </html>                  } else {
132      });                          warn "fallback to status page\n";
133  =cut                          $html = Frey::HTML->page( 'status' );
134      $req->next;                  }
135    }  
136                    $req->print( $html );
137                    warn ">> ",length( $html ), " bytes\n";
138                    $req->next;
139            }
140  }  }
141    
142    1;

Legend:
Removed from v.2  
changed lines
  Added in v.53

  ViewVC Help
Powered by ViewVC 1.1.26