/[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 11 - (hide annotations)
Sat Jun 28 22:30:06 2008 UTC (15 years, 9 months ago) by dpavlin
Original Path: trunk/server.pl
File MIME type: text/plain
File size: 3597 byte(s)
return 500 Internal Server Error (RFC 2616) for records which doesn't exist
1 dpavlin 2 #!/usr/bin/perl
2    
3 dpavlin 10 # server of yet unnamed framework
4 dpavlin 2
5 dpavlin 10 use warnings;
6 dpavlin 2 use strict;
7 dpavlin 10
8 dpavlin 2 use lib 'lib';
9 dpavlin 10
10 dpavlin 2 use Continuity;
11     use Continuity::REPL;
12     use Data::Dump qw/dump/;
13    
14 dpavlin 10 use HTML;
15 dpavlin 2
16     my @messages; # Global (shared) list of messages
17     my $got_message; # Flag to indicate that there is a new message to display
18    
19 dpavlin 10 use vars qw( $repl $server );
20 dpavlin 2 $repl = Continuity::REPL->new;
21    
22     $server = Continuity->new(
23     port => 16001,
24     path_session => 1,
25     cookie_session => 'sid',
26     );
27     $server->debug_level( 2 );
28    
29     $server->loop;
30    
31     # This is the main entrypoint. We are looking for one of three things -- a
32     # pushstream, a sent message, or a request for the main HTML. We delegate each
33     # of these cases, none of which will return (they all loop forever).
34     sub main {
35     my ($req) = @_;
36    
37     my $path = $req->request->url->path;
38     print STDERR "Path: '$path'\n";
39    
40     warn $req->request->header('User_Agent');
41     #warn dump( $req );
42    
43     # If this is a request for the pushtream, then give them that
44     if($path =~ /pushstream/) {
45     pushstream($req);
46     }
47    
48     # If they are sending us a message, we give them a thread for that too
49     if($path =~ /sendmessage/) {
50     send_message($req);
51     }
52    
53     # Otherwise, lets give them the base page
54     send_base_page($req);
55     }
56    
57     # Here we accept a connection to the browser, and keep it open. Meanwhile we
58     # watch the global $got_message variable, and when it gets touched we send off
59     # the list of messages through the held-open connection. Then we let the
60     # browser open a new connection and begin again.
61     sub pushstream {
62     my ($req) = @_;
63     # Set up watch event -- this will be triggered when $got_message is written
64     my $w = Coro::Event->var(var => \$got_message, poll => 'w');
65     while(1) {
66     print STDERR "**** GOT MESSAGE, SENDING ****\n";
67     my $log = join "<br>", @messages;
68     $req->print($log);
69     $req->next;
70     print STDERR "**** Waiting for got_message indicator ****\n";
71     $w->next;
72     }
73     }
74    
75    
76     # Watch for the user to send us a message. As soon as we get it, we add it to
77     # our list of messages and touch the $got_message flag to let all the
78     # pushstreams know.
79     sub send_message {
80     my ($req) = @_;
81     while(1) {
82     my $msg = $req->param('message');
83     my $name = $req->param('username');
84     if($msg) {
85     unshift @messages, "$name: $msg";
86     pop @messages if $#messages > 15; # Only keep the recent 15 messages
87     }
88     $got_message = 1;
89     $req->print("Got it!");
90     $req->next;
91     }
92     }
93    
94     # This isn't a pushstream, nor a new message. It is just the main page. We loop
95     # in case they ask for it multiple times :)
96     sub send_base_page {
97     my ($req) = @_;
98     while(1) {
99 dpavlin 10 warn "param = ",dump($req->param);
100    
101     my $id = $req->param('id');
102     # $id = 1 unless $id =~ m/^\d+$/;
103    
104     use Strix::User;
105     my $user = Strix::User->new( id => $id );
106    
107     if ( ! $user ) {
108 dpavlin 11 $req->conn->send_status_line( 500, "user $id" );
109 dpavlin 10 $req->print( "Can't find user with id $id" );
110     } else {
111     $req->print( HTML->view( 'user', $user ) );
112     }
113    
114 dpavlin 2 =for later
115     $req->print(qq{
116     <html>
117     <head>
118     <title>Chat!</title>
119     <script src="jquery.js" type="text/javascript"></script>
120     <script src="chat-ajax-push.js" type="text/javascript"></script>
121     </head>
122     <body>
123     <form id=f>
124     <input type=text id=username name=usernamename size=10>
125     <input type=text id=message name=message size=50>
126     <input type=submit name="sendbutton" value="Send" id="sendbutton">
127     <span id=status></span>
128     </form>
129     <br>
130     <div id=log>-- no messages yet --</div>
131     </body>
132     </html>
133     });
134     =cut
135     $req->next;
136     }
137     }
138    
139    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26