vmem, relaunch
This commit is contained in:
parent
133d960919
commit
3cd23b0956
BIN
client/client
BIN
client/client
Binary file not shown.
|
@ -30,9 +30,16 @@ void* recieve(void* arg) {
|
|||
while (true) {
|
||||
char buffer[128];
|
||||
ssize_t recieved = recv(client_socket, buffer, sizeof(buffer), 0);
|
||||
if (recieved == 0 || recieved == -1) {
|
||||
if (recieved == 0) {
|
||||
std::cout << "Server shutted down" << std::endl;
|
||||
close(client_socket);
|
||||
break;
|
||||
}
|
||||
|
||||
if (recieved == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << buffer << std::endl;
|
||||
}
|
||||
|
||||
|
@ -53,6 +60,7 @@ void* timer(void* arg) {
|
|||
|
||||
int main() {
|
||||
pthread_t t1 = -1, t2 = -1;
|
||||
pthread_t tid1, tid2;
|
||||
|
||||
std::cout << "Usage:\n\tconnectN - connect to the server\n\tdisconnectN - disconnect from the server\n\tsendN - send request to the server\n\ttimerN - begin polling of the server\n\tstopN - stop polling of the server\n\tquit - quit" << std::endl;
|
||||
|
||||
|
@ -71,10 +79,10 @@ int main() {
|
|||
server_address1.sin_addr.s_addr = inet_addr(SERVER_IP1.c_str());
|
||||
if (connect(client_socket1, (struct sockaddr*)&server_address1, sizeof(server_address1)) == 0) {
|
||||
std::cout << "Connected to the server 1" << std::endl;
|
||||
pthread_t tid1;
|
||||
int* client_socket_copy1 = new int(client_socket1);
|
||||
pthread_create(&tid1, nullptr, &recieve, client_socket_copy1);
|
||||
} else {
|
||||
client_socket1 = -1;
|
||||
std::cout << "Failed to connect " << std::strerror(errno) << std::endl;
|
||||
}
|
||||
} else if (command == "connect2") {
|
||||
|
@ -85,25 +93,38 @@ int main() {
|
|||
server_address2.sin_addr.s_addr = inet_addr(SERVER_IP2.c_str());
|
||||
if (connect(client_socket2, (struct sockaddr*)&server_address2, sizeof(server_address2)) == 0) {
|
||||
std::cout << "Connected to the server 2" << std::endl;
|
||||
pthread_t tid2;
|
||||
int* client_socket_copy2 = new int(client_socket2);
|
||||
pthread_create(&tid2, nullptr, &recieve, client_socket_copy2);
|
||||
} else {
|
||||
client_socket2 = -1;
|
||||
std::cout << "Failed to connect " << std::strerror(errno) << std::endl;
|
||||
}
|
||||
} else if (command == "disconnect1") {
|
||||
if (client_socket1 != -1) {
|
||||
close(client_socket1);
|
||||
pthread_cancel(tid1);
|
||||
client_socket1 = -1;
|
||||
std::cout << "Disconnected" << std::endl;
|
||||
|
||||
if (t1 != -1) {
|
||||
pthread_cancel(t1);
|
||||
t1 = -1;
|
||||
std::cout << "Timer removed" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "Server is not connected" << std::endl;
|
||||
}
|
||||
} else if (command == "disconnect2") {
|
||||
if (client_socket2 != -1) {
|
||||
close(client_socket1);
|
||||
close(client_socket2);
|
||||
pthread_cancel(tid2);
|
||||
client_socket2 = -1;
|
||||
std::cout << "Disconnected" << std::endl;
|
||||
if (t2 != -1) {
|
||||
pthread_cancel(t2);
|
||||
t2 = -1;
|
||||
std::cout << "Timer removed" << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "Server is not connected" << std::endl;
|
||||
}
|
||||
|
@ -129,7 +150,7 @@ int main() {
|
|||
std::cout << "Server is not connected" << std::endl;
|
||||
}
|
||||
} else if (command == "timer2") {
|
||||
if (client_socket1 != -1) {
|
||||
if (client_socket2 != -1) {
|
||||
int* client_socket_copy2 = new int(client_socket2);
|
||||
pthread_create(&t2, nullptr, &timer, client_socket_copy2);
|
||||
pthread_detach(t2);
|
||||
|
|
BIN
server1/server
BIN
server1/server
Binary file not shown.
|
@ -17,6 +17,8 @@
|
|||
#include <cstdio>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
|
||||
/* Configuration */
|
||||
|
@ -51,9 +53,6 @@ int active_connections = 0;
|
|||
// Total amount of handled requests
|
||||
int handndled_requests = 0;
|
||||
|
||||
// Amount of active timers for sending messages
|
||||
int active_timer_threads = 0;
|
||||
|
||||
|
||||
// Initializes ncurses lib
|
||||
void init_ncurses() {
|
||||
|
@ -140,12 +139,43 @@ std::string get_mem_percent() {
|
|||
return "Physical memory: " + std::to_string(100 * (total_mem - free_mem)/total_mem) + "%\n";
|
||||
}
|
||||
|
||||
|
||||
std::string get_vmem() {
|
||||
std::ifstream meminfo("/proc/self/status");
|
||||
if(!meminfo) {
|
||||
return "Error: failed to open /proc/meminfo\n";
|
||||
}
|
||||
|
||||
std::string line;
|
||||
long not_free_mem = -1, total_mem = -1;
|
||||
|
||||
while((std::getline(meminfo, line))) {
|
||||
if(line.find("VmRSS:") == 0) {
|
||||
std::sscanf(line.c_str(), "VmRSS: %ld kB", ¬_free_mem);
|
||||
} else if(line.find("VmSize:") == 0) {
|
||||
std::sscanf(line.c_str(), "VmSize: %ld kB", &total_mem);
|
||||
}
|
||||
|
||||
if(not_free_mem != -1 && total_mem != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
meminfo.close();
|
||||
|
||||
if(total_mem == -1 || not_free_mem == -1) {
|
||||
return "Error: failed to read info from /proc/meminfo\n";
|
||||
}
|
||||
|
||||
return "Virtual memory of the proccess: " + std::to_string(100.0 * not_free_mem/total_mem) + "%\n";
|
||||
}
|
||||
|
||||
// Returns message to send according to the server type
|
||||
std::string get_sysinfo() {
|
||||
if (SERVER_TYPE == "server1") {
|
||||
return '\t' + get_last_error() + "\t\t\t" + get_cursor_position();
|
||||
}
|
||||
return '\t' + get_mem_percent() + "\t\t\t" + "WIP\n"; // TODO: vmem
|
||||
return '\t' + get_mem_percent() + "\t\t\t" + get_vmem(); // TODO: vmem
|
||||
}
|
||||
|
||||
// Writes log message on screen and passes it to the logging pipe
|
||||
|
@ -247,6 +277,7 @@ void* handle_connection(void* arg) {
|
|||
std::string msg = timestamp + info;
|
||||
send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0);
|
||||
log("Sent new info to client " + std::to_string(client_socket));
|
||||
handndled_requests++;
|
||||
} else {
|
||||
log("Actual info already on client " + std::to_string(client_socket));
|
||||
}
|
||||
|
@ -263,6 +294,8 @@ void* draw(void* arg) {
|
|||
// Update info
|
||||
clear();
|
||||
|
||||
printw("Use q + Enter to quit\n\n");
|
||||
|
||||
// Print configuration
|
||||
printw(
|
||||
"LOG_FIFO_NAME=%s\nSERVER_TYPE=%s\nPORT=%i\nMAX_CONNECTIONS=%i\nCMD_LOG_BUFFER=%i\nCMD_REFRESH_TIME=%i\n\n",
|
||||
|
@ -270,7 +303,7 @@ void* draw(void* arg) {
|
|||
);
|
||||
|
||||
// Print statistics
|
||||
printw("Active connections: %i\nHandled messages: %i\nActive timers: %i\n", active_connections, handndled_requests, active_timer_threads);
|
||||
printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests);
|
||||
|
||||
// Print logs
|
||||
for (int i = CMD_LOG_BUFFER-1; i >= 0; i--) {
|
||||
|
@ -284,6 +317,24 @@ void* draw(void* arg) {
|
|||
}
|
||||
}
|
||||
|
||||
// Read commands from screen
|
||||
void* commands(void* arg) {
|
||||
// Cast server socket descriptor
|
||||
int server_socket = *(int*)arg;
|
||||
delete (int*)arg;
|
||||
|
||||
while (true) {
|
||||
std::string command;
|
||||
std::cin >> command;
|
||||
|
||||
if (command == "quit" || command == "exit" || command == "q") {
|
||||
endwin();
|
||||
close(server_socket);
|
||||
kill(0, 15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_ncurses();
|
||||
init_log_pipe();
|
||||
|
@ -292,13 +343,23 @@ int main() {
|
|||
pthread_t tid;
|
||||
if (pthread_create(&tid, nullptr, &draw, nullptr) != 0) {
|
||||
std::cout << ("Failed to create drawing thread") << std::endl;
|
||||
// endwin();
|
||||
endwin();
|
||||
return 2;
|
||||
}
|
||||
pthread_detach(tid);
|
||||
|
||||
|
||||
int server_socket = start_socket();
|
||||
|
||||
// Create thread to read commands
|
||||
pthread_t tcid;
|
||||
int* server_socket_copy = new int(server_socket);
|
||||
if (pthread_create(&tcid, nullptr, &commands, server_socket_copy) != 0) {
|
||||
std::cout << ("Failed to create commands thread") << std::endl;
|
||||
endwin();
|
||||
return 2;
|
||||
}
|
||||
pthread_detach(tcid);
|
||||
|
||||
while (true) {
|
||||
int client_socket = accept(server_socket, nullptr, nullptr);
|
||||
|
||||
|
|
BIN
server2/server
BIN
server2/server
Binary file not shown.
|
@ -17,6 +17,8 @@
|
|||
#include <cstdio>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
|
||||
/* Configuration */
|
||||
|
@ -51,9 +53,6 @@ int active_connections = 0;
|
|||
// Total amount of handled requests
|
||||
int handndled_requests = 0;
|
||||
|
||||
// Amount of active timers for sending messages
|
||||
int active_timer_threads = 0;
|
||||
|
||||
|
||||
// Initializes ncurses lib
|
||||
void init_ncurses() {
|
||||
|
@ -140,12 +139,43 @@ std::string get_mem_percent() {
|
|||
return "Physical memory: " + std::to_string(100 * (total_mem - free_mem)/total_mem) + "%\n";
|
||||
}
|
||||
|
||||
|
||||
std::string get_vmem() {
|
||||
std::ifstream meminfo("/proc/self/status");
|
||||
if(!meminfo) {
|
||||
return "Error: failed to open /proc/meminfo\n";
|
||||
}
|
||||
|
||||
std::string line;
|
||||
long not_free_mem = -1, total_mem = -1;
|
||||
|
||||
while((std::getline(meminfo, line))) {
|
||||
if(line.find("VmRSS:") == 0) {
|
||||
std::sscanf(line.c_str(), "VmRSS: %ld kB", ¬_free_mem);
|
||||
} else if(line.find("VmSize:") == 0) {
|
||||
std::sscanf(line.c_str(), "VmSize: %ld kB", &total_mem);
|
||||
}
|
||||
|
||||
if(not_free_mem != -1 && total_mem != -1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
meminfo.close();
|
||||
|
||||
if(total_mem == -1 || not_free_mem == -1) {
|
||||
return "Error: failed to read info from /proc/meminfo\n";
|
||||
}
|
||||
|
||||
return "Virtual memory of the proccess: " + std::to_string(100.0 * not_free_mem/total_mem) + "%\n";
|
||||
}
|
||||
|
||||
// Returns message to send according to the server type
|
||||
std::string get_sysinfo() {
|
||||
if (SERVER_TYPE == "server1") {
|
||||
return '\t' + get_last_error() + "\t\t\t" + get_cursor_position();
|
||||
}
|
||||
return '\t' + get_mem_percent() + "\t\t\t" + "WIP\n"; // TODO: vmem
|
||||
return '\t' + get_mem_percent() + "\t\t\t" + get_vmem(); // TODO: vmem
|
||||
}
|
||||
|
||||
// Writes log message on screen and passes it to the logging pipe
|
||||
|
@ -247,6 +277,7 @@ void* handle_connection(void* arg) {
|
|||
std::string msg = timestamp + info;
|
||||
send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0);
|
||||
log("Sent new info to client " + std::to_string(client_socket));
|
||||
handndled_requests++;
|
||||
} else {
|
||||
log("Actual info already on client " + std::to_string(client_socket));
|
||||
}
|
||||
|
@ -263,6 +294,8 @@ void* draw(void* arg) {
|
|||
// Update info
|
||||
clear();
|
||||
|
||||
printw("Use q + Enter to quit\n\n");
|
||||
|
||||
// Print configuration
|
||||
printw(
|
||||
"LOG_FIFO_NAME=%s\nSERVER_TYPE=%s\nPORT=%i\nMAX_CONNECTIONS=%i\nCMD_LOG_BUFFER=%i\nCMD_REFRESH_TIME=%i\n\n",
|
||||
|
@ -270,7 +303,7 @@ void* draw(void* arg) {
|
|||
);
|
||||
|
||||
// Print statistics
|
||||
printw("Active connections: %i\nHandled messages: %i\nActive timers: %i\n", active_connections, handndled_requests, active_timer_threads);
|
||||
printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests);
|
||||
|
||||
// Print logs
|
||||
for (int i = CMD_LOG_BUFFER-1; i >= 0; i--) {
|
||||
|
@ -284,6 +317,24 @@ void* draw(void* arg) {
|
|||
}
|
||||
}
|
||||
|
||||
// Read commands from screen
|
||||
void* commands(void* arg) {
|
||||
// Cast server socket descriptor
|
||||
int server_socket = *(int*)arg;
|
||||
delete (int*)arg;
|
||||
|
||||
while (true) {
|
||||
std::string command;
|
||||
std::cin >> command;
|
||||
|
||||
if (command == "quit" || command == "exit" || command == "q") {
|
||||
endwin();
|
||||
close(server_socket);
|
||||
kill(0, 15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
init_ncurses();
|
||||
init_log_pipe();
|
||||
|
@ -292,13 +343,23 @@ int main() {
|
|||
pthread_t tid;
|
||||
if (pthread_create(&tid, nullptr, &draw, nullptr) != 0) {
|
||||
std::cout << ("Failed to create drawing thread") << std::endl;
|
||||
// endwin();
|
||||
endwin();
|
||||
return 2;
|
||||
}
|
||||
pthread_detach(tid);
|
||||
|
||||
|
||||
int server_socket = start_socket();
|
||||
|
||||
// Create thread to read commands
|
||||
pthread_t tcid;
|
||||
int* server_socket_copy = new int(server_socket);
|
||||
if (pthread_create(&tcid, nullptr, &commands, server_socket_copy) != 0) {
|
||||
std::cout << ("Failed to create commands thread") << std::endl;
|
||||
endwin();
|
||||
return 2;
|
||||
}
|
||||
pthread_detach(tcid);
|
||||
|
||||
while (true) {
|
||||
int client_socket = accept(server_socket, nullptr, nullptr);
|
||||
|
||||
|
|
Loading…
Reference in New Issue