Line data Source code
1 : /* 2 : * Copyright (C) 2004-2025 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, MessageToken t, bool success, const std::string& deviceId = {}); 60 : 61 : /** 62 : * @TODO change MessageEngine by a queue, 63 : * @NOTE retryOnTimeout is used for failing SIP messages (jamiAccount::sendTextMessage) 64 : */ 65 : void onPeerOnline(const std::string& peer, const std::string& deviceId = {}, bool retryOnTimeout = true); 66 : 67 : /** 68 : * Load persisted messages 69 : */ 70 : void load(); 71 : 72 : /** 73 : * Persist messages 74 : */ 75 : void save() const; 76 : 77 : private: 78 : static const constexpr unsigned MAX_RETRIES = 20; 79 : using clock = std::chrono::system_clock; 80 : 81 : void retrySend(const std::string& peer, const std::string& deviceId, bool retryOnTimeout); 82 : 83 : void save_() const; 84 : void scheduleSave(); 85 : 86 : struct Message 87 : { 88 : MessageToken token {}; 89 : std::string to {}; 90 : std::map<std::string, std::string> payloads {}; 91 : MessageStatus status {MessageStatus::IDLE}; 92 : unsigned retried {0}; 93 : clock::time_point last_op {}; 94 : 95 194 : MSGPACK_DEFINE_MAP(token, to, payloads, status, retried, last_op) 96 : }; 97 : 98 : SIPAccountBase& account_; 99 : const std::filesystem::path savePath_; 100 : std::shared_ptr<asio::io_context> ioContext_; 101 : asio::steady_timer saveTimer_; 102 : 103 : std::map<std::string, std::list<Message>> messages_; 104 : std::map<std::string, std::list<Message>> messagesDevices_; 105 : 106 : mutable std::mutex messagesMutex_ {}; 107 : }; 108 : 109 : } // namespace im 110 : } // namespace jami 111 : 112 194 : MSGPACK_ADD_ENUM(jami::im::MessageStatus);