Ring Daemon 16.0.0
Loading...
Searching...
No Matches
message_engine.h
Go to the documentation of this file.
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
30namespace jami {
31
32class SIPAccountBase;
33
34namespace im {
35
37
39
41{
42public:
43 MessageEngine(SIPAccountBase&, const std::filesystem::path& path);
44
52 MessageToken sendMessage(const std::string& to,
53 const std::string& deviceId,
54 const std::map<std::string, std::string>& payloads,
56
58
59 void onMessageSent(const std::string& peer,
61 bool success,
62 const std::string& deviceId = {});
63
68 void onPeerOnline(const std::string& peer,
69 const std::string& deviceId = {},
70 bool retryOnTimeout = true);
71
75 void load();
76
80 void save() const;
81
82private:
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 {};
99 unsigned retried {0};
100 clock::time_point last_op {};
101
102 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
void load()
Load persisted messages.
MessageToken sendMessage(const std::string &to, const std::string &deviceId, const std::map< std::string, std::string > &payloads, uint64_t refreshToken)
Add a message to the engine and try to send it.
void onPeerOnline(const std::string &peer, const std::string &deviceId={}, bool retryOnTimeout=true)
@TODO change MessageEngine by a queue, @NOTE retryOnTimeout is used for failing SIP messages (jamiAcc...
void onMessageSent(const std::string &peer, MessageToken t, bool success, const std::string &deviceId={})
void save() const
Persist messages.
MessageStatus getStatus(MessageToken t) const
MSGPACK_ADD_ENUM(jami::im::MessageStatus)
uint64_t MessageToken
void emitSignal(Args... args)
Definition ring_signal.h:64