Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 1999-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2008 David Sugar, Tycho Softworks. |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | // |
| 18 | // As a special exception to the GNU General Public License, permission is |
| 19 | // granted for additional uses of the text contained in its release |
| 20 | // of APE. |
| 21 | // |
| 22 | // The exception is that, if you link the APE library with other files |
| 23 | // to produce an executable, this does not by itself cause the |
| 24 | // resulting executable to be covered by the GNU General Public License. |
| 25 | // Your use of that executable is in no way restricted on account of |
| 26 | // linking the APE library code into it. |
| 27 | // |
| 28 | // This exception does not however invalidate any other reasons why |
| 29 | // the executable file might be covered by the GNU General Public License. |
| 30 | // |
| 31 | // This exception applies only to the code released under the |
| 32 | // name APE. If you copy code from other releases into a copy of |
| 33 | // APE, as the General Public License permits, the exception does |
| 34 | // not apply to the code that you add in this way. To avoid misleading |
| 35 | // anyone as to the status of such modified files, you must delete |
| 36 | // this exception notice from them. |
| 37 | // |
| 38 | // If you write modifications of your own for APE, it is your choice |
| 39 | // whether to permit this exception to apply to your modifications. |
| 40 | // If you do not wish that, delete this exception notice. |
| 41 | |
| 42 | #include <cc++/socket.h> |
| 43 | #include <cstdlib> |
| 44 | |
| 45 | #ifdef CCXX_NAMESPACES |
| 46 | using namespace std; |
| 47 | using namespace ost; |
| 48 | #endif |
| 49 | |
| 50 | class myTCPSocket : public TCPSocket |
| 51 | { |
| 52 | protected: |
| 53 | bool onAccept(const InetHostAddress &ia, tpport_t port); |
| 54 | |
| 55 | public: |
| 56 | myTCPSocket(InetAddress &ia); |
| 57 | }; |
| 58 | |
| 59 | class myTCPSession : public TCPSession |
| 60 | { |
| 61 | private: |
| 62 | static Mutex mutex; |
| 63 | void run(void); |
| 64 | void final(void); |
| 65 | |
| 66 | public: |
| 67 | myTCPSession(TCPSocket &server); |
| 68 | static volatile int count; |
| 69 | }; |
| 70 | |
| 71 | myTCPSocket::myTCPSocket(InetAddress &ia) : TCPSocket(ia, 4096) {} |
| 72 | |
| 73 | bool myTCPSocket::onAccept(const InetHostAddress &ia, tpport_t port) |
| 74 | { |
| 75 | cout << "accepting from: " << ia.getHostname() << ":" << port << endl;; |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | volatile int myTCPSession::count = 0; |
| 80 | |
| 81 | Mutex myTCPSession::mutex; |
| 82 | |
| 83 | myTCPSession::myTCPSession(TCPSocket &server) : |
| 84 | TCPSession(server) |
| 85 | { |
| 86 | cout << "creating session client object" << endl; |
| 87 | mutex.enterMutex(); |
| 88 | ++count; |
| 89 | mutex.leaveMutex(); |
| 90 | // unsetf(ios::binary); |
| 91 | } |
| 92 | |
| 93 | void myTCPSession::run(void) |
| 94 | { |
| 95 | tpport_t port; |
| 96 | IPV4Address addr = getLocal(&port); |
| 97 | *tcp() << "welcome to " << addr.getHostname() << " from socket " << (int)so << endl; |
| 98 | mutex.enterMutex(); |
| 99 | *tcp() << "called from thread " << count << endl; |
| 100 | mutex.leaveMutex(); |
| 101 | sleep(5000); |
| 102 | *tcp() << "ending session" << endl; |
| 103 | } |
| 104 | |
| 105 | void myTCPSession::final(void) |
| 106 | { |
| 107 | } |
| 108 | |
| 109 | int main() |
| 110 | { |
| 111 | myTCPSession *tcp; |
| 112 | BroadcastAddress addr; |
| 113 | addr = "255.255.255.255"; |
| 114 | cout << "testing addr: " << addr.getHostname() << ":" << 4096 << endl; |
| 115 | addr = "127.0.0.1"; |
| 116 | cout << "binding for: " << addr.getHostname() << ":" << 4096 << endl; |
| 117 | |
| 118 | try { |
| 119 | myTCPSocket server(addr); |
| 120 | |
| 121 | while(server.isPendingConnection(30000)) { |
| 122 | cout << "before create" << endl; |
| 123 | tcp = new myTCPSession(server); |
| 124 | cout << "after create" << endl; |
| 125 | tcp->detach(); |
| 126 | } |
| 127 | } |
| 128 | catch(Socket *socket) { |
| 129 | tpport_t port; |
| 130 | int err = socket->getErrorNumber(); |
| 131 | InetAddress saddr = (InetAddress)socket->getPeer(&port); |
| 132 | cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl; |
| 133 | if(err == Socket::errBindingFailed) { |
| 134 | cerr << "bind failed; port busy" << endl; |
| 135 | ::exit(-1); |
| 136 | } |
| 137 | else |
| 138 | cerr << "client socket failed" << endl; |
| 139 | } |
| 140 | cout << "timeout after 30 seconds inactivity, exiting" << endl; |
| 141 | return 0; |
| 142 | } |
| 143 | |