Ring Daemon
Loading...
Searching...
No Matches
message_engine.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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 <chrono>
22#include <mutex>
23#include <cstdint>
24#include <filesystem>
25
26#include <msgpack.hpp>
27#include <asio/steady_timer.hpp>
28
29namespace jami {
30
31class SIPAccountBase;
32
33namespace im {
34
36
37enum class MessageStatus : std::int8_t { UNKNOWN = 0, IDLE, SENDING, SENT, FAILURE };
38
40{
41public:
42 MessageEngine(SIPAccountBase&, const std::filesystem::path& path);
43
51 MessageToken sendMessage(const std::string& to,
52 const std::string& deviceId,
53 const std::map<std::string, std::string>& payloads,
55
57
58 void onMessageSent(const std::string& peer, MessageToken t, bool success, const std::string& deviceId = {});
59
64 void onPeerOnline(const std::string& peer, const std::string& deviceId = {}, bool retryOnTimeout = true);
65
69 void load();
70
74 void save() const;
75
76private:
77 static const constexpr unsigned MAX_RETRIES = 20;
78 using clock = std::chrono::system_clock;
79
80 void retrySend(const std::string& peer, const std::string& deviceId, bool retryOnTimeout);
81
82 void save_() const;
83 void scheduleSave();
84
85 struct Message
86 {
87 MessageToken token {};
88 std::string to {};
89 std::map<std::string, std::string> payloads {};
91 unsigned retried {0};
92 clock::time_point last_op {};
93
94 MSGPACK_DEFINE_MAP(token, to, payloads, status, retried, last_op)
95 };
96
97 SIPAccountBase& account_;
98 const std::filesystem::path savePath_;
99 std::shared_ptr<asio::io_context> ioContext_;
100 asio::steady_timer saveTimer_;
101
102 std::map<std::string, std::list<Message>> messages_;
103 std::map<std::string, std::list<Message>> messagesDevices_;
104
105 mutable std::mutex messagesMutex_ {};
106};
107
108} // namespace im
109} // namespace jami
110
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 jami_signal.h:64