PIRViewCLI.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "PIRViewCLI.hpp"
  18. #include "PIRController.hpp"
  19. /**
  20. * Class constructor.
  21. * Param :
  22. *
  23. **/
  24. PIRViewCLI::PIRViewCLI(PIRController* controller_) :
  25. PIRView(controller_)
  26. {}
  27. /**
  28. * Message notification, display event content.
  29. * Param :
  30. * - MessageEvent& event : MessageEvent reference to display.
  31. **/
  32. void PIRViewCLI::messageUpdate(MessageEvent& event)
  33. {
  34. if(event.getMessageType() == RETRY){
  35. getUserInputRetry();
  36. }
  37. else if(event.getMessageType() == DEFAULT){
  38. std::cout << event.getMessage() << std::endl;
  39. }
  40. else
  41. {
  42. std::string color = (event.getMessageType() == WARNING) ? ORANGE : RED;
  43. std::cout << BOLD << event.getInfo() << " : " << RESET_COLOR << color << event.getMessage() << RESET_COLOR << std::endl;
  44. }
  45. }
  46. /**
  47. * Catalog notification, display catalog content in a framed list.
  48. * Param :
  49. * - CatalogEvent& event : CatalogEvent reference to display.
  50. **/
  51. void PIRViewCLI::catalogUpdate(CatalogEvent& event)
  52. {
  53. using namespace std;
  54. cout << endl;
  55. cout << "##############################################" << endl;
  56. cout << "# #" << endl;
  57. cout << "# Connection established #" << endl;
  58. cout << "# #" << endl;
  59. cout << "##############################################" << endl;
  60. cout << "# #" << endl;
  61. cout << "# File List : #" << endl;
  62. cout << "# °°°°°°°°°°° #" << endl;
  63. cout << "# #" << endl;
  64. for (unsigned int i = 0 ; i < event.getCatalog().size() ; i++)
  65. {
  66. cout << "# " << i+1 << ") " << event.getCatalog().at(i);
  67. for(unsigned int j = 0; j < 40 - event.getCatalog().at(i).length(); j++)
  68. cout << " ";
  69. cout << "#" << endl;
  70. }
  71. cout << "# #" << endl;
  72. cout << "##############################################" << endl;
  73. cout << "# #" << endl;
  74. cout << "# Which file do you want ? #" << endl;
  75. getUserInputFile(event.getCatalog().size()) ;
  76. }
  77. /**
  78. * Writing notification, used when the client wants to display the writing state of the files.
  79. * Param :
  80. * - WriteEvent& event : WriteEvent reference.
  81. **/
  82. void PIRViewCLI::writeUpdate(WriteEvent& event)
  83. {
  84. using namespace std;
  85. /*
  86. std::cout << "\033[1GPIRReplyWriter: Remaning Bytes to write : " << event.getSizeToWrite() - event.getWrittenSize() << " \033[5G" << "\xd"<< std::flush;
  87. if(event.getSizeToWrite() == event.getWrittenSize())
  88. {
  89. cout << endl << endl <<"\t## SUCESS ! ##" << endl;
  90. cout << "\t °°°°°°" << endl;
  91. }
  92. */
  93. }
  94. /**
  95. * Get user input for retry questions :
  96. * Param :
  97. * - const std::string& message : message to display.
  98. **/
  99. void PIRViewCLI::getUserInputRetry()
  100. {
  101. using namespace std;
  102. char choice;
  103. cout << "Enable de reach the server, would you like to retry ? (Y/n) : ";
  104. retrying:
  105. cin.clear();
  106. choice = cin.get();
  107. if (choice == 'N'||choice == 'n') {
  108. controller->notifyClientChoice(false);
  109. }
  110. else if (choice == 'Y' || choice == 'y'|| choice == '\n') {
  111. controller->notifyClientChoice(true);
  112. }
  113. else
  114. {
  115. cout << "Bad input, retry : ";
  116. cin.ignore(1);
  117. goto retrying;
  118. }
  119. }
  120. /**
  121. * Get user choice, used with updateCatalog method.
  122. * Param :
  123. * - int maxVulue : maximum value that the user can enter.
  124. **/
  125. void PIRViewCLI::getUserInputFile(int maxValue)
  126. {
  127. using namespace std;
  128. int choice = -1;
  129. bool retry;
  130. MessageEvent event(WARNING);
  131. do
  132. {
  133. retry = false;
  134. cin >> choice ;
  135. cin.clear();
  136. cin.get();
  137. if (choice > maxValue || choice <= 0)
  138. {
  139. retry = true;
  140. event.setMessage("This file doesn't exist, retry :");
  141. messageUpdate(event);
  142. }
  143. }while(retry);
  144. controller->notifyClientChoice(--choice);
  145. }