void configure(RuntimeErrorValidator * v){ setupConfiguration(v); closingEvents(); if(hasError(v)){ return; } //setLoggingLevelEnabled(LOGGING_LEVEL_INFO); setFssRunningStatus(TRUE); setupDirectoryIndexes(); if(isInfoEnabled()) info("Conectando con modulo KSS"); ListenSocket s = openClientConnection(getKssHost() , getKssPort()); if(s == INVALID_SOCKET){ setError(v , "no se puede abrir la coneccion"); return; } MpsHandshake * handshake = buildMpsHandhake(generateRandomKey(16) , FSS_HANDSHAKE); handshake = sendAndReceiveMpsHandshake(handshake , s , v); if(handshake != NULL && handshake->statusCode == MPS_RESPONSE_STATUS_CODE_SUCCESS ){ info(concatAll(2 , "Handshake realizado satisfactoriamente con DescriptorId: " , handshake->descriptorId)); setFileSystemSocket(s); }else{ setError(v , "Hubo un problema y no se pudo realizar el handshake"); return; } }
void executeKssCommandList(char * arg , ListenSocket ls){ RuntimeErrorValidator * validator = buildErrorSuccessValidator(); //List params = getFirstParameter("Dispositivo o directorio a Listar"); List params = buildStringsListWithValues(1 , arg); MpsRequest * req = buildMpsRequest( generateRandomKey(16) , SHELL_LS , params); MpsResponse * resp = sendAndReceiveMpsMessages(ls , req ,validator); if(isDebugEnabled()) debug(concatAll(2, "Codigo de resultado: " , resp->statusCode)); Iterator * ite = buildIterator(resp->returnValues); if( equalsStrings(resp->statusCode , MPS_RESPONSE_STATUS_CODE_SUCCESS)){ char * serializedFiles = next(ite); List files = deserializeList(serializedFiles); ite = buildIterator(files); printf("Contenido del directorio:\n"); printf("--------------------------------------------------------------------------------------\n"); while(hasMoreElements(ite)){ char * elem = next(ite); printf("%s\n " , trim(elem)); } }else{ printf("%s" , ( hasMoreElements(ite) ? (char *) next(ite) : "Ha ocurrido un error")); } }
VdaInformation * callVdaOperationGetChs(VdaComponent * vdaComp , RuntimeErrorValidator * validator){ MpsMessage * req = buildMpsMessage(generateRandomKey(16) , MPS_RESPONSE_STATUS_CODE_SUCCESS, VDA_CHS_GETTING , NULL); MpsMessage * response = sendAndReceiveMpsMessages(vdaComp->listenSocket , req , validator); if( response->statusCode != MPS_RESPONSE_STATUS_CODE_SUCCESS ){ Iterator * ite = buildIterator(response->commands); char * errorMessage = hasMoreElements(ite)?next(ite):NULL; setError(validator , errorMessage); return NULL; }else{ return buildVdaInformation(vdaComp->vdaName , response->commands); } }
void executeKssCommandTddDump(ListenSocket ls){ RuntimeErrorValidator * validator = buildErrorSuccessValidator(); MpsRequest * req = buildMpsRequest( generateRandomKey(16) , SHELL_TDD_DUMP , NULL); MpsResponse * resp = sendAndReceiveMpsMessages(ls , req ,validator); Iterator * ite = buildIterator(resp->returnValues); if(isDebugEnabled()) debug(concatAll(2, "Codigo de resultado: " , resp->statusCode)); if( equalsStrings(resp->statusCode , MPS_RESPONSE_STATUS_CODE_SUCCESS)){ info("Resultado satisfactorio "); }else{ printf("%s\n" , ( hasMoreElements(ite) ? (char *) next(ite) : "Ha ocurrido un error")); } }
void testRandomKeys (void){ printf("%s\n", generateRandomKey(20 , getGlobalHeap())); printf("%s\n", generateRandomKey(20 , getGlobalHeap())); printf("%s\n", generateRandomKey(20 , getGlobalHeap())); printf("%s\n", generateRandomKey(20 , getGlobalHeap())); printf("%s\n", generateRandomKey(20 , getGlobalHeap())); printf("%s\n", generateRandomKey(20 , getGlobalHeap())); printf("%s\n", generateRandomKey(20 , getGlobalHeap())); printf("%s\n", generateRandomKey(20 , getGlobalHeap())); }
void testFileTransfer(){ MpsHeader header; header.payloadDescriptor = '0'; memcpy(header.descriptorId , generateRandomKey(16) , 16); //strcpy(header.descriptorId , generateRandomKey(16)); File * file1 = fopen("/home/gonzalo/images/anonimo.jpg" , "r"); char readingBuffer[1024]; char buffer[65536]; char * opName = "snd_img1"; int offset = 0; while(! feof(file1) && header.payloadDescriptor == '0'){ bzero(buffer , 65536); fread(readingBuffer , sizeof(char) , 1024 , file1); FileBlock * block = getFileBlock(readingBuffer); header.payloadDescriptor = 'b'; header.payloadSize = sizeof(FileBlock) + strlen("snd_img1"); memcpy(buffer , &header , sizeof(MpsHeader)); offset += sizeof(MpsHeader); memcpy(buffer + 21 , opName , strlen("snd_img1")); offset += strlen("snd_img1"); memcpy(buffer + 29 , block , sizeof(FileBlock)); offset += sizeof(FileBlock); sendBytes(getFileSystemConnection() , buffer , offset ); recv(getFileSystemConnection() , &header , sizeof(MpsHeader) , 0); } header.payloadSize = 0; header.payloadDescriptor = 'a'; sendBytes(getFileSystemConnection() , &header , sizeof(MpsHeader)); fclose(file1); }
void callVdaOperationPutSectores(VdaComponent * vdaComp , FileBlock * fileBlock , RuntimeErrorValidator * validator){ MpsMessage * message = buildMpsMessage(generateRandomKey(16) , MPS_RESPONSE_STATUS_CODE_SUCCESS , VDA_SECTORS_PUTTING , NULL); message->block = fileBlock; message = sendBlockAndReceiveMpsMessage(vdaComp->listenSocket , message , validator); if(hasError(validator)){ error(validator->errorDescription); return; }else if(message->statusCode == MPS_RESPONSE_STATUS_CODE_ERROR){ setError(validator , "Se ha producido un error en la operacion PutSectores"); error(validator->errorDescription); return; } else{ debug("La operacion PutSectores se ejecuto correctamente"); } }
void executeKssCommandFormat(char * arg , ListenSocket ls){ RuntimeErrorValidator * validator = buildErrorSuccessValidator(); //List params = getFirstParameter("Dispositivo a Formatear"); List params = buildStringsListWithValues(1 , arg); MpsRequest * req = buildMpsRequest( generateRandomKey(16) , SHELL_FORMAT , params); MpsResponse * resp = sendAndReceiveMpsMessages(ls , req ,validator); if(isDebugEnabled()) debug(concatAll(2, "Codigo de resultado: " , resp->statusCode)); Iterator * ite = buildIterator(resp->returnValues); if( equalsStrings(resp->statusCode , MPS_RESPONSE_STATUS_CODE_SUCCESS)){ printf("Dispositivo formateado\n"); }else{ printf("%s" , ( hasMoreElements(ite) ? (char *) next(ite) : "Ha ocurrido un error")); } }
void initializeKssConnection(){ ListenSocket kssConnectionSocket; if( isInfoEnabled()) info( "Iniciando la conexion con el KSS"); kssConnectionSocket = openConnectionSocket( getKssAddress() , getKssPort()); setKssConnection( kssConnectionSocket ); if( isInfoEnabled()) if( isKssConnectionOpen() ){ RuntimeErrorValidator * validator; List arguments ; MpsRequest * request ; MpsResponse * response ; info( concat(getGlobalHeap() , 4, "Proceso VDA conectado con proceso remoto Kss en host " , getKssAddress() , ":" , getKssPort())); validator = buildErrorSuccessValidator(getGlobalHeap()); arguments = buildStringsListWithValues(getGlobalHeap() , 1, getVdaName()); request = buildMpsRequest(generateRandomKey(16 , getGlobalHeap()) , VDA_IDENTIFY_NAME , arguments , getGlobalHeap()); response = sendAndReceiveMpsMessages(kssConnectionSocket , request , validator, getGlobalHeap()); if(equalsStrings(response->statusCode , MPS_RESPONSE_STATUS_CODE_SUCCESS)){ info(concat(getGlobalHeap() , 2 , "Proceso VDA identificado con el Kss satisfactoriamente bajo el nombre: " , getVdaName())); }else{ error("El proceso no se pudo identificar con el Kss por lo que el Kss no lo reconoce como dispositivo"); } }else { info( "Proceso VDA trabajando en modo offline. Fallo la conexion con servicio KSS"); } }
FileBlock * callVdaOperationGetSectores(VdaComponent * vdaComp , int sector1 , int sector2 , RuntimeErrorValidator * validator){ MpsMessage * message = buildMpsMessage(generateRandomKey(16) , MPS_RESPONSE_STATUS_CODE_SUCCESS , VDA_SECTORS_GETTING , buildStringsListWithValues(2 , itoa(sector1) , itoa(sector2))); message = sendMpsMessageAndReceiveBlock(vdaComp->listenSocket , message , validator); if(hasError(validator)){ error(validator->errorDescription); return NULL; }else if ( message == NULL || message->statusCode == MPS_RESPONSE_STATUS_CODE_ERROR ){ setError(validator , "Se ha producido un error en la operacion GetSectores del VDA"); error(validator->errorDescription); return NULL; }else if(message->block == NULL){ setError(validator , "Se ha producido un error en getSectores, el block esta en null"); return NULL; }else{ return message->block; } }
void* console_thread(void* arg) { BankSocketThread* bankSocketThread = (BankSocketThread*) arg; Bank* bank = bankSocketThread->bank; //Let's generate our keys for(unsigned int i = 1; i <= 50; ++i) { byte* key = new byte[CryptoPP::AES::DEFAULT_KEYLENGTH]; generateRandomKey(to_string((int)i),key, CryptoPP::AES::DEFAULT_KEYLENGTH); bank->keys.push_back(key); bank->keysInUse.push_back(false); } //Create Accounts Account* new_account = new Account(); //Alice new_account->createAccount(std::string("alice"), 1, std::string("123456"), bank->appSalt); new_account->Deposit(100); bank->addAccount(new_account); //Bob new_account = new Account(); new_account->createAccount(std::string("bob"), 2, std::string("234567"), bank->appSalt); new_account->Deposit(50); bank->addAccount(new_account); //Eve new_account = new Account(); new_account->createAccount(std::string("eve"), 3, std::string("345678"), bank->appSalt); new_account->Deposit(0); bank->addAccount(new_account); char buf[80]; while(1) { printf("bank> "); fgets(buf, 79, stdin); buf[strlen(buf)-1] = '\0'; //trim off trailing newline std::vector<std::string> tokens; split(buf,' ',tokens); if(tokens.size() <= 0) { printf("Invalid input\n"); continue; } if(tokens[0] == "balance") { if(tokens.size() != 2) { printf("Invalid input\n"); continue; } Account* current_account = bank->getAccountByName(tokens[1]); if(!current_account) { printf("Invalid account\n"); continue; } printf("Balance: %.2Lf\n", current_account->getBalance()); continue; } if(tokens[0] == "deposit") { if(tokens.size() != 3) { printf("Invalid input\n"); continue; } long double amount = atof(tokens[2].c_str()); if(amount <= 0) { printf("Invalid amount\n"); continue; } Account* current_account = bank->getAccountByName(tokens[1]); if(!current_account) { printf("Invalid account\n"); continue; } if(current_account->Deposit(amount)) { long double cur_balance = current_account->getBalance(); printf("Money deposited!\nNew balance: %.2Lf\n", cur_balance); } else { printf("Error depositing money!\n"); } continue; } } }