Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 21 | #include "csd.hpp" |
| 22 | #include <thread> |
Alexander Afanasyev | 2b0363a | 2013-01-28 21:42:12 -0800 | [diff] [blame] | 23 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 24 | namespace ndn { |
| 25 | namespace chronoshare { |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | int |
| 28 | main(int argc, char* argv[]) |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 29 | { |
Alexander Afanasyev | 2b0363a | 2013-01-28 21:42:12 -0800 | [diff] [blame] | 30 | QCoreApplication app(argc, argv); |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 31 | Runner runner(&app); |
| 32 | QObject::connect(&runner, SIGNAL(terminateApp()), &app, SLOT(quit()), Qt::QueuedConnection); |
Alexander Afanasyev | 2b0363a | 2013-01-28 21:42:12 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 34 | if (argc != 4) { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 35 | std::cerr << "Usage: ./csd <username> <shared-folder> <path>" << std::endl; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 36 | return 1; |
| 37 | } |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 39 | std::string username = argv[1]; |
| 40 | std::string sharedFolder = argv[2]; |
| 41 | std::string path = argv[3]; |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 43 | std::cout << "Starting ChronoShare for [" << username << "] shared-folder [" << sharedFolder |
| 44 | << "] at [" << path << "]" << std::endl; |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 46 | boost::asio::io_service ioService; |
| 47 | Face face(ioService); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 49 | Dispatcher dispatcher(username, sharedFolder, path, face); |
| 50 | |
| 51 | std::thread ioThread([&ioService, &runner] { |
| 52 | try { |
| 53 | ioService.run(); |
| 54 | runner.retval = 0; |
| 55 | } |
| 56 | catch (boost::exception& e) { |
| 57 | runner.retval = 2; |
| 58 | if (&dynamic_cast<std::exception&>(e) != nullptr) { |
| 59 | std::cerr << "ERROR: " << dynamic_cast<std::exception&>(e).what() << std::endl; |
| 60 | } |
| 61 | std::cerr << boost::diagnostic_information(e, true) << std::endl; |
| 62 | } |
| 63 | catch (std::exception& e) { |
| 64 | runner.retval = 2; |
| 65 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 66 | } |
| 67 | |
| 68 | QTimer::singleShot(0, &runner, SLOT(notifyAsioThread())); |
| 69 | }); |
| 70 | |
| 71 | FsWatcher watcher(ioService, path.c_str(), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | bind(&Dispatcher::Did_LocalFile_AddOrModify, &dispatcher, _1), |
| 73 | bind(&Dispatcher::Did_LocalFile_Delete, &dispatcher, _1)); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 75 | int retval = 0; |
| 76 | try { |
| 77 | retval = app.exec(); |
| 78 | } |
| 79 | catch (boost::exception& e) { |
| 80 | retval = 1; |
| 81 | if (&dynamic_cast<std::exception&>(e) != nullptr) { |
| 82 | std::cerr << "ERROR: " << dynamic_cast<std::exception&>(e).what() << std::endl; |
| 83 | } |
| 84 | std::cerr << boost::diagnostic_information(e, true) << std::endl; |
| 85 | } |
| 86 | catch (std::exception& e) { |
| 87 | retval = 1; |
| 88 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 89 | } |
| 90 | |
| 91 | ioService.stop(); |
| 92 | ioThread.join(); |
| 93 | |
| 94 | return std::max(retval, runner.retval); |
| 95 | } |
| 96 | |
| 97 | } // namespace chronoshare |
| 98 | } // namespace ndn |
| 99 | |
| 100 | int |
| 101 | main(int argc, char* argv[]) |
| 102 | { |
| 103 | return ndn::chronoshare::main(argc, argv); |
Alexander Afanasyev | 548d38d | 2013-01-26 16:36:06 -0800 | [diff] [blame] | 104 | } |