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

panda/src/net/test_spam_client.cxx

Go to the documentation of this file.
00001 // Filename: test_spam_client.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 "queuedConnectionManager.h"
00020 #include "queuedConnectionReader.h"
00021 #include "connectionWriter.h"
00022 #include "netAddress.h"
00023 #include "connection.h"
00024 #include "netDatagram.h"
00025 
00026 #include "datagram_ui.h"
00027 
00028 #include <prinrval.h>
00029 
00030 int
00031 main(int argc, char *argv[]) {
00032   if (argc != 3) {
00033     nout << "test_spam_client host port\n";
00034     exit(1);
00035   }
00036 
00037   string hostname = argv[1];
00038   int port = atoi(argv[2]);
00039 
00040   NetAddress host;
00041   if (!host.set_host(hostname, port)) {
00042     nout << "Unknown host: " << hostname << "\n";
00043   }
00044 
00045   QueuedConnectionManager cm;
00046   PT(Connection) c = cm.open_TCP_client_connection(host, 5000);
00047 
00048   if (c.is_null()) {
00049     nout << "No connection.\n";
00050     exit(1);
00051   }
00052 
00053   nout << "Successfully opened TCP connection to " << hostname
00054        << " on port " << port << "\n";
00055 
00056   QueuedConnectionReader reader(&cm, 10);
00057   reader.add_connection(c);
00058   ConnectionWriter writer(&cm, 10);
00059 
00060   bool lost_connection = false;
00061 
00062   NetDatagram datagram;
00063   cout << "Enter a datagram.\n";
00064   cin >> datagram;
00065 
00066   nout << "Read datagram " << datagram << "\n";
00067   datagram.dump_hex(nout);
00068   nout << "\n";
00069 
00070   int num_sent = 0;
00071   int num_received = 0;
00072   PRIntervalTime last_reported_time = PR_IntervalNow();
00073   PRIntervalTime report_interval = PR_SecondsToInterval(5);
00074 
00075   while (!lost_connection) {
00076     // Send the datagram.
00077     if (writer.send(datagram, c, host)) {
00078       num_sent++;
00079     }
00080 
00081     // Check for a lost connection.
00082     while (cm.reset_connection_available()) {
00083       PT(Connection) connection;
00084         if (cm.get_reset_connection(connection)) {
00085         nout << "Lost connection from "
00086              << connection->get_address() << "\n";
00087         cm.close_connection(connection);
00088         if (connection == c) {
00089           lost_connection = true;
00090         }
00091       }
00092     }
00093 
00094     // Now poll for new datagrams on the socket.
00095     if (reader.data_available()) {
00096       NetDatagram new_datagram;
00097       if (reader.get_data(new_datagram)) {
00098         num_received++;
00099       }
00100     }
00101 
00102     PRIntervalTime now = PR_IntervalNow();
00103     if ((PRIntervalTime)(now - last_reported_time) > report_interval) {
00104       nout << "Sent " << num_sent << ", received "
00105            << num_received << " datagrams.\n";
00106       last_reported_time = now;
00107     }
00108 
00109     // Yield the timeslice before we poll again.
00110     //    PR_Sleep(PR_MillisecondsToInterval(1));
00111   }
00112 
00113   return (0);
00114 }
00115 
00116 
00117 
00118 
00119 

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