build info, accept info - but sents grable
This commit is contained in:
parent
0459b43b64
commit
599b0a19cb
@ -34,6 +34,9 @@ add_library(sha1_ngcft1 STATIC
|
||||
./hash_utils.hpp
|
||||
./hash_utils.cpp
|
||||
|
||||
./ft1_sha1_info.hpp
|
||||
./ft1_sha1_info.cpp
|
||||
|
||||
./sha1_ngcft1.hpp
|
||||
./sha1_ngcft1.cpp
|
||||
)
|
||||
|
130
src/ft1_sha1_info.cpp
Normal file
130
src/ft1_sha1_info.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
#include "./ft1_sha1_info.hpp"
|
||||
|
||||
#include <sodium.h>
|
||||
|
||||
SHA1Digest::SHA1Digest(const std::vector<uint8_t>& v) {
|
||||
assert(v.size() == data.size());
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
data[i] = v[i];
|
||||
}
|
||||
}
|
||||
|
||||
SHA1Digest::SHA1Digest(const uint8_t* d, size_t s) {
|
||||
assert(s == data.size());
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
data[i] = d[i];
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const SHA1Digest& v) {
|
||||
std::string str{};
|
||||
str.resize(v.size()*2, '?');
|
||||
|
||||
// HECK, std is 1 larger than size returns ('\0')
|
||||
sodium_bin2hex(str.data(), str.size()+1, v.data.data(), v.data.size());
|
||||
|
||||
out << str;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> FT1InfoSHA1::toBuffer(void) const {
|
||||
std::vector<uint8_t> buffer;
|
||||
|
||||
assert(!file_name.empty());
|
||||
// TODO: optimize
|
||||
for (size_t i = 0; i < 256; i++) {
|
||||
if (i < file_name.size()) {
|
||||
buffer.push_back(file_name.at(i));
|
||||
} else {
|
||||
buffer.push_back(0);
|
||||
}
|
||||
}
|
||||
assert(buffer.size() == 256);
|
||||
|
||||
{ // HACK: endianess
|
||||
buffer.push_back((file_size>>(0*8)) & 0xff);
|
||||
buffer.push_back((file_size>>(1*8)) & 0xff);
|
||||
buffer.push_back((file_size>>(2*8)) & 0xff);
|
||||
buffer.push_back((file_size>>(3*8)) & 0xff);
|
||||
buffer.push_back((file_size>>(4*8)) & 0xff);
|
||||
buffer.push_back((file_size>>(5*8)) & 0xff);
|
||||
buffer.push_back((file_size>>(6*8)) & 0xff);
|
||||
buffer.push_back((file_size>>(7*8)) & 0xff);
|
||||
}
|
||||
assert(buffer.size() == 256+8);
|
||||
|
||||
// chunk size
|
||||
{ // HACK: endianess
|
||||
buffer.push_back((chunk_size>>(0*8)) & 0xff);
|
||||
buffer.push_back((chunk_size>>(1*8)) & 0xff);
|
||||
buffer.push_back((chunk_size>>(2*8)) & 0xff);
|
||||
buffer.push_back((chunk_size>>(3*8)) & 0xff);
|
||||
}
|
||||
|
||||
assert(buffer.size() == 256+8+4);
|
||||
|
||||
for (const auto& chunk : chunks) {
|
||||
for (size_t i = 0; i < chunk.data.size(); i++) {
|
||||
buffer.push_back(chunk.data[i]);
|
||||
}
|
||||
}
|
||||
assert(buffer.size() == 256+8+4+20*chunks.size());
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void FT1InfoSHA1::fromBuffer(const std::vector<uint8_t>& buffer) {
|
||||
assert(buffer.size() >= 256+8+4);
|
||||
|
||||
// TODO: optimize
|
||||
file_name.clear();
|
||||
for (size_t i = 0; i < 256; i++) {
|
||||
char next_char = static_cast<char>(buffer[i]);
|
||||
if (next_char == 0) {
|
||||
break;
|
||||
}
|
||||
file_name.push_back(next_char);
|
||||
}
|
||||
|
||||
{ // HACK: endianess
|
||||
file_size = 0;
|
||||
file_size |= uint64_t(buffer[256+0]) << (0*8);
|
||||
file_size |= uint64_t(buffer[256+1]) << (1*8);
|
||||
file_size |= uint64_t(buffer[256+2]) << (2*8);
|
||||
file_size |= uint64_t(buffer[256+3]) << (3*8);
|
||||
file_size |= uint64_t(buffer[256+4]) << (4*8);
|
||||
file_size |= uint64_t(buffer[256+5]) << (5*8);
|
||||
file_size |= uint64_t(buffer[256+6]) << (6*8);
|
||||
file_size |= uint64_t(buffer[256+7]) << (7*8);
|
||||
}
|
||||
|
||||
{ // HACK: endianess
|
||||
chunk_size = 0;
|
||||
chunk_size |= uint32_t(buffer[256+8+0]) << (0*8);
|
||||
chunk_size |= uint32_t(buffer[256+8+1]) << (1*8);
|
||||
chunk_size |= uint32_t(buffer[256+8+2]) << (2*8);
|
||||
chunk_size |= uint32_t(buffer[256+8+3]) << (3*8);
|
||||
}
|
||||
|
||||
assert((buffer.size()-(256+8+4)) % 20 == 0);
|
||||
|
||||
for (size_t offset = 256+8+4; offset < buffer.size();) {
|
||||
assert(buffer.size() >= offset + 20);
|
||||
|
||||
auto& chunk = chunks.emplace_back();
|
||||
for (size_t i = 0; i < chunk.size(); i++, offset++) {
|
||||
chunk.data[i] = buffer.at(offset);
|
||||
}
|
||||
// TODO: error/leftover checking
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const FT1InfoSHA1& v) {
|
||||
out << " file_name: " << v.file_name << "\n";
|
||||
out << " file_size: " << v.file_size << "\n";
|
||||
out << " chunk_size: " << v.chunk_size << "\n";
|
||||
out << " chunks.size(): " << v.chunks.size() << "\n";
|
||||
return out;
|
||||
}
|
||||
|
53
src/ft1_sha1_info.hpp
Normal file
53
src/ft1_sha1_info.hpp
Normal file
@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
|
||||
struct SHA1Digest {
|
||||
std::array<uint8_t, 20> data;
|
||||
|
||||
SHA1Digest(void) = default;
|
||||
SHA1Digest(const std::vector<uint8_t>& v);
|
||||
SHA1Digest(const uint8_t* d, size_t s);
|
||||
|
||||
bool operator==(const SHA1Digest& other) const { return data == other.data; }
|
||||
bool operator!=(const SHA1Digest& other) const { return data != other.data; }
|
||||
|
||||
size_t size(void) const { return data.size(); }
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const SHA1Digest& v);
|
||||
|
||||
namespace std { // inject
|
||||
template<> struct hash<SHA1Digest> {
|
||||
std::size_t operator()(const SHA1Digest& h) const noexcept {
|
||||
return
|
||||
size_t(h.data[0]) << (0*8) |
|
||||
size_t(h.data[1]) << (1*8) |
|
||||
size_t(h.data[2]) << (2*8) |
|
||||
size_t(h.data[3]) << (3*8) |
|
||||
size_t(h.data[4]) << (4*8) |
|
||||
size_t(h.data[5]) << (5*8) |
|
||||
size_t(h.data[6]) << (6*8) |
|
||||
size_t(h.data[7]) << (7*8)
|
||||
;
|
||||
}
|
||||
};
|
||||
} // std
|
||||
|
||||
struct FT1InfoSHA1 {
|
||||
std::string file_name;
|
||||
uint64_t file_size {0};
|
||||
uint32_t chunk_size {128*1024}; // 128KiB for now
|
||||
std::vector<SHA1Digest> chunks;
|
||||
|
||||
std::vector<uint8_t> toBuffer(void) const;
|
||||
void fromBuffer(const std::vector<uint8_t>& buffer);
|
||||
};
|
||||
std::ostream& operator<<(std::ostream& out, const FT1InfoSHA1& v);
|
||||
|
@ -550,7 +550,7 @@ bool NGCFT1::onEvent(const Events::NGCEXT_ft1_data_ack& e) {
|
||||
|
||||
// delete if all packets acked
|
||||
if (transfer.file_size == transfer.file_size_current && transfer.ssb.size() == 0) {
|
||||
std::cout << "NGCFT1: " << e.transfer_id << " done\n";
|
||||
std::cout << "NGCFT1: " << int(e.transfer_id) << " done\n";
|
||||
peer.send_transfers[e.transfer_id].reset();
|
||||
}
|
||||
|
||||
|
@ -7,9 +7,6 @@
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
// fwd
|
||||
//class RegMessageModel;
|
||||
|
||||
#define RESOLVE_INSTANCE(x) static_cast<x*>(solana_api->resolveInstance(#x))
|
||||
#define PROVIDE_INSTANCE(x, p, v) solana_api->provideInstance(#x, p, static_cast<x*>(v))
|
||||
|
||||
@ -37,12 +34,14 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api)
|
||||
|
||||
ToxI* tox_i = nullptr;
|
||||
ToxEventProviderI* tox_event_provider_i = nullptr;
|
||||
Contact3Registry* cr = nullptr;
|
||||
RegistryMessageModel* rmm = nullptr;
|
||||
ToxContactModel2* tcm = nullptr;
|
||||
|
||||
{ // make sure required types are loaded
|
||||
tox_i = RESOLVE_INSTANCE(ToxI);
|
||||
tox_event_provider_i = RESOLVE_INSTANCE(ToxEventProviderI);
|
||||
cr = RESOLVE_INSTANCE(Contact3Registry);
|
||||
rmm = RESOLVE_INSTANCE(RegistryMessageModel);
|
||||
tcm = RESOLVE_INSTANCE(ToxContactModel2);
|
||||
|
||||
@ -56,6 +55,11 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api)
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (cr == nullptr) {
|
||||
std::cerr << "PLUGIN NGCEXT missing Contact3Registry\n";
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (rmm == nullptr) {
|
||||
std::cerr << "PLUGIN NGCEXT missing RegistryMessageModel\n";
|
||||
return 2;
|
||||
@ -71,7 +75,7 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api)
|
||||
// construct with fetched dependencies
|
||||
g_ngcextep = std::make_unique<NGCEXTEventProvider>(*tox_event_provider_i);
|
||||
g_ngcft1 = std::make_unique<NGCFT1>(*tox_i, *tox_event_provider_i, *g_ngcextep.get());
|
||||
g_sha1_ngcft1 = std::make_unique<SHA1_NGCFT1>(*rmm, *g_ngcft1.get(), *tcm);
|
||||
g_sha1_ngcft1 = std::make_unique<SHA1_NGCFT1>(*cr, *rmm, *g_ngcft1.get(), *tcm);
|
||||
|
||||
// register types
|
||||
PROVIDE_INSTANCE(NGCEXTEventProviderI, "NGCEXT", g_ngcextep.get());
|
||||
|
@ -2,21 +2,52 @@
|
||||
|
||||
#include <solanaceae/toxcore/utils.hpp>
|
||||
|
||||
#include <solanaceae/contact/components.hpp>
|
||||
#include <solanaceae/tox_contacts/components.hpp>
|
||||
#include <solanaceae/message3/components.hpp>
|
||||
|
||||
#include <solanaceae/message3/file_r_file.hpp>
|
||||
|
||||
#include "./ft1_sha1_info.hpp"
|
||||
#include "./hash_utils.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace Components {
|
||||
|
||||
using FT1InfoSHA1 = FT1InfoSHA1;
|
||||
|
||||
struct FT1InfoSHA1Data {
|
||||
std::vector<uint8_t> data;
|
||||
};
|
||||
|
||||
struct FT1InfoSHA1Hash {
|
||||
std::vector<uint8_t> hash;
|
||||
};
|
||||
|
||||
} // Components
|
||||
|
||||
SHA1_NGCFT1::SHA1_NGCFT1(
|
||||
Contact3Registry& cr,
|
||||
RegistryMessageModel& rmm,
|
||||
NGCFT1EventProviderI& nftep,
|
||||
NGCFT1& nft,
|
||||
ToxContactModel2& tcm
|
||||
) :
|
||||
_cr(cr),
|
||||
_rmm(rmm),
|
||||
_nftep(nftep),
|
||||
_nft(nft),
|
||||
_tcm(tcm)
|
||||
{
|
||||
_nftep.subscribe(this, NGCFT1_Event::recv_request);
|
||||
_nftep.subscribe(this, NGCFT1_Event::recv_init);
|
||||
_nftep.subscribe(this, NGCFT1_Event::recv_data);
|
||||
_nftep.subscribe(this, NGCFT1_Event::send_data);
|
||||
_nft.subscribe(this, NGCFT1_Event::recv_request);
|
||||
_nft.subscribe(this, NGCFT1_Event::recv_init);
|
||||
_nft.subscribe(this, NGCFT1_Event::recv_data);
|
||||
_nft.subscribe(this, NGCFT1_Event::send_data);
|
||||
|
||||
//_rmm.subscribe(this, RegistryMessageModel_Event::message_construct);
|
||||
//_rmm.subscribe(this, RegistryMessageModel_Event::message_updated);
|
||||
//_rmm.subscribe(this, RegistryMessageModel_Event::message_destroy);
|
||||
|
||||
_rmm.subscribe(this, RegistryMessageModel_Event::send_file_path);
|
||||
}
|
||||
|
||||
bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) {
|
||||
@ -27,6 +58,32 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_recv_request& e) {
|
||||
|
||||
//std::cout << "SHA1_NGCFT1: FT1_REQUEST fk:" << int(e.file_kind) << " [" << bin2hex({e.file_id, e.file_id+e.file_id_size}) << "]\n";
|
||||
|
||||
if (e.file_kind == NGCFT1_file_kind::HASH_SHA1_INFO) {
|
||||
if (e.file_id_size != 20) {
|
||||
// error
|
||||
return false;
|
||||
}
|
||||
|
||||
SHA1Digest info_hash{e.file_id, e.file_id_size};
|
||||
if (!_info_to_message.count(info_hash)) {
|
||||
// we dont know about this
|
||||
return false;
|
||||
}
|
||||
|
||||
auto msg = _info_to_message.at(info_hash);
|
||||
|
||||
assert(msg.all_of<Components::FT1InfoSHA1Data>());
|
||||
|
||||
// assume we have the info, send init
|
||||
_nft.NGC_FT1_send_init_private(
|
||||
e.group_number, e.peer_number,
|
||||
static_cast<uint32_t>(e.file_kind),
|
||||
e.file_id, e.file_id_size,
|
||||
msg.get<Components::FT1InfoSHA1Data>().data.size(),
|
||||
nullptr
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -47,3 +104,113 @@ bool SHA1_NGCFT1::onEvent(const Events::NGCFT1_send_data& e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SHA1_NGCFT1::sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) {
|
||||
if (
|
||||
// TODO: add support of offline queuing
|
||||
!_cr.all_of<Contact::Components::ToxGroupEphemeral>(c)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "SHA1_NGCFT1: got sendFilePath()\n";
|
||||
|
||||
auto* reg_ptr = _rmm.get(c);
|
||||
if (reg_ptr == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: rw
|
||||
auto file_impl = std::make_unique<FileRFile>(file_path);
|
||||
if (!file_impl->isGood()) {
|
||||
std::cerr << "SHA1_NGCFT1 error: failed opening file '" << file_path << "'!\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
// get current time unix epoch utc
|
||||
uint64_t ts = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
|
||||
// 1. build info by hashing all chunks
|
||||
|
||||
FT1InfoSHA1 sha1_info;
|
||||
// build info
|
||||
sha1_info.file_name = file_name;
|
||||
sha1_info.file_size = file_impl->_file_size;
|
||||
|
||||
{ // build chunks
|
||||
// HACK: load file fully
|
||||
// TODO: the speed is truly horrid
|
||||
const auto file_data = file_impl->read(0, file_impl->_file_size);
|
||||
size_t i = 0;
|
||||
for (; i + sha1_info.chunk_size < file_data.size(); i += sha1_info.chunk_size) {
|
||||
sha1_info.chunks.push_back(hash_sha1(file_data.data()+i, sha1_info.chunk_size));
|
||||
}
|
||||
|
||||
if (i < file_data.size()) {
|
||||
sha1_info.chunks.push_back(hash_sha1(file_data.data()+i, file_data.size()-i));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. hash info
|
||||
std::vector<uint8_t> sha1_info_data;
|
||||
std::vector<uint8_t> sha1_info_hash;
|
||||
|
||||
std::cout << "SHA1_NGCFT1 info is: \n" << sha1_info;
|
||||
sha1_info_data = sha1_info.toBuffer();
|
||||
std::cout << "SHA1_NGCFT1 sha1_info size: " << sha1_info_data.size() << "\n";
|
||||
sha1_info_hash = hash_sha1(sha1_info_data.data(), sha1_info_data.size());
|
||||
std::cout << "SHA1_NGCFT1 sha1_info_hash: " << bin2hex(sha1_info_hash) << "\n";
|
||||
|
||||
const auto c_self = _cr.get<Contact::Components::Self>(c).self;
|
||||
if (!_cr.valid(c_self)) {
|
||||
std::cerr << "SHA1_NGCFT1 error: failed to get self!\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
const auto e = reg_ptr->create();
|
||||
reg_ptr->emplace<Message::Components::ContactTo>(e, c);
|
||||
reg_ptr->emplace<Message::Components::ContactFrom>(e, c_self);
|
||||
reg_ptr->emplace<Message::Components::Timestamp>(e, ts); // reactive?
|
||||
|
||||
reg_ptr->emplace<Message::Components::Transfer::TagHaveAll>(e);
|
||||
reg_ptr->emplace<Message::Components::Transfer::TagSending>(e);
|
||||
|
||||
reg_ptr->emplace<Components::FT1InfoSHA1>(e, sha1_info);
|
||||
reg_ptr->emplace<Components::FT1InfoSHA1Data>(e, sha1_info_data); // keep around? or file?
|
||||
reg_ptr->emplace<Components::FT1InfoSHA1Hash>(e, sha1_info_hash);
|
||||
_info_to_message[sha1_info_hash] = {*reg_ptr, e};
|
||||
|
||||
//reg_ptr->emplace<Message::Components::Transfer::FileKind>(e, file_kind);
|
||||
// file id would be sha1_info hash or something
|
||||
//reg_ptr->emplace<Message::Components::Transfer::FileID>(e, file_id);
|
||||
|
||||
{ // file info
|
||||
auto& file_info = reg_ptr->emplace<Message::Components::Transfer::FileInfo>(e);
|
||||
file_info.file_list.emplace_back() = {std::string{file_name}, file_impl->_file_size};
|
||||
file_info.total_size = file_impl->_file_size;
|
||||
|
||||
reg_ptr->emplace<Message::Components::Transfer::FileInfoLocal>(e, std::vector{std::string{file_path}});
|
||||
}
|
||||
|
||||
reg_ptr->emplace<Message::Components::Transfer::BytesSent>(e);
|
||||
|
||||
// TODO: determine if this is true
|
||||
//reg_ptr->emplace<Message::Components::Transfer::TagPaused>(e);
|
||||
|
||||
// TODO: ft1 specific comp
|
||||
reg_ptr->emplace<Message::Components::Transfer::File>(e, std::move(file_impl));
|
||||
#if 0
|
||||
const auto friend_number = _cr.get<Contact::Components::ToxFriendEphemeral>(c).friend_number;
|
||||
const auto&& [transfer_id, err] = _t.toxFileSend(friend_number, file_kind, file_impl->_file_size, file_id, file_name);
|
||||
if (err == TOX_ERR_FILE_SEND_OK) {
|
||||
reg_ptr->emplace<Message::Components::Transfer::ToxTransferFriend>(e, friend_number, transfer_id.value());
|
||||
// TODO: add tag signifying init sent status?
|
||||
|
||||
toxFriendLookupAdd({*reg_ptr, e});
|
||||
} // else queue?
|
||||
#endif
|
||||
|
||||
_rmm.throwEventConstruct(*reg_ptr, e);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -2,29 +2,39 @@
|
||||
|
||||
// solanaceae port of sha1 fts for NGCFT1
|
||||
|
||||
#include <solanaceae/contact/contact_model3.hpp>
|
||||
#include <solanaceae/message3/registry_message_model.hpp>
|
||||
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
|
||||
|
||||
#include "./ngcft1.hpp"
|
||||
|
||||
#include "./ft1_sha1_info.hpp"
|
||||
|
||||
class SHA1_NGCFT1 : public RegistryMessageModelEventI, public NGCFT1EventI {
|
||||
Contact3Registry& _cr;
|
||||
RegistryMessageModel& _rmm;
|
||||
NGCFT1EventProviderI& _nftep;
|
||||
NGCFT1& _nft;
|
||||
ToxContactModel2& _tcm;
|
||||
|
||||
// limit this to each group?
|
||||
entt::dense_map<SHA1Digest, Message3Handle> _info_to_message;
|
||||
|
||||
public:
|
||||
SHA1_NGCFT1(
|
||||
Contact3Registry& cr,
|
||||
RegistryMessageModel& rmm,
|
||||
NGCFT1EventProviderI& nftep,
|
||||
NGCFT1& nft,
|
||||
ToxContactModel2& tcm
|
||||
);
|
||||
|
||||
//void iterate(float delta);
|
||||
|
||||
protected:
|
||||
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 sendFilePath(const Contact3 c, std::string_view file_name, std::string_view file_path) override;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user