Implemented stats logging on the connector level - closes #1

This commit is contained in:
Thulinma 2011-11-26 03:16:28 +01:00
parent 0fb103a3b4
commit ff4e2aa285
2 changed files with 17 additions and 0 deletions

View file

@ -11,12 +11,19 @@
#include <netinet/in.h>
#endif
std::string uint2string(unsigned int i){
std::stringstream st;
st << i;
return st.str();
}
/// Create a new base socket. This is a basic constructor for converting any valid socket to a Socket::Connection.
/// \param sockNo Integer representing the socket to convert.
Socket::Connection::Connection(int sockNo){
sock = sockNo;
up = 0;
down = 0;
conntime = time(0);
Error = false;
Blocking = false;
}//Socket::Connection basic constructor
@ -27,6 +34,7 @@ Socket::Connection::Connection(){
sock = -1;
up = 0;
down = 0;
conntime = time(0);
Error = false;
Blocking = false;
}//Socket::Connection basic constructor
@ -64,6 +72,7 @@ Socket::Connection::Connection(std::string address, bool nonblock){
Blocking = false;
up = 0;
down = 0;
conntime = time(0);
sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, address.c_str(), address.size()+1);
@ -92,6 +101,7 @@ Socket::Connection::Connection(std::string host, int port, bool nonblock){
Blocking = false;
up = 0;
down = 0;
conntime = time(0);
std::stringstream ss;
ss << port;
@ -205,6 +215,11 @@ unsigned int Socket::Connection::dataDown(){
return down;
}
/// Returns a std::string of stats, ended by a newline.
std::string Socket::Connection::getStats(){
return getHost() + uint2string(time(0) - conntime) + " " + uint2string(up) + uint2string(down) + "\n";
}
/// Writes data to socket. This function blocks if the socket is blocking and all data cannot be written right away.
/// If the socket is nonblocking and not all data can be written, this function sets internal variable Blocking to true
/// and returns false.

View file

@ -25,6 +25,7 @@ namespace Socket{
std::string remotehost; ///< Stores remote host address.
unsigned int up;
unsigned int down;
unsigned int conntime;
public:
Connection(); ///< Create a new disconnected base socket.
Connection(int sockNo); ///< Create a new base socket.
@ -51,6 +52,7 @@ namespace Socket{
std::string getError(); ///< Returns a string describing the last error that occured.
unsigned int dataUp(); ///< Returns total amount of bytes sent.
unsigned int dataDown(); ///< Returns total amount of bytes received.
std::string getStats(); ///< Returns a std::string of stats, ended by a newline.
friend class Server;
bool Error; ///< Set to true if a socket error happened.
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.