From bf7aa1422b199fcde6f1e4181acf4c4a64a9211e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Fri, 4 Jan 2008 09:37:36 +0000 Subject: [PATCH] Les connexions ont maintenant in id unique --- sls/trunk/BUGS | 0 sls/trunk/TODO | 4 ---- sls/trunk/src/client.cc | 11 ++++++++--- sls/trunk/src/client_daemon.cc | 5 ++++- sls/trunk/src/connection.cc | 18 ++++++++++++++++-- sls/trunk/src/connection.hh | 8 ++++++++ sls/trunk/src/server.cc | 12 +++++------- 7 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 sls/trunk/BUGS diff --git a/sls/trunk/BUGS b/sls/trunk/BUGS new file mode 100644 index 0000000..e69de29 diff --git a/sls/trunk/TODO b/sls/trunk/TODO index 6e098cb..e69de29 100644 --- a/sls/trunk/TODO +++ b/sls/trunk/TODO @@ -1,4 +0,0 @@ -Gerer les control-C sur les sockets. Le server ne ferme pas la connection... -Implementer un conn id pour qu'un serveur puisse numeroter les connecxions - - diff --git a/sls/trunk/src/client.cc b/sls/trunk/src/client.cc index ae6b77f..4888018 100644 --- a/sls/trunk/src/client.cc +++ b/sls/trunk/src/client.cc @@ -35,7 +35,13 @@ Client *Client::login(Connection &c) { return login_fail(c); if (sscanf(slogin.c_str(), "HOST %512s\n", loginbuf) == 1) { - std::cout << "Host " << loginbuf << " logged from " << c.getremoteip() + // check login and password + + // Register login and pass in client + + // Print succefful login + std::cout << "Connection id " << c.getid() << ": Host " << loginbuf + << " logged from " << c.getremoteip() << " on port " << c.getremoteport() << ".\n"; return new Daemon(c); } @@ -47,8 +53,7 @@ Client *Client::login(Connection &c) { } Client *Client::login_fail(Connection &c) { - std::cout << "Bad authentification from " << c.getremoteip() << " on port " - << c.getremoteport() << ".\n"; + std::cout << "Connection id " << c.getid() << ": Bad authentification.\n"; c.sendln("Bad authentification."); return 0; } diff --git a/sls/trunk/src/client_daemon.cc b/sls/trunk/src/client_daemon.cc index 4e32f3c..6413600 100644 --- a/sls/trunk/src/client_daemon.cc +++ b/sls/trunk/src/client_daemon.cc @@ -22,6 +22,9 @@ Daemon::Daemon(Connection &c) : Client(c) {} void Daemon::exec() { + // send scripts + + // starting scripts + c_.sendln("EXIT"); - while (1) { c_.sendln("toto"); } } diff --git a/sls/trunk/src/connection.cc b/sls/trunk/src/connection.cc index 5472922..288afbe 100644 --- a/sls/trunk/src/connection.cc +++ b/sls/trunk/src/connection.cc @@ -27,6 +27,14 @@ #include #include +/******************************************************************************* + ** Class method + ******************************************************************************/ +unsigned long int Connection::getconnid() { + static unsigned long int id = 1; + return id++; +} + /******************************************************************************* ** Public method ******************************************************************************/ @@ -36,13 +44,15 @@ * * @param fd socket of the connection (-1) is not exist */ -Connection::Connection(int fd) : socket_fd_(fd), local_port_(-1), remote_port_(-1) { +Connection::Connection(int fd) : socket_fd_(fd), local_port_(-1), remote_port_(-1), id_(0) { pthread_mutex_init(&c_mutex_, 0); pthread_mutex_init(&r_mutex_, 0); pthread_mutex_init(&w_mutex_, 0); - if (socket_fd_ >= 0) + if (socket_fd_ >= 0) { + id_ = getconnid(); setallinfo_(); + } } /** @@ -352,6 +362,10 @@ int Connection::getremoteport() { return port; } +unsigned long int Connection::getid() { + return id_; +} + int Connection::getsocket() { pthread_mutex_lock(&c_mutex_); int ret = socket_fd_; diff --git a/sls/trunk/src/connection.hh b/sls/trunk/src/connection.hh index 0379ff7..194cd85 100644 --- a/sls/trunk/src/connection.hh +++ b/sls/trunk/src/connection.hh @@ -22,6 +22,10 @@ # include class Connection { + // class methods +public: + unsigned long int getconnid(); + // public methods public: Connection(int fd = -1); @@ -51,6 +55,8 @@ public: string getremoteip(); int getremoteport(); + unsigned long int getid(); + int getsocket(); // protected methods @@ -75,6 +81,8 @@ protected: string local_ip_; string remote_ip_; + unsigned long int id_; + string rbuf_; // read buffer string wbuf_; // write buffer diff --git a/sls/trunk/src/server.cc b/sls/trunk/src/server.cc index e5a6d1f..4886b3a 100644 --- a/sls/trunk/src/server.cc +++ b/sls/trunk/src/server.cc @@ -73,7 +73,7 @@ void Server::start(int port, size_t max_conn, bool verbose) { // check max conn if (threads_.size() >= max_conn_) { - std::cout << "Connection refused from ip " << nc->getremoteip() + std::cout << "Connection id " << nc->getid() << ": Refused from ip " << nc->getremoteip() << " on port " << nc->getremoteport() << ": Max connections reached.\n"; nc->disconnect(); @@ -81,8 +81,8 @@ void Server::start(int port, size_t max_conn, bool verbose) { } // Print connection - std::cout << "New connection from ip " << nc->getremoteip() - << " on port " << nc->getremoteport(); + std::cout << "Connection id " << nc->getid() << ": New connection from ip " + << nc->getremoteip() << " on port " << nc->getremoteport(); if (verbose_) std::cout << " (socket " << nc->getsocket() << ")" ; std::cout << ".\n"; @@ -177,16 +177,14 @@ void *Server::start_client(void *voidconn) { } } catch (const Error &e) { - std::cerr << "On connection " << conn->getremoteip() << " port " - << conn->getremoteport() << ": " << e << ".\n"; + std::cerr << "Connection id " << conn->getid() << ": " << e << ".\n"; } // stop connexion conn->disconnect(); // Print closing connection - std::cout << "Disconnected from ip " << conn->getremoteip() - << " on port " << conn->getremoteport() << ".\n"; + std::cout << "Connection id " << conn->getid() << ": Closed.\n"; // remove from thread set pthread_mutex_lock(&S.threads_mutex_); -- GitLab