libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

gnunet_chat_discourse_intern.c (2536B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2024 GNUnet e.V.
      4 
      5    GNUnet is free software: you can redistribute it and/or modify it
      6    under the terms of the GNU Affero General Public License as published
      7    by the Free Software Foundation, either version 3 of the License,
      8    or (at your option) any later version.
      9 
     10    GNUnet is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18    SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @author Tobias Frisch
     22  * @file gnunet_chat_discourse_intern.c
     23  */
     24 
     25 #include "gnunet_chat_context.h"
     26 
     27 #define GNUNET_UNUSED __attribute__ ((unused))
     28 
     29 #define MAX_WRITE_SIZE (    \
     30   GNUNET_MAX_MESSAGE_SIZE - \
     31   GNUNET_MIN_MESSAGE_SIZE - \
     32   sizeof (struct GNUNET_MESSENGER_Message))
     33 
     34 static void
     35 cb_read_discourse_pipe (void *cls);
     36 
     37 void
     38 cb_reinit_discourse_pipe (void *cls)
     39 {
     40   struct GNUNET_CHAT_Discourse *discourse = cls;
     41 
     42   GNUNET_assert(discourse);
     43 
     44   discourse->pipe_task = NULL;
     45 
     46   if (-1 == discourse->pipe[0])
     47     return;
     48 
     49   struct GNUNET_NETWORK_FDSet *rs = GNUNET_NETWORK_fdset_create();
     50 
     51   GNUNET_NETWORK_fdset_set_native(rs, discourse->pipe[0]);
     52 
     53   discourse->pipe_task = GNUNET_SCHEDULER_add_select(
     54     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
     55     GNUNET_TIME_UNIT_FOREVER_REL,
     56     rs,
     57     NULL,
     58     cb_read_discourse_pipe,
     59     discourse
     60   );
     61 
     62   GNUNET_NETWORK_fdset_destroy(rs);
     63 }
     64 
     65 static void
     66 cb_read_discourse_pipe (void *cls)
     67 {
     68   struct GNUNET_CHAT_Discourse *discourse = cls;
     69 
     70   GNUNET_assert((discourse) && (-1 != discourse->pipe[0]));
     71 
     72   discourse->pipe_task = NULL;
     73 
     74   struct GNUNET_MESSENGER_Message msg;
     75   memset(&msg, 0, sizeof(msg));
     76 
     77   msg.header.kind = GNUNET_MESSENGER_KIND_TALK;
     78 
     79   util_shorthash_from_discourse_id(
     80     &(discourse->id),
     81     &(msg.body.talk.discourse)
     82   );
     83 
     84   char data [MAX_WRITE_SIZE];
     85   ssize_t len;
     86 
     87   do
     88   {
     89     len = read(discourse->pipe[0], data, MAX_WRITE_SIZE);
     90 
     91     if (len <= 0)
     92       break;
     93 
     94     msg.body.talk.data = data;
     95     msg.body.talk.length = (uint16_t) len;
     96 
     97     GNUNET_MESSENGER_send_message(discourse->context->room, &msg, NULL);
     98   }
     99   while (MAX_WRITE_SIZE == len);
    100 
    101   if (len < 0)
    102     return;
    103 
    104   discourse->pipe_task = GNUNET_SCHEDULER_add_now(
    105     cb_reinit_discourse_pipe, discourse
    106   );
    107 }