Split accumulation, trace-presentation and ncurses-presentation more neatly

Still more work to be done there :)
This commit is contained in:
Arnout Engelen
2013-05-12 14:42:02 +00:00
parent a84b62a802
commit cf7c07847b

136
cui.cpp
View File

@@ -66,6 +66,7 @@ public:
} }
void show (int row, unsigned int proglen); void show (int row, unsigned int proglen);
void log ();
double sent_value; double sent_value;
double recv_value; double recv_value;
@@ -113,12 +114,6 @@ void Line::show (int row, unsigned int proglen)
// https://sourceforge.net/tracker/?func=detail&aid=3459408&group_id=110349&atid=656353 // https://sourceforge.net/tracker/?func=detail&aid=3459408&group_id=110349&atid=656353
//assert (m_pid <= 100000); //assert (m_pid <= 100000);
if (DEBUG || tracemode)
{
std::cout << m_name << '/' << m_pid << '/' << m_uid << "\t" << sent_value << "\t" << recv_value << std::endl;
return;
}
if (m_pid == 0) if (m_pid == 0)
mvprintw (row, 0, "?"); mvprintw (row, 0, "?");
else else
@@ -157,6 +152,10 @@ void Line::show (int row, unsigned int proglen)
} }
} }
void Line::log() {
std::cout << m_name << '/' << m_pid << '/' << m_uid << "\t" << sent_value << "\t" << recv_value << std::endl;
}
int GreatestFirst (const void * ma, const void * mb) int GreatestFirst (const void * ma, const void * mb)
{ {
Line ** pa = (Line **)ma; Line ** pa = (Line **)ma;
@@ -335,14 +334,36 @@ void gettotalb(Process * curproc, float * recvd, float * sent)
*recvd = sum_recv; *recvd = sum_recv;
} }
// Display all processes and relevant network traffic using show function void show_trace(Line * lines[], int nproc) {
void do_refresh() std::cout << "\nRefreshing:\n";
{
/* print them */
for (int i=0; i<nproc; i++)
{
lines[i]->log();
delete lines[i];
}
/* print the 'unknown' connections, for debugging */
ConnList * curr_unknownconn = unknowntcp->connections;
while (curr_unknownconn != NULL) {
std::cout << "Unknown connection: " <<
curr_unknownconn->getVal()->refpacket->gethashstring() << std::endl;
curr_unknownconn = curr_unknownconn->getNext();
}
}
void show_ncurses(Line * lines[], int nproc) {
int rows; // number of terminal rows int rows; // number of terminal rows
int cols; // number of terminal columns int cols; // number of terminal columns
unsigned int proglen; // max length of the "PROGRAM" column unsigned int proglen; // max length of the "PROGRAM" column
double sent_global = 0;
double recv_global = 0;
getmaxyx(stdscr, rows, cols); /* find the boundaries of the screeen */ getmaxyx(stdscr, rows, cols); /* find the boundaries of the screeen */
if (cols < 60) { if (cols < 60) {
clear(); clear();
mvprintw(0,0, "The terminal is too narrow! Please make it wider.\nI'll wait..."); mvprintw(0,0, "The terminal is too narrow! Please make it wider.\nI'll wait...");
@@ -353,28 +374,53 @@ void do_refresh()
proglen = cols - 53; proglen = cols - 53;
clear();
mvprintw (0, 0, "%s", caption->c_str());
attron(A_REVERSE);
mvprintw (2, 0, " PID USER %-*.*s DEV SENT RECEIVED ", proglen, proglen, "PROGRAM");
attroff(A_REVERSE);
/* print them */
int i;
for (i=0; i<nproc; i++)
{
if (i+3 < rows)
lines[i]->show(i+3, proglen);
recv_global += lines[i]->recv_value;
sent_global += lines[i]->sent_value;
delete lines[i];
}
attron(A_REVERSE);
int totalrow = std::min(rows-1, 3+1+i);
mvprintw (totalrow, 0, " TOTAL %-*.*s %10.3f %10.3f ", proglen, proglen, " ", sent_global, recv_global);
if (viewMode == VIEWMODE_KBPS)
{
mvprintw (3+1+i, cols - 7, "KB/sec ");
} else if (viewMode == VIEWMODE_TOTAL_B) {
mvprintw (3+1+i, cols - 7, "B ");
} else if (viewMode == VIEWMODE_TOTAL_KB) {
mvprintw (3+1+i, cols - 7, "KB ");
} else if (viewMode == VIEWMODE_TOTAL_MB) {
mvprintw (3+1+i, cols - 7, "MB ");
}
attroff(A_REVERSE);
mvprintw (totalrow+1, 0, "");
refresh();
}
// Display all processes and relevant network traffic using show function
void do_refresh()
{
refreshconninode(); refreshconninode();
refreshcount++; refreshcount++;
if (DEBUG || tracemode)
{
std::cout << "\nRefreshing:\n";
}
else
{
clear();
mvprintw (0, 0, "%s", caption->c_str());
attron(A_REVERSE);
mvprintw (2, 0, " PID USER %-*.*s DEV SENT RECEIVED ", proglen, proglen, "PROGRAM");
attroff(A_REVERSE);
}
ProcList * curproc = processes; ProcList * curproc = processes;
ProcList * previousproc = NULL; ProcList * previousproc = NULL;
int nproc = processes->size(); int nproc = processes->size();
/* initialise to null pointers */ /* initialise to null pointers */
Line * lines [nproc]; Line * lines [nproc];
int n = 0, i = 0; int n = 0;
double sent_global = 0;
double recv_global = 0;
#ifndef NDEBUG #ifndef NDEBUG
// initialise to null pointers // initialise to null pointers
@@ -473,44 +519,10 @@ void do_refresh()
/* sort the accumulated lines */ /* sort the accumulated lines */
qsort (lines, nproc, sizeof(Line *), GreatestFirst); qsort (lines, nproc, sizeof(Line *), GreatestFirst);
/* print them */ if (tracemode || DEBUG)
for (i=0; i<nproc; i++) show_trace(lines, nproc);
{ else
if (i+3 < rows) show_ncurses(lines, nproc);
lines[i]->show(i+3, proglen);
recv_global += lines[i]->recv_value;
sent_global += lines[i]->sent_value;
delete lines[i];
}
if (tracemode || DEBUG) {
/* print the 'unknown' connections, for debugging */
ConnList * curr_unknownconn = unknowntcp->connections;
while (curr_unknownconn != NULL) {
std::cout << "Unknown connection: " <<
curr_unknownconn->getVal()->refpacket->gethashstring() << std::endl;
curr_unknownconn = curr_unknownconn->getNext();
}
}
if ((!tracemode) && (!DEBUG)){
attron(A_REVERSE);
int totalrow = std::min(rows-1, 3+1+i);
mvprintw (totalrow, 0, " TOTAL %-*.*s %10.3f %10.3f ", proglen, proglen, " ", sent_global, recv_global);
if (viewMode == VIEWMODE_KBPS)
{
mvprintw (3+1+i, cols - 7, "KB/sec ");
} else if (viewMode == VIEWMODE_TOTAL_B) {
mvprintw (3+1+i, cols - 7, "B ");
} else if (viewMode == VIEWMODE_TOTAL_KB) {
mvprintw (3+1+i, cols - 7, "KB ");
} else if (viewMode == VIEWMODE_TOTAL_MB) {
mvprintw (3+1+i, cols - 7, "MB ");
}
attroff(A_REVERSE);
mvprintw (totalrow+1, 0, "");
refresh();
}
if (refreshlimit != 0 && refreshcount >= refreshlimit) if (refreshlimit != 0 && refreshcount >= refreshlimit)
quit_cb(0); quit_cb(0);