00001 // 00002 // plumbing - C++ wrapper facades for Berkeley sockets 00003 // Copyright (C) 2009 Peter Miller 00004 // 00005 // This program is free software; you can redistribute it and/or modify it 00006 // under the terms of the GNU General Public License, version 3, as published 00007 // by the Free Software Foundation. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT 00010 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00012 // more details. 00013 // 00014 // You should have received a copy of the GNU General Public License along 00015 // with this program. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 00018 #ifndef LIBPLUMBING_ENDPOINT_CLIENT_H 00019 #define LIBPLUMBING_ENDPOINT_CLIENT_H 00020 00021 #include <ctime> 00022 #include <deque> 00023 00024 #include <libplumbing/endpoint.h> 00025 00026 namespace plumbing { 00027 00033 class endpoint_client: 00034 public endpoint 00035 { 00036 public: 00040 virtual ~endpoint_client(); 00041 00042 protected: 00053 endpoint_client(const std::string &hostname, int port); 00054 00066 endpoint_client(const std::string &hostname, const std::string &port); 00067 00068 public: 00083 void write(const std::string &text); 00084 00089 bool more_to_send() const { return !write_queue.empty(); } 00090 00098 bool is_connected() const { return (fd >= 0 && !connecting); } 00099 00107 int get_port() const { return port; } 00108 00113 const std::string &get_host() const { return hostname; } 00114 00115 protected: 00116 // See base class for documentation. 00117 int get_read_file_descriptor(); 00118 00119 // See base class for documentation. 00120 void process_read(); 00121 00122 // See base class for documentation. 00123 int get_write_file_descriptor(); 00124 00125 // See base class for documentation. 00126 void process_write(); 00127 00138 virtual void data_received(const void *data, size_t nbytes) = 0; 00139 00140 private: 00145 std::string hostname; 00146 00151 int port; 00152 00158 void *sock_addr; 00159 00164 int sock_addr_size; 00165 00170 bool connecting; 00171 00176 time_t retry_after; 00177 00184 typedef std::deque<std::string> write_queue_t; 00185 00192 write_queue_t write_queue; 00193 00198 void try_to_connect(); 00199 00203 endpoint_client(); 00204 00208 endpoint_client(const endpoint_client &); 00209 00213 endpoint_client &operator=(const endpoint_client &); 00214 }; 00215 00216 } 00217 00218 #endif // LIBPLUMBING_ENDPOINT_CLIENT_H 00219 // vim: set ts=8 sw=4 et