Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

IPopen.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2002 Frederic Morin 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 /* ******************************************************* 00035 * $Id: IPopen.h,v 1.5 2004/07/21 16:30:54 chrish42 Exp $ 00036 * This file is part of the PLearn library. 00037 ******************************************************* */ 00038 00039 #ifndef IPopen_INC 00040 #define IPopen_INC 00041 00042 #include <fstream> 00043 #include <string> 00044 #include <vector> 00045 #include <unistd.h> 00046 #include <plearn/base/PP.h> 00047 #include <plearn/base/general.h> 00048 00049 #ifndef _MINGW_ 00050 #include <unistd.h> 00051 #include <netinet/in.h> 00052 #include <netinet/tcp.h> 00053 #include <arpa/inet.h> 00054 #include <sys/socket.h> 00055 #include <netdb.h> 00056 #endif 00057 00058 namespace PLearn { 00059 using namespace std; 00060 00061 #ifndef _MINGW_ 00062 class IPServer { 00063 public: 00064 IPServer(int port_no, int max_connections) { 00065 DelayedConstructor(port_no, max_connections); 00066 }; 00067 IPServer(int max_connections_) { 00068 DelayedConstructor(ip_port, max_connections_); 00069 }; 00070 IPServer() { 00071 DelayedConstructor(ip_port, max_connections); 00072 }; 00073 ~IPServer() { 00074 shutdown(socket_fd, 2); 00075 close(socket_fd); 00076 }; 00077 00078 int get_socket_fd() { return socket_fd; }; 00079 struct sockaddr_in *get_address() { return &address; }; 00080 string machine_name() { return hostname(); }; 00081 int port_no() { return port; }; 00082 00083 protected: 00084 int socket_fd; 00085 int port; 00086 struct sockaddr_in address; 00087 private: 00088 void DelayedConstructor(int port_no, int max_connections_) { 00089 port = port_no; 00090 00091 int addr_len = sizeof(struct sockaddr_in); 00092 00093 socket_fd = socket(AF_INET, SOCK_STREAM, 0); 00094 if (socket_fd <= 0) 00095 PLERROR("Cannot create socket"); 00096 address.sin_family = AF_INET; 00097 address.sin_addr.s_addr = INADDR_ANY; 00098 address.sin_port = htons(port_no); 00099 00100 if (bind(socket_fd, (struct sockaddr *)&address, addr_len)) 00101 PLERROR("Cannot bind socket"); // ... and in the darknest bind them 00102 listen(socket_fd, max_connections); 00103 int nodelay = 1; 00104 setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, (char *)nodelay, sizeof(int)); 00105 } 00106 public: 00107 static int ip_port; 00108 static int max_connections; 00109 static void set_ip_port(int port_no, int max_connections_ = 100) { IPServer::ip_port = port_no; IPServer::max_connections = max_connections_; }; 00110 }; // class IPServer 00111 00112 00113 class IPopen: public PPointable { 00114 public: 00115 // Attributs 00116 PStream pipe; // Bi-directional pipe 00117 00118 // Methods 00119 IPopen(IPServer &server) 00120 { establish_communication(server); }; 00121 IPopen(IPServer &server, const string &command, bool the_verbose = false) 00122 { verbose = the_verbose; launch(server, command); } 00123 ~IPopen(); 00124 00125 int get_socket_fd() const { return socket_fd; }; 00126 00127 protected: 00130 void launch(IPServer &server, const string &command); 00131 void establish_communication(IPServer &server); 00132 00133 bool verbose; 00134 int socket_fd; 00135 }; // class IPopen 00136 00137 // This is called by the client side to establish communication. 00138 // Note that the command-line arguments from the client's program 00139 // are directly sent to this function. This is because argument 00140 // argv[1] and argv[2] should be, respectively, the server's machine 00141 // name and the port number to use. 00142 int establish_connection(const int n_hosts, const char *hostnames[], int port_no); 00143 inline int establish_connection(const char *hostname, int port_no) 00144 { return establish_connection(1, &hostname, port_no); } 00145 inline int establish_connection(const int argc, const char *argv[]) 00146 { 00147 if (argc >= 3) return establish_connection(1, &argv[1], atoi(argv[2])); 00148 PLERROR("Wrong number of arguments"); 00149 return -1; // Dummy return value 00150 } 00151 00152 #endif // ~_MINGW_ 00153 00154 } // end of namespace PLearn 00155 00156 #endif

Generated on Tue Aug 17 15:55:40 2004 for PLearn by doxygen 1.3.7