123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #include "PIRViewCLI.hpp"
- #include "PIRController.hpp"
- PIRViewCLI::PIRViewCLI(PIRController* controller_) :
- PIRView(controller_)
- {}
- void PIRViewCLI::messageUpdate(MessageEvent& event)
- {
- if(event.getMessageType() == RETRY){
- getUserInputRetry();
- }
- else if(event.getMessageType() == DEFAULT){
- std::cout << event.getMessage() << std::endl;
- }
- else
- {
- std::string color = (event.getMessageType() == WARNING) ? ORANGE : RED;
- std::cout << BOLD << event.getInfo() << " : " << RESET_COLOR << color << event.getMessage() << RESET_COLOR << std::endl;
- }
- }
- void PIRViewCLI::catalogUpdate(CatalogEvent& event)
- {
- using namespace std;
- cout << endl;
- cout << "##############################################" << endl;
- cout << "# #" << endl;
- cout << "# Connection established #" << endl;
- cout << "# #" << endl;
- cout << "##############################################" << endl;
- cout << "# #" << endl;
- cout << "# File List : #" << endl;
- cout << "# °°°°°°°°°°° #" << endl;
- cout << "# #" << endl;
- for (unsigned int i = 0 ; i < event.getCatalog().size() ; i++)
- {
- cout << "# " << i+1 << ") " << event.getCatalog().at(i);
- for(unsigned int j = 0; j < 40 - event.getCatalog().at(i).length(); j++)
- cout << " ";
- cout << "#" << endl;
- }
- cout << "# #" << endl;
- cout << "##############################################" << endl;
- cout << "# #" << endl;
- cout << "# Which file do you want ? #" << endl;
- getUserInputFile(event.getCatalog().size()) ;
- }
- void PIRViewCLI::writeUpdate(WriteEvent& event)
- {
- using namespace std;
- std::cout << "\033[1GPIRReplyWriter: Remaning Bytes to write : " << event.getSizeToWrite() - event.getWrittenSize() << " \033[5G" << "\xd"<< std::flush;
- if(event.getSizeToWrite() == event.getWrittenSize())
- {
- cout << endl << endl <<"\t## SUCESS ! ##" << endl;
- cout << "\t °°°°°°" << endl;
- }
- }
- void PIRViewCLI::getUserInputRetry()
- {
- using namespace std;
- char choice;
- cout << "Enable de reach the server, would you like to retry ? (Y/n) : ";
- retrying:
- cin.clear();
- choice = cin.get();
- if (choice == 'N'||choice == 'n') {
- controller->notifyClientChoice(false);
- }
- else if (choice == 'Y' || choice == 'y'|| choice == '\n') {
- controller->notifyClientChoice(true);
- }
- else
- {
- cout << "Bad input, retry : ";
- cin.ignore(1);
- goto retrying;
- }
- }
- void PIRViewCLI::getUserInputFile(int maxValue)
- {
- using namespace std;
- int choice = -1;
- bool retry;
- MessageEvent event(WARNING);
-
- do
- {
- retry = false;
- cin >> choice ;
- cin.clear();
- cin.get();
-
- if (choice > maxValue || choice <= 0)
- {
- retry = true;
- event.setMessage("This file doesn't exist, retry :");
- messageUpdate(event);
- }
- }while(retry);
- controller->notifyClientChoice(--choice);
- }
|