From 62ae3613c18a7430f63f0600f6c245f8b477e4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 24 Nov 2018 19:09:22 +0100 Subject: [PATCH] Add Mirai, Start Nagare --- lib/CMakeLists.txt | 8 +++- lib/include/chiaki/mirai.h | 47 ++++++++++++++++++++++++ lib/include/chiaki/nagare.h | 41 +++++++++++++++++++++ lib/include/chiaki/session.h | 3 ++ lib/src/mirai.c | 71 ++++++++++++++++++++++++++++++++++++ lib/src/nagare.c | 30 +++++++++++++++ lib/src/senkusha.c | 41 +++++++-------------- lib/src/session.c | 2 +- 8 files changed, 212 insertions(+), 31 deletions(-) create mode 100644 lib/include/chiaki/mirai.h create mode 100644 lib/include/chiaki/nagare.h create mode 100644 lib/src/mirai.c create mode 100644 lib/src/nagare.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 14b334c..c6647bd 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -9,7 +9,9 @@ set(HEADER_FILES include/chiaki/ctrl.h include/chiaki/rpcrypt.h include/chiaki/takion.h - include/chiaki/senkusha.h) + include/chiaki/senkusha.h + include/chiaki/nagare.h + include/chiaki/mirai.h) set(SOURCE_FILES src/common.c @@ -23,7 +25,9 @@ set(SOURCE_FILES src/takion.c src/senkusha.c src/utils.h - src/pb_utils.h) + src/pb_utils.h + src/nagare.c + src/mirai.c) add_subdirectory(protobuf) include_directories("${NANOPB_SOURCE_DIR}") diff --git a/lib/include/chiaki/mirai.h b/lib/include/chiaki/mirai.h new file mode 100644 index 0000000..a0a03db --- /dev/null +++ b/lib/include/chiaki/mirai.h @@ -0,0 +1,47 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_MIRAI_H +#define CHIAKI_MIRAI_H + +#include "thread.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct chiaki_mirai_t +{ + bool expected; + bool success; + ChiakiMutex mutex; + ChiakiCond cond; +} ChiakiMirai; + +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_init(ChiakiMirai *mirai); +CHIAKI_EXPORT void chiaki_mirai_fini(ChiakiMirai *mirai); +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_signal(ChiakiMirai *mirai, bool success); +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_begin(ChiakiMirai *mirai); +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_wait(ChiakiMirai *mirai, uint64_t timeout_ms); + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_MIRAI_H diff --git a/lib/include/chiaki/nagare.h b/lib/include/chiaki/nagare.h new file mode 100644 index 0000000..b625926 --- /dev/null +++ b/lib/include/chiaki/nagare.h @@ -0,0 +1,41 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_NAGARE_H +#define CHIAKI_NAGARE_H + +#include "takion.h" +#include "log.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct chiaki_nagare_t +{ + struct chiaki_session_t *session; + ChiakiLog *log; + ChiakiTakion takion; +} ChiakiNagare; + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_NAGARE_H diff --git a/lib/include/chiaki/session.h b/lib/include/chiaki/session.h index 3e3e131..1a85128 100644 --- a/lib/include/chiaki/session.h +++ b/lib/include/chiaki/session.h @@ -18,6 +18,7 @@ #ifndef CHIAKI_SESSION_H #define CHIAKI_SESSION_H +#include "nagare.h" #include "common.h" #include "thread.h" #include "log.h" @@ -110,6 +111,8 @@ typedef struct chiaki_session_t bool ctrl_session_id_received; ChiakiLog log; + + ChiakiNagare nagare; } ChiakiSession; CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, ChiakiConnectInfo *connect_info); diff --git a/lib/src/mirai.c b/lib/src/mirai.c new file mode 100644 index 0000000..153af61 --- /dev/null +++ b/lib/src/mirai.c @@ -0,0 +1,71 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include + +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_init(ChiakiMirai *mirai) +{ + mirai->expected = false; + mirai->success = false; + ChiakiErrorCode err = chiaki_mutex_init(&mirai->mutex); + if(err != CHIAKI_ERR_SUCCESS) + return err; + err = chiaki_cond_init(&mirai->cond); + if(err != CHIAKI_ERR_SUCCESS) + { + chiaki_mutex_fini(&mirai->mutex); + return err; + } + return CHIAKI_ERR_SUCCESS; +} + +CHIAKI_EXPORT void chiaki_mirai_fini(ChiakiMirai *mirai) +{ + chiaki_mutex_fini(&mirai->mutex); + chiaki_cond_fini(&mirai->cond); +} + +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_signal(ChiakiMirai *mirai, bool success) +{ + ChiakiErrorCode err = chiaki_mutex_lock(&mirai->mutex); + if(err != CHIAKI_ERR_SUCCESS) + return err; + mirai->success = success; + err = chiaki_cond_signal(&mirai->cond); + if(err != CHIAKI_ERR_SUCCESS) + return err; + return chiaki_mutex_unlock(&mirai->mutex); +} + +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_begin(ChiakiMirai *mirai) +{ + ChiakiErrorCode err = chiaki_mutex_lock(&mirai->mutex); + mirai->expected = true; + return err; +} + +CHIAKI_EXPORT ChiakiErrorCode chiaki_mirai_expect_wait(ChiakiMirai *mirai, uint64_t timeout_ms) +{ + ChiakiErrorCode err = chiaki_cond_timedwait(&mirai->cond, &mirai->mutex, timeout_ms); + if(err != CHIAKI_ERR_SUCCESS && err != CHIAKI_ERR_TIMEOUT) + return err; + mirai->expected = false; + ChiakiErrorCode err2 = chiaki_mutex_unlock(&mirai->mutex); + if(err2 != CHIAKI_ERR_SUCCESS) + return err2; + return err; +} \ No newline at end of file diff --git a/lib/src/nagare.c b/lib/src/nagare.c new file mode 100644 index 0000000..85b1980 --- /dev/null +++ b/lib/src/nagare.c @@ -0,0 +1,30 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + + +#include +#include + + +CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session) +{ + ChiakiNagare *nagare = &session->nagare; + nagare->log = &session->log; + nagare->log = &session->log; + + +} \ No newline at end of file diff --git a/lib/src/senkusha.c b/lib/src/senkusha.c index 3d5f8ea..47ae23a 100644 --- a/lib/src/senkusha.c +++ b/lib/src/senkusha.c @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -43,10 +44,7 @@ typedef struct senkusha_t ChiakiLog *log; ChiakiTakion takion; - bool bang_expected; - bool bang_received; - ChiakiMutex bang_mutex; - ChiakiCond bang_cond; + ChiakiMirai bang_mirai; } Senkusha; @@ -58,14 +56,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session) { Senkusha senkusha; senkusha.log = &session->log; - senkusha.bang_expected = false; - senkusha.bang_received = false; - ChiakiErrorCode err = chiaki_mutex_init(&senkusha.bang_mutex); + ChiakiErrorCode err = chiaki_mirai_init(&senkusha.bang_mirai); if(err != CHIAKI_ERR_SUCCESS) - return err; - err = chiaki_cond_init(&senkusha.bang_cond); - if(err != CHIAKI_ERR_SUCCESS) - goto error_bang_mutex; + goto error_bang_mirai; ChiakiTakionConnectInfo takion_info; takion_info.log = senkusha.log; @@ -85,16 +78,14 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session) if(err != CHIAKI_ERR_SUCCESS) { CHIAKI_LOGE(&session->log, "Senkusha connect failed\n"); - goto error_bang_cond; + goto error_bang_mirai; } CHIAKI_LOGI(&session->log, "Senkusha sending big\n"); - err = chiaki_mutex_lock(&senkusha.bang_mutex); + err = chiaki_mirai_expect_begin(&senkusha.bang_mirai); assert(err == CHIAKI_ERR_SUCCESS); - senkusha.bang_expected = true; - err = senkusha_send_big(&senkusha); if(err != CHIAKI_ERR_SUCCESS) { @@ -102,11 +93,10 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session) goto error_takion; } - err = chiaki_cond_timedwait(&senkusha.bang_cond, &senkusha.bang_mutex, BIG_TIMEOUT_MS); - senkusha.bang_expected = false; - chiaki_mutex_unlock(&senkusha.bang_mutex); + err = chiaki_mirai_expect_wait(&senkusha.bang_mirai, BIG_TIMEOUT_MS); + assert(err == CHIAKI_ERR_SUCCESS || err == CHIAKI_ERR_TIMEOUT); - if(!senkusha.bang_received) + if(!senkusha.bang_mirai.success) { if(err == CHIAKI_ERR_TIMEOUT) CHIAKI_LOGE(&session->log, "Senkusha bang receive timeout\n"); @@ -126,10 +116,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session) error_takion: chiaki_takion_close(&senkusha.takion); CHIAKI_LOGI(&session->log, "Senkusha closed takion\n"); -error_bang_cond: - chiaki_cond_init(&senkusha.bang_cond); -error_bang_mutex: - chiaki_mutex_fini(&senkusha.bang_mutex); +error_bang_mirai: + chiaki_mirai_fini(&senkusha.bang_mirai); return err; } @@ -148,17 +136,14 @@ static void senkusha_takion_data(uint8_t *buf, size_t buf_size, void *user) return; } - if(senkusha->bang_expected) + if(senkusha->bang_mirai.expected) { if(msg.type != tkproto_TakionMessage_PayloadType_BANG || !msg.has_bang_payload) { CHIAKI_LOGE(senkusha->log, "Senkusha expected bang payload but received something else\n"); return; } - chiaki_mutex_lock(&senkusha->bang_mutex); - senkusha->bang_received = true; - chiaki_cond_signal(&senkusha->bang_cond); - chiaki_mutex_unlock(&senkusha->bang_mutex); + chiaki_mirai_signal(&senkusha->bang_mirai, true); } } diff --git a/lib/src/session.c b/lib/src/session.c index 0e86651..00f2161 100644 --- a/lib/src/session.c +++ b/lib/src/session.c @@ -151,7 +151,6 @@ static void *session_thread_func(void *arg) if(!session->ctrl_session_id_received) { CHIAKI_LOGE(&session->log, "Ctrl has failed, shutting down\n"); - chiaki_ctrl_join(&session->ctrl); goto quit_ctrl; } @@ -161,6 +160,7 @@ static void *session_thread_func(void *arg) if(err != CHIAKI_ERR_SUCCESS) { CHIAKI_LOGE(&session->log, "Senkusha failed\n"); + goto quit_ctrl; } else {