Skip to contentSkip to conversation

conga/ricci/common ClientSocket.cpp Except.cpp ...

Cluster-devel [email protected] 1 participant 1 message Aug 30, 2007

lore
Share & tools
lore
#0 lore raw

CVSROOT: /cvs/cluster
Module name: conga
Changes by: rmccabe at sourceware.org 2007-08-30 22:50:14

Modified files:
ricci/common : ClientSocket.cpp Except.cpp File.cpp Socket.cpp
executils.cpp

Log message:
- More cleanup
- Better error messages

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/ClientSocket.cpp.diff?cvsroot=cluster&r1=1.8&r2=1.9
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Except.cpp.diff?cvsroot=cluster&r1=1.2&r2=1.3
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/File.cpp.diff?cvsroot=cluster&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Socket.cpp.diff?cvsroot=cluster&r1=1.7&r2=1.8
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/executils.cpp.diff?cvsroot=cluster&r1=1.9&r2=1.10

ricci/common/ClientSocket.cpp +167 -153
@@ -13,7 +13,7 @@
 
   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to the
-  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
+  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge,
   MA 02139, USA.
 */
 /*
@@ -35,200 +35,214 @@
 #include <netdb.h>
 
 
-
 ClientSocket::ClientSocket() :
-  Socket(-1)
+	Socket(-1)
 {}
 
 ClientSocket::ClientSocket(int sock, u_int32_t addr) :
-  Socket(sock),
-  _addr(addr)
+	Socket(sock),
+	_addr(addr)
 {}
 
 ClientSocket::ClientSocket(const String& sock_path) :
-  Socket(-1)
+	Socket(-1)
 {
-  _sock = socket(PF_UNIX, SOCK_STREAM, 0);
-  if (_sock == -1)
-    throw String("ClientSocket(String): socket() failed");
-  
-  struct sockaddr_un {
-    sa_family_t  sun_family;
-    char         sun_path[100];
-  } addr;
-  addr.sun_family = AF_UNIX;
-  memcpy(addr.sun_path, sock_path.c_str(), sock_path.size()+1);
-  
-  if (connect(_sock, (struct sockaddr*) &addr, sizeof(addr))) {
-    throw String("ClientSocket(String): connect() failed");
-  }
-  
-  //  String msg = String("created client socket ") + _sock;
-  //  msg += ", and connected to " + sock_path;
-  //  log(msg, LogSocket);
-}
-
-ClientSocket::ClientSocket(const String& hostname, 
-			   unsigned short port, 
-			   unsigned int timeout_ms) :
-  Socket(-1)
-{
-  _sock = socket(PF_INET, SOCK_STREAM, 0);
-  if (_sock == -1)
-    throw String("ClientSocket(hostname, port, timeout): socket() failed");
-  
-  if (timeout_ms)
-    nonblocking(true);
-  
-  counting_auto_ptr<Network::Hostent> ent = Network::getHostByName(hostname);
-  
-  char** addrs = (*ent)->h_addr_list;
-  for (int i=0; addrs[i]; i++) {
-    struct sockaddr_in addr_in;
-    addr_in.sin_family = AF_INET;
-    addr_in.sin_port = htons(port);
-    addr_in.sin_addr.s_addr = *((u_int32_t*) addrs[i]);
-    
-    if (connect(_sock, (struct sockaddr*) &addr_in, sizeof(addr_in)) == 0) {
-      // connected
-      nonblocking(false);
-      _addr = addr_in.sin_addr.s_addr;
-      return;
-    }
-    
-    // connect() error
-    if (errno != EINPROGRESS)
-      continue;
-    bool can_read=false, can_write=true;
-    poll(can_read, can_write, timeout_ms);
-    if (can_write == false) {
-      // connect() not completed
-      throw String("ClientSocket(hostname, port, timeout): connect() timed out");
-    }
-    // connect() completed, check successfulness
-    int err = 1;
-    socklen_t err_size = sizeof(err);
-    getsockopt(_sock, SOL_SOCKET, SO_ERROR,
-	       &err, &err_size);
-    if (err)
-      continue;
-    
-    // connected
-    nonblocking(false);
-    _addr = addr_in.sin_addr.s_addr;
-    return;
-  }
-  throw String("ClientSocket(hostname, port, timeout): connect() failed");
+	_sock = socket(PF_UNIX, SOCK_STREAM, 0);
+	if (_sock == -1) {
+		throw String("ClientSocket(String): socket() failed: ")
+				+ String(strerror(errno));
+	}
+
+	struct sockaddr_un addr;
+	addr.sun_family = AF_UNIX;
+	if (sock_path.size() >= sizeof(addr.sun_path))
+		throw String("path to client unix socket is too long");
+	memcpy(addr.sun_path, sock_path.c_str(), sock_path.size()+1);
+
+	if (connect(_sock, (struct sockaddr*) &addr, sizeof(addr))) {
+		throw String("ClientSocket(String): connect() failed: ")
+				+ String(strerror(errno));
+	}
+
+	//String msg = String("created client socket ") + _sock;
+	//msg += ", and connected to " + sock_path;
+	//log(msg, LogSocket);
+}
+
+ClientSocket::ClientSocket(	const String& hostname,
+							unsigned short port,
+							unsigned int timeout_ms) :
+	Socket(-1)
+{
+	_sock = socket(PF_INET, SOCK_STREAM, 0);
+	if (_sock == -1) {
+		throw String("ClientSocket(hostname, port, timeout): socket() failed")
+				+ String(strerror(errno));
+	}
+
+	if (timeout_ms)
+		nonblocking(true);
+
+	counting_auto_ptr<Network::Hostent> ent = Network::getHostByName(hostname);
+
+	char **addrs = (*ent)->h_addr_list;
+	for (int i = 0 ; addrs[i] ; i++) {
+		struct sockaddr_in addr_in;
+
+		addr_in.sin_family = AF_INET;
+		addr_in.sin_port = htons(port);
+		addr_in.sin_addr.s_addr = *((u_int32_t*) addrs[i]);
+
+		if (connect(_sock, (struct sockaddr *) &addr_in, sizeof(addr_in)) == 0)
+		{
+			// connected
+			nonblocking(false);
+			_addr = addr_in.sin_addr.s_addr;
+			return;
+		}
+
+		// connect() error
+		if (errno != EINPROGRESS)
+			continue;
+
+		bool can_read = false, can_write = true;
+		poll(can_read, can_write, timeout_ms);
+
+		if (can_write == false) {
+			// connect() not completed
+			throw String("ClientSocket(hostname, port, timeout): connect() timed out") + String(strerror(errno));
+		}
+
+		// connect() completed, check successfulness
+		int err = 1;
+		socklen_t err_size = sizeof(err);
+		getsockopt(_sock, SOL_SOCKET, SO_ERROR, &err, &err_size);
+
+		if (err)
+			continue;
+
+		// connected
+		nonblocking(false);
+		_addr = addr_in.sin_addr.s_addr;
+		return;
+	}
+
+	throw String("ClientSocket(hostname, port, timeout): connect() failed");
 }
 
 ClientSocket::ClientSocket(const ClientSocket& s) :
-  Socket(s),
-  _addr(s._addr)
+	Socket(s),
+	_addr(s._addr)
 {}
 
-ClientSocket& 
+ClientSocket&
 ClientSocket::operator= (const ClientSocket& s)
 {
-  if (&s != this) {
-    this->Socket::operator= (s);
-    _addr = s._addr;
-  }
-  return *this;
+	if (&s != this) {
+		this->Socket::operator= (s);
+		_addr = s._addr;
+	}
+	return *this;
 }
 
 ClientSocket::~ClientSocket()
 {}
 
-
-bool 
+bool
 ClientSocket::connected_to(const String& hostname)
 {
-  try {
-    counting_auto_ptr<Network::Hostent> ent = Network::getHostByName(hostname);
-    char** addrs = (*ent)->h_addr_list;
-    for (int i=0; addrs[i]; i++)
-      if (*((u_int32_t*) addrs[i]) == _addr)
-        return true;
-  } catch ( ... ) {}
-  return false;
+	try {
+		counting_auto_ptr<Network::Hostent> ent = Network::getHostByName(hostname);
+		char **addrs = (*ent)->h_addr_list;
+		for (int i = 0 ; addrs[i] ; i++) {
+			if (*((u_int32_t *) addrs[i]) == _addr)
+				return true;
+		}
+	} catch ( ... ) {}
+
+	return false;
 }
 
-String 
+String
 ClientSocket::recv()
 {
-  if (_sock == -1)
-    throw String("ClientSocket::recv(): socket already closed");
-  
-  while (true) {
-    char buffer[4096];
-    int ret = ::recv(_sock, buffer, sizeof(buffer), 0);
-    if (ret == -1) {
-      if (errno == EINTR)
-	continue;
-      else if (errno == EAGAIN)
-	return "";
-      throw String("ClientSocket::recv(): recv error: ") + String(strerror(errno));
-    }
-    
-    if (ret == 0) {
-      close();
-      throw String("ClientSocket::recv(): socket has been shutdown");
-    }
-    
-    //    log(String("received ") + ret + " bytes from socket " + _sock, 
-    //	LogLevel(LogSocket|LogTransfer));
-    String data(buffer, ret);
-    shred(buffer, ret);
-    return data;
-  }
+	if (_sock == -1)
+		throw String("ClientSocket::recv(): socket already closed");
+
+	while (true) {
+		char buffer[4096];
+		int ret = ::recv(_sock, buffer, sizeof(buffer), 0);
+		if (ret == -1) {
+			if (errno == EINTR)
+				continue;
+			else if (errno == EAGAIN)
+				return "";
+			throw String("ClientSocket::recv(): recv error: ")
+					+ String(strerror(errno));
+		}
+
+		if (ret == 0) {
+			close();
+			throw String("ClientSocket::recv(): socket has been shutdown");
+		}
+
+		//log(String("received ") + ret + " bytes from socket " + _sock,
+		//LogLevel(LogSocket|LogTransfer));
+		String data(buffer, ret);
+		shred(buffer, ret);
+		return data;
+	}
 }
 
 String
 ClientSocket::recv(int timeout)
 {
-  bool in=true, out=false;
-  poll(in, out, timeout);
-  if (in)
-    return recv();
-  else
-    return "";
+	bool in = true, out = false;
+
+	poll(in, out, timeout);
+	if (in)
+		return recv();
+	else
+		return "";
 }
 
-String 
+String
 ClientSocket::send(const String& msg)
 {
-  if (_sock == -1)
-    throw String("ClientSocket::send(): socket already closed");
-  
-  while (true) {
-    int ret = ::send(_sock, msg.c_str(), msg.size(), 0);
-    if (ret == -1) {
-      if (errno == EINTR)
-	continue;
-      else if (errno == EAGAIN || errno == EWOULDBLOCK)
-	return msg;
-      throw String("ClientSocket::send(): socket error: ") + String(strerror(errno));
-    }
-    
-    //    log(String("sent ") + ret + " bytes thru socket " + _sock, 
-    //	LogLevel(LogSocket|LogTransfer));
-    return msg.substr(ret);
-  }
+	if (_sock == -1)
+		throw String("ClientSocket::send(): socket already closed");
+
+	while (true) {
+		int ret = ::send(_sock, msg.c_str(), msg.size(), 0);
+		if (ret == -1) {
+			if (errno == EINTR)
+				continue;
+			else if (errno == EAGAIN || errno == EWOULDBLOCK)
+				return msg;
+			throw String("ClientSocket::send(): socket error: ")
+					+ String(strerror(errno));
+		}
+
+		//log(String("sent ") + ret + " bytes thru socket " + _sock,
+		//LogLevel(LogSocket|LogTransfer));
+		return msg.substr(ret);
+	}
 }
 
 String
 ClientSocket::send(const String& msg, int timeout)
 {
-  bool in=false, out=true;
-  poll(in, out, timeout);
-  if (out)
-    return send(msg);
-  else
-    return msg;
+	bool in = false, out = true;
+
+	poll(in, out, timeout);
+	if (out)
+		return send(msg);
+	else
+		return msg;
 }
 
-void 
+void
 ClientSocket::ready(bool& recv, bool& send, int timeout)
 {
-  poll(recv, send, timeout);
+	poll(recv, send, timeout);
 }
ricci/common/Except.cpp +10 -12
@@ -1,5 +1,5 @@
 /*
-  Copyright Red Hat, Inc. 2005
+  Copyright Red Hat, Inc. 2005-2007
 
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
 
   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to the
-  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
+  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge,
   MA 02139, USA.
 */
 /*
@@ -27,26 +27,24 @@
 using namespace std;
 
 
-
-Except::Except(long long error_code,
-	       const String& msg) : 
-  _code(error_code), _msg(msg)
+Except::Except(long long error_code, const String& msg) :
+	_code(error_code), _msg(msg)
 {
-  if (_code == generic_error)
-    throw String("Exception() invalid error_code");
+	if (_code == generic_error)
+		throw String("Exception() invalid error_code");
 }
 
 Except::~Except()
 {}
 
-long long 
+long long
 Except::code() const
 {
-  return _code;
+	return _code;
 }
 
-String 
+String
 Except::description() const
 {
-  return _msg;
+	return _msg;
 }
ricci/common/File.cpp +144 -141

350 lines not on this page · raw message

ricci/common/Socket.cpp +105 -100

259 lines not on this page · raw message

ricci/common/executils.cpp +226 -226

481 lines not on this page · raw message