GlobalBpiLib  1.0
GlobalBpiLib.hpp
Go to the documentation of this file.
1 /*
2  * GlobalBpiLib.h
3  *
4  * Author: Valentin Fitz
5  * Institute: University of Duisburg-Essen, Germany
6  */
7 
17 #ifndef GLOBALBPILIB_HPP_
18 #define GLOBALBPILIB_HPP_
19 
20 //#include "LocalBpiLib.h"
21 #include "include/json.hpp"
22 #include <string>
23 #include <sstream>
24 #include <vector>
25 #include <csignal>
26 #include <chrono>
27 
28 #define STAMPLENGTH 6
29 #define GEN_BUFFERSIZE 4096
30 #define DLE "\x10"
31 #define ST_UDP 1
32 #define ST_RAW 0
33 #define SOCKETTYPE ST_RAW
34 
41 class BpiOutStream {
42 private:
43  std::ostream& _os;
44  std::stringstream _localStringStream;
45 public:
46  BpiOutStream(std::ostream& os);
47  virtual ~BpiOutStream();
48  friend BpiOutStream& operator<<(BpiOutStream &bout, std::string s);
49  friend BpiOutStream& operator<<(BpiOutStream &bout, int i);
50  friend BpiOutStream& operator<<(BpiOutStream &bout, std::ostream& (*pf)(std::ostream&));
51 };
52 
60 private:
61  std::ostream& _os;
62  std::stringstream _localStringStream;
63 public:
64  DebugOutStream(std::ostream& os);
65  virtual ~DebugOutStream();
66  friend DebugOutStream& operator<<(DebugOutStream &dout, std::string s);
67  friend DebugOutStream& operator<<(DebugOutStream &dout, int i);
68  friend DebugOutStream& operator<<(DebugOutStream &dout, std::ostream& (*pf)(std::ostream&));
69 };
70 
81 public:
82  Configuration();
83  virtual ~Configuration();
84  static std::string getConfig(std::string jsonFilePath, std::string attribute);
85  static bool setConfig(std::string jsonFilePath, std::string attribute, std::string value);
86  static nlohmann::json readJsonFile(std::string pathToFile);
87  static void writeJsonFile(nlohmann::json json, std::string pathToFile);
88  static bool deleteConfig(std::string pathToFile, std::string attribute);
89 private:
90 };
91 
97 class Timer {
98 public:
99  Timer() :
100  beg_(clock_::now()) {
101  }
102  void reset() {
103  beg_ = clock_::now();
104  }
105  double elapsed() const {
106  return std::chrono::duration_cast<std::chrono::microseconds>(clock_::now() - beg_).count();
107  }
108 
109 private:
110  typedef std::chrono::high_resolution_clock clock_;
111  std::chrono::time_point<clock_> beg_;
112 };
113 
114 static std::string bpiName = "------";
115 static std::vector<int> childs;
117 void bpiSetName(std::string name);
118 std::string bpiGetName();
119 bool registerChild(int pid);
120 bool deregisterChild(int pid);
121 std::vector<int> getAllChilds();
122 void sigtermHandler(int signum);
123 void bpiSystemCall(std::string cmd, int (*printFct)(std::string) = nullptr, int (*printErrFct)(std::string) = nullptr);
124 void bpiSystemCall2(std::string cmd, int (*printFct)(std::string) = nullptr, int (*printErrFct)(std::string) = nullptr, std::string cmdId = "");
125 std::string bpiTimestamp();
126 std::vector<std::string> bpiSplitIntoItems(std::string str, char delimiter);
127 std::string bpiGetNamestamp(std::string name);
128 std::string bpiGetNamestampErr(std::string name);
129 std::string bpiConfirm(std::string command, std::string optional);
130 bool readInputFromStdin(std::string* s, int bufsize = BUFSIZ);
131 int readFromFileDescriptor(int fileDescriptor, std::string& data, int buffersize);
132 int writeToFileDescriptor(int fileDescriptor, std::string data);
133 std::string createCtrlMessage(int argc, ...);
134 std::string createStatCtrlMessage(int argc, ...);
135 std::string macToString(int len, char* mac);
136 double getCurrentCpuUsage();
137 
138 #endif /* GLOBALBPILIB_HPP_ */
void bpiSystemCall2(std::string cmd, int(*printFct)(std::string)=nullptr, int(*printErrFct)(std::string)=nullptr, std::string cmdId="")
Definition: GlobalBpiLib.cpp:248
Custom bpi printer class.
Definition: GlobalBpiLib.hpp:41
Custom debug printer class.
Definition: GlobalBpiLib.hpp:59
std::string createStatCtrlMessage(int argc,...)
Definition: GlobalBpiLib.cpp:399
std::string bpiGetNamestamp(std::string name)
Definition: GlobalBpiLib.cpp:317
std::string bpiGetNamestampErr(std::string name)
Definition: GlobalBpiLib.cpp:323
int readFromFileDescriptor(int fileDescriptor, std::string &data, int buffersize)
Definition: GlobalBpiLib.cpp:352
void bpiSystemCall(std::string cmd, int(*printFct)(std::string)=nullptr, int(*printErrFct)(std::string)=nullptr)
Definition: GlobalBpiLib.cpp:216
bool deregisterChild(int pid)
Definition: GlobalBpiLib.cpp:197
std::vector< std::string > bpiSplitIntoItems(std::string str, char delimiter)
std::string bpiTimestamp()
Definition: GlobalBpiLib.cpp:287
void bpiSetName(std::string name)
Definition: GlobalBpiLib.cpp:180
void sigtermHandler(int signum)
Definition: GlobalBpiLib.cpp:363
std::string createCtrlMessage(int argc,...)
Definition: GlobalBpiLib.cpp:377
friend BpiOutStream & operator<<(BpiOutStream &bout, std::string s)
Definition: GlobalBpiLib.cpp:118
BpiOutStream(std::ostream &os)
Definition: GlobalBpiLib.cpp:111
std::string bpiConfirm(std::string command, std::string optional)
Definition: GlobalBpiLib.cpp:329
Interface between the application and a local stored json file.
Definition: GlobalBpiLib.hpp:80
bool registerChild(int pid)
Definition: GlobalBpiLib.cpp:188
virtual ~BpiOutStream()
Definition: GlobalBpiLib.cpp:115
Simple timer with µs precision.
Definition: GlobalBpiLib.hpp:97
std::vector< int > getAllChilds()
Definition: GlobalBpiLib.cpp:206
int writeToFileDescriptor(int fileDescriptor, std::string data)
Definition: GlobalBpiLib.cpp:347