Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/net/fake_http_server.cxx

Go to the documentation of this file.
00001 // Filename: fake_http_server.cxx
00002 // Created by:  drose (10Dec02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include <pandabase.h>
00020 
00021 #include "queuedConnectionManager.h"
00022 #include "queuedConnectionListener.h"
00023 #include "queuedConnectionReader.h"
00024 #include "connectionWriter.h"
00025 #include "netAddress.h"
00026 #include "connection.h"
00027 #include "netDatagram.h"
00028 #include "pmap.h"
00029 
00030 #include <ctype.h>
00031 
00032 QueuedConnectionManager cm;
00033 QueuedConnectionReader reader(&cm, 10);
00034 ConnectionWriter writer(&cm, 10);
00035 
00036 class ClientState {
00037 public:
00038   ClientState(Connection *client);
00039   void receive_data(const Datagram &data);
00040   void receive_line(string line);
00041 
00042   Connection *_client;
00043   string _received;
00044 };
00045 
00046 ClientState::
00047 ClientState(Connection *client) {
00048   _client = client;
00049 }
00050 
00051 void ClientState::
00052 receive_data(const Datagram &data) {
00053   _received += data.get_message();
00054   size_t next = 0;
00055   size_t newline = _received.find('\n', next);
00056   while (newline != string::npos) {
00057     size_t last = next;
00058     next = newline + 1;
00059     if (newline > 0 && _received[newline - 1] == '\r') {
00060       newline--;
00061     }
00062     receive_line(_received.substr(last, newline - last));
00063     if (next < _received.size() && _received[next] == '\r') {
00064       next++;
00065     }
00066     newline = _received.find('\n', next);
00067   }
00068   _received = _received.substr(next);
00069 }
00070 
00071 void ClientState::
00072 receive_line(string line) {
00073   cerr << "received: " << line << "\n";
00074   // trim trailing whitespace.
00075   size_t size = line.size();
00076   while (size > 0 && isspace(line[size - 1])) {
00077     size--;
00078   }
00079   if (size != line.size()) {
00080     line = line.substr(0, size);
00081   }
00082 
00083   /*
00084   if (line.empty()) {
00085     // Start to honor the request, as if we cared.
00086     Datagram dg;
00087     dg.append_data("HTTP/1.1 200 OK\r\n");
00088     writer.send(dg, _client);
00089     // Close the connection!
00090     cm.close_connection(_client);
00091   }
00092   */
00093 }
00094 
00095 
00096 int
00097 main(int argc, char *argv[]) {
00098   if (argc != 2) {
00099     nout << "fake_http_server port\n";
00100     exit(1);
00101   }
00102 
00103   int port = atoi(argv[1]);
00104 
00105   PT(Connection) rendezvous = cm.open_TCP_server_rendezvous(port, 5);
00106 
00107   if (rendezvous.is_null()) {
00108     nout << "Cannot grab port " << port << ".\n";
00109     exit(1);
00110   }
00111 
00112   nout << "Listening for connections on port " << port << "\n";
00113 
00114   QueuedConnectionListener listener(&cm, 1);
00115   listener.add_connection(rendezvous);
00116 
00117   typedef pmap< PT(Connection), ClientState > Clients;
00118   Clients clients;
00119 
00120   reader.set_raw_mode(1);
00121   writer.set_raw_mode(1);
00122 
00123   bool shutdown = false;
00124   while (!shutdown) {
00125     // Check for new clients.
00126     while (listener.new_connection_available()) {
00127       PT(Connection) rv;
00128       NetAddress address;
00129       PT(Connection) new_connection;
00130       if (listener.get_new_connection(rv, address, new_connection)) {
00131         nout << "Got connection from " << address << "\n";
00132         reader.add_connection(new_connection);
00133         clients.insert(Clients::value_type(new_connection, ClientState(new_connection)));
00134       }
00135     }
00136 
00137     // Check for reset clients.
00138     while (cm.reset_connection_available()) {
00139       PT(Connection) connection;
00140       if (cm.get_reset_connection(connection)) {
00141         nout << "Lost connection from "
00142              << connection->get_address() << "\n";
00143         clients.erase(connection);
00144         cm.close_connection(connection);
00145       }
00146     }
00147 
00148     // Process all available datagrams.
00149     while (reader.data_available()) {
00150       NetDatagram datagram;
00151       if (reader.get_data(datagram)) {
00152         PT(Connection) client = datagram.get_connection();
00153         Clients::iterator ci = clients.find(client);
00154         if (ci == clients.end()) {
00155           nout << "Received data from unexpected client " << (void *)client
00156                << "\n";
00157         } else {
00158           ClientState &state = (*ci).second;
00159           state.receive_data(datagram);
00160         }
00161       }
00162     }
00163   }
00164 
00165   return (0);
00166 }
00167 
00168 
00169 
00170 
00171 

Generated on Fri May 2 00:40:34 2003 for Panda by doxygen1.3