Skip to content

Commit

Permalink
Merge pull request #85 from DICL/refactor-context
Browse files Browse the repository at this point in the history
Made Context class a singleton
  • Loading branch information
vicentebolea committed Apr 12, 2016
2 parents e229e96 + 38d935b commit 513598f
Show file tree
Hide file tree
Showing 30 changed files with 136 additions and 140 deletions.
26 changes: 26 additions & 0 deletions src/common/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@

using namespace std;

Context& context = *Context::connect();

Context* Context::singleton = nullptr;

// Constructors {{{
Context* Context::connect (string title) {

if (singleton == nullptr) {
singleton = new Context(title);
singleton->init();
singleton->run();
}

return singleton;
}

Context* Context::connect () {

if (singleton == nullptr) {
singleton = new Context();
singleton->init();
singleton->run();
}

return singleton;
}

Context::Context(string s): settings(s), work(io) {
init();
}
Expand Down
29 changes: 18 additions & 11 deletions src/common/context.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@
#include <vector>
#include <thread>

struct Context {
boost::asio::io_service io;
std::unique_ptr<Logger, decltype(&Logger::disconnect)>
logger {nullptr, Logger::disconnect};
Settings settings;
int id;
class Context {
public:
boost::asio::io_service io;
std::unique_ptr<Logger, decltype(&Logger::disconnect)>
logger {nullptr, Logger::disconnect};
Settings settings;
int id;

void run ();
bool join ();
void run ();
bool join ();

Context(std::string);
Context();
~Context();
static Context* singleton;
static Context* connect(std::string);
static Context* connect();

protected:
Context(std::string);
Context();
~Context();

void init();
std::vector<std::unique_ptr<std::thread>> threads;
boost::asio::io_service::work work;
};

extern Context& context;
14 changes: 6 additions & 8 deletions src/fs/dfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ using namespace eclipse;

namespace eclipse{
void DFS::load_settings () {
Context con;
BLOCK_SIZE = con.settings.get<int>("filesystem.block");
NUM_SERVERS = con.settings.get<vector<string>>("network.nodes").size();
path = con.settings.get<string>("path.scratch");
replica = con.settings.get<int>("filesystem.replica");
port = con.settings.get<int> ("network.port_mapreduce");
nodes = con.settings.get<vector<string>>("network.nodes");
BLOCK_SIZE = context.settings.get<int>("filesystem.block");
NUM_SERVERS = context.settings.get<vector<string>>("network.nodes").size();
path = context.settings.get<string>("path.scratch");
replica = context.settings.get<int>("filesystem.replica");
port = context.settings.get<int> ("network.port_mapreduce");
nodes = context.settings.get<vector<string>>("network.nodes");
}

unique_ptr<tcp::socket> DFS::connect (uint32_t hash_value) {
Expand Down Expand Up @@ -52,7 +51,6 @@ namespace eclipse{

int DFS::put(int argc, char* argv[])
{
Context con;
if(argc < 3)
{
cout << "[INFO] dfs put file_1 file_2 ..." << endl;
Expand Down
Loading

0 comments on commit 513598f

Please sign in to comment.