Add Mirai, Start Nagare

This commit is contained in:
Florian Märkl 2018-11-24 19:09:22 +01:00
commit 62ae3613c1
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
8 changed files with 212 additions and 31 deletions

View file

@ -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}")

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_MIRAI_H
#define CHIAKI_MIRAI_H
#include "thread.h"
#include <stdbool.h>
#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

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_NAGARE_H
#define CHIAKI_NAGARE_H
#include "takion.h"
#include "log.h"
#include <stdbool.h>
#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

View file

@ -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);

71
lib/src/mirai.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <chiaki/mirai.h>
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;
}

30
lib/src/nagare.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <chiaki/nagare.h>
#include <chiaki/session.h>
CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
{
ChiakiNagare *nagare = &session->nagare;
nagare->log = &session->log;
nagare->log = &session->log;
}

View file

@ -17,6 +17,7 @@
#include <chiaki/senkusha.h>
#include <chiaki/session.h>
#include <chiaki/mirai.h>
#include <string.h>
#include <assert.h>
@ -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);
}
}

View file

@ -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
{