00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 extern "C" {
00048 #include <unistd.h>
00049 #include <string.h>
00050 #include <sys/types.h>
00051 #include <sys/socket.h>
00052 #include <sys/un.h>
00053 #include <netinet/in.h>
00054 }
00055
00056 #include <sstream>
00057
00058 #include "consmgr.h"
00059 #include "fdconnector.h"
00060 #include "guard.h"
00061
00062 FDConnector::FDConnector()
00063 : fd(-1)
00064 {
00065
00066 int ret = pthread_mutex_init(&mutex, NULL);
00067 PTHREAD_CHECK_AND_THROW(ret, "mutex_init(mutex)");
00068 }
00069
00070 FDConnector::FDConnector(int the_fd)
00071 : fd(the_fd)
00072 {
00073
00074 int ret = pthread_mutex_init(&mutex, NULL);
00075 PTHREAD_CHECK_AND_THROW(ret, "mutex_init(mutex)");
00076 }
00077
00078 FDConnector::~FDConnector(void)
00079 {
00080 disconnect();
00081
00082 int ret = pthread_mutex_destroy(&mutex);
00083 if (ret != 0)
00084 cerr << "FDConnector::~FDConnector: Unable to destroy mutex ("
00085 << &mutex << "): " << strerror(ret) << endl;
00086 }
00087
00088 #if 0
00089 void
00090 FDConnector::notify_on_disconnect(const Messageable *owner)
00091 {
00092 add_recipient(owner);
00093 }
00094 #endif
00095
00096 std::string
00097 FDConnector::get_id() const
00098 {
00099 std::ostringstream strs;
00100
00101 strs << "FDConnector[";
00102 if (fd >= 0)
00103 strs << "fd#" << fd;
00104 else
00105 strs << "disconnected";
00106 strs << "]";
00107 return(strs.str());
00108 }
00109
00110
00111
00112
00113
00114 void
00115 FDConnector::connect(int new_fd)
00116 {
00117
00118
00119
00120 Guard g(&mutex);
00121
00122 if (connected())
00123 throw(CMUnsupported("Already connected in call to FDConnector::connect!"));
00124
00125 fd = new_fd;
00126 }
00127
00129 void
00130 FDConnector::disconnect()
00131 {
00132 clog << "In FDConnector::disconnect" << endl;
00133
00134
00135
00136
00137
00138
00139 Guard g(&mutex);
00140
00141 if (connected()) {
00142 close(fd);
00143 fd = -1;
00144 }
00145
00146 clog << "returning from FDConnector::disconnect()" << endl;
00147 }
00148
00149 bool
00150 FDConnector::connected() const
00151 {
00152 return fd >= 0;
00153 }
00154
00155 Data *
00156 FDConnector::read()
00157 {
00158 char *buf = new char[Connector::BufSize];
00159 int len;
00160 while ((len = ::read(fd, buf, Connector::BufSize)) <= 0) {
00161 if (errno != EINTR && errno != EAGAIN) {
00162 disconnect();
00163
00164 throw CMStateChange();
00165 }
00166
00167
00168 }
00169
00170
00171 return new Data((u_char *)buf, len);
00172 }
00173
00174 ssize_t
00175 FDConnector::write(const Data &d)
00176 {
00177 const size_t dlen = d.len();
00178 size_t total = 0;
00179 int len;
00180
00181 do {
00182 len = ::write(fd, (d.data() + total), (dlen - total));
00183 if (len < 0) {
00184
00185 if (errno == EAGAIN || errno == EINTR)
00186 continue;
00187
00188
00189
00190
00191
00192
00193 disconnect();
00194
00195
00196 throw CMStateChange();
00197 }
00198 total += len;
00199 } while (total < dlen);
00200
00201 return total;
00202 }