Skip to content

Commit 513598f

Browse files
committed
Merge pull request #85 from DICL/refactor-context
Made Context class a singleton
2 parents e229e96 + 38d935b commit 513598f

30 files changed

+136
-140
lines changed

src/common/context.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,33 @@
33

44
using namespace std;
55

6+
Context& context = *Context::connect();
7+
8+
Context* Context::singleton = nullptr;
9+
610
// Constructors {{{
11+
Context* Context::connect (string title) {
12+
13+
if (singleton == nullptr) {
14+
singleton = new Context(title);
15+
singleton->init();
16+
singleton->run();
17+
}
18+
19+
return singleton;
20+
}
21+
22+
Context* Context::connect () {
23+
24+
if (singleton == nullptr) {
25+
singleton = new Context();
26+
singleton->init();
27+
singleton->run();
28+
}
29+
30+
return singleton;
31+
}
32+
733
Context::Context(string s): settings(s), work(io) {
834
init();
935
}

src/common/context.hh

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,29 @@
77
#include <vector>
88
#include <thread>
99

10-
struct Context {
11-
boost::asio::io_service io;
12-
std::unique_ptr<Logger, decltype(&Logger::disconnect)>
13-
logger {nullptr, Logger::disconnect};
14-
Settings settings;
15-
int id;
10+
class Context {
11+
public:
12+
boost::asio::io_service io;
13+
std::unique_ptr<Logger, decltype(&Logger::disconnect)>
14+
logger {nullptr, Logger::disconnect};
15+
Settings settings;
16+
int id;
1617

17-
void run ();
18-
bool join ();
18+
void run ();
19+
bool join ();
1920

20-
Context(std::string);
21-
Context();
22-
~Context();
21+
static Context* singleton;
22+
static Context* connect(std::string);
23+
static Context* connect();
2324

2425
protected:
26+
Context(std::string);
27+
Context();
28+
~Context();
29+
2530
void init();
2631
std::vector<std::unique_ptr<std::thread>> threads;
2732
boost::asio::io_service::work work;
2833
};
34+
35+
extern Context& context;

src/fs/dfs.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ using namespace eclipse;
44

55
namespace eclipse{
66
void DFS::load_settings () {
7-
Context con;
8-
BLOCK_SIZE = con.settings.get<int>("filesystem.block");
9-
NUM_SERVERS = con.settings.get<vector<string>>("network.nodes").size();
10-
path = con.settings.get<string>("path.scratch");
11-
replica = con.settings.get<int>("filesystem.replica");
12-
port = con.settings.get<int> ("network.port_mapreduce");
13-
nodes = con.settings.get<vector<string>>("network.nodes");
7+
BLOCK_SIZE = context.settings.get<int>("filesystem.block");
8+
NUM_SERVERS = context.settings.get<vector<string>>("network.nodes").size();
9+
path = context.settings.get<string>("path.scratch");
10+
replica = context.settings.get<int>("filesystem.replica");
11+
port = context.settings.get<int> ("network.port_mapreduce");
12+
nodes = context.settings.get<vector<string>>("network.nodes");
1413
}
1514

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

5352
int DFS::put(int argc, char* argv[])
5453
{
55-
Context con;
5654
if(argc < 3)
5755
{
5856
cout << "[INFO] dfs put file_1 file_2 ..." << endl;

0 commit comments

Comments
 (0)