void QTNServer::printClients() { std::cout << getStatusDescription().c_str() << "clients connected: "; int i = 0; for (QMap<int, QTNServerThread*>::iterator it = m_clients.begin(), end = m_clients.end();it != end;++it) { std::cout << "\n [client num " << ++i << "/" << m_numMaxClients << "] " << (*it)->getClientConnectedIp() << ";"; } if (i == 0) { std::cout << "NONE"; } std::cout << "\n"; QTimer::singleShot(1000, this, SLOT(printClients())); }
bool QTNServer::init() { std::cout << getStatusDescription().c_str() << "is initializing " << "ip: " << SERVER_IP << ", port: " << SERVER_PORT << "\n"; bool isInitialized = listen(QHostAddress(SERVER_IP), SERVER_PORT); if (isInitialized) { m_serverStatus = SERVER_STATUS_OK; std::cout << getStatusDescription().c_str() << "is listening\n"; QThreadPool::globalInstance()->setMaxThreadCount(QTNConst::NUM_MAX_CLIENTS); connect(this, SIGNAL(newConnection()), this, SLOT(onNewConnection())); QTimer::singleShot(1000, this, SLOT(printClients())); } else { m_serverStatus = SERVER_STATUS_NOK; std::cout << getStatusDescription().c_str() << "is NOT listening\n"; } return isInitialized; }
/** * Handle a new connection * * @param {int} newSocketFd */ void handleConnection(int newSocketFd) { char ioBuffer[256]; memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); printf("Before call to isValidClientId(), "); printClients(); int clientId = atoi(ioBuffer); int isValidId = isValidClientId(clientId); printf("Checking for valid client id...\n"); if (isValidId == 0) { attemptWrite(newSocketFd, INVALID_CLIENT_ID, strlen(INVALID_CLIENT_ID)); return; } else { attemptWrite(newSocketFd, VALID_CLIENT_ID, strlen(VALID_CLIENT_ID)); memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); printf("%s\n", ioBuffer); // print client id response message from client addClient(clientId); printf("After call to addClient(), "); printClients(); attemptWrite(newSocketFd, WELCOME_MESSAGE, strlen(WELCOME_MESSAGE)); printf("New client #%d\n", clientId); while (1) { printf("Waiting for command from client #%d...\n", clientId); char opFile[256]; // read opFile from client memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); strcpy(opFile, ioBuffer); // if client requests, end session if (strcmp(ioBuffer, END_SESSION) == 0) { printf("Terminating client #%d\n", clientId); removeClient(clientId); return; } // otherwise, send continue response attemptWrite(newSocketFd, CONTINUE, strlen(CONTINUE)); // read opCode from client memset(ioBuffer, '\0', 256); // reset the ioBuffer for i/o attemptRead(newSocketFd, ioBuffer, 255); // proceed based on opCode if (strcmp(ioBuffer, PROCESS_READ) == 0) { printf("Processing read for file: `%s`\n", opFile); readFile(opFile, newSocketFd); // send file to client } else if (strcmp(ioBuffer, PROCESS_WRITE) == 0) { printf("Processing write for file: `%s`\n", opFile); attemptWrite(newSocketFd, CONTINUE, strlen(CONTINUE)); memset(ioBuffer, '\0', 256); attemptRead(newSocketFd, ioBuffer, 255); // read file contents writeToFile(opFile, ioBuffer); } } } }