Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Typedefs | Functions | Variables
JSYSTEM Namespace Reference

Auxiliary classes and methods for operating system calls. More...

Classes

class  JDateAndTime
 Auxililary class to get ASCII formatted date and time. More...
 
class  JKeypress
 Enable unbuffered terminal input. More...
 
class  JProcess
 Streaming of input and output from Linux command. More...
 
class  JShell
 The JShell clas can be used to interact with the shell via I/O streams. More...
 
struct  JStat
 Auxiliary class for file status. More...
 
struct  JSysinfo
 Auxiliary class for file status. More...
 
struct  ls
 Auxiliary class to list files. More...
 
struct  JLocalTime
 Auxililary class to local get time in micro seconds. More...
 

Typedefs

typedef long long int localtime_t
 Type definition of local time. More...
 

Functions

const char * getDate ()
 Get ASCII formatted date. More...
 
const char * getTime ()
 Get ASCII formatted time. More...
 
std::string getHostname ()
 Get host name. More...
 
std::string getHostname (const int ip)
 Get host name. More...
 
int getIPnumber (const std::string &host_name)
 Get IP number. More...
 
int getIPnumber ()
 Get IP number. More...
 
std::string getIPaddress (const int ip)
 Get IP address (decimal-dot notation). More...
 
std::string getIPaddress ()
 Get IP address (decimal-dot notation). More...
 
unsigned short getSubaddress (const int ip)
 Get host identifier within network. More...
 
unsigned short getSubaddress ()
 Get host identifier within network. More...
 
float getMemoryUsage (JShell &shell, const pid_t pid)
 Get memory usage in percent of given process identifier. More...
 
float getMemoryUsage (JShell &shell)
 Get memory usage in percent of this process. More...
 
float getMemoryUsage ()
 Get memory usage in percent of this process. More...
 
float getCpuUsage (JShell &shell, const pid_t pid)
 Get cpu usage in percent of given process identifier. More...
 
float getCpuUsage (JShell &shell)
 Get cpu usage in percent of this process. More...
 
float getCpuUsage ()
 Get cpu usage in percent of this process. More...
 
pid_t getPID (JShell &shell, const char *process)
 Get process identifier. More...
 
pid_t getPID (const char *process)
 Get process identifier. More...
 
pid_t getParentID (JShell &shell, pid_t pid)
 Get parent process identifier. More...
 
pid_t getParentID (const pid_t pid)
 Get parent process identifier. More...
 
std::string getShell (JShell &shell)
 Get shell name. More...
 
std::string getShell ()
 Get shell name of this process. More...
 
unsigned long long int getRAM ()
 Get RAM of this CPU. More...
 
const std::string which (JShell &shell, const char *process)
 Get process path. More...
 
const std::string which (const char *process)
 Get process path. More...
 
void gprint (const std::string &message)
 Print method. More...
 
int gexit (int status, const std::string &message="")
 Exit method. More...
 
void set_variable (const std::string &name, const std::string &value)
 Set environment variable. More...
 

Variables

static const JDateAndTime getDateAndTime
 Function object to get ASCII formatted date and time. More...
 
static const JCompareEndian compareEndian
 Function object operator. More...
 
static JStat getFileStatus
 Function object for file status. More...
 
static const char *const ZSH = "zsh"
 Shell names. More...
 
static const char *const KSH = "ksh"
 
static const char *const BASH = "bash"
 
static const char *const CSH = "csh"
 
static const char *const TCSH = "tcsh"
 
static const JLocalTime getLocalTime
 Function object to get local time in micro seconds. More...
 

Detailed Description

Auxiliary classes and methods for operating system calls.

Author
mdejong

Typedef Documentation

typedef long long int JSYSTEM::localtime_t

Type definition of local time.

Definition at line 21 of file JSystem/JTime.hh.

Function Documentation

const char* JSYSTEM::getDate ( )
inline

Get ASCII formatted date.

Returns
date

Definition at line 22 of file JDate.hh.

23  {
24  static time_t ts;
25  static const int MAX_SIZE = 256;
26  static char buffer[MAX_SIZE];
27 
28  time(&ts);
29 
30  strftime(buffer, MAX_SIZE, "%x", localtime(&ts));
31 
32  return buffer;
33  }
const char* JSYSTEM::getTime ( )
inline

Get ASCII formatted time.

Returns
time

Definition at line 41 of file JDate.hh.

42  {
43  static time_t ts;
44  static const int MAX_SIZE = 256;
45  static char buffer[MAX_SIZE];
46 
47  time(&ts);
48 
49  strftime(buffer, MAX_SIZE, "%X", localtime(&ts));
50 
51  return buffer;
52  }
std::string JSYSTEM::getHostname ( )
inline

Get host name.

Note that to obtain a host name including the domain name, JSYSTEM::getHostname(JSYSTEM::getIPnumber()) should be used.

Returns
host name

Definition at line 75 of file JNetwork.hh.

76  {
77  const size_t MAXIMUM_LENGTH = 256;
78  char buffer[MAXIMUM_LENGTH];
79 
80  if (gethostname(buffer, MAXIMUM_LENGTH) == 0)
81  return std::string(buffer);
82  else
83  throw JSystemException("Unknown host name.");
84  }
std::string JSYSTEM::getHostname ( const int  ip)
inline

Get host name.

Parameters
ipIP number
Returns
host name

Definition at line 94 of file JNetwork.hh.

95  {
96  in_addr buffer;
97 
98  buffer.s_addr = ip;
99 
100  hostent* p = gethostbyaddr(&buffer, sizeof(in_addr), AF_INET);
101 
102  if (p != NULL)
103  return std::string(p->h_name);
104  else
105  throw JSystemException("Unknown IP address.");
106  }
int JSYSTEM::getIPnumber ( const std::string &  host_name)
inline

Get IP number.

Parameters
host_namehost name
Returns
IP number

Definition at line 115 of file JNetwork.hh.

116  {
117  int ip = -1;
118 
119  std::string buffer = host_name;
120 
121  if (buffer == "" || buffer == "localhost")
122  buffer = getHostname();
123 
124  const hostent* hp = gethostbyname(buffer.c_str());
125 
126  if (hp != NULL)
127  memcpy((char *) &ip, hp->h_addr, sizeof(int));
128 
129  return ip;
130  }
std::string getHostname()
Get host name.
Definition: JNetwork.hh:75
int JSYSTEM::getIPnumber ( )
inline

Get IP number.

Returns
IP number

Definition at line 138 of file JNetwork.hh.

139  {
140  return getIPnumber(getHostname());
141  }
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:115
std::string getHostname()
Get host name.
Definition: JNetwork.hh:75
std::string JSYSTEM::getIPaddress ( const int  ip)
inline

Get IP address (decimal-dot notation).

Parameters
ipIP number
Returns
IP address

Definition at line 150 of file JNetwork.hh.

151  {
152  std::ostringstream os;
153 
154  if (compareEndian())
155  os << ((ip >> 24) & 0xFF) << '.'
156  << ((ip >> 16) & 0xFF) << '.'
157  << ((ip >> 8) & 0xFF) << '.'
158  << ((ip >> 0) & 0xFF);
159  else
160  os << ((ip >> 0) & 0xFF) << '.'
161  << ((ip >> 8) & 0xFF) << '.'
162  << ((ip >> 16) & 0xFF) << '.'
163  << ((ip >> 24) & 0xFF);
164 
165  return os.str();
166  }
static const JCompareEndian compareEndian
Function object operator.
Definition: JNetwork.hh:64
std::string JSYSTEM::getIPaddress ( )
inline

Get IP address (decimal-dot notation).

Returns
IP address

Definition at line 174 of file JNetwork.hh.

175  {
176  return getIPaddress(getIPnumber());
177  }
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:115
std::string getIPaddress(const int ip)
Get IP address (decimal-dot notation).
Definition: JNetwork.hh:150
unsigned short JSYSTEM::getSubaddress ( const int  ip)
inline

Get host identifier within network.

Parameters
ipIP number
Returns
ID

Definition at line 186 of file JNetwork.hh.

187  {
188  if (ntohs(0x1234) == 0x1234)
189  return (unsigned short) (ip & 0x0000FFFF);
190  else
191  return (unsigned short) (((ip & 0xFF000000) >> 24) |
192  ((ip & 0x00FF0000) >> 8) );
193  }
unsigned short JSYSTEM::getSubaddress ( )
inline

Get host identifier within network.

Returns
ID

Definition at line 201 of file JNetwork.hh.

202  {
203  return getSubaddress(getIPnumber());
204  }
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:115
unsigned short getSubaddress(const int ip)
Get host identifier within network.
Definition: JNetwork.hh:186
float JSYSTEM::getMemoryUsage ( JShell shell,
const pid_t  pid 
)
inline

Get memory usage in percent of given process identifier.

Parameters
shellshell interface
pidprocess identifier
Returns
memory usage [%]

Definition at line 46 of file JSystemToolkit.hh.

47  {
48  using namespace std;
49 
50  float value = 0.0;
51 
52  shell << "ps -o %mem= -p " << pid << endl;
53 
54  if (shell.get(value))
55  return value;
56  else
57  THROW(JSystemException, "No process data for PID " << pid);
58  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
bool get(T &value)
Get value.
Definition: JShell.hh:106
float JSYSTEM::getMemoryUsage ( JShell shell)
inline

Get memory usage in percent of this process.

Parameters
shellshell interface
Returns
memory usage [%]

Definition at line 67 of file JSystemToolkit.hh.

68  {
69  return getMemoryUsage(shell, getpid());
70  }
float getMemoryUsage(JShell &shell, const pid_t pid)
Get memory usage in percent of given process identifier.
float JSYSTEM::getMemoryUsage ( )
inline

Get memory usage in percent of this process.

Returns
memory usage [%]

Definition at line 78 of file JSystemToolkit.hh.

79  {
80  return getMemoryUsage(JShell::getInstance(), getpid());
81  }
float getMemoryUsage(JShell &shell, const pid_t pid)
Get memory usage in percent of given process identifier.
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
float JSYSTEM::getCpuUsage ( JShell shell,
const pid_t  pid 
)
inline

Get cpu usage in percent of given process identifier.

Parameters
shellshell interface
pidprocess identifier
Returns
cpu usage [%]

Definition at line 91 of file JSystemToolkit.hh.

92  {
93  using namespace std;
94 
95  float value = 0.0;
96 
97  shell << "ps -o %cpu= -p " << pid << endl;
98 
99  if (shell.get(value))
100  return value;
101  else
102  THROW(JSystemException, "No process data for PID " << pid);
103  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
bool get(T &value)
Get value.
Definition: JShell.hh:106
float JSYSTEM::getCpuUsage ( JShell shell)
inline

Get cpu usage in percent of this process.

Parameters
shellshell interface
Returns
cpu usage [%]

Definition at line 112 of file JSystemToolkit.hh.

113  {
114  return getCpuUsage(shell, getpid());
115  }
float getCpuUsage(JShell &shell, const pid_t pid)
Get cpu usage in percent of given process identifier.
float JSYSTEM::getCpuUsage ( )
inline

Get cpu usage in percent of this process.

Returns
cpu usage [%]

Definition at line 123 of file JSystemToolkit.hh.

124  {
125  return getCpuUsage(JShell::getInstance(), getpid());
126  }
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
float getCpuUsage(JShell &shell, const pid_t pid)
Get cpu usage in percent of given process identifier.
pid_t JSYSTEM::getPID ( JShell shell,
const char *  process 
)
inline

Get process identifier.

Parameters
shellshell interface
processprocess name
Returns
process identifier

Definition at line 136 of file JSystemToolkit.hh.

137  {
138  using namespace std;
139 
140  pid_t pid = -1;
141 
142  shell << "ps -o pid= -C " << process << endl;
143 
144  if (shell.get(pid))
145  return pid;
146  else
147  THROW(JSystemException, "No process " << process);
148  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
bool get(T &value)
Get value.
Definition: JShell.hh:106
pid_t JSYSTEM::getPID ( const char *  process)
inline

Get process identifier.

Parameters
processprocess name
Returns
process identifier

Definition at line 157 of file JSystemToolkit.hh.

158  {
159  return getPID(JShell::getInstance(), process);
160  }
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
pid_t getPID(JShell &shell, const char *process)
Get process identifier.
pid_t JSYSTEM::getParentID ( JShell shell,
pid_t  pid 
)
inline

Get parent process identifier.

Parameters
shellshell interface
pidprocess identifier
Returns
parent identifier

Definition at line 170 of file JSystemToolkit.hh.

171  {
172  using namespace std;
173 
174  shell << "ps -o ppid= -p " << pid << endl;
175 
176  if (shell.get(pid))
177  return pid;
178  else
179  THROW(JSystemException, "No parent identifier " << pid);
180  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
bool get(T &value)
Get value.
Definition: JShell.hh:106
pid_t JSYSTEM::getParentID ( const pid_t  pid)
inline

Get parent process identifier.

Parameters
pidprocess identifier
Returns
parent identifier

Definition at line 189 of file JSystemToolkit.hh.

190  {
191  return getParentID(JShell::getInstance(), getpid());
192  }
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
pid_t getParentID(JShell &shell, pid_t pid)
Get parent process identifier.
std::string JSYSTEM::getShell ( JShell shell)
inline

Get shell name.

Parameters
shellshell interface
Returns
shell name

Definition at line 201 of file JSystemToolkit.hh.

202  {
203  using namespace std;
204 
205  static string value = "";
206 
207  if (value == "") {
208 
209  pid_t pid = getppid();
210 
211  shell << "ps -o ppid= -o args= -p " << pid << endl;
212 
213  if (shell >> pid >> value) {
214 
215  shell.flush();
216 
217  if (!value.empty() && value[0] == '-') {
218  value = value.substr(1);
219  }
220 
221  } else {
222 
223  static_cast<istream&>(shell).clear();
224  shell.flush();
225 
226  THROW(JSystemException, "No shell");
227  }
228  }
229 
230  return value;
231  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
JShell & flush(std::ostream &out=null)
Extracts characters from this shell and flush them to the given output stream until the prompt is rea...
Definition: JShell.hh:184
std::string JSYSTEM::getShell ( )
inline

Get shell name of this process.

Returns
shell name

Definition at line 239 of file JSystemToolkit.hh.

240  {
241  return getShell(JShell::getInstance());
242  }
std::string getShell(JShell &shell)
Get shell name.
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
unsigned long long int JSYSTEM::getRAM ( )
inline

Get RAM of this CPU.

Returns
number of bytes

Definition at line 250 of file JSystemToolkit.hh.

251  {
252  const JSysinfo info;
253 
254  return info.getTotalRAM();
255  }
Auxiliary class for file status.
Definition: JSysinfo.hh:21
unsigned long long int getTotalRAM() const
Get total RAM.
Definition: JSysinfo.hh:38
const std::string JSYSTEM::which ( JShell shell,
const char *  process 
)
inline

Get process path.

Parameters
shellshell interface
processprocess name
Returns
path

Definition at line 265 of file JSystemToolkit.hh.

266  {
267  using namespace std;
268 
269  string buffer;
270 
271  shell << "which " << process << endl;
272 
273  shell.getline(buffer);
274 
275  shell.flush();
276 
277  return buffer;
278  }
JShell & flush(std::ostream &out=null)
Extracts characters from this shell and flush them to the given output stream until the prompt is rea...
Definition: JShell.hh:184
bool getline(std::string &buffer, const char eol= '\n')
Get line of text.
Definition: JShell.hh:137
const std::string JSYSTEM::which ( const char *  process)
inline

Get process path.

Parameters
processprocess name
Returns
path

Definition at line 287 of file JSystemToolkit.hh.

288  {
289  return which(JShell::getInstance(), process);
290  }
T & getInstance(const T &object)
Get static instance from temporary object.
Definition: JObject.hh:73
const std::string which(JShell &shell, const char *process)
Get process path.
void JSYSTEM::gprint ( const std::string &  message)
inline

Print method.

Parameters
messagemessage

Definition at line 352 of file JSystemToolkit.hh.

353  {
354  using namespace std;
355 
356  istringstream is(message);
357 
358  for (string buffer; getline(is, buffer); ) {
359  cout << "echo \"" << buffer << "\";" << endl;
360  }
361  }
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
int JSYSTEM::gexit ( int  status,
const std::string &  message = "" 
)
inline

Exit method.

Parameters
statusexit status
messageoptional message

Definition at line 370 of file JSystemToolkit.hh.

371  {
372  using namespace std;
373 
374  gprint(message);
375 
376  cout << "exit " << status << endl;
377 
378  return status;
379  }
void gprint(const std::string &message)
Print method.
void JSYSTEM::set_variable ( const std::string &  name,
const std::string &  value 
)
inline

Set environment variable.

This method prints the shell command to set the variable with the given name to the specified value.

Parameters
namevariable name
valuevariable value

Definition at line 390 of file JSystemToolkit.hh.

391  {
392  using namespace std;
393  using namespace JSYSTEM;
394 
395  static const string shell = getShell();
396 
397  string buffer(value);
398 
399  if (buffer.find(' ') != string::npos) {
400  buffer = "\"" + buffer + "\"";
401  }
402 
403  if (shell.find(ZSH) != string::npos ||
404  shell.find(KSH) != string::npos ||
405  shell.find(BASH) != string::npos)
406 
407  cout << "export " << name << "=" << buffer << ";" << endl;
408 
409  else if (shell.find(CSH) != string::npos ||
410  shell.find(TCSH) != string::npos)
411 
412  cout << "setenv " << name << " " << buffer << ";" << endl;
413 
414  else
415 
416  THROW(JSystemException, "unknown shell " << shell);
417  }
std::string getShell(JShell &shell)
Get shell name.
static const char *const TCSH
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
static const char *const ZSH
Shell names.
static const char *const BASH
static const char *const KSH
static const char *const CSH

Variable Documentation

const JDateAndTime JSYSTEM::getDateAndTime
static

Function object to get ASCII formatted date and time.

Definition at line 115 of file JDate.hh.

const JCompareEndian JSYSTEM::compareEndian
static

Function object operator.

Returns
true if network and system endianess same; else false

Definition at line 64 of file JNetwork.hh.

JStat JSYSTEM::getFileStatus
static

Function object for file status.

Definition at line 173 of file JStat.hh.

const char* const JSYSTEM::ZSH = "zsh"
static

Shell names.

Definition at line 32 of file JSystemToolkit.hh.

const char* const JSYSTEM::KSH = "ksh"
static

Definition at line 33 of file JSystemToolkit.hh.

const char* const JSYSTEM::BASH = "bash"
static

Definition at line 34 of file JSystemToolkit.hh.

const char* const JSYSTEM::CSH = "csh"
static

Definition at line 35 of file JSystemToolkit.hh.

const char* const JSYSTEM::TCSH = "tcsh"
static

Definition at line 36 of file JSystemToolkit.hh.

const JLocalTime JSYSTEM::getLocalTime
static

Function object to get local time in micro seconds.

Definition at line 61 of file JSystem/JTime.hh.