PIRViewCLI.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. std::cout << "\033[1GPIRReplyWriter: Remaning Bytes to write : " << event.getSizeToWrite() - event.getWrittenSize() << " \033[5G" << "\xd"<< std::flush;
  86. if(event.getSizeToWrite() == event.getWrittenSize())
  87. {
  88. cout << endl << endl <<"\t## SUCESS ! ##" << endl;
  89. cout << "\t °°°°°°" << endl;
  90. }
  91. }
  92. /**
  93. * Get user input for retry questions :
  94. * Param :
  95. * - const std::string& message : message to display.
  96. **/
  97. void PIRViewCLI::getUserInputRetry()
  98. {
  99. using namespace std;
  100. char choice;
  101. cout << "Enable de reach the server, would you like to retry ? (Y/n) : ";
  102. retrying:
  103. cin.clear();
  104. choice = cin.get();
  105. if (choice == 'N'||choice == 'n') {
  106. controller->notifyClientChoice(false);
  107. }
  108. else if (choice == 'Y' || choice == 'y'|| choice == '\n') {
  109. controller->notifyClientChoice(true);
  110. }
  111. else
  112. {
  113. cout << "Bad input, retry : ";
  114. cin.ignore(1);
  115. goto retrying;
  116. }
  117. }
  118. /**
  119. * Get user choice, used with updateCatalog method.
  120. * Param :
  121. * - int maxVulue : maximum value that the user can enter.
  122. **/
  123. void PIRViewCLI::getUserInputFile(int maxValue)
  124. {
  125. using namespace std;
  126. int choice = -1;
  127. bool retry;
  128. MessageEvent event(WARNING);
  129. do
  130. {
  131. retry = false;
  132. cin >> choice ;
  133. cin.clear();
  134. cin.get();
  135. if (choice > maxValue || choice <= 0)
  136. {
  137. retry = true;
  138. event.setMessage("This file doesn't exist, retry :");
  139. messageUpdate(event);
  140. }
  141. }while(retry);
  142. controller->notifyClientChoice(--choice);
  143. }