LCOV - code coverage report
Current view: top level - src/im - message_engine.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 2 2 100.0 %
Date: 2024-12-21 08:56:24 Functions: 2 4 50.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : #pragma once
      18             : 
      19             : #include <string>
      20             : #include <map>
      21             : #include <set>
      22             : #include <chrono>
      23             : #include <mutex>
      24             : #include <cstdint>
      25             : #include <filesystem>
      26             : 
      27             : #include <msgpack.hpp>
      28             : #include <asio/steady_timer.hpp>
      29             : 
      30             : namespace jami {
      31             : 
      32             : class SIPAccountBase;
      33             : 
      34             : namespace im {
      35             : 
      36             : using MessageToken = uint64_t;
      37             : 
      38             : enum class MessageStatus { UNKNOWN=0, IDLE, SENDING, SENT, FAILURE };
      39             : 
      40             : class MessageEngine
      41             : {
      42             : public:
      43             :     MessageEngine(SIPAccountBase&, const std::filesystem::path& path);
      44             : 
      45             :     /**
      46             :      * Add a message to the engine and try to send it
      47             :      * @param to            Uri of the peer
      48             :      * @param deviceId      (Optional) if we want to send to a specific device
      49             :      * @param payloads      The message
      50             :      * @param refreshToken  The token of the message
      51             :      */
      52             :     MessageToken sendMessage(const std::string& to,
      53             :                              const std::string& deviceId,
      54             :                              const std::map<std::string, std::string>& payloads,
      55             :                              uint64_t refreshToken);
      56             : 
      57             :     MessageStatus getStatus(MessageToken t) const;
      58             : 
      59             :     void onMessageSent(const std::string& peer,
      60             :                        MessageToken t,
      61             :                        bool success,
      62             :                        const std::string& deviceId = {});
      63             : 
      64             :     /**
      65             :      * @TODO change MessageEngine by a queue,
      66             :      * @NOTE retryOnTimeout is used for failing SIP messages (jamiAccount::sendTextMessage)
      67             :      */
      68             :     void onPeerOnline(const std::string& peer,
      69             :                       const std::string& deviceId = {},
      70             :                       bool retryOnTimeout = true);
      71             : 
      72             :     /**
      73             :      * Load persisted messages
      74             :      */
      75             :     void load();
      76             : 
      77             :     /**
      78             :      * Persist messages
      79             :      */
      80             :     void save() const;
      81             : 
      82             : private:
      83             :     static const constexpr unsigned MAX_RETRIES = 20;
      84             :     using clock = std::chrono::system_clock;
      85             : 
      86             :     void retrySend(const std::string& peer,
      87             :                    const std::string& deviceId,
      88             :                    bool retryOnTimeout);
      89             : 
      90             :     void save_() const;
      91             :     void scheduleSave();
      92             : 
      93             :     struct Message
      94             :     {
      95             :         MessageToken token {};
      96             :         std::string to {};
      97             :         std::map<std::string, std::string> payloads {};
      98             :         MessageStatus status {MessageStatus::IDLE};
      99             :         unsigned retried {0};
     100             :         clock::time_point last_op {};
     101             : 
     102         837 :         MSGPACK_DEFINE_MAP(token, to, payloads, status, retried, last_op)
     103             :     };
     104             : 
     105             :     SIPAccountBase& account_;
     106             :     const std::filesystem::path savePath_;
     107             :     std::shared_ptr<asio::io_context> ioContext_;
     108             :     asio::steady_timer saveTimer_;
     109             : 
     110             :     std::map<std::string, std::list<Message>> messages_;
     111             :     std::map<std::string, std::list<Message>> messagesDevices_;
     112             : 
     113             :     mutable std::mutex messagesMutex_ {};
     114             : };
     115             : 
     116             : } // namespace im
     117             : } // namespace jami
     118             : 
     119         837 : MSGPACK_ADD_ENUM(jami::im::MessageStatus);

Generated by: LCOV version 1.14