Commit bf7aa142 authored by Seblu's avatar Seblu

Les connexions ont maintenant in id unique

parent e6ac348e
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
......@@ -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;
}
......
......@@ -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"); }
}
......@@ -27,6 +27,14 @@
#include <errno.h>
#include <poll.h>
/*******************************************************************************
** 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_;
......
......@@ -22,6 +22,10 @@
# include <pthread.h>
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
......
......@@ -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_);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment