2014-09-19 18:42:07 +03:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
|
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"
|
2014-09-19 16:46:17 +03:00
|
|
|
#include "interface/file_watch.h"
|
2014-09-24 12:52:24 +03:00
|
|
|
#include "interface/fs.h"
|
2014-09-19 15:54:23 +03:00
|
|
|
#include "client_file/api.h"
|
|
|
|
#include "network/api.h"
|
2014-09-19 17:34:00 +03:00
|
|
|
#include <cereal/archives/portable_binary.hpp>
|
2014-09-18 23:47:05 +03:00
|
|
|
#include <cereal/types/string.hpp>
|
2014-09-19 17:29:26 +03:00
|
|
|
#include <cereal/types/vector.hpp>
|
|
|
|
#include <cereal/types/tuple.hpp>
|
2014-09-18 23:47:05 +03:00
|
|
|
#include <fstream>
|
|
|
|
#include <streambuf>
|
|
|
|
|
|
|
|
using interface::Event;
|
|
|
|
|
|
|
|
struct FileInfo {
|
|
|
|
ss_ name;
|
|
|
|
ss_ content;
|
|
|
|
ss_ hash;
|
2014-10-17 13:49:55 +03:00
|
|
|
ss_ path; // Empty if not a physical file
|
2014-09-19 17:29:26 +03:00
|
|
|
FileInfo(const ss_ &name, const ss_ &content, const ss_ &hash, const ss_ &path):
|
|
|
|
name(name), content(content), hash(hash), path(path){}
|
2014-09-18 23:47:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace client_file {
|
|
|
|
|
|
|
|
struct Module: public interface::Module, public client_file::Interface
|
|
|
|
{
|
|
|
|
interface::Server *m_server;
|
|
|
|
sm_<ss_, sp_<FileInfo>> m_files;
|
2014-10-04 08:31:29 +03:00
|
|
|
sp_<interface::FileWatch> m_watch;
|
2014-09-18 23:47:05 +03:00
|
|
|
|
|
|
|
Module(interface::Server *server):
|
2014-09-19 16:46:17 +03:00
|
|
|
interface::Module("client_file"),
|
2014-09-18 23:47:05 +03:00
|
|
|
m_server(server),
|
2014-10-04 08:31:29 +03:00
|
|
|
m_watch(interface::createFileWatch())
|
2014-09-18 23:47:05 +03:00
|
|
|
{
|
|
|
|
log_v(MODULE, "client_file construct");
|
|
|
|
}
|
|
|
|
|
|
|
|
~Module()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "client_file destruct");
|
2014-09-19 16:46:17 +03:00
|
|
|
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"));
|
2014-09-19 17:00:15 +03:00
|
|
|
m_server->sub_event(this, Event::t("core:unload"));
|
|
|
|
m_server->sub_event(this, Event::t("core:continue"));
|
2014-10-05 14:21:06 +03:00
|
|
|
m_server->sub_event(this, Event::t("network:client_connected"));
|
2014-09-19 02:22:53 +03:00
|
|
|
m_server->sub_event(this,
|
|
|
|
Event::t("network:packet_received/core:request_file"));
|
2014-09-19 02:54:36 +03:00
|
|
|
m_server->sub_event(this,
|
|
|
|
Event::t("network:packet_received/core:all_files_transferred"));
|
2014-09-19 16:46:17 +03:00
|
|
|
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)
|
2014-09-19 17:00:15 +03:00
|
|
|
EVENT_VOIDN("core:unload", on_unload)
|
|
|
|
EVENT_VOIDN("core:continue", on_continue)
|
2014-10-08 00:48:30 +03:00
|
|
|
EVENT_TYPEN("network:client_connected", on_client_connected,
|
|
|
|
network::NewClient)
|
2014-09-19 02:22:53 +03:00
|
|
|
EVENT_TYPEN("network:packet_received/core:request_file", on_request_file,
|
|
|
|
network::Packet)
|
2014-09-19 02:54:36 +03:00
|
|
|
EVENT_TYPEN("network:packet_received/core:all_files_transferred",
|
|
|
|
on_all_files_transferred, network::Packet)
|
2014-09-19 16:46:17 +03:00
|
|
|
EVENT_TYPEN("watch_file:watch_fd_event", on_watch_fd_event,
|
|
|
|
interface::SocketEvent);
|
2014-09-18 23:47:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_start()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-19 17:00:15 +03:00
|
|
|
void on_unload()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "on_unload");
|
2014-09-19 17:29:26 +03:00
|
|
|
|
|
|
|
// name, content, path
|
|
|
|
sv_<std::tuple<ss_, ss_, ss_>> file_restore_info;
|
|
|
|
for(auto &pair : m_files){
|
|
|
|
const FileInfo &info = *pair.second.get();
|
|
|
|
if(info.path != ""){
|
|
|
|
file_restore_info.push_back(std::tuple<ss_, ss_, ss_>(
|
2014-10-08 00:48:30 +03:00
|
|
|
info.name, "", info.path));
|
2014-09-19 17:29:26 +03:00
|
|
|
} else {
|
|
|
|
file_restore_info.push_back(std::tuple<ss_, ss_, ss_>(
|
2014-10-08 00:48:30 +03:00
|
|
|
info.name, info.content, ""));
|
2014-09-19 17:29:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
{
|
2014-09-19 17:34:00 +03:00
|
|
|
cereal::PortableBinaryOutputArchive ar(os);
|
2014-09-19 17:29:26 +03:00
|
|
|
ar(file_restore_info);
|
|
|
|
}
|
|
|
|
m_server->tmp_store_data("client_file:restore_info", os.str());
|
2014-09-19 17:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_continue()
|
|
|
|
{
|
|
|
|
log_v(MODULE, "on_continue");
|
2014-09-19 17:29:26 +03:00
|
|
|
ss_ data = m_server->tmp_restore_data("client_file:restore_info");
|
|
|
|
// name, content, path
|
|
|
|
sv_<std::tuple<ss_, ss_, ss_>> file_restore_info;
|
|
|
|
std::istringstream is(data, std::ios::binary);
|
|
|
|
{
|
2014-09-19 17:34:00 +03:00
|
|
|
cereal::PortableBinaryInputArchive ar(is);
|
2014-09-19 17:29:26 +03:00
|
|
|
ar(file_restore_info);
|
|
|
|
}
|
|
|
|
for(auto &tuple : file_restore_info){
|
2014-10-08 00:48:30 +03:00
|
|
|
const ss_ &name = std::get<0>(tuple);
|
2014-09-19 17:29:26 +03:00
|
|
|
const ss_ &content = std::get<1>(tuple);
|
2014-10-08 00:48:30 +03:00
|
|
|
const ss_ &path = std::get<2>(tuple);
|
2014-09-19 17:29:26 +03:00
|
|
|
log_i(MODULE, "Restoring: %s", cs(name));
|
|
|
|
if(path != ""){
|
|
|
|
add_file_path(name, path);
|
|
|
|
} else {
|
|
|
|
add_file_content(name, content);
|
|
|
|
}
|
|
|
|
}
|
2014-09-19 17:00:15 +03:00
|
|
|
}
|
|
|
|
|
2014-10-05 14:21:06 +03:00
|
|
|
void on_client_connected(const network::NewClient &client_connected)
|
2014-09-18 23:47:05 +03:00
|
|
|
{
|
2014-10-08 00:48:30 +03:00
|
|
|
log_v(MODULE, "Sending file hashes to new client %zu",
|
|
|
|
client_connected.info.id);
|
2014-09-18 23:47:05 +03:00
|
|
|
|
|
|
|
// Tell file hashes to client
|
|
|
|
for(auto &pair : m_files){
|
|
|
|
const FileInfo &info = *pair.second.get();
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
{
|
2014-09-19 17:34:00 +03:00
|
|
|
cereal::PortableBinaryOutputArchive ar(os);
|
2014-09-18 23:47:05 +03:00
|
|
|
ar(info.name);
|
|
|
|
ar(info.hash);
|
|
|
|
}
|
2014-10-08 00:48:30 +03:00
|
|
|
network::access(m_server, [&](network::Interface *inetwork){
|
|
|
|
inetwork->send(client_connected.info.id, "core:announce_file",
|
|
|
|
os.str());
|
2014-09-18 23:47:05 +03:00
|
|
|
});
|
|
|
|
}
|
2014-10-08 00:48:30 +03:00
|
|
|
network::access(m_server, [&](network::Interface *inetwork){
|
2014-10-05 14:21:06 +03:00
|
|
|
inetwork->send(client_connected.info.id,
|
2014-09-19 02:54:36 +03:00
|
|
|
"core:tell_after_all_files_transferred", "");
|
|
|
|
});
|
2014-09-18 23:47:05 +03:00
|
|
|
}
|
|
|
|
|
2014-09-19 02:22:53 +03:00
|
|
|
void on_request_file(const network::Packet &packet)
|
|
|
|
{
|
|
|
|
ss_ file_name;
|
|
|
|
ss_ file_hash;
|
|
|
|
std::istringstream is(packet.data, std::ios::binary);
|
|
|
|
{
|
2014-09-19 17:34:00 +03:00
|
|
|
cereal::PortableBinaryInputArchive ar(is);
|
2014-09-19 02:22:53 +03:00
|
|
|
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);
|
|
|
|
{
|
2014-09-19 17:34:00 +03:00
|
|
|
cereal::PortableBinaryOutputArchive ar(os);
|
2014-09-19 02:22:53 +03:00
|
|
|
ar(info.name);
|
|
|
|
ar(info.hash);
|
|
|
|
ar(info.content);
|
|
|
|
}
|
2014-10-08 00:48:30 +03:00
|
|
|
network::access(m_server, [&](network::Interface *inetwork){
|
2014-09-19 02:22:53 +03:00
|
|
|
inetwork->send(packet.sender, "core:file_content", os.str());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-19 02:54:36 +03:00
|
|
|
void on_all_files_transferred(const network::Packet &packet)
|
|
|
|
{
|
|
|
|
m_server->emit_event(ss_()+"client_file:files_transmitted",
|
|
|
|
new FilesTransmitted(packet.sender));
|
|
|
|
}
|
|
|
|
|
2014-09-19 16:46:17 +03:00
|
|
|
void on_watch_fd_event(const interface::SocketEvent &event)
|
|
|
|
{
|
2014-09-24 10:58:46 +03:00
|
|
|
log_d(MODULE, "on_watch_fd_event()");
|
2014-09-19 16:46:17 +03:00
|
|
|
m_watch->report_fd(event.fd);
|
|
|
|
}
|
|
|
|
|
2014-09-18 23:47:05 +03:00
|
|
|
// Interface
|
|
|
|
|
2014-09-19 16:46:17 +03:00
|
|
|
void update_file_content(const ss_ &name, const ss_ &content)
|
2014-09-18 23:47:05 +03:00
|
|
|
{
|
|
|
|
ss_ hash = interface::sha1::calculate(content);
|
2014-09-19 16:46:17 +03:00
|
|
|
|
|
|
|
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){
|
2014-09-24 10:58:46 +03:00
|
|
|
log_d(MODULE, "File stayed the same: %s: %s", cs(name),
|
2014-09-19 16:46:17 +03:00
|
|
|
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)));
|
2014-09-19 17:29:26 +03:00
|
|
|
m_files[name] = sp_<FileInfo>(new FileInfo(name, content, hash, ""));
|
2014-09-19 16:46:17 +03:00
|
|
|
|
2014-09-24 10:58:46 +03:00
|
|
|
// Notify clients of modified file
|
|
|
|
std::ostringstream os(std::ios::binary);
|
|
|
|
{
|
|
|
|
cereal::PortableBinaryOutputArchive ar(os);
|
|
|
|
ar(name);
|
|
|
|
ar(hash);
|
|
|
|
}
|
2014-10-08 00:48:30 +03:00
|
|
|
network::access(m_server, [&](network::Interface *inetwork){
|
2014-09-24 10:58:46 +03:00
|
|
|
sv_<network::PeerInfo::Id> peers = inetwork->list_peers();
|
2014-10-08 00:48:30 +03:00
|
|
|
for(const network::PeerInfo::Id &peer: peers){
|
2014-09-24 10:58:46 +03:00
|
|
|
inetwork->send(peer, "core:announce_file", os.str());
|
|
|
|
}
|
|
|
|
});
|
2014-09-19 16:46:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-09-28 18:28:57 +03:00
|
|
|
std::ifstream f(path, std::ios::binary);
|
2014-09-24 10:58:46 +03:00
|
|
|
if(!f.good())
|
|
|
|
throw Exception("client_file::add_file_path(): Couldn't open \""+
|
2014-10-08 00:48:30 +03:00
|
|
|
name+"\" from \""+path+"\"");
|
2014-09-19 16:46:17 +03:00
|
|
|
std::string content((std::istreambuf_iterator<char>(f)),
|
2014-10-08 00:48:30 +03:00
|
|
|
std::istreambuf_iterator<char>());
|
2014-09-19 16:46:17 +03:00
|
|
|
ss_ hash = interface::sha1::calculate(content);
|
|
|
|
log_v(MODULE, "File added: %s: %s (%s)", cs(name),
|
|
|
|
cs(interface::sha1::hex(hash)), cs(path));
|
2014-09-19 17:29:26 +03:00
|
|
|
m_files[name] = sp_<FileInfo>(new FileInfo(name, content, hash, path));
|
2014-10-02 20:45:04 +03:00
|
|
|
|
|
|
|
// Tell path to server so that it can be used in network-synced Scene
|
|
|
|
m_server->add_file_path(name, path);
|
|
|
|
|
2014-09-24 12:52:24 +03:00
|
|
|
ss_ dir_path = interface::Filesystem::strip_file_name(path);
|
2014-10-08 00:48:30 +03:00
|
|
|
m_watch->add(dir_path, [this, name, path](const ss_ &path_){
|
2014-09-24 12:52:24 +03:00
|
|
|
if(path_ != path){
|
|
|
|
//log_d(MODULE, "Ignoring file watch callback: %s (we want %s)",
|
|
|
|
// cs(path_), cs(path));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log_d(MODULE, "File watch callback: %s (%s)", cs(name), cs(path_));
|
2014-09-28 18:28:57 +03:00
|
|
|
std::ifstream f(path, std::ios::binary);
|
2014-09-24 10:58:46 +03:00
|
|
|
if(!f.good()){
|
2014-09-24 12:52:24 +03:00
|
|
|
log_w(MODULE, "client_file: Couldn't open updated file "
|
2014-09-24 10:58:46 +03:00
|
|
|
"\"%s\" from \"%s\"", cs(name), cs(path));
|
|
|
|
return;
|
|
|
|
}
|
2014-09-19 16:46:17 +03:00
|
|
|
std::string content((std::istreambuf_iterator<char>(f)),
|
|
|
|
std::istreambuf_iterator<char>());
|
2014-09-24 12:52:24 +03:00
|
|
|
if(content.empty()){
|
2014-09-28 18:28:57 +03:00
|
|
|
log_w(MODULE, "client_file: Updated file is empty: "
|
2014-09-24 12:52:24 +03:00
|
|
|
"\"%s\" from \"%s\"", cs(name), cs(path));
|
|
|
|
return;
|
|
|
|
}
|
2014-09-19 16:46:17 +03:00
|
|
|
update_file_content(name, content);
|
|
|
|
});
|
2014-09-18 23:47:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void* get_interface()
|
|
|
|
{
|
|
|
|
return dynamic_cast<Interface*>(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
extern "C" {
|
2014-10-03 15:51:34 +03:00
|
|
|
BUILDAT_EXPORT void* createModule_client_file(interface::Server *server){
|
2014-09-18 23:47:05 +03:00
|
|
|
return (void*)(new Module(server));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-24 14:53:57 +03:00
|
|
|
// vim: set noet ts=4 sw=4:
|