-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkModule.cpp
84 lines (68 loc) · 2.33 KB
/
NetworkModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* NetworkModule.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kdenisov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/03 16:08:01 by kdenisov #+# #+# */
/* Updated: 2019/11/14 11:13:13 by kdenisov ### ########.fr */
/* */
/* ************************************************************************** */
#include <fstream>
#include <unistd.h>
#include <string>
#include <sys/sysctl.h>
#include "includes/NetworkModule.hpp"
NetworkModule::NetworkModule() {
}
NetworkModule::NetworkModule(int pos, int gpos) : _pos(pos), _gpos(gpos) {
this->_module = "NETWORK";
refresh();
}
NetworkModule::~NetworkModule() {
}
NetworkModule::NetworkModule(NetworkModule const & src) {
*this = src;
}
NetworkModule & NetworkModule::operator=(NetworkModule const & rfs) {
this->_module = rfs._module;
this->_info = rfs._info;
this->_pos = rfs._pos;
this->_gpos = rfs._gpos;
return (*this);
}
void NetworkModule::render(IMonitorDisplay *d) {
d->render(this);
}
void NetworkModule::refresh() {
system("top -l 1 | grep \"Network\" | awk '{print $2\" \"$3\" \"$4\" \"$5\" \"$6}' > ./logs/netlog");
std::ifstream ifs("./logs/netlog");
std::string buff;
try {
if (!ifs.is_open())
throw std::exception();
}
catch(std::exception& e) {
std::cerr << e.what() << "(" << getName() << ")" << ": Failed on openning file" << std::endl;
exit(1);
}
std::getline(ifs, buff);
this->_info = buff;
ifs.close();
}
std::string NetworkModule::getName() const {
return (this->_module);
}
int NetworkModule::getPos() const {
return (this->_pos);
}
int NetworkModule::getGPos() const {
return (this->_gpos);
}
std::string NetworkModule::getInfo() const {
return (this->_info);
}
int NetworkModule::getSize(std::string const name) const {
return (name.length());
}