Ring Daemon
Loading...
Searching...
No Matches
presence_manager.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
18#pragma once
19
20#include <opendht/dhtrunner.h>
21#include <opendht/infohash.h>
22
23#include <map>
24#include <set>
25#include <mutex>
26#include <string>
27#include <functional>
28#include <vector>
29#include <future>
30#include <atomic>
31
32namespace jami {
33
35{
36public:
37 using PresenceCallback = std::function<void(const std::string& uri, bool online)>;
38 using DevicePresenceCallback = std::function<void(const std::string& uri, const dht::PkId& deviceId, bool online)>;
39
40 PresenceManager(const std::shared_ptr<dht::DhtRunner>& dht);
42
48 void trackBuddy(const std::string& uri);
49
55 void untrackBuddy(const std::string& uri);
56
60 bool isOnline(const std::string& uri) const;
61
65 std::map<std::string, bool> getTrackedBuddyPresence() const;
66
70 std::vector<dht::PkId> getDevices(const std::string& uri) const;
71
78
83 void removeListener(uint64_t token);
84
91
96
101 void refresh();
102
103private:
104 struct TrackedBuddy
105 {
106 dht::InfoHash id;
107 std::set<dht::PkId> devices;
108 std::future<size_t> listenToken;
109 int refCount {0};
110
111 TrackedBuddy(dht::InfoHash h)
112 : id(h)
113 {}
114 };
115
116 void trackPresence(const dht::InfoHash& h, TrackedBuddy& buddy);
117 void notifyListeners(const std::string& uri, bool online);
118 void notifyDeviceListeners(const std::string& uri, const dht::PkId& deviceId, bool online);
119
120 std::shared_ptr<dht::DhtRunner> dht_;
121
122 mutable std::mutex mutex_;
123 std::map<dht::InfoHash, TrackedBuddy> trackedBuddies_;
124
125 std::mutex listenersMutex_;
126 std::map<uint64_t, PresenceCallback> listeners_;
127 std::map<uint64_t, DevicePresenceCallback> deviceListeners_;
128 std::atomic_uint64_t nextListenerId_ {1};
129};
130
131} // namespace jami
std::function< void(const std::string &uri, const dht::PkId &deviceId, bool online)> DevicePresenceCallback
uint64_t addListener(PresenceCallback cb)
Add a listener for presence changes.
void refresh()
Refresh all tracked buddies.
bool isOnline(const std::string &uri) const
Check if a buddy is currently online.
std::map< std::string, bool > getTrackedBuddyPresence() const
Get the presence status of all tracked buddies.
void removeDeviceListener(uint64_t token)
Remove a listener using the token returned by addDeviceListener.
uint64_t addDeviceListener(DevicePresenceCallback cb)
Add a listener for device presence changes.
std::function< void(const std::string &uri, bool online)> PresenceCallback
void trackBuddy(const std::string &uri)
Start tracking a buddy.
void removeListener(uint64_t token)
Remove a listener using the token returned by addListener.
void untrackBuddy(const std::string &uri)
Stop tracking a buddy.
std::vector< dht::PkId > getDevices(const std::string &uri) const
Get the list of devices for a tracked buddy.
Definition account.h:50
void emitSignal(Args... args)
Definition jami_signal.h:64