tcpthread.cpp

00001 // Copyright (C) 1999-2001 Open Source Telecom Corporation.
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 //
00017 // As a special exception to the GNU General Public License, permission is
00018 // granted for additional uses of the text contained in its release
00019 // of APE.
00020 //
00021 // The exception is that, if you link the APE library with other files
00022 // to produce an executable, this does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public License.
00024 // Your use of that executable is in no way restricted on account of
00025 // linking the APE library code into it.
00026 //
00027 // This exception does not however invalidate any other reasons why
00028 // the executable file might be covered by the GNU General Public License.
00029 //
00030 // This exception applies only to the code released under the
00031 // name APE.  If you copy code from other releases into a copy of
00032 // APE, as the General Public License permits, the exception does
00033 // not apply to the code that you add in this way.  To avoid misleading
00034 // anyone as to the status of such modified files, you must delete
00035 // this exception notice from them.
00036 //
00037 // If you write modifications of your own for APE, it is your choice
00038 // whether to permit this exception to apply to your modifications.
00039 // If you do not wish that, delete this exception notice.
00040 
00041 #include <cc++/socket.h>
00042 #include <cstdlib>
00043 
00044 #ifdef  CCXX_NAMESPACES
00045 using namespace std;
00046 using namespace ost;
00047 #endif
00048 
00049 class myTCPSocket : public TCPSocket
00050 {
00051 protected:
00052         bool onAccept(const InetHostAddress &ia, tpport_t port);
00053 
00054 public:
00055         myTCPSocket(InetAddress &ia);
00056 };
00057 
00058 class myTCPSession : public TCPSession
00059 {
00060 private:
00061         static Mutex mutex;
00062         void run(void);
00063         void final(void);
00064 
00065 public:
00066         myTCPSession(TCPSocket &server);
00067         static volatile int count;
00068 };
00069 
00070 myTCPSocket::myTCPSocket(InetAddress &ia) : TCPSocket(ia, 4096) {}
00071 
00072 bool myTCPSocket::onAccept(const InetHostAddress &ia, tpport_t port)
00073 {
00074         cout << "accepting from: " << ia.getHostname() << ":" << port << endl;;
00075         return true;
00076 }
00077 
00078 volatile int myTCPSession::count = 0;
00079 
00080 Mutex myTCPSession::mutex;
00081 
00082 myTCPSession::myTCPSession(TCPSocket &server) :
00083 TCPSession(server)
00084 {
00085         cout << "creating session client object" << endl;
00086         mutex.enterMutex();
00087         ++count;
00088         mutex.leaveMutex();
00089         // unsetf(ios::binary);
00090 }
00091 
00092 void myTCPSession::run(void)
00093 {
00094         tpport_t port;
00095         IPV4Address addr = getLocal(&port);
00096         *tcp() << "welcome to " << addr.getHostname() << " from socket " << (int)so << endl;
00097         mutex.enterMutex();
00098         *tcp() << "called from thread " << count << endl;
00099         mutex.leaveMutex();
00100         sleep(5000);
00101         *tcp() << "ending session" << endl;
00102 }
00103 
00104 void myTCPSession::final(void)
00105 {
00106 }
00107 
00108 int main()
00109 {
00110         myTCPSession *tcp;
00111         BroadcastAddress addr;
00112         addr = "255.255.255.255";
00113         cout << "testing addr: " << addr.getHostname() << ":" << 4096 << endl;
00114         addr = "127.0.0.1";
00115         cout << "binding for: " << addr.getHostname() << ":" << 4096 << endl;
00116 
00117         try {
00118                 myTCPSocket server(addr);
00119 
00120                 while(server.isPendingConnection(30000)) {
00121                         cout << "before create" << endl;
00122                         tcp = new myTCPSession(server);
00123                         cout << "after create" << endl;
00124                         tcp->detach();
00125                 }
00126         }
00127         catch(Socket *socket) {
00128                 tpport_t port;
00129                 int err = socket->getErrorNumber();
00130                 InetAddress saddr = (InetAddress)socket->getPeer(&port);
00131                 cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl;
00132                 if(err == Socket::errBindingFailed) {
00133                         cerr << "bind failed; port busy" << endl;
00134                         ::exit(-1);
00135                 }
00136                 else
00137                         cerr << "client socket failed" << endl;
00138         }
00139         cout << "timeout after 30 seconds inactivity, exiting" << endl;
00140         return 0;
00141 }
00142 

Generated on Fri Apr 27 09:44:59 2007 for GNU CommonC++ by  doxygen 1.4.7