Ring Daemon 16.0.0
Loading...
Searching...
No Matches
data_transfer.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
20#include "noncopyable.h"
21
22#include <dhtnet/multiplexed_socket.h>
23
24#include <fstream>
25#include <functional>
26#include <memory>
27#include <optional>
28#include <string>
29
30namespace jami {
31
33
34class Stream;
35
37{
39 std::shared_ptr<Stream> stream;
40};
41
43{
44 std::string fileId;
45 std::string interactionId;
46 std::string sha3sum;
47 std::string path;
48 std::size_t totalSize;
50};
51
52typedef std::function<void(const std::string&)> InternalCompletionCb;
53typedef std::function<void()> OnFinishedCb;
54
56{
57public:
58 FileInfo(const std::shared_ptr<dhtnet::ChannelSocket>& channel,
59 const std::string& fileId,
60 const std::string& interactionId,
62 virtual ~FileInfo() {}
63 virtual void process() = 0;
64 std::shared_ptr<dhtnet::ChannelSocket> channel() const { return channel_; }
66 virtual void cancel() = 0;
67 void onFinished(std::function<void(uint32_t)>&& cb) { finishedCb_ = std::move(cb); }
69
70protected:
71 std::atomic_bool isUserCancelled_ {false};
72 std::string fileId_ {};
73 std::string interactionId_ {};
75 std::shared_ptr<dhtnet::ChannelSocket> channel_ {};
76 std::function<void(uint32_t)> finishedCb_ {};
77};
78
79class IncomingFile : public FileInfo, public std::enable_shared_from_this<IncomingFile>
80{
81public:
82 IncomingFile(const std::shared_ptr<dhtnet::ChannelSocket>& channel,
84 const std::string& fileId,
85 const std::string& interactionId,
86 const std::string& sha3Sum);
88 void process() override;
89 void cancel() override;
90
91private:
92 std::mutex streamMtx_;
93 std::ofstream stream_;
94 std::string sha3Sum_ {};
95 std::filesystem::path path_;
96};
97
98class OutgoingFile : public FileInfo
99{
100public:
101 OutgoingFile(const std::shared_ptr<dhtnet::ChannelSocket>& channel,
102 const std::string& fileId,
103 const std::string& interactionId,
105 size_t start = 0,
106 size_t end = 0);
108 void process() override;
109 void cancel() override;
110
111private:
112 std::ifstream stream_;
113 size_t start_ {0};
114 size_t end_ {0};
115};
116
117class TransferManager : public std::enable_shared_from_this<TransferManager>
118{
119public:
120 TransferManager(const std::string& accountId, const std::string& accountUri, const std::string& to, const std::mt19937_64& rand);
122
132 void transferFile(const std::shared_ptr<dhtnet::ChannelSocket>& channel,
133 const std::string& fileId,
134 const std::string& interactionId,
135 const std::string& path,
136 size_t start = 0,
137 size_t end = 0,
138 OnFinishedCb onFinished = {});
139
144 bool cancel(const std::string& fileId);
145
154 bool info(const std::string& fileId,
155 std::string& path,
156 int64_t& total,
157 int64_t& progress) const noexcept;
158
167 void waitForTransfer(const std::string& fileId,
168 const std::string& interactionId,
169 const std::string& sha3sum,
170 const std::string& path,
171 std::size_t total);
172
179 void onIncomingFileTransfer(const std::string& fileId,
180 const std::shared_ptr<dhtnet::ChannelSocket>& channel,
181 size_t start);
182
187 std::filesystem::path path(const std::string& fileId) const;
188
193 std::vector<WaitingRequest> waitingRequests() const;
194 bool isWaiting(const std::string& fileId) const;
195 void onIncomingProfile(const std::shared_ptr<dhtnet::ChannelSocket>& channel, const std::string& sha3Sum = "");
196
201 std::filesystem::path profilePath(const std::string& contactId) const;
202
203private:
204 std::weak_ptr<TransferManager> weak()
205 {
206 return std::static_pointer_cast<TransferManager>(shared_from_this());
207 }
209 class Impl;
210 std::unique_ptr<Impl> pimpl_;
211};
212
213} // namespace jami
virtual void process()=0
void emit(libjami::DataTransferEventCode code)
void onFinished(std::function< void(uint32_t)> &&cb)
std::string interactionId_
std::shared_ptr< dhtnet::ChannelSocket > channel() const
libjami::DataTransferInfo info() const
libjami::DataTransferInfo info_
std::atomic_bool isUserCancelled_
virtual ~FileInfo()
std::function< void(uint32_t)> finishedCb_
std::shared_ptr< dhtnet::ChannelSocket > channel_
virtual void cancel()=0
std::string fileId_
void cancel() override
void process() override
void process() override
void cancel() override
void waitForTransfer(const std::string &fileId, const std::string &interactionId, const std::string &sha3sum, const std::string &path, std::size_t total)
Inform the transfer manager that a transfer is waited (and will be automatically accepted)
std::filesystem::path path(const std::string &fileId) const
Retrieve path of a file.
void onIncomingFileTransfer(const std::string &fileId, const std::shared_ptr< dhtnet::ChannelSocket > &channel, size_t start)
Handle incoming transfer.
void onIncomingProfile(const std::shared_ptr< dhtnet::ChannelSocket > &channel, const std::string &sha3Sum="")
std::vector< WaitingRequest > waitingRequests() const
Retrieve waiting files.
std::filesystem::path profilePath(const std::string &contactId) const
void transferFile(const std::shared_ptr< dhtnet::ChannelSocket > &channel, const std::string &fileId, const std::string &interactionId, const std::string &path, size_t start=0, size_t end=0, OnFinishedCb onFinished={})
Send a file to a channel.
bool info(const std::string &fileId, std::string &path, int64_t &total, int64_t &progress) const noexcept
Get current transfer info.
bool cancel(const std::string &fileId)
Refuse a transfer.
bool isWaiting(const std::string &fileId) const
std::function< void()> OnFinishedCb
void emitSignal(Args... args)
Definition ring_signal.h:64
libjami::DataTransferId generateUID(std::mt19937_64 &engine)
std::function< void(const std::string &)> InternalCompletionCb
enum LIBJAMI_PUBLIC DataTransferEventCode
uint64_t DataTransferId
Simple macro to hide class' copy constructor and assignment operator.
#define NON_COPYABLE(ClassName)
Definition noncopyable.h:30
std::shared_ptr< Stream > stream
libjami::DataTransferId id
std::string interactionId