00001 // Filename: test_spam_server.cxx 00002 // Created by: drose (24Feb00) 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 00029 #include "datagram_ui.h" 00030 00031 #include <prinrval.h> 00032 00033 #include "pset.h" 00034 #include <sys/time.h> 00035 #include <sys/types.h> 00036 #include <unistd.h> 00037 00038 int 00039 main(int argc, char *argv[]) { 00040 if (argc != 2) { 00041 nout << "test_spam_server port\n"; 00042 exit(1); 00043 } 00044 00045 int port = atoi(argv[1]); 00046 00047 QueuedConnectionManager cm; 00048 PT(Connection) rendezvous = cm.open_TCP_server_rendezvous(port, 5); 00049 00050 if (rendezvous.is_null()) { 00051 nout << "Cannot grab port " << port << ".\n"; 00052 exit(1); 00053 } 00054 00055 nout << "Listening for connections on port " << port << "\n"; 00056 00057 QueuedConnectionListener listener(&cm, 1); 00058 listener.add_connection(rendezvous); 00059 00060 typedef pset< PT(Connection) > Clients; 00061 Clients clients; 00062 00063 QueuedConnectionReader reader(&cm, 10); 00064 ConnectionWriter writer(&cm, 10); 00065 00066 int num_sent = 0; 00067 int num_received = 0; 00068 PRIntervalTime last_reported_time = PR_IntervalNow(); 00069 PRIntervalTime report_interval = PR_SecondsToInterval(5); 00070 00071 bool shutdown = false; 00072 while (!shutdown) { 00073 // Check for new clients. 00074 while (listener.new_connection_available()) { 00075 PT(Connection) rv; 00076 NetAddress address; 00077 PT(Connection) new_connection; 00078 if (listener.get_new_connection(rv, address, new_connection)) { 00079 nout << "Got connection from " << address << "\n"; 00080 reader.add_connection(new_connection); 00081 clients.insert(new_connection); 00082 } 00083 } 00084 00085 // Check for reset clients. 00086 while (cm.reset_connection_available()) { 00087 PT(Connection) connection; 00088 if (cm.get_reset_connection(connection)) { 00089 nout << "Lost connection from " 00090 << connection->get_address() << "\n"; 00091 clients.erase(connection); 00092 cm.close_connection(connection); 00093 } 00094 } 00095 00096 // Process all available datagrams. 00097 while (reader.data_available()) { 00098 NetDatagram datagram; 00099 if (reader.get_data(datagram)) { 00100 num_received++; 00101 Clients::iterator ci; 00102 for (ci = clients.begin(); ci != clients.end(); ++ci) { 00103 if (writer.send(datagram, (*ci))) { 00104 num_sent++; 00105 } 00106 } 00107 } 00108 } 00109 00110 PRIntervalTime now = PR_IntervalNow(); 00111 if ((PRIntervalTime)(now - last_reported_time) > report_interval) { 00112 nout << "Sent " << num_sent << ", received " 00113 << num_received << " datagrams.\n"; 00114 last_reported_time = now; 00115 } 00116 00117 // Yield the timeslice before we poll again. 00118 // PR_Sleep(PR_MillisecondsToInterval(1)); 00119 } 00120 00121 return (0); 00122 } 00123 00124 00125 00126 00127