void QueryDayBillForm::Query() { if (editDate_->GetText().length() < 10) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "日期小于10位"; int result = JMessageBox::Show("-ERROR-", msg, v); ClearWindow(); Show(); editDate_->Show(); return; } //TODO 以下为实际的查询操作(某日交易记录) try { BankSession bs; bs.SetCmd(CMD_DAY_BILL); bs.SetAttribute("date", editDate_->GetText()); bs.SetAttribute("page", "0"); Singleton<TransactionManager>::Instance().DoAction(bs); if (bs.GetErrorCode() == 0) { ReportForm* form; form = dynamic_cast<ReportForm*>(Singleton<FormManager>::Instance().Get("ReportForm")); form->SetCmd(CMD_DAY_BILL); list<TransDetail>& tds = bs.GetDetails(); list<TransDetail>::iterator it = tds.begin(); form->SetLines(it->total); form->SetDetails(bs.GetDetails()); form->SetDate(editDate_->GetText()); Reset(); form->ClearWindow(); form->Show(); } else { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", bs.GetErrorMsg(), v); Reset(); ClearWindow(); Show(); return; } } catch (Exception& e) { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", e.what(), v); Reset(); ClearWindow(); Show(); return; } }
void BalanceInquiryForm::Submit() { if (editAccountId_->GetText().length() < 6) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "帐号小于6位"; int result = JMessageBox::Show("-ERROR-", msg, v); ClearWindow(); Show(); editAccountId_->Show(); return; } if (editPass_->GetText().length() < 6) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "密码小于6位"; int result = JMessageBox::Show("-ERROR-", msg, v); ClearWindow(); Show(); editPass_->Show(); return; } //TODO 以下为实际的余额查询操作 try { BankSession* bs = Singleton<Client>::Instance().GetBankSession(); bs->Clear(); bs->SetCmd(CMD_BALANCE_INQUIRY); bs->SetAttribute("account_id", editAccountId_->GetText()); bs->SetAttribute("pass", editPass_->GetText()); Singleton<TransactionManager>::Instance().DoAction(*bs); if (bs->GetErrorCode() == 0) { Reset(); ReceiptForm* form; form = dynamic_cast<ReceiptForm*>(Singleton<FormManager>::Instance().Get("BalanceInquiryReceiptForm")); form->SetReceiptFormType(ReceiptForm::RFT_BALANCE_INQUIRY); form->SetTitle("余额查询成功"); form->SetItemText("交易日期", bs->GetResponse("trans_date")); form->SetItemText("户 名", bs->GetResponse("name")); form->SetItemText("帐 号", bs->GetAttribute("account_id")); form->SetItemText("余 额", bs->GetResponse("balance")); form->Show(); } else { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", bs->GetErrorMsg(), v); ClearWindow(); Show(); return; } } catch (Exception& e) { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", e.what(), v); ClearWindow(); Show(); return; } }
void TransferForm::Submit() { if (editAccountId_->GetText().length() < 6) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "帐号小于6位"; int result = JMessageBox::Show("-ERROR-", msg,v); ClearWindow(); Show(); editAccountId_->Show(); return; } if (editPass_->GetText().length() < 6) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "密码小于6位"; int result = JMessageBox::Show("-ERROR-", msg,v); ClearWindow(); Show(); editPass_->Show(); return; } if (editOtherAccountId_->GetText().length() < 6) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "帐号小于6位"; int result = JMessageBox::Show("-ERROR-", msg,v); ClearWindow(); Show(); editAccountId_->Show(); return; } if (editMoney_->GetText().empty()) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "金额不能为空"; JMessageBox::Show("-ERROR-", msg, v); ClearWindow(); Show(); editAccountId_->Show(); return; } if (editAccountId_->GetText() == editOtherAccountId_->GetText()) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "不能转入到自身帐户"; JMessageBox::Show("-ERROR-", msg, v); ClearWindow(); Show(); editAccountId_->Show(); return; } // 以下为实际的转帐操作 try { BankSession bs; bs.Connect(); bs.SetCmd(CMD_TRANSFER); bs.SetAttribute("account_id", editAccountId_->GetText()); bs.SetAttribute("pass", editPass_->GetText()); bs.SetAttribute("money", editMoney_->GetText()); bs.SetAttribute("other_account_id", editOtherAccountId_->GetText()); Singleton<TransactionManager>::Instance().DoAction(bs); if (bs.GetErrorCode() == 0) { Reset(); std::vector<std::string> v; v.push_back(" YES "); std::string msg = "转帐成功"+bs.GetResponse("account_id"); /*int result = JMessageBox::Show("-MESSAGE-", msg,v); ClearWindow(); Show();*/ ReceiptForm* form; form = dynamic_cast<ReceiptForm*>(Singleton<FormManager>::Instance().Get("TransferReceiptForm")); form->SetReceiptFormType(ReceiptForm::RFT_TRANSFER); form->SetTitle("转帐成功"); form->SetItemText("交易日期", bs.GetResponse("trans_date")); form->SetItemText("户 名", bs.GetResponse("name")); form->SetItemText("帐 号", bs.GetAttribute("account_id")); form->SetItemText("对方帐号", bs.GetAttribute("other_account_id")); form->SetItemText("交易金额", bs.GetAttribute("money")); form->SetItemText("摘 要", "转帐"); form->SetItemText("余 额", bs.GetResponse("balance")); form->ClearWindow(); form->Show(); } else { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", bs.GetErrorMsg(), v); ClearWindow(); Show(); return; } } catch (Exception& e) { std::vector<std::string> v; v.push_back(" YES "); int result = JMessageBox::Show("-ERROR-", e.what(), v); ClearWindow(); Show(); return; } }
void LoginForm::Login() { if (editUser_->GetText().length() < 3) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "用户名小于3位"; int result = JMessageBox::Show("-ERROR-", msg, v); JWindow* win = jApp->GetCurrent(); //这里的win是之前按纽,就是引发消息窗的按纽 editUser_->Show(); win->Draw(); win->Refresh(); return; } if (editPass_->GetText().length() < 6) { std::vector<std::string> v; v.push_back(" YES "); std::string msg = "密码小于6位"; int result = JMessageBox::Show("-ERROR-", msg, v); JWindow* win = jApp->GetCurrent(); editPass_->Show(); win->Draw(); win->Refresh(); return; } //TODO:以下为实际的登陆操作 /* Socket sock; sock.Create(); sock.Connect("127.0.0.1", 8888); std::string str; str.append(editUser_->GetText()); str.append("#"); str.append(editPass_->GetText()); sock.Send(str.c_str(), str.length()); char buffer[1024] = {0}; sock.Recv(buffer, sizeof(buffer)); std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", buffer, v); Rest(); JForm* form = Singleton<FormManager>::Instance().Get("MainMenuForm"); form->ClearWindow(); form->Show(); */ try { BankSession bs; bs.SetCmd(CMD_LOGIN); bs.SetAttribute("name", editUser_->GetText()); bs.SetAttribute("pass", editPass_->GetText()); Singleton<TransactionManager>::Instance().DoAction(bs); if (bs.GetErrorCode() == 0) { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-MESSAGE-", "登录成功", v); Rest(); JForm* form = Singleton<FormManager>::Instance().Get("MainMenuForm"); form->ClearWindow(); form->Show(); } else { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", bs.GetErrorMsg(), v); ClearWindow(); Show(); } } catch (Exception& e) { std::vector<std::string> v; v.push_back(" YES "); JMessageBox::Show("-ERROR-", e.what(), v); ClearWindow(); Show(); } }
void* client_thread(void* arg) { BankSocketThread* bankSocketThread = (BankSocketThread*) arg; Bank* bank = bankSocketThread->bank; BankSession* bankSession = new BankSession(); bankSession->state = 0; bankSession->bank = bank; bankSession->key = 0; long int csock = (long int)*(bankSocketThread->csock); printf("[bank] client ID #%ld connected\n", csock); //input loop int length; char packet[1024]; bool fatalError = false; std::vector<std::string> tokens; while(1) { fatalError = false; tokens.clear(); if(!listenPacket(csock, packet)) { printf("[bank] fail to read packet\n"); break; } if(!bankSession->key) { if(bankSession->state != 0) { printf("[error] Unexpected state\n"); break; } for(unsigned int i = 0; i < bank->keys.size(); ++i) { if(bank->keysInUse[i]) { continue; } if(decryptPacket((char*)packet,bank->keys[i]) && std::string(packet).substr(0,9) == "handshake") { bankSession->key = bank->keys[i]; bank->keysInUse[i] = true; break; } } if(!bankSession->key) { printf("[error] Key not found.\n"); break; } } else { if(!decryptPacket((char*)packet, bankSession->key)) { printf("[error] Invalid key\n"); break; } } //Parse the packet //std::string strPacket = packet; split(std::string(packet),',', tokens); //We should get something, if not ignore this packet if(tokens.size() < 1) { continue; } if(tokens[0] == "logout") { bankSession->endSession(); break; } //Now we're compare what we go to what state we expect to be in switch(bankSession->state) { case 0: case 1: if(tokens.size() == 2 && tokens[0] == "handshake" && tokens[1].size() == 128) { bankSession->atmNonce = tokens[1]; bankSession->bankNonce = makeHash(randomString(128)); if(bankSession->bankNonce.size() == 0) { printf("Unexpected error\n"); fatalError = true; break; } buildPacket(packet, "handshakeResponse," + bankSession->atmNonce + "," + bankSession->bankNonce); if(!encryptPacket((char*)packet,bankSession->key)) { printf("Unexpected error\n"); fatalError = true; break; } if(!sendPacket(csock, packet)) { printf("Unexpected error\n"); fatalError = true; break; } bankSession->state = 2; } break; //Expecting a login case 2: if(!bankSession->validateNonce(std::string(packet))) { printf("Unexpected error\n"); fatalError = true; break; } if(tokens.size() == 5 && tokens[0] == "login" && tokens[1].size() == 128) { //Now we'll try to find the account //bankSession->account = bank->tryLoginHash(tokens[1]); bankSession->account = bank->getAccountByName(tokens[2]); if(!bankSession->account || !bankSession->account->tryHash(tokens[1])) { //Failed login //TODO Blacklist hash bankSession->error = true; //printf("[notice] Failed login!\n"); } bankSession->account->inUse = true; bankSession->state = 5; if(!bankSession->sendP(csock,packet, "ack")) { printf("Unexpected error!\n"); fatalError = true; break; } } break; case 5: bool returnBalance = false; bankSession->state = 4; if(bankSession->error) { returnBalance = false; } else if(tokens.size() == 3 && tokens[0] == "balance") { returnBalance = true; } else if(tokens.size() == 4 && tokens[0] == "withdraw" && isDouble(tokens[1])) { double amount = atof(tokens[1].c_str()); if(!bankSession->account->Withdraw(amount)) { printf("[error] Failed withdraw\n"); returnBalance = false; bankSession->error = true; } returnBalance = true; } else if(tokens.size() == 5 && tokens[0] == "transfer" && !isDouble(tokens[1]) && isDouble(tokens[2])) { Account* accountTo = bank->getAccountByName(tokens[1]); double amount = atof(tokens[2].c_str()); if(!bankSession->account->Transfer(amount, accountTo)) { printf("[error] Failed transfer\n"); returnBalance = false; bankSession->error = true; } returnBalance = true; } if(bankSession->error) { bankSession->sendP(csock, packet, "denied"); } else if(returnBalance) { char moneyStr[256]; sprintf(moneyStr,"%.2Lf",bankSession->account->getBalance()); bankSession->sendP(csock, packet, std::string(moneyStr)); } //Reset back to initial state bankSession->endSession(); break; } if(fatalError) { bankSession->endSession(); break; } } bankSession->endSession(); printf("[bank] client ID #%ld disconnected\n", csock); close(csock); delete bankSession; return NULL; }