Skip to content

Commit

Permalink
migration to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikrueger committed Mar 12, 2012
1 parent 5a36456 commit d125fd5
Show file tree
Hide file tree
Showing 12 changed files with 621 additions and 355 deletions.
12 changes: 11 additions & 1 deletion include/VZException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace vz {
std::string _reason;
};

// Connection failed
// Option invalid type
class InvalidTypeException : public vz::VZException{
public:
InvalidTypeException(const std::string& reason) :
Expand All @@ -59,6 +59,16 @@ namespace vz {
protected:
};

// Option item not found
class OptionNotFoundException : public vz::VZException{
public:
OptionNotFoundException(const std::string& reason) :
vz::VZException(reason) {}
virtual ~OptionNotFoundException() throw() {}

protected:
};


// Connection failed
class ConnectionException : public vz::VZException{
Expand Down
20 changes: 17 additions & 3 deletions include/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,21 @@ typedef struct {
struct curl_slist *headers;
} api_handle_t;

int api_init(channel_t *ch, api_handle_t *api);
void api_free(api_handle_t *api);
namespace vz {

class Api {
public:
Api(Channel::Ptr ch);
//Api(Channel *ch);
~Api();
CURL *curl() { return _api.curl; }


private:
Channel::Ptr _ch;
api_handle_t _api;
};
} // namespace vz

/**
* Reformat CURLs debugging output
Expand All @@ -62,7 +75,8 @@ size_t curl_custom_write_callback(void *ptr, size_t size, size_t nmemb, void *da
* @param last the last tuple of our linked list which should be encoded
* @return the json_object (has to be free'd)
*/
json_object * api_json_tuples(buffer_t *buf, reading_t *first, reading_t *last);
json_object * api_json_tuples(Buffer::Ptr buf);
//Reading *first, Reading *last);

/**
* Parses JSON encoded exception and stores describtion in err
Expand Down
7 changes: 5 additions & 2 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define _COMMON_H_

#include <stdarg.h>
#include <string.h>
#include <stdlib.h>


#include "../config.h" /* GNU buildsystem config */

Expand All @@ -15,7 +18,7 @@ typedef enum {
} log_level_t;

/* types */
typedef unsigned char bool;
//typedef unsigned char bool;

/* constants */
#define SUCCESS 0
Expand All @@ -24,6 +27,6 @@ typedef unsigned char bool;
#define ERR_INVALID_TYPE -3

/* prototypes */
void print(log_level_t lvl, const char *format, void *id, ... );
void print(log_level_t lvl, const char *format, const char *id, ... );

#endif /* _COMMON_H_ */
197 changes: 161 additions & 36 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,174 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_

#include <json/json.h>
#include <list>
#include <vector>

#include <json.hpp>

//#include <json/json.h>

#include <common.h>

//namespace vz {
// using std::list;
// using boost::enable_shared_from_this;
//}


#include <meter.h>
#include <channel.h>
#include <options.h>
#include <meter_protocol.hpp>

/* forward declarartions */
class Map {
public:
typedef vz::shared_ptr<Map> Ptr;
typedef std::vector<Channel>::iterator iterator;
typedef std::vector<Channel>::const_iterator const_iterator;

Map(std::list<Option> options) : _meter(new Meter(options)){}
~Map() {};
/*
Map(const Map &map) {
std::cout << "=====> copy\n";
_meter.reset(new Meter(map._meter.get()));
}
*/
Meter::Ptr meter() { return _meter; }

void stop() {}

void start() {
if(_meter->isEnabled()) {

_meter->open();
print(log_debug, "meter is opened. Start reader.", _meter->name());
pthread_create(&_thread, NULL, &reading_thread, (void *) this);
print(log_debug, "meter is opened. Start channels.", _meter->name());
for(iterator it = _channels.begin(); it!=_channels.end(); it++) {
it->start();
}
} else {
print(log_debug, "Skipping disabled meter.", _meter->name());
}

}

void cancel() {
pthread_cancel(_thread);
for(iterator it = _channels.begin(); it!=_channels.end(); it++) {
it->cancel();
}
}

void push_back(Channel channel) { _channels.push_back(channel); }

iterator begin() { return _channels.begin(); }
iterator end() { return _channels.end(); }
size_t size() { return _channels.size(); }

private:
Meter::Ptr _meter;
std::vector<Channel> _channels;

pthread_t _thread;
};

class MapContainer {
public:
typedef vz::shared_ptr<MapContainer> Ptr;
typedef std::vector<Map>::iterator iterator;
typedef std::vector<Map>::const_iterator const_iterator;

MapContainer() {};
~MapContainer() {};

void quit(int sig) {
print(log_info, "Closing connections to terminate", (char*)0);

for(iterator it = _mappings.begin(); it!=_mappings.end(); it++) {
it->cancel();
}
}
const size_t size() const { return _mappings.size(); }
iterator begin() { return _mappings.begin(); }
iterator end() { return _mappings.end(); }
void push_back(const Map &map) { _mappings.push_back(map); }

private:
std::vector<Map> _mappings;

};

#include "meter.h"
#include "list.h"
#include "options.h"

/**
* General options from CLI
*/
typedef struct {
char *config; /* filename of configuration */
char *log; /* filename for logging */
FILE *logfd;
class Config_Options {
public:
Config_Options();
Config_Options(const std::string filename);
~Config_Options() {};

/**
* Parse JSON formatted configuration file
*
* @param const char *filename the path of the configuration file
* @param list_t *mappings a pointer to a list, where new channel<->meter mappings should be stored
* @param config_options_t *options a pointer to a structure of global configuration options
* @return int non-zero on success
*/
void config_parse(MapContainer::Ptr mappings);
//Map::Ptr config_parse_meter(struct json_object *jso);
//void config_parse_channel(struct json_object *jso, Map::Ptr mapping);
Map::Ptr config_parse_meter(Json::Ptr jso);
void config_parse_channel(Json& jso, Map::Ptr mapping);

int port; /* TCP port for local interface */
int verbosity; /* verbosity level */
int comet_timeout; /* in seconds; */
int buffer_length; /* in seconds; how long to buffer readings for local interfalce */
int retry_pause; /* in seconds; how long to pause after an unsuccessful HTTP request */
// getter
const std::string &config() const { return _config; }
const std::string &log() const { return _log; }
FILE *logfd() { return _logfd; }
const int &port() const { return _port; }
const int &verbosity() const { return _verbosity; }
const int retry_pause() const { return _retry_pause; }

/* boolean bitfields, padding at the end of struct */
int channel_index:1; /* give a index of all available channels via local interface */
int daemon:1; /* run in background */
int foreground:1; /* dont fork in background */
int local:1; /* enable local interface */
int logging:1; /* start logging threads, depends on local & daemon */
} config_options_t;
const bool daemon() const { return _daemon; }
const bool foreground()const { return _foreground; }
const bool local() const { return _local; }
const bool logging() const { return _logging; }

// setter
void config(const std::string &v) { _config = v; }
void log(const std::string &v) { _log = v; }
void logfd(FILE *fd) { _logfd = fd; }
void port(const int v) { _port = v; }
void verbosity(int v) { _verbosity = v; }

/* forward declarartions */
struct map;
struct channel;
void daemon(const bool v) { _daemon = v; }
void foreground(const bool v){ _foreground = v; }
void local(const bool v) { _local = v; }
void logging(const bool v) { _logging = v; }

private:
std::string _config; /* filename of configuration */
std::string _log; /* filename for logging */
FILE *_logfd;

int _port; /* TCP port for local interface */
int _verbosity; /* verbosity level */
int _comet_timeout; /* in seconds; */
int _buffer_length; /* in seconds; how long to buffer readings for local interfalce */
int _retry_pause; /* in seconds; how long to pause after an unsuccessful HTTP request */

/* boolean bitfields, padding at the end of struct */
int _channel_index:1; /* give a index of all available channels via local interface */
int _daemon:1; /* run in background */
int _foreground:1; /* dont fork in background */
int _local:1; /* enable local interface */
int _logging:1; /* start logging threads, depends on local & daemon */
};

/**
* Reads key, value and type from JSON object
Expand All @@ -66,7 +203,7 @@ struct channel;
* @param option a pointer to a uninitialized option_t structure
* @return 0 on succes, <0 on error
*/
int option_init(struct json_object *jso, char *key, option_t *option);
//int option_init(struct json_object *jso, char *key, Option *option);

/**
* Validate UUID
Expand All @@ -77,17 +214,5 @@ int option_init(struct json_object *jso, char *key, option_t *option);
*/
int config_validate_uuid(const char *uuid);

/**
* Parse JSON formatted configuration file
*
* @param const char *filename the path of the configuration file
* @param list_t *mappings a pointer to a list, where new channel<->meter mappings should be stored
* @param config_options_t *options a pointer to a structure of global configuration options
* @return int non-zero on success
*/
int config_parse(const char *filename, list_t *mappings, config_options_t *options);

struct channel * config_parse_channel(struct json_object *jso, meter_protocol_t protocol);
struct map * config_parse_meter(struct json_object *jso);

#endif /* _CONFIG_H_ */
23 changes: 23 additions & 0 deletions include/json.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _json_hpp_
#define _json_hpp_

#include <json/json.h>
#include <shared_ptr.hpp>


class Json {
public:
typedef vz::shared_ptr<Json> Ptr;

Json(struct json_object *jso) : _jso(jso) {};
~Json() { _jso=NULL;};

struct json_object* Object() { return _jso; }

private:
struct json_object* _jso;

};


#endif /* _json_hpp_ */
Loading

0 comments on commit d125fd5

Please sign in to comment.