Commit 467e6bad authored by Stanislav Bohm's avatar Stanislav Bohm

RF: Arg parsing moved from worker into libloom

parent 1f14b3b7
......@@ -53,6 +53,8 @@ add_library(libloom
log.h
log.cpp
types.h
config.cpp
config.h
utils.h
utils.cpp)
......
#include "config.h"
loom::Config::Config()
: work_dir("/tmp"), cpus(0), debug(false)
{
}
void loom::Config::parse_args(int argc, char **argv)
{
struct argp_option options[] = {
{ "debug", 300, 0, 0, "Debug mode"},
{ "cpus", 301, "NUMBER", 0, "Number of cpus (default: autodetect)"},
{ "wdir", 302, "DIRECTORY", 0, "Working directory (default: /tmp)"},
{ 0 }
};
struct argp argp = { options, parse_opt, "SERVER-ADDRESS PORT" };
int r = argp_parse (&argp, argc, argv, 0, 0, this);
if (r) {
exit(r);
}
}
int
loom::Config::parse_opt (int key, char *arg,
struct argp_state *state)
{
Config *config = (Config*) state->input;
switch (key)
{
case 300:
config->debug = true;
break;
case 301:
config->cpus = atoi(arg);
if (config->cpus <= 0) {
fprintf(stderr, "Invalid number of cpus\n");
exit(1);
}
break;
case 302:
config->work_dir = arg;
break;
case ARGP_KEY_ARG:
switch(state->arg_num) {
case 0:
config->server_address = arg;
break;
case 1:
config->port = atoi(arg);
if (config->port <= 0 || config->port > 65535) {
fprintf(stderr, "Invalid port number\n");
exit(1);
}
break;
default:
argp_usage(state); /* no return */
}
break;
case ARGP_KEY_END:
if (state->arg_num < 2)
{
argp_usage (state);
/* no return */
}
break;
}
return 0;
}
#ifndef LIBLOOM_INIT_H
#define LIBLOOM_INIT_H
#include <string>
#include <argp.h>
namespace loom {
class Config {
public:
Config();
void parse_args(int argc, char **argv);
const std::string& get_server_address() const {
return server_address;
}
const std::string& get_work_dir() const {
return server_address;
}
int get_port() const {
return port;
}
int get_cpus() const {
return cpus;
}
bool get_debug() const {
return debug;
}
protected:
std::string server_address;
std::string work_dir;
int port;
int cpus;
bool debug;
private:
static int parse_opt(int key, char *arg, struct argp_state *state);
};
}
#endif
......@@ -3,6 +3,7 @@
#include "utils.h"
#include "log.h"
#include "types.h"
#include "config.h"
#include "data/rawdata.h"
#include "data/array.h"
......@@ -22,18 +23,25 @@ using namespace loom;
Worker::Worker(uv_loop_t *loop,
const std::string &server_address,
int server_port,
const std::string &work_dir_root)
const Config &config)
: loop(loop),
server_conn(*this),
server_port(server_port)
server_port(config.get_port())
{
spdlog::set_pattern("%H:%M:%S [%l] %v");
if (config.get_debug()) {
spdlog::set_level(spdlog::level::debug);
} else {
spdlog::set_level(spdlog::level::info);
}
loom::llog->info("New worker; listening on port {}", config.get_port());
GOOGLE_PROTOBUF_VERIFY_VERSION;
UV_CHECK(uv_tcp_init(loop, &listen_socket));
listen_socket.data = this;
start_listen();
auto &server_address = config.get_server_address();
if (!server_address.empty()) {
llog->info("Connecting to server {}:{}", server_address, server_port);
uv_getaddrinfo_t* handle = new uv_getaddrinfo_t;
......@@ -43,9 +51,11 @@ Worker::Worker(uv_loop_t *loop,
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
UV_CHECK(uv_getaddrinfo(
loop, handle, _on_getaddrinfo, server_address.c_str(), "80", &hints));
loop, handle, _on_getaddrinfo,
server_address.c_str(), "80", &hints));
}
auto &work_dir_root = config.get_work_dir();
if (!work_dir_root.empty()) {
std::stringstream s;
s << work_dir_root;
......@@ -76,8 +86,7 @@ Worker::Worker(uv_loop_t *loop,
add_unpacker<RawDataUnpacker>();
add_unpacker<ArrayUnpacker>();
add_unpacker<IndexUnpacker>();
resource_cpus = 1;
resource_cpus = config.get_cpus();
}
void Worker::register_basic_tasks()
......
......@@ -18,6 +18,7 @@ namespace loom {
class Worker;
class DataUnpacker;
class Config;
/** Class representing connection to the server */
class ServerConnection : public SimpleConnectionCallback {
......@@ -43,10 +44,7 @@ class Worker {
friend class ServerConnection;
public:
Worker(uv_loop_t* loop,
const std::string& server_address,
int server_port,
const std::string& work_dir_root);
Worker(uv_loop_t* loop, const Config &config);
uv_loop_t *get_loop() {
return loop;
......
#include "libloom/worker.h"
#include "libloom/log.h"
#include "libloom/config.h"
#include <memory>
#include <argp.h>
using namespace loom;
struct Config {
Config() : work_dir("/tmp"),
cpus(0),
debug(false) {
}
std::string server_address;
std::string work_dir;
int port;
int cpus;
bool debug;
};
static int
parse_opt (int key, char *arg,
struct argp_state *state)
{
Config *config = (Config*) state->input;
switch (key)
{
case 300:
config->debug = true;
break;
case 301:
config->cpus = atoi(arg);
if (config->cpus <= 0) {
fprintf(stderr, "Invalid number of cpus\n");
exit(1);
}
break;
case 302:
config->work_dir = arg;
break;
case ARGP_KEY_ARG:
switch(state->arg_num) {
case 0:
config->server_address = arg;
break;
case 1:
config->port = atoi(arg);
if (config->port <= 0 || config->port > 65535) {
fprintf(stderr, "Invalid port number\n");
exit(1);
}
break;
default:
argp_usage(state); /* no return */
}
break;
case ARGP_KEY_END:
if (state->arg_num < 2)
{
argp_usage (state);
/* no return */
}
break;
}
return 0;
}
static int parse_args(int argc, char **argv, Config *config)
{
struct argp_option options[] = {
{ "debug", 300, 0, 0, "Debug mode"},
{ "cpus", 301, "NUMBER", 0, "Number of cpus (default: autodetect)"},
{ "wdir", 302, "DIRECTORY", 0, "Working directory (default: /tmp)"},
{ 0 }
};
struct argp argp = { options, parse_opt, "SERVER-ADDRESS PORT" };
return argp_parse (&argp, argc, argv, 0, 0, config);
}
using namespace loom;
int main(int argc, char **argv)
{
/* Create a configuration and parse args */
Config config;
int r = parse_args(argc, argv, &config);
if (r) {
return r;
}
spdlog::set_pattern("%H:%M:%S [%l] %v");
if (config.debug) {
spdlog::set_level(spdlog::level::debug);
} else {
spdlog::set_level(spdlog::level::info);
}
config.parse_args(argc, argv);
/* Init libuv */
uv_loop_t loop;
uv_loop_init(&loop);
loom::Worker worker(&loop,
config.server_address,
config.port,
config.work_dir);
/* Create worker */
loom::Worker worker(&loop, config);
worker.register_basic_tasks();
loom::llog->info("Worker started; listening on port {}", worker.get_listen_port());
worker.set_cpus(config.cpus);
/* Start loop */
uv_run(&loop, UV_RUN_DEFAULT);
uv_loop_close(&loop);
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment