blob: 65b1abcc4a12d6d28c71d04cce2fb0776dfed303 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyev548d38d2013-01-26 16:36:06 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev548d38d2013-01-26 16:36:06 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyev548d38d2013-01-26 16:36:06 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyev548d38d2013-01-26 16:36:06 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyev548d38d2013-01-26 16:36:06 -080019 */
20
Alexander Afanasyev61ced272015-05-31 16:25:16 -070021#include "csd.hpp"
22#include <thread>
Alexander Afanasyev2b0363a2013-01-28 21:42:12 -080023
Alexander Afanasyev61ced272015-05-31 16:25:16 -070024namespace ndn {
25namespace chronoshare {
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080026
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080027int
28main(int argc, char* argv[])
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080029{
Alexander Afanasyev2b0363a2013-01-28 21:42:12 -080030 QCoreApplication app(argc, argv);
Alexander Afanasyev61ced272015-05-31 16:25:16 -070031 Runner runner(&app);
32 QObject::connect(&runner, SIGNAL(terminateApp()), &app, SLOT(quit()), Qt::QueuedConnection);
Alexander Afanasyev2b0363a2013-01-28 21:42:12 -080033
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080034 if (argc != 4) {
Alexander Afanasyev61ced272015-05-31 16:25:16 -070035 std::cerr << "Usage: ./csd <username> <shared-folder> <path>" << std::endl;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080036 return 1;
37 }
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080038
Alexander Afanasyev61ced272015-05-31 16:25:16 -070039 std::string username = argv[1];
40 std::string sharedFolder = argv[2];
41 std::string path = argv[3];
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080042
Alexander Afanasyev61ced272015-05-31 16:25:16 -070043 std::cout << "Starting ChronoShare for [" << username << "] shared-folder [" << sharedFolder
44 << "] at [" << path << "]" << std::endl;
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080045
Alexander Afanasyev61ced272015-05-31 16:25:16 -070046 boost::asio::io_service ioService;
47 Face face(ioService);
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080048
Alexander Afanasyev61ced272015-05-31 16:25:16 -070049 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 Afanasyeveda3b7a2016-12-25 11:26:40 -080072 bind(&Dispatcher::Did_LocalFile_AddOrModify, &dispatcher, _1),
73 bind(&Dispatcher::Did_LocalFile_Delete, &dispatcher, _1));
Alexander Afanasyev548d38d2013-01-26 16:36:06 -080074
Alexander Afanasyev61ced272015-05-31 16:25:16 -070075 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
100int
101main(int argc, char* argv[])
102{
103 return ndn::chronoshare::main(argc, argv);
Alexander Afanasyev548d38d2013-01-26 16:36:06 -0800104}