int main(int argc, char *argv[]) { { std::string s = "a b\"MID\" c d"; std::vector<std::string> result; unsigned int ret = stringTokenize(s, ' ', &result); assert(ret == 4); assert(result.size() == 4); assert(result[0] == "a"); assert(result[1] == "b\"MID\""); assert(result[2] == "c"); assert(result[3] == "d"); ret = stringTokenize(s, "\" ", &result); assert(result.size() == 6); assert(result[0] == "a"); assert(result[1] == "b"); assert(result[2] == "MID"); assert(result[3] == ""); assert(result[4] == "c"); assert(result[5] == "d"); s = ""; ret = stringTokenize(s, " ", &result); assert(result.size() == 1); assert(result[0] == ""); } { std::string s = "a b\"MID\" c d"; std::string piece; std::vector <std::string> result; StringTokenizer st1(s, ' '); while (st1.next(&piece)) { //printf("piece = %s\n", piece.c_str()); result.push_back(piece); } assert(result.size() == 4); assert(result[0] == "a"); assert(result[1] == "b\"MID\""); assert(result[2] == "c"); assert(result[3] == "d"); result.clear(); StringTokenizer st2(s, "\" "); while (st2.next(&piece)) { printf("piece = %s\n", piece.c_str()); result.push_back(piece); } assert(result.size() == 6); assert(result[0] == "a"); assert(result[1] == "b"); assert(result[2] == "MID"); assert(result[3] == ""); assert(result[4] == "c"); assert(result[5] == "d"); result.clear(); s = ""; StringTokenizer st3(s, " "); while (st3.next(&piece)) { result.push_back(piece); } assert(result.size() == 1); assert(result[0] == ""); } { std::string s = ""; std::string res = stringStrip(s); assert(res.size() == 0); s = " "; res = stringStrip(s); assert(res.size() == 0); } return 0; }
/** @param[out] commands Vector to save the commands that must be executed by Application @param[in] timeOut The sleep time in select (100 by default) */ int Server::listening(vector<Message> &commands, int timeOut){ fd_set readfds, writefds, errorfds; //multimile de descriptori de citire, scriere si eroare timeval timeout; sockaddr_in client = {}; //creem multimile de descriptori FD_ZERO(&readfds);FD_ZERO(&writefds);FD_ZERO(&errorfds); FD_SET(0, &readfds); //includem tastatura in readfds FD_SET(udpsd, &readfds); timeout.tv_sec = timeOut/1000; timeout.tv_usec = timeOut%1000; select(nfds+1, &readfds, &writefds, &errorfds, &timeout); cleanUUIDs(); /* Putem avea ori intrari de la tastatura (0), ori mesaje legate de retea (3), ori bucati de fisiere */ //verificam separat stdin, stdout, stderr if (FD_ISSET(0, &readfds)){ Message msg, comm; memset(msgBuffer, 0, sizeof(msgBuffer)); if (read(0, msgBuffer, sizeof(msgBuffer))<=0){ perror("[server] Eroare la citirea de la tastatura"); } string input = msgBuffer; input.erase(input.end()-1, input.end()); //input.pop_back(); //stergem caracterul linie noua if (input == "quit" || input == "exit"){ msg.push_back(MSG_quit); commands.push_back(msg); } else if (!input.compare(0, strlen("search"), "search")){ const char *uuid = getUUID(); int sd = socket(AF_INET, SOCK_STREAM, 0); //socketul folosit pentru a comunica cu peers ce au fisierul sockaddr_in server={}; server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(0); bind(sd, (sockaddr*)&server, sizeof(server)); listen(sd, 5); //sockaddr_in tcpserver = {}; //socklen_t sz = sizeof(tcpserver); //calculam expresia regulata input.erase(input.begin(), input.begin()+strlen("search")); stringStrip(input); printf("[Search] %s %s\n", input.c_str(), uuid); uuids[uuid] = getTicks(); comm.push_back(MSG_request); comm.push_back(sizeof(sd), &sd); commands.push_back(comm); //trimit la toti peeri mesajul for (list<Peer>::iterator it=peers.begin(); it!=peers.end(); ++it){ int ip=getIP(sd); unsigned short port=getPort(sd); msg.clear(); msg.push_back(MSG_search); msg.push_back(40, uuid); msg.push_back(sizeof(ip), &ip); msg.push_back(sizeof(port), &port); msg.push_back(input.size(), input.c_str()); sendto(udpsd, msg.getMessage(), msg.getSize(), 0, (sockaddr*)&it->address, sizeof(it->address)); } } else{ } } for (int d=3; d<=nfds; ++d){ if (FD_ISSET(d, &errorfds)){ } if (FD_ISSET(d, &readfds)){ if (d==udpsd){ MSG msgType; char ipString[40]; int port, size; socklen_t sock_size = sizeof(client); if ((size=recvfrom(d, msgBuffer, 100, 0, (sockaddr*)&client, &sock_size))<=0){ printf("Clientul s-a inchis"); continue; } Message msg(size, msgBuffer); msg.pop_front(msgType); switch (msgType){ case MSG_connectAsServer: printf("[server] Connection request P2P_connectAsServer\n"); serverPeers.push_back(client); case MSG_connectAsPeer: inet_ntop(client.sin_family, &client.sin_addr.s_addr, ipString, sizeof(ipString)); port = client.sin_port; printf("[server] Connection request\n"); printf("[server] S-a conectat un peer %s %d\n", ipString, port); peers.push_back(client); sock_size = sizeof(client); msg.clear(); msg.push_back(MSG_connectedOK); sendto(udpsd, msg.getMessage(), msg.getSize(), 0, (sockaddr*)&client, sock_size); ///inainte de push verificam sa nu fie in lista (nu e deja peer) printf("[server] Numar total de conexiuni: %lu\n", peers.size()); break; case MSG_pong: inet_ntop(client.sin_family, &client.sin_addr.s_addr, ipString, sizeof(ipString)); port = client.sin_port; for (list<Peer>::iterator it=peers.begin(); it!=peers.end(); ++it){ if (!memcmp(&it->address, &client, sizeof(it->address))){ ///client a raspuns la ping printf("[server] Pong from %s %d\n", ipString, port); it->lastPong = getTicks(); it->tries = 0; break; } } break; case MSG_search: commands.push_back(msg); for (list<Peer>::iterator it=peers.begin(); it!=peers.end(); ++it){ sendto(udpsd, msg.getMessage(), msg.getSize(), 0, (sockaddr*)&it->address, sizeof(it->address)); } break; default: printf("[server] Wrong option\n"); } } else{ } } if (FD_ISSET(d, &writefds)){ } } ping(); return 0; //maybe return value is not needed }
void commandLine(){ inputKey = 0; exit = 0; temp = 0; counter = 0; vgaSetup(3); vgaSetPos(0,0); vgaPrintString( getOSVersion() ); while( 1 ){ exit = 0; counter = 0; vgaPrintString( "BuenOS >" );//prompt input while( ( exit == 0 && inputKey == 0 ) || exit == 0 ){//fill input buffer inputKey = getKey(); if( isInput( inputKey ) ){ if( inputKey == 27 ){//esc cursorPos = vgaGetPos(); vgaSetPos( ( cursorPos->x - counter ), cursorPos->y ); for( temp = 0; temp < counter; temp++){ vgaPrint( ' ' ); } vgaSetPos( ( cursorPos->x - counter ), cursorPos->y ); counter = 0; inputBuffer[counter] = 0; } else if( inputKey == 13 ){//enter exit = 1;//finish buffer loop inputBuffer[counter] = 0;//null terminate string while( counter < sizeOfBuffer ){//null out remaining buffer inputBuffer[counter] = 0; counter++; } vgaPrintString( "\r\n" ); } else if( inputKey == 8 ){//backspace if( counter != 0 ){ vgaPrintString( "\b \b" ); counter--; inputBuffer[counter] = 0; } } else if( counter != sizeOfBuffer ){ vgaPrint( inputKey ); inputBuffer[counter] = inputKey; counter++; } } } stringCopy( inputBuffer, tempBuffer ); stringParseInfo = stringParse( tempBuffer ); stringParseInfo->argc++; stringUppercase( stringParseInfo->argv[0] ); if( stringLength( stringParseInfo->argv[0] ) != 0 ){ if( stringEqual( stringParseInfo->argv[0], "HELP" ) ) vgaPrintString( helpString ); else if( stringEqual( stringParseInfo->argv[0], "CLEAR" ) ) vgaSetup( 3 ); else if( stringEqual( stringParseInfo->argv[0], "VERSION" ) ) vgaPrintString( getOSVersion() ); else if( stringEqual( stringParseInfo->argv[0], "ECHO" ) ) { stringStrip( stringParseInfo->argv[1], '"' ); vgaPrintString( stringParseInfo->argv[1] ); vgaPrintString( "\r\n" ); } else if( stringEqual( stringParseInfo->argv[0], "DIR" ) ){ getFileList( tempBuffer ); stringFindAndReplace( tempBuffer, ',', ' ' ); stringParseInfo = stringParse( tempBuffer ); temp = 0; while( temp <= stringParseInfo->argc ){ vgaPrintString( stringParseInfo->argv[temp] ); cursorPos = vgaGetPos(); vgaSetPos( 12, cursorPos->y ); vgaPrintString( intToString( getFileSize( stringParseInfo->argv[temp] ) ) ); vgaPrintString( " bytes\r\n" ); temp++; } } else if( stringEqual( stringParseInfo->argv[0], "SIZEOF" ) ){ vgaPrintString( intToString( getFileSize( stringParseInfo->argv[1] ) ) ); vgaPrintString( " bytes\r\n" ); } else if( stringEqual( stringParseInfo->argv[0], "RENAME" ) ){ stringUppercase( stringParseInfo->argv[1] ); stringUppercase( stringParseInfo->argv[2] ); if( fileExists( stringParseInfo->argv[1] ) && stringLength( stringParseInfo->argv[1] ) != 0 && stringLength( stringParseInfo->argv[2] ) != 0 ){ if( stringEqual( stringParseInfo->argv[1], "KERNEL.BIN" ) ) vgaPrintString( "Nice try.\r\n" ); else if( fileExists( stringParseInfo->argv[2] ) || renameFile( stringParseInfo->argv[1], stringParseInfo->argv[2] ) ){ vgaPrintString( "File " ); vgaPrintString( stringParseInfo->argv[1] ); vgaPrintString( " could not be renamed to " ); vgaPrintString( stringParseInfo->argv[2] ); vgaPrintString( "\r\n" ); } else{ vgaPrintString( "File " ); vgaPrintString( stringParseInfo->argv[1] ); vgaPrintString( " successfully renamed to " ); vgaPrintString( stringParseInfo->argv[2] ); vgaPrintString( "\r\n" ); } } else{ vgaPrintString( "File " ); vgaPrintString( stringParseInfo->argv[1] ); vgaPrintString( " cannot be found.\r\n" ); } } else if( stringEqual( stringParseInfo->argv[0], "DELETE" ) ){ stringUppercase( stringParseInfo->argv[1] ); if( fileExists( stringParseInfo->argv[1] ) && stringLength( stringParseInfo->argv[1] ) != 0 ){ if( stringEqual( stringParseInfo->argv[1], "KERNEL.BIN" ) != 1 ){ if( removeFile( stringParseInfo->argv[1] ) ){ vgaPrintString( "File " ); vgaPrintString( stringParseInfo->argv[1] ); vgaPrintString( " could not be deleted\r\n" ); } else{ vgaPrintString( "File " ); vgaPrintString( stringParseInfo->argv[1] ); vgaPrintString( " successfully deleted\r\n" ); } } else vgaPrintString( "OMG just stop it. I NEED THAT!\r\n\nIT'S NOT FUNNY! ARGH!\r\n\n\n" ); } } else if( stringEqual( stringParseInfo->argv[0], "SHUTDOWN" ) ) shutdown(); else if( stringEqual( stringParseInfo->argv[0], "RESTART" ) ) restart(); else runApplication( stringParseInfo ); } } }