PIRServer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "PIRServer.hpp"
  18. #include "PIRSession.hpp"
  19. /**
  20. * Class constructor:
  21. * Params :
  22. * - tcp::endpoint& endpoint_down : download enpoint ;
  23. * - tcp::endpoint& endpoint_up : upload endpoit.
  24. **/
  25. PIRServer::PIRServer(boost::asio::io_service &ios, unsigned int port, uint64_t split_value, bool usedbgenerator, uint64_t dbgenerator_n, uint64_t dbgenerator_l) :
  26. acceptor(ios, tcp::endpoint(tcp::v4(), port)),
  27. drivenMode(true),
  28. pirParamsFilePath("exp/PIRParams.cfg"),
  29. no_pipeline_mode(false)
  30. {
  31. acceptor.set_option(tcp::acceptor::reuse_address(true));
  32. session_option.driven_mode = true;
  33. session_option.keep_database = false;
  34. session_option.got_preimported_database = false;
  35. if (usedbgenerator)
  36. {
  37. std::cout << "PIRServer: Launching DBGenerator" << std::endl;
  38. dbhandler = new DBGenerator(dbgenerator_n, dbgenerator_l, false);
  39. }
  40. else if (split_value == 1)
  41. {
  42. std::cout << "PIRServer: Launching DBDirectoryProcessor without file splitting" << std::endl;
  43. dbhandler = new DBDirectoryProcessor();
  44. }
  45. else
  46. {
  47. std::cout << "PIRServer: Launching DBDirectoryProcessor with split_value=" << split_value
  48. << std::endl;
  49. dbhandler = new DBDirectoryProcessor(split_value);
  50. }
  51. }
  52. /**
  53. * Wait new client and fork when new connexion.
  54. **/
  55. void PIRServer::serve()
  56. {
  57. PIRSession::pointer new_session = PIRSession::create(acceptor.get_io_service());
  58. new_session->no_pipeline(no_pipeline_mode);
  59. new_session->setDBHandler(dbhandler);
  60. if (!drivenMode) new_session->setPIRParams(savedPIRParams);
  61. acceptor.async_accept(new_session->getSessionSocket(),
  62. boost::bind( &PIRServer::handleAccept, this, new_session, boost::asio::placeholders::error ));
  63. }
  64. void PIRServer::processDrivenSession(const std::string & file_path)
  65. {
  66. std::cout <<"PIRServer: Creating initial PIRSession" << std::endl;
  67. std::cout <<"PIRServer: Please launch a client to configure the server"<<std::endl;
  68. session_option.driven_mode = true;
  69. session_option.keep_database = true;
  70. // Loop while we get dry-run queries
  71. PIRSession* session_ptr = new PIRSession(acceptor.get_io_service());
  72. do
  73. {
  74. // Trick to have only one PIRSession at a time ... must be a better way :)
  75. delete session_ptr;
  76. session_ptr = new PIRSession(acceptor.get_io_service());
  77. session_ptr->setDBHandler(dbhandler);
  78. session_ptr->no_pipeline(no_pipeline_mode);
  79. acceptor.accept(session_ptr->getSessionSocket());
  80. }while(session_ptr->start(session_option));
  81. savedPIRParams = session_ptr->getPIRParams();
  82. session_option.data = session_ptr->getSavedDatabase();
  83. session_option.driven_mode = false;
  84. session_option.keep_database = false;
  85. if (session_option.data.imported_database_ptr != NULL) session_option.got_preimported_database = true;
  86. if(ServerService::writePIRParameters(savedPIRParams, file_path)) {
  87. cout << "Unable to write PIR parameters configuration data, maybe target repertory doesn't exist ?" << std::endl;
  88. return;
  89. }
  90. std::cout << "PIRServer: Configuration session successfuly ended" << endl << endl;
  91. drivenMode = false;
  92. }
  93. void PIRServer::handleAccept(PIRSession::pointer pir_session, const boost::system::error_code& error)
  94. {
  95. std::cout<<"PIRServer: Creating new PIRSession"<<std::endl;
  96. if(!error)
  97. {
  98. boost::thread t(boost::bind(&PIRSession::start, pir_session, session_option));
  99. }
  100. serve();
  101. }
  102. void PIRServer::readPIRParamsFromFile(const std::string& file_path)
  103. {
  104. if (ServerService::readPIRParameters(savedPIRParams, file_path)) {
  105. cout << "PIR parameters configuration file not found, try to launch the server in driven mode." << std::endl;
  106. exit(1);
  107. }
  108. drivenMode = false;
  109. }
  110. void PIRServer::no_pipeline(bool b)
  111. {
  112. no_pipeline_mode = b;
  113. }
  114. PIRServer::~PIRServer()
  115. {
  116. delete dbhandler;
  117. }