server: Move stuff to rccpp_util.{h,cpp}

master
Perttu Ahola 2014-10-26 12:48:08 +02:00
parent eaaa55b1c7
commit ab1b49cf6b
4 changed files with 90 additions and 61 deletions

View File

@ -208,6 +208,7 @@ if(BUILD_SERVER)
src/server/state.cpp
src/server/rccpp.cpp
src/server/config.cpp
src/server/rccpp_util.cpp
)
add_executable(${SERVER_EXE_NAME} ${SERVER_SRCS})

72
src/server/rccpp_util.cpp Normal file
View File

@ -0,0 +1,72 @@
// http://www.apache.org/licenses/LICENSE-2.0
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
#include "rccpp_util.h"
#include "core/log.h"
#include "interface/sha1.h"
#include <c55/string_util.h>
#include <c55/filesys.h>
#include <fstream>
#define MODULE "rccpp_util"
namespace server {
sv_<ss_> list_includes(const ss_ &path, const sv_<ss_> &include_dirs)
{
ss_ base_dir = c55fs::stripFilename(path);
std::ifstream ifs(path);
sv_<ss_> result;
ss_ line;
while(std::getline(ifs, line)){
c55::Strfnd f(line);
f.next("#");
if(f.atend())
continue;
f.next("include");
f.while_any(" ");
ss_ quote = f.while_any("<\"");
ss_ include = f.next(quote == "<" ? ">" : "\"");
if(include == "")
continue;
bool found = false;
sv_<ss_> include_dirs_now = include_dirs;
if(quote == "\"")
include_dirs_now.insert(include_dirs_now.begin(), base_dir);
else
include_dirs_now.push_back(base_dir);
for(const ss_ &dir : include_dirs){
ss_ include_path = dir+"/"+include;
//log_v(MODULE, "Trying %s", cs(include_path));
std::ifstream ifs2(include_path);
if(ifs2.good()){
result.push_back(include_path);
found = true;
break;
}
}
if(!found){
// Not a huge problem, just log at debug
log_d(MODULE, "Include file not found for watching: %s", cs(include));
}
}
return result;
}
ss_ hash_files(const sv_<ss_> &paths)
{
std::ostringstream os(std::ios::binary);
for(const ss_ &path : paths){
std::ifstream f(path);
try {
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
os<<content;
} catch(std::ios_base::failure &e){
// Just ignore errors
log_w(MODULE, "hash_files: failed to read file %s: %s",
cs(path), e.what());
}
}
return interface::sha1::calculate(os.str());
}
}

11
src/server/rccpp_util.h Normal file
View File

@ -0,0 +1,11 @@
// http://www.apache.org/licenses/LICENSE-2.0
// Copyright 2014 Perttu Ahola <celeron55@gmail.com>
#pragma once
#include "core/types.h"
namespace server
{
sv_<ss_> list_includes(const ss_ &path, const sv_<ss_> &include_dirs);
ss_ hash_files(const sv_<ss_> &paths);
}
// vim: set noet ts=4 sw=4:

View File

@ -3,6 +3,7 @@
#include "state.h"
#include "core/log.h"
#include "rccpp.h"
#include "rccpp_util.h"
#include "config.h"
#include "urho3d_log_redirect.h"
#include "interface/module.h"
@ -15,8 +16,6 @@
#include "interface/sha1.h"
#include "interface/mutex.h"
#include "interface/thread_pool.h"
#include <c55/string_util.h>
#include <c55/filesys.h>
#include <Variant.h>
#include <Context.h>
#include <Engine.h>
@ -47,65 +46,6 @@ extern bool g_sigint_received;
namespace server {
static sv_<ss_> list_includes(const ss_ &path, const sv_<ss_> &include_dirs)
{
ss_ base_dir = c55fs::stripFilename(path);
std::ifstream ifs(path);
sv_<ss_> result;
ss_ line;
while(std::getline(ifs, line)){
c55::Strfnd f(line);
f.next("#");
if(f.atend())
continue;
f.next("include");
f.while_any(" ");
ss_ quote = f.while_any("<\"");
ss_ include = f.next(quote == "<" ? ">" : "\"");
if(include == "")
continue;
bool found = false;
sv_<ss_> include_dirs_now = include_dirs;
if(quote == "\"")
include_dirs_now.insert(include_dirs_now.begin(), base_dir);
else
include_dirs_now.push_back(base_dir);
for(const ss_ &dir : include_dirs){
ss_ include_path = dir+"/"+include;
//log_v(MODULE, "Trying %s", cs(include_path));
std::ifstream ifs2(include_path);
if(ifs2.good()){
result.push_back(include_path);
found = true;
break;
}
}
if(!found){
// Not a huge problem, just log at debug
log_d(MODULE, "Include file not found for watching: %s", cs(include));
}
}
return result;
}
static ss_ hash_files(const sv_<ss_> &paths)
{
std::ostringstream os(std::ios::binary);
for(const ss_ &path : paths){
std::ifstream f(path);
try {
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
os<<content;
} catch(std::ios_base::failure &e){
// Just ignore errors
log_w(MODULE, "hash_files: failed to read file %s: %s",
cs(path), e.what());
}
}
return interface::sha1::calculate(os.str());
}
class BuildatResourceRouter: public magic::ResourceRouter
{
OBJECT(BuildatResourceRouter);
@ -781,6 +721,11 @@ struct CState: public State, public interface::Server
log_t("state", "emit_event(): %s (%zu)",
cs(evreg->name(event.type)), event.type);
}
// TODO: Run modules in threads and have a separate event queue in each
// of them, and copy the event to the queues in here according to
// subscriptions
interface::MutexScope ms(m_event_queue_mutex);
m_event_queue.push_back(std::move(event));
}