예제 #1
0
Server::Server(QObject* parent) :
    QObject(parent)
{
    connect(&server, SIGNAL(newConnection()), SLOT(newConnectionHandler()));
	quint16 port = Protocol::ServerPort;

	if (settings.contains(SettingsKey::PORT_KEY)){
		port = quint16(settings.value(SettingsKey::PORT_KEY).toInt());
	}
	bool res = server.listen(QHostAddress::Any, port);
    qDebug() << "is listening = " << res;
}
예제 #2
0
/*
 * Function:    eventHandler()
 * Parameters:  None
 * Returns:     None
 * Description: This function creates a listening socket and accepts new incoming connections, apart from it it also reads from stdin such that server works as a unix shell and also handles incoming connections
 */
void Server::eventHandler() {
    char buffer[1024];
    startListenServer();
    
    for(;;) {
        // copy master set to read set
        m_readSet = m_masterSet;
        // start polling on read set, which is blocking indefinitely
        if(select(m_nMaxFd+1, &m_readSet, NULL, NULL, NULL) == -1) {
            perror("select");
            exit(EXIT_FAILURE);
        }
        // check if there is data on any of the sockets
        int bytesRead;
        for(int i=0; i<=m_nMaxFd; i++) {
            if(FD_ISSET(i, &m_readSet)) {
                if(i == STDIN) {
                    // data from stdin, process approriate commands
                    fgets(buffer, sizeof(buffer), stdin);
                    buffer[strlen(buffer)-1] = '\0';
                    if(strlen(buffer) != 0)
                    	commandShell(buffer);
                }
                else if(i == m_nListenSd) {
                    // new connection
                	newConnectionHandler();
                }
                else {
                    // handle data from existing client

                	// process SYNC message
                	char recvBuff[1024] = {0};
                	int bytesRead;
                	if( (bytesRead = recv(i, recvBuff, sizeof(recvBuff), 0)) > 0) {
                		recvBuff[strlen(recvBuff)] = 0;
                		if(strcmp(recvBuff, "SYNC") == 0) {
                			cout << "Received Message: " << recvBuff << " from client" << endl;
                			m_nQueIx = 0;
                			// update this message to all connected clients
                			for(int i=0; i<10; i++) {
                				if(m_nodeList[i].state == ACTIVE) {
                					sendall(m_nodeList[i].sockFd, recvBuff, &bytesRead);
                				}
                			}
                		}
                		if(strcmp(recvBuff, "QUIT") == 0) {
                			cout << "Received Message: " << recvBuff << " from client" << endl;
                			for(int j=0; j<10; j++) {
                				if(m_nodeList[j].sockFd == i) {
                					handle_terminate(j);
                				}
                			}
                			updateNodesinList();
                		}
                		if(strcmp(recvBuff, "SYNC WRITE") == 0) {
                			/*cout << "Received Message: " << recvBuff << " from client ";
                			int p;
                			for(p=0; p<10; p++) {
                				if(m_nodeList[p].state == ACTIVE && m_nodeList[p].sockFd == i)
                					cout << m_nodeList[p].hostName << endl;
                			}*/
                			// add this socket to sync que
                			m_syncQue[m_nQueIx] = i;
                			m_nQueIx++;
                			// if message has been recieved from all registered clients then start writing
                			if(m_nQueIx == m_nLatestIndex) {
                				// send Sync OK message to first client in Queue
                				char buff[1024] = {0};
                				sprintf(buff,"SYNC OK");
                				int len = sizeof buff;
                				sendall(m_syncQue[0], buff, &len);
                				// send SYNC READ message to rest of the clients
                				memset(buff, 0, sizeof buff);
                				sprintf(buff, "SYNC READ %s", m_nodeList[0].hostName);
                				for(int j=1; j<m_nQueIx; j++) {
                					len = sizeof buff;
                					sendall(m_syncQue[j], buff, &len);
                				}
                			}
                		}
                		if(strcmp(recvBuff, "SYNC FIN") == 0) {
                			/*cout << "Received Message: " << recvBuff << " from client ";
                			int p;
							for(p=0; p<10; p++) {
								if(m_nodeList[p].state == ACTIVE && m_nodeList[p].sockFd == i)
									cout << m_nodeList[p].listenPort << endl;
							}*/
                			// get the index of client which sent fin
							int j;
							for(j=0; j<m_nQueIx; j++)
								if(i == m_syncQue[j]) break;

							if(j == m_nQueIx-1) {
								m_nQueIx = 0;
							}
							else {
								// send SYNC OK message to the next client in the queue
								char buff[1024] = {0};
								sprintf(buff,"SYNC OK");
								int len = sizeof buff;
								sendall(m_syncQue[j+1], buff, &len);
								// send SYNC READ message to rest of the clients
								memset(buff, 0, sizeof buff);
								sprintf(buff, "SYNC READ %s", m_nodeList[j+1].hostName);
								for(int k=0; k<m_nQueIx; k++) {
									if(k != j+1) {
										len = sizeof buff;
										sendall(m_syncQue[k], buff, &len);
									}
								}
							}
                		}
                		cout << endl;
                	}
                	// if bytesRead=0 then terminate connection
                	else {
						cout << "Client disconnected: " <<  m_nodeList[i].hostName << endl;
						for(int j=0; j<10; j++) {
							if(m_nodeList[j].sockFd == i)
								handle_terminate(j);
						}
						updateNodesinList();
					}
                }

            }
        }
    }
}