summaryrefslogtreecommitdiff
path: root/trunk/src
diff options
context:
space:
mode:
Diffstat (limited to 'trunk/src')
-rw-r--r--trunk/src/AbstractSecushareController.cpp242
-rw-r--r--trunk/src/AbstractSecushareView.cpp13
-rw-r--r--trunk/src/AbstractUserConfigModel.cpp8
-rw-r--r--trunk/src/CMakeLists.txt61
-rw-r--r--trunk/src/Identity.cpp27
-rw-r--r--trunk/src/NotifyPipe.cpp36
-rw-r--r--trunk/src/Place.cpp9
-rw-r--r--trunk/src/QtUserConfigModel.cpp44
-rw-r--r--trunk/src/RedThreadQt.cpp40
-rw-r--r--trunk/src/SecushareQtController.cpp44
-rw-r--r--trunk/src/SecushareQtView.cpp77
-rw-r--r--trunk/src/main.cpp21
12 files changed, 622 insertions, 0 deletions
diff --git a/trunk/src/AbstractSecushareController.cpp b/trunk/src/AbstractSecushareController.cpp
new file mode 100644
index 0000000..db720fb
--- /dev/null
+++ b/trunk/src/AbstractSecushareController.cpp
@@ -0,0 +1,242 @@
1/**
2 * @file AbstractSecushareController.cpp
3 * @brief Abstract controller class
4 *
5 * @author lurchi
6 * @date 2016-02-05
7 *
8 * @copyright
9 * This file is part of redthread which may be redistributed and/or modified
10 * under the terms of the GNU General Public License, either version 3 or any
11 * later version. See COPYING and <http://www.gnu.org/licenses/>.
12 */
13
14#include "AbstractSecushareController.hpp"
15#include <iostream>
16#include <vector>
17
18using namespace redthread;
19
20AbstractSecushareController::AbstractSecushareController(
21 std::shared_ptr<AbstractSecushareView> view,
22 const std::string& appName,
23 const std::string& appVersion,
24 const std::string& configPath
25)
26 : mView {view},
27 mAppName {appName},
28 mAppVersion {appVersion},
29 mConfigFilePath {configPath},
30 mRunning {false}
31{
32 auto config = mView->getUserConfigModel();
33
34 // TODO: read config from disk
35 config->setShowNewIdentityDialog(true);
36}
37
38int AbstractSecushareController::run()
39{
40 const std::vector<struct GNUNET_GETOPT_CommandLineOption> options
41 {
42 GNUNET_GETOPT_OPTION_END
43 };
44
45 std::string configFileOption {"-c"};
46
47 const std::vector<char*> argv
48 {
49 &mAppName[0],
50 &configFileOption[0],
51 &mConfigFilePath[0]
52 };
53
54 mRunning = true;
55
56 return
57 GNUNET_PROGRAM_run(argv.size(),
58 argv.data(),
59 mAppName.data(),
60 gettext_noop("binary description text"),
61 options.data(),
62 &AbstractSecushareController::runCb,
63 this);
64}
65
66void AbstractSecushareController::addExternalEventFD()
67{
68 auto fileHandle = GNUNET_DISK_get_handle_from_int_fd(mGnunetTaskPipe.receiverFd());
69
70 mExternalEventFdHandle =
71 GNUNET_SCHEDULER_add_read_file
72 (
73 GNUNET_TIME_UNIT_FOREVER_REL,
74 fileHandle,
75 &AbstractSecushareController::processGnunetTasksCb,
76 this
77 );
78
79}
80
81void AbstractSecushareController::addToGnunetTaskQueue(std::function<void()> task)
82{
83 {
84 std::lock_guard<std::mutex> lock {mGnunetTasksMutex};
85 mGnunetTasks.push(task);
86 }
87
88 mGnunetTaskPipe.notify();
89}
90
91void AbstractSecushareController::processGnunetTasksCb(
92 void* closure,
93 const struct GNUNET_SCHEDULER_TaskContext* context
94)
95{
96 //DEBUG
97 std::cout << "AbstractSecushareController::processGnunetTasksCb" << std::endl;
98
99 AbstractSecushareController* controller {static_cast<AbstractSecushareController*>(closure)};
100 auto& tasks = controller->mGnunetTasks;
101
102 {
103 std::lock_guard<std::mutex> lock {controller->mGnunetTasksMutex};
104
105 while(not tasks.empty())
106 {
107 tasks.front()();
108 tasks.pop();
109 }
110 }
111
112 if(controller->mRunning)
113 {
114 controller->addExternalEventFD();
115 }
116}
117
118void AbstractSecushareController::runCb(
119 void* closure,
120 char* const* args,
121 const char* configFilePath,
122 const struct GNUNET_CONFIGURATION_Handle* configHandle
123)
124{
125 AbstractSecushareController* controller {static_cast<AbstractSecushareController*>(closure)};
126
127 controller->mConfigHandle = configHandle;
128
129 controller->mAppHandle =
130 GNUNET_SOCIAL_app_connect(
131 configHandle,
132 controller->mAppName.c_str(),
133 &AbstractSecushareController::egoAvailableCb,
134 &AbstractSecushareController::placeAvailableAsHostCb,
135 &AbstractSecushareController::placeAvailableAsGuestCb,
136 closure
137 );
138
139 controller->mIdentityHandle =
140 GNUNET_IDENTITY_connect(controller->mConfigHandle,
141 &AbstractSecushareController::egoCreatedCb,
142 controller);
143
144 controller->addExternalEventFD();
145}
146
147void AbstractSecushareController::shutdown()
148{
149 addToGnunetTaskQueue(
150 [this]()
151 {
152 GNUNET_IDENTITY_disconnect(mIdentityHandle);
153 GNUNET_SOCIAL_app_disconnect(mAppHandle);
154 }
155 );
156
157 mRunning = false;
158}
159
160void AbstractSecushareController::addEgo(const std::string& name)
161{
162 addToGnunetTaskQueue(
163 [this, &name]()
164 {
165 GNUNET_IDENTITY_create(
166 mIdentityHandle,
167 name.data(),
168 &AbstractSecushareController::operationFinishedCb,
169 this
170 );
171 }
172 );
173}
174
175void AbstractSecushareController::addFriend(const std::string& nymPubKey, const std::string& name)
176{
177
178}
179
180void AbstractSecushareController::sendMessage(const PlaceId& placeId, const std::string& message)
181{
182
183}
184
185void AbstractSecushareController::egoAvailableCb(
186 void* closure,
187 struct GNUNET_SOCIAL_Ego* ego,
188 const struct GNUNET_CRYPTO_EcdsaPublicKey* egoPubKey,
189 const char* name
190)
191{
192 std::cout << "ego available: " << GNUNET_CRYPTO_ecdsa_public_key_to_string (egoPubKey) << std::endl;
193}
194
195void AbstractSecushareController::placeAvailableAsHostCb(
196 void* closure,
197 struct GNUNET_SOCIAL_HostConnection* connection,
198 struct GNUNET_SOCIAL_Ego* ego,
199 const struct GNUNET_CRYPTO_EddsaPublicKey* hostPubKey,
200 enum GNUNET_SOCIAL_PlaceState placeState
201)
202{
203 struct GNUNET_HashCode hostPubKeyHash;
204 GNUNET_CRYPTO_hash (hostPubKey, sizeof (*hostPubKey), &hostPubKeyHash);
205 std::cout << "place available as host: " << GNUNET_h2s(&hostPubKeyHash) << std::endl;
206}
207
208void AbstractSecushareController::placeAvailableAsGuestCb(
209 void* closure,
210 struct GNUNET_SOCIAL_GuestConnection* connection,
211 struct GNUNET_SOCIAL_Ego* ego,
212 const struct GNUNET_CRYPTO_EddsaPublicKey* guestPubKey,
213 enum GNUNET_SOCIAL_PlaceState placeState
214)
215{
216 struct GNUNET_HashCode guestPubKeyHash;
217 GNUNET_CRYPTO_hash (guestPubKey, sizeof (*guestPubKey), &guestPubKeyHash);
218 std::cout << "place available as guest: " << GNUNET_h2s(&guestPubKeyHash) << std::endl;
219}
220
221void AbstractSecushareController::egoCreatedCb(
222 void *closure,
223 struct GNUNET_IDENTITY_Ego* ego,
224 void** ctx,
225 const char* name
226)
227{
228 std::cout << "ego created" << name << std::endl;
229}
230
231void AbstractSecushareController::operationFinishedCb(void* closure, const char* errorMessage)
232{
233 // DEBUG:
234 if(errorMessage)
235 {
236 std::cout << "ERROR: " << errorMessage << std::endl;
237 }
238 else
239 {
240 std::cout << "no error" << std::endl;
241 }
242}
diff --git a/trunk/src/AbstractSecushareView.cpp b/trunk/src/AbstractSecushareView.cpp
new file mode 100644
index 0000000..2dfc460
--- /dev/null
+++ b/trunk/src/AbstractSecushareView.cpp
@@ -0,0 +1,13 @@
1// AbstractSecushareView.cpp
2//
3// author: lurchi
4// date: 2016-02-01
5
6#include "AbstractSecushareView.hpp"
7
8using namespace redthread;
9
10AbstractSecushareView::AbstractSecushareView()
11{
12
13}
diff --git a/trunk/src/AbstractUserConfigModel.cpp b/trunk/src/AbstractUserConfigModel.cpp
new file mode 100644
index 0000000..bba79ed
--- /dev/null
+++ b/trunk/src/AbstractUserConfigModel.cpp
@@ -0,0 +1,8 @@
1#include "AbstractUserConfigModel.hpp"
2
3using namespace redthread;
4
5AbstractUserConfigModel::AbstractUserConfigModel()
6{
7
8}
diff --git a/trunk/src/CMakeLists.txt b/trunk/src/CMakeLists.txt
new file mode 100644
index 0000000..cd186ca
--- /dev/null
+++ b/trunk/src/CMakeLists.txt
@@ -0,0 +1,61 @@
1# src/CMakeLists.txt
2
3include(CheckIncludeFileCXX)
4
5set(THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party")
6
7# gnunet
8set(GNUNET_SOCIAL_NEEDED true)
9find_package(Gnunet REQUIRED)
10set(LIBS ${LIBS} ${GNUNET_LIBRARIES})
11set(REDTHREAD_INCLUDE_DIRS ${REDTHREAD_INCLUDE_DIRS} ${GNUNET_INCLUDE_DIRS})
12
13# ncurses
14set(CURSES_NEED_NCURSES true)
15find_package(Curses 6 REQUIRED)
16set(LIBS ${LIBS} ${CURSES_LIBRARIES})
17set(REDTHREAD_INCLUDE_DIRS ${REDTHREAD_INCLUDE_DIRS} ${CURSES_INCLUDE_DIRS})
18
19# Qt
20set(CMAKE_AUTOMOC ON)
21find_package(Qt5Widgets REQUIRED)
22find_package(Qt5Quick REQUIRED)
23file(GLOB hpp_files "${PROJECT_SOURCE_DIR}/include/*.hpp")
24qt_wrap_cpp(redthread qt_wrappers ${hpp_files})
25set(source_files ${source_files} ${qt_wrappers})
26set(LIBS ${LIBS} Qt5::Widgets Qt5::Quick)
27
28# sigc++
29#find_package(SigC++ REQUIRED)
30#set(LIBS ${LIBS} ${SIGC++_LIBRARIES})
31#set(REDTHREAD_INCLUDE_DIRS ${REDTHREAD_INCLUDE_DIRS} ${SIGC++_INCLUDE_DIRS})
32
33# boost::signals2 (header only)
34#find_package(Boost REQUIRED)
35#set(REDTHREAD_INCLUDE_DIRS ${REDTHREAD_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
36
37#set(CMAKE_CXX_FLAGS "-O2 -Os -std=c++11 -Wall -Wextra -pedantic -lstdc++" CACHE STRING "" FORCE)
38set(CMAKE_CXX_FLAGS "-g -std=c++11 -Wall -Wextra")
39set(CMAKE_EXE_LINKER_FLAGS "")
40
41set(source_files ${source_files}
42 AbstractSecushareController.cpp
43 AbstractSecushareView.cpp
44 AbstractUserConfigModel.cpp
45 Identity.cpp
46 main.cpp
47 NotifyPipe.cpp
48 Place.cpp
49 QtUserConfigModel.cpp
50 RedThreadQt.cpp
51 SecushareQtController.cpp
52 SecushareQtView.cpp
53)
54
55add_executable(redthread ${source_files})
56target_include_directories(redthread PUBLIC
57 ${REDTHREAD_INCLUDE_DIRS}
58 ${PROJECT_SOURCE_DIR} # for config.h
59 ${PROJECT_SOURCE_DIR}/include)
60target_link_libraries(redthread ${LIBS})
61
diff --git a/trunk/src/Identity.cpp b/trunk/src/Identity.cpp
new file mode 100644
index 0000000..61e5344
--- /dev/null
+++ b/trunk/src/Identity.cpp
@@ -0,0 +1,27 @@
1#include "Identity.hpp"
2#include <algorithm>
3
4using namespace redthread;
5
6Identity::Identity(const std::string& masterPubKey,
7 const std::vector<std::string>& subordinatePubKeys)
8 : mMasterPubKey {masterPubKey}, mSubordinatePubKeys {subordinatePubKeys}
9{
10
11}
12
13bool Identity::operator==(const Identity& other) const
14{
15 return
16 (mMasterPubKey == other.mMasterPubKey) or
17 std::any_of(
18 other.mSubordinatePubKeys.cbegin(),
19 other.mSubordinatePubKeys.cend(),
20 [this](const std::string& k)
21 {
22 return
23 std::find(mSubordinatePubKeys.cbegin(), mSubordinatePubKeys.cend(), k)
24 != mSubordinatePubKeys.cend();
25 }
26 );
27}
diff --git a/trunk/src/NotifyPipe.cpp b/trunk/src/NotifyPipe.cpp
new file mode 100644
index 0000000..3127cf4
--- /dev/null
+++ b/trunk/src/NotifyPipe.cpp
@@ -0,0 +1,36 @@
1#include "NotifyPipe.hpp"
2
3#include <unistd.h>
4#include <assert.h>
5#include <fcntl.h>
6
7using namespace Util;
8
9NotifyPipe::NotifyPipe()
10{
11 int pipefd[2];
12 int ret = pipe(pipefd);
13 assert(ret == 0); // For real usage put proper check here
14 m_receiveFd = pipefd[0];
15 m_sendFd = pipefd[1];
16 fcntl(m_sendFd, F_SETFL, O_NONBLOCK);
17}
18
19
20NotifyPipe::~NotifyPipe()
21{
22 close(m_sendFd);
23 close(m_receiveFd);
24}
25
26
27int NotifyPipe::receiverFd()
28{
29 return m_receiveFd;
30}
31
32
33void NotifyPipe::notify()
34{
35 write(m_sendFd, "1", 1);
36}
diff --git a/trunk/src/Place.cpp b/trunk/src/Place.cpp
new file mode 100644
index 0000000..a2a36b0
--- /dev/null
+++ b/trunk/src/Place.cpp
@@ -0,0 +1,9 @@
1#include "Place.hpp"
2
3using namespace redthread;
4
5Place::Place(const std::string& pubKey, const IdentityId& owner)
6 : mPubKey {pubKey}, mOwner {owner}
7{
8
9}
diff --git a/trunk/src/QtUserConfigModel.cpp b/trunk/src/QtUserConfigModel.cpp
new file mode 100644
index 0000000..fb6b96c
--- /dev/null
+++ b/trunk/src/QtUserConfigModel.cpp
@@ -0,0 +1,44 @@
1#include "QtUserConfigModel.hpp"
2
3using namespace redthread;
4
5QtUserConfigModel::QtUserConfigModel(QObject* parent)
6 : QAbstractItemModel(parent), AbstractUserConfigModel()
7{
8
9}
10
11QtUserConfigModel::~QtUserConfigModel()
12{
13
14}
15
16int QtUserConfigModel::columnCount(const QModelIndex & parent) const
17{
18 // FIXME
19 return 0;
20}
21
22QVariant QtUserConfigModel::data(const QModelIndex & index, int role) const
23{
24 // FIXME
25 return {};
26}
27
28QModelIndex QtUserConfigModel::index(int row, int column, const QModelIndex & parent) const
29{
30 // FIXME
31 return {};
32}
33
34QModelIndex QtUserConfigModel::parent(const QModelIndex & index) const
35{
36 // FIXME
37 return {};
38}
39
40int QtUserConfigModel::rowCount(const QModelIndex & parent) const
41{
42 // FIXME
43 return 0;
44}
diff --git a/trunk/src/RedThreadQt.cpp b/trunk/src/RedThreadQt.cpp
new file mode 100644
index 0000000..63e0ca8
--- /dev/null
+++ b/trunk/src/RedThreadQt.cpp
@@ -0,0 +1,40 @@
1// RedThread.cpp
2//
3// author: lurchi
4// date: 2016-02-01
5
6#include "RedThreadQt.hpp"
7#include <QApplication>
8
9using namespace redthread;
10
11RedThreadQt::RedThreadQt(int argc, char** argv)
12 : mView {new SecushareQtView(argc, argv)},
13 mController {mView, "redthread", "0.1", "/home/christian/.config/gnunet.conf"}
14{
15 auto view = mView.get();
16 auto controller = &mController;
17
18 QApplication::connect(view, &SecushareQtView::shutdownRequest, controller, &SecushareQtController::shutdown);
19}
20
21RedThreadQt::~RedThreadQt()
22{
23 if(mControlThread.get_id() != std::thread::id {})
24 {
25 mControlThread.join();
26 }
27}
28
29int RedThreadQt::run()
30{
31 mControlThread = std::thread {&SecushareQtController::run, &mController};
32
33 /**
34 * FIXME:
35 * is there a race condition with GNUNET_SCHEDULER_run setting signal handlers?
36 */
37 int exitCode {mView->run()};
38
39 return exitCode;
40}
diff --git a/trunk/src/SecushareQtController.cpp b/trunk/src/SecushareQtController.cpp
new file mode 100644
index 0000000..512cc67
--- /dev/null
+++ b/trunk/src/SecushareQtController.cpp
@@ -0,0 +1,44 @@
1// SecushareQtController.cpp
2//
3// author: lurchi
4// date: 2016-02-01
5
6#include "SecushareQtController.hpp"
7
8using namespace redthread;
9
10SecushareQtController::SecushareQtController(std::shared_ptr<SecushareQtView> view,
11 const std::string& appName,
12 const std::string& appVersion,
13 const std::string& configPath,
14 QObject* parent)
15 : QObject(parent),
16 AbstractSecushareController(view, appName, appVersion, configPath)
17{
18
19}
20
21SecushareQtController::~SecushareQtController()
22{
23
24}
25
26void SecushareQtController::shutdown()
27{
28 AbstractSecushareController::shutdown();
29}
30
31void SecushareQtController::addEgo(const std::string& name)
32{
33 AbstractSecushareController::addEgo(name);
34}
35
36void SecushareQtController::addFriend(const std::string& nymPubKey, const std::string& name)
37{
38 AbstractSecushareController::addFriend(nymPubKey, name);
39}
40
41void SecushareQtController::sendMessage(const PlaceId& placeId, const std::string& message)
42{
43 AbstractSecushareController::sendMessage(placeId, message);
44}
diff --git a/trunk/src/SecushareQtView.cpp b/trunk/src/SecushareQtView.cpp
new file mode 100644
index 0000000..2253d58
--- /dev/null
+++ b/trunk/src/SecushareQtView.cpp
@@ -0,0 +1,77 @@
1// SecushareQtView.cpp
2//
3// author: lurchi
4// date: 2016-02-01
5
6#include "SecushareQtView.hpp"
7#include "config.h"
8#include <signal.h>
9#include <QQuickView>
10#include <QQuickItem>
11#include <QQmlEngine>
12
13using namespace redthread;
14
15Util::NotifyPipe SecushareQtView::shutdownPipe {};
16
17SecushareQtView::SecushareQtView(int argc, char** argv)
18 : QObject(),
19 AbstractSecushareView(),
20 mArgc {argc},
21 mArgv {argv},
22 mUserConfigModel {new QtUserConfigModel()}
23{
24 std::cout << "SecushareQtView ctor" << std::endl;
25}
26
27SecushareQtView::~SecushareQtView()
28{
29
30}
31
32void SecushareQtView::catchUnixSignals(
33 const std::vector<int>& quitSignals,
34 const std::vector<int>& ignoreSignals
35)
36{
37 sigNotifier = new QSocketNotifier(shutdownPipe.receiverFd(), QSocketNotifier::Read, nullptr);
38 QApplication::connect(sigNotifier, &QSocketNotifier::activated, &QApplication::quit);
39
40 auto handler = [](int sig) -> void
41 {
42 shutdownPipe.notify();
43 };
44
45 // all these signals will be ignored.
46 for(int sig : ignoreSignals)
47 {
48 signal(sig, SIG_IGN);
49 }
50
51 // each of these signals calls the handler (quits the QCoreApplication).
52 for(int sig : quitSignals)
53 {
54 signal(sig, handler);
55 }
56}
57
58int SecushareQtView::run()
59{
60 QApplication app(mArgc, mArgv);
61
62 QObject::connect(&app, &QApplication::aboutToQuit, this, &SecushareQtView::shutdownRequest);
63
64 catchUnixSignals({SIGQUIT, SIGINT, SIGTERM, SIGHUP});
65
66 QQuickView view (QUrl::fromLocalFile(QML_DIR "/application.qml"));
67 QObject* root = view.rootObject();
68 QQmlEngine *engine = QtQml::qmlEngine(root);
69 //QObject::connect(root, SIGNAL(shutdownButtonClicked()), this, SIGNAL(shutdownRequest()));
70 QObject::connect(engine, SIGNAL(quit()), this, SIGNAL(shutdownRequest()));
71 QObject::connect(engine, SIGNAL(quit()), &app, SLOT(quit()));
72
73 //view.setSource(QUrl::fromLocalFile(QML_DIR "/application.qml"));
74 view.show();
75
76 return app.exec();
77}
diff --git a/trunk/src/main.cpp b/trunk/src/main.cpp
new file mode 100644
index 0000000..d505293
--- /dev/null
+++ b/trunk/src/main.cpp
@@ -0,0 +1,21 @@
1// main.cpp
2//
3// author: lurchi
4// date: 2016-02-01
5
6#include "RedThreadQt.hpp"
7#include <iostream>
8
9int main(int argc, char** argv)
10{
11 using namespace redthread;
12
13 std::cout << "red thread" << std::endl;
14
15 RedThreadQt app {argc, argv};
16 int exitCode {app.run()};
17
18 std::cout << "exiting with code " << exitCode << std::endl;
19
20 return exitCode;
21}