From e94296f7f25161f6c16ca3a547ed3d3a43a128c6 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Thu, 31 May 2018 22:07:49 +0200 Subject: [PATCH] move connect/disconnect thread creation to cli program --- cmdline/cmdline.c | 13 ++----- cmdline/main.c | 96 +++++++++++++++++++++++++++++++++++++---------- src/mrimap.c | 5 +++ 3 files changed, 84 insertions(+), 30 deletions(-) diff --git a/cmdline/cmdline.c b/cmdline/cmdline.c index 530ee4d3..8e2c9bb1 100644 --- a/cmdline/cmdline.c +++ b/cmdline/cmdline.c @@ -32,6 +32,7 @@ your library */ #include "../src/mrpgp.h" + /* * Reset database tables. This function is called from Core cmdline. * @@ -451,8 +452,8 @@ char* mrmailbox_cmdline(mrmailbox_t* mailbox, const char* cmdline) "set []\n" "get \n" "configure\n" - "idle\n" - "interruptidle\n" + "connect\n" + "disconnect\n" "poll\n" "help imex (Import/Export)\n" "==============================Chat commands==\n" @@ -680,14 +681,6 @@ char* mrmailbox_cmdline(mrmailbox_t* mailbox, const char* cmdline) { ret = mrmailbox_configure(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED; } - else if( strcmp(cmd, "idle")==0 ) - { - ret = mrmailbox_idle(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED; - } - else if( strcmp(cmd, "interruptidle")==0 ) - { - ret = mrmailbox_interrupt_idle(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED; - } else if( strcmp(cmd, "poll")==0 ) { ret = mrmailbox_poll(mailbox)? COMMAND_SUCCEEDED : COMMAND_FAILED; diff --git a/cmdline/main.c b/cmdline/main.c index cd706181..de7c413f 100644 --- a/cmdline/main.c +++ b/cmdline/main.c @@ -35,28 +35,15 @@ all further options can be set using the set-command (type ? for help). */ #include "stress.h" -#define ANSI_RED "\e[31m" -#define ANSI_YELLOW "\e[33m" -#define ANSI_NORMAL "\e[0m" - - -static char* read_cmd(void) -{ - printf("> "); - static char cmdbuffer[1024]; - fgets(cmdbuffer, 1000, stdin); - - while( strlen(cmdbuffer)>0 - && (cmdbuffer[strlen(cmdbuffer)-1]=='\n' || cmdbuffer[strlen(cmdbuffer)-1]==' ') ) - { - cmdbuffer[strlen(cmdbuffer)-1] = '\0'; - } - - return cmdbuffer; -} +/******************************************************************************* + * Event Handler + ******************************************************************************/ static int s_do_log_info = 1; +#define ANSI_RED "\e[31m" +#define ANSI_YELLOW "\e[33m" +#define ANSI_NORMAL "\e[0m" static uintptr_t receive_event(mrmailbox_t* mailbox, int event, uintptr_t data1, uintptr_t data2) @@ -132,6 +119,65 @@ static uintptr_t receive_event(mrmailbox_t* mailbox, int event, uintptr_t data1, } +/******************************************************************************* + * The idle thread - wait for push messages + ******************************************************************************/ + + +pthread_t idle_thread; +int idle_thread_started = 0; + + +static void* idle_thread_entry_point(void* entry_arg) +{ + mrmailbox_t* mailbox = (mrmailbox_t*)entry_arg; + mrmailbox_idle(mailbox); // this may take hours ... + return NULL; +} + + +static void idle_connect(mrmailbox_t* mailbox) +{ + if( !idle_thread_started ) + { + idle_thread_started = 1; + pthread_create(&idle_thread, NULL, idle_thread_entry_point, mailbox); + } +} + + +static void idle_disconnect(mrmailbox_t* mailbox) +{ + if( idle_thread_started ) + { + mrmailbox_interrupt_idle(mailbox); + pthread_join(idle_thread, NULL); + idle_thread_started = 0; + } +} + + +/******************************************************************************* + * The main loop + ******************************************************************************/ + + +static char* read_cmd(void) +{ + printf("> "); + static char cmdbuffer[1024]; + fgets(cmdbuffer, 1000, stdin); + + while( strlen(cmdbuffer)>0 + && (cmdbuffer[strlen(cmdbuffer)-1]=='\n' || cmdbuffer[strlen(cmdbuffer)-1]==' ') ) + { + cmdbuffer[strlen(cmdbuffer)-1] = '\0'; + } + + return cmdbuffer; +} + + int main(int argc, char ** argv) { char* cmd = NULL; @@ -166,13 +212,22 @@ int main(int argc, char ** argv) char* arg1 = strchr(cmd, ' '); if( arg1 ) { *arg1 = 0; arg1++; } - if( strcmp(cmd, "clear")==0 ) + if( strcmp(cmd, "connect")==0 ) + { + idle_connect(mailbox); + } + else if( strcmp(cmd, "disconnect")==0 ) + { + idle_disconnect(mailbox); + } + else if( strcmp(cmd, "clear")==0 ) { printf("\n\n\n\n"); /* insert some blank lines to visualize the break in the buffer */ printf("\e[1;1H\e[2J"); /* should work on ANSI terminals and on Windows 10. If not, well, then not. */ } else if( strcmp(cmd, "getqr")==0 || strcmp(cmd, "getbadqr")==0 ) { + idle_connect(mailbox); char* qrstr = mrmailbox_get_securejoin_qr(mailbox, arg1? atoi(arg1) : 0); if( qrstr && qrstr[0] ) { if( strcmp(cmd, "getbadqr")==0 && strlen(qrstr)>40 ) { @@ -204,6 +259,7 @@ int main(int argc, char ** argv) } free(cmd); + idle_disconnect(mailbox); mrmailbox_close(mailbox); mrmailbox_unref(mailbox); mailbox = NULL; diff --git a/src/mrimap.c b/src/mrimap.c index 98c4661c..2818ffbf 100644 --- a/src/mrimap.c +++ b/src/mrimap.c @@ -893,6 +893,10 @@ void mrimap_watch_n_wait(mrimap_t* ths) time_t last_fullread_time = 0; + if( ths->m_watch_thread_running ) { + goto exit_; + } + ths->m_watch_thread_running = 1; mrmailbox_log_info(ths->m_mailbox, 0, "IMAP-watch-thread started."); @@ -1090,6 +1094,7 @@ exit_: UNBLOCK_IDLE ths->m_watch_thread_running = 0; + ths->m_watch_do_exit = 0; }