buildat/builtin/client_file/client_file.cpp

223 lines
6.0 KiB
C++
Raw Normal View History

2014-09-18 23:47:05 +03:00
#include "core/log.h"
#include "interface/module.h"
#include "interface/server.h"
#include "interface/event.h"
#include "interface/sha1.h"
#include "interface/file_watch.h"
#include "client_file/api.h"
#include "network/api.h"
2014-09-18 23:47:05 +03:00
#include <cereal/archives/binary.hpp>
#include <cereal/types/string.hpp>
#include <fstream>
#include <streambuf>
using interface::Event;
struct FileInfo {
ss_ name;
ss_ content;
ss_ hash;
FileInfo(const ss_ &name, const ss_ &content, const ss_ &hash):
name(name), content(content), hash(hash){}
};
namespace client_file {
struct Module: public interface::Module, public client_file::Interface
{
interface::Server *m_server;
sm_<ss_, sp_<FileInfo>> m_files;
sp_<interface::MultiFileWatch> m_watch;
2014-09-18 23:47:05 +03:00
Module(interface::Server *server):
interface::Module("client_file"),
2014-09-18 23:47:05 +03:00
m_server(server),
m_watch(interface::createMultiFileWatch())
2014-09-18 23:47:05 +03:00
{
log_v(MODULE, "client_file construct");
}
~Module()
{
log_v(MODULE, "client_file destruct");
for(int fd : m_watch->get_fds())
m_server->remove_socket_event(fd);
2014-09-18 23:47:05 +03:00
}
void init()
{
log_v(MODULE, "client_file init");
m_server->sub_event(this, Event::t("core:start"));
m_server->sub_event(this, Event::t("core:unload"));
m_server->sub_event(this, Event::t("core:continue"));
2014-09-18 23:47:05 +03:00
m_server->sub_event(this, Event::t("network:new_client"));
m_server->sub_event(this,
Event::t("network:packet_received/core:request_file"));
m_server->sub_event(this,
Event::t("network:packet_received/core:all_files_transferred"));
m_server->sub_event(this, Event::t("watch_file:watch_fd_event"));
for(int fd : m_watch->get_fds())
m_server->add_socket_event(fd, Event::t("watch_file:watch_fd_event"));
2014-09-18 23:47:05 +03:00
}
void event(const Event::Type &type, const Event::Private *p)
{
EVENT_VOIDN("core:start", on_start)
EVENT_VOIDN("core:unload", on_unload)
EVENT_VOIDN("core:continue", on_continue)
2014-09-18 23:47:05 +03:00
EVENT_TYPEN("network:new_client", on_new_client, network::NewClient)
EVENT_TYPEN("network:packet_received/core:request_file", on_request_file,
network::Packet)
EVENT_TYPEN("network:packet_received/core:all_files_transferred",
on_all_files_transferred, network::Packet)
EVENT_TYPEN("watch_file:watch_fd_event", on_watch_fd_event,
interface::SocketEvent);
2014-09-18 23:47:05 +03:00
}
void on_start()
{
}
void on_unload()
{
log_v(MODULE, "on_unload");
}
void on_continue()
{
log_v(MODULE, "on_continue");
}
2014-09-18 23:47:05 +03:00
void on_new_client(const network::NewClient &new_client)
{
log_i(MODULE, "client_file::on_new_client: id=%zu", new_client.info.id);
// Tell file hashes to client
for(auto &pair : m_files){
const FileInfo &info = *pair.second.get();
std::ostringstream os(std::ios::binary);
{
cereal::BinaryOutputArchive ar(os);
ar(info.name);
ar(info.hash);
}
network::access(m_server, [&](network::Interface * inetwork){
inetwork->send(new_client.info.id, "core:announce_file", os.str());
});
}
network::access(m_server, [&](network::Interface * inetwork){
inetwork->send(new_client.info.id,
"core:tell_after_all_files_transferred", "");
});
2014-09-18 23:47:05 +03:00
}
void on_request_file(const network::Packet &packet)
{
ss_ file_name;
ss_ file_hash;
std::istringstream is(packet.data, std::ios::binary);
{
cereal::BinaryInputArchive ar(is);
ar(file_name);
ar(file_hash);
}
log_v(MODULE, "File request: %s %s", cs(file_name),
cs(interface::sha1::hex(file_hash)));
auto it = m_files.find(file_name);
if(it == m_files.end()){
log_w(MODULE, "Requested file does not exist: \"%s\"", cs(file_name));
return;
}
const FileInfo &info = *it->second.get();
if(info.hash != file_hash){
log_w(MODULE, "Requested file differs in hash: \"%s\": "
"requested %s, actual %s", cs(file_name),
cs(interface::sha1::hex(file_hash)),
cs(interface::sha1::hex(info.hash)));
return;
}
std::ostringstream os(std::ios::binary);
{
cereal::BinaryOutputArchive ar(os);
ar(info.name);
ar(info.hash);
ar(info.content);
}
network::access(m_server, [&](network::Interface * inetwork){
inetwork->send(packet.sender, "core:file_content", os.str());
});
}
void on_all_files_transferred(const network::Packet &packet)
{
m_server->emit_event(ss_()+"client_file:files_transmitted",
new FilesTransmitted(packet.sender));
}
void on_watch_fd_event(const interface::SocketEvent &event)
{
log_v(MODULE, "on_watch_fd_event()");
m_watch->report_fd(event.fd);
}
2014-09-18 23:47:05 +03:00
// Interface
void update_file_content(const ss_ &name, const ss_ &content)
2014-09-18 23:47:05 +03:00
{
ss_ hash = interface::sha1::calculate(content);
auto it = m_files.find(name);
if(it != m_files.end()){
// File already added; ignore if content wasn't modified
sp_<FileInfo> old_info = it->second;
if(old_info->hash == hash){
log_v(MODULE, "File stayed the same: %s: %s", cs(name),
cs(interface::sha1::hex(hash)));
return;
}
}
log_v(MODULE, "File updated: %s: %s", cs(name),
2014-09-18 23:47:05 +03:00
cs(interface::sha1::hex(hash)));
m_files[name] = sp_<FileInfo>(new FileInfo(name, content, hash));
// Note: Clients have to reconnect to see the new file content
}
void add_file_content(const ss_ &name, const ss_ &content)
{
update_file_content(name, content);
}
void add_file_path(const ss_ &name, const ss_ &path)
{
std::ifstream f(path);
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
ss_ hash = interface::sha1::calculate(content);
log_v(MODULE, "File added: %s: %s (%s)", cs(name),
cs(interface::sha1::hex(hash)), cs(path));
m_files[name] = sp_<FileInfo>(new FileInfo(name, content, hash));
m_watch->add(path, [this, name, path](const ss_ & path_){
log_v(MODULE, "File modified: %s (%s)", cs(name), cs(path));
std::ifstream f(path);
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
update_file_content(name, content);
});
2014-09-18 23:47:05 +03:00
}
void* get_interface()
{
return dynamic_cast<Interface*>(this);
}
};
extern "C" {
EXPORT void* createModule_client_file(interface::Server *server){
return (void*)(new Module(server));
}
}
}