2023-08-19 22:37:55 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// solanaceae port of sha1 fts for NGCFT1
|
|
|
|
|
2024-04-29 11:55:11 +02:00
|
|
|
#include <solanaceae/object_store/object_store.hpp>
|
2023-08-19 22:37:55 +02:00
|
|
|
#include <solanaceae/contact/contact_model3.hpp>
|
|
|
|
#include <solanaceae/message3/registry_message_model.hpp>
|
|
|
|
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
|
2024-07-10 12:27:19 +02:00
|
|
|
#include <solanaceae/util/bitset.hpp>
|
2023-08-19 22:37:55 +02:00
|
|
|
|
2023-08-19 22:50:52 +02:00
|
|
|
#include <solanaceae/ngc_ft1/ngcft1.hpp>
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
#include "./ft1_sha1_info.hpp"
|
2024-07-13 13:52:43 +02:00
|
|
|
#include "./sending_transfers.hpp"
|
2024-06-30 14:03:06 +02:00
|
|
|
#include "./receiving_transfers.hpp"
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
#include <entt/entity/registry.hpp>
|
|
|
|
#include <entt/entity/handle.hpp>
|
|
|
|
#include <entt/container/dense_map.hpp>
|
|
|
|
|
|
|
|
#include <variant>
|
|
|
|
#include <random>
|
2023-08-21 00:01:14 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <list>
|
2023-08-19 22:37:55 +02:00
|
|
|
|
2024-06-22 17:01:52 +02:00
|
|
|
class SHA1_NGCFT1 : public ToxEventI, public RegistryMessageModelEventI, public NGCFT1EventI, public NGCEXTEventI {
|
2024-04-29 11:55:11 +02:00
|
|
|
ObjectStore2& _os;
|
|
|
|
// TODO: backend abstraction
|
2023-08-19 22:37:55 +02:00
|
|
|
Contact3Registry& _cr;
|
|
|
|
RegistryMessageModel& _rmm;
|
|
|
|
NGCFT1& _nft;
|
|
|
|
ToxContactModel2& _tcm;
|
2024-06-22 14:08:12 +02:00
|
|
|
ToxEventProviderI& _tep;
|
2024-06-23 10:17:48 +02:00
|
|
|
NGCEXTEventProvider& _neep;
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
std::minstd_rand _rng {1337*11};
|
|
|
|
|
2024-07-13 11:46:33 +02:00
|
|
|
using clock = std::chrono::steady_clock;
|
|
|
|
clock::time_point _time_start_offset {clock::now()};
|
|
|
|
float getTimeNow(void) const {
|
|
|
|
return std::chrono::duration<float>{clock::now() - _time_start_offset}.count();
|
|
|
|
}
|
|
|
|
|
2023-08-19 22:37:55 +02:00
|
|
|
// limit this to each group?
|
2024-04-29 11:55:11 +02:00
|
|
|
entt::dense_map<SHA1Digest, ObjectHandle> _info_to_content;
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
// sha1 chunk index
|
|
|
|
// TODO: optimize lookup
|
|
|
|
// TODO: multiple contents. hashes might be unique, but data is not
|
2024-04-29 11:55:11 +02:00
|
|
|
entt::dense_map<SHA1Digest, ObjectHandle> _chunks;
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
// group_number, peer_number, content, chunk_hash, timer
|
2024-04-29 11:55:11 +02:00
|
|
|
std::deque<std::tuple<uint32_t, uint32_t, ObjectHandle, SHA1Digest, float>> _queue_requested_chunk;
|
2023-08-19 22:37:55 +02:00
|
|
|
//void queueUpRequestInfo(uint32_t group_number, uint32_t peer_number, const SHA1Digest& hash);
|
2024-04-29 11:55:11 +02:00
|
|
|
void queueUpRequestChunk(uint32_t group_number, uint32_t peer_number, ObjectHandle content, const SHA1Digest& hash);
|
2023-08-19 22:37:55 +02:00
|
|
|
|
2024-07-13 13:52:43 +02:00
|
|
|
SendingTransfers _sending_transfers;
|
2024-06-30 14:03:06 +02:00
|
|
|
ReceivingTransfers _receiving_transfers;
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
// makes request rotate around open content
|
2024-04-29 11:55:11 +02:00
|
|
|
std::deque<ObjectHandle> _queue_content_want_info;
|
2023-08-19 22:37:55 +02:00
|
|
|
|
2024-07-10 12:27:19 +02:00
|
|
|
struct QBitsetEntry {
|
|
|
|
Contact3Handle c;
|
|
|
|
ObjectHandle o;
|
|
|
|
};
|
|
|
|
std::deque<QBitsetEntry> _queue_send_bitset;
|
|
|
|
|
2024-07-13 13:52:43 +02:00
|
|
|
// FIXME: workaround missing contact events
|
2024-07-09 11:00:59 +02:00
|
|
|
// only used to remove participation on peer exit
|
|
|
|
entt::dense_map<uint64_t, Contact3Handle> _tox_peer_to_contact;
|
|
|
|
|
2023-08-21 00:01:14 +02:00
|
|
|
std::atomic_bool _info_builder_dirty {false};
|
|
|
|
std::mutex _info_builder_queue_mutex;
|
|
|
|
using InfoBuilderEntry = std::function<void(void)>;
|
|
|
|
std::list<InfoBuilderEntry> _info_builder_queue;
|
|
|
|
|
2024-04-29 11:55:11 +02:00
|
|
|
void updateMessages(ObjectHandle ce);
|
2023-08-19 22:37:55 +02:00
|
|
|
|
2024-04-29 11:55:11 +02:00
|
|
|
std::optional<std::pair<uint32_t, uint32_t>> selectPeerForRequest(ObjectHandle ce);
|
2023-08-19 22:37:55 +02:00
|
|
|
|
2024-07-10 12:27:19 +02:00
|
|
|
void queueBitsetSendFull(Contact3Handle c, ObjectHandle o);
|
|
|
|
|
2023-08-19 22:37:55 +02:00
|
|
|
public: // TODO: config
|
|
|
|
bool _udp_only {false};
|
|
|
|
|
2024-07-09 11:00:59 +02:00
|
|
|
size_t _max_concurrent_in {4}; // info only
|
2024-07-12 15:04:49 +02:00
|
|
|
size_t _max_concurrent_out {4*10}; // HACK: allow "ideal" number for 10 peers
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
SHA1_NGCFT1(
|
2024-04-29 11:55:11 +02:00
|
|
|
ObjectStore2& os,
|
2023-08-19 22:37:55 +02:00
|
|
|
Contact3Registry& cr,
|
|
|
|
RegistryMessageModel& rmm,
|
|
|
|
NGCFT1& nft,
|
2024-06-22 14:08:12 +02:00
|
|
|
ToxContactModel2& tcm,
|
2024-06-22 17:01:52 +02:00
|
|
|
ToxEventProviderI& tep,
|
2024-06-23 10:17:48 +02:00
|
|
|
NGCEXTEventProvider& neep
|
2023-08-19 22:37:55 +02:00
|
|
|
);
|
|
|
|
|
2024-07-07 13:21:59 +02:00
|
|
|
float iterate(float delta);
|
2023-08-19 22:37:55 +02:00
|
|
|
|
|
|
|
protected: // rmm events (actions)
|
|
|
|
bool onEvent(const Message::Events::MessageUpdated&) override;
|
|
|
|
|
|
|
|
protected: // events
|
|
|
|
bool onEvent(const Events::NGCFT1_recv_request&) override;
|
|
|
|
bool onEvent(const Events::NGCFT1_recv_init&) override;
|
|
|
|
bool onEvent(const Events::NGCFT1_recv_data&) override;
|
|
|
|
bool onEvent(const Events::NGCFT1_send_data&) override; // const?
|
|
|
|
bool onEvent(const Events::NGCFT1_recv_done&) override;
|
|
|
|
bool onEvent(const Events::NGCFT1_send_done&) override;
|
|
|
|
bool onEvent(const Events::NGCFT1_recv_message&) override;
|
|
|
|
|
|
|
|
bool sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) override;
|
2024-06-22 14:08:12 +02:00
|
|
|
|
|
|
|
bool onToxEvent(const Tox_Event_Group_Peer_Exit* e) override;
|
2024-06-22 17:01:52 +02:00
|
|
|
|
2024-06-23 15:12:31 +02:00
|
|
|
bool onEvent(const Events::NGCEXT_ft1_have&) override;
|
|
|
|
bool onEvent(const Events::NGCEXT_ft1_bitset&) override;
|
2024-07-10 15:47:33 +02:00
|
|
|
bool onEvent(const Events::NGCEXT_ft1_have_all&) override;
|
2024-06-23 15:12:31 +02:00
|
|
|
|
2024-06-22 17:01:52 +02:00
|
|
|
bool onEvent(const Events::NGCEXT_pc1_announce&) override;
|
2023-08-19 22:37:55 +02:00
|
|
|
};
|
|
|
|
|