[bwm-tools-devel] COMMIT - r40 - in branch: . bwm_monitor-treeview/bwm_monitor bwm_monitor-treeview/
Brought to you by:
nkukard
From: SVN C. <sv...@li...> - 2005-01-12 18:38:14
|
Author: nkukard Date: 2005-01-12 20:38:02 +0200 (Wed, 12 Jan 2005) New Revision: 40 Added: branch/bwm_monitor-treeview/ Modified: branch/bwm_monitor-treeview/bwm_monitor/bwm_monitor.c branch/bwm_monitor-treeview/bwmd/flowControl.c branch/bwm_monitor-treeview/include/flowControl.h Log: * Created branch to develop bwm_monitor treeview Copied: branch/bwm_monitor-treeview (from rev 39, trunk) Modified: branch/bwm_monitor-treeview/bwm_monitor/bwm_monitor.c =================================================================== --- trunk/bwm_monitor/bwm_monitor.c 2005-01-12 18:05:22 UTC (rev 39) +++ branch/bwm_monitor-treeview/bwm_monitor/bwm_monitor.c 2005-01-12 18:38:02 UTC (rev 40) @@ -43,8 +43,35 @@ #define PORT 9034 +#define NODE_STYLE 0x01 +#define NODE_EXPAND 0x02 +typedef struct node_t { + char *name; + char flags; + // relation fields + struct node_t *prio; + struct node_t *next; + struct node_t *parent; + struct node_t *childs; +} node_t; +#define NODE struct node_t +typedef struct tree_t { + WINDOW *window; + NODE *nodes; + NODE *topnode; + NODE *selnode; + short x,y,c,l; +} tree_t; +#define TREE struct tree_t +TREE *new_tree(int l, int c, int y, int x); +NODE *new_node(TREE *tree, char *name, char *parent, char flag); +void draw_tree(TREE *tree, NODE *start); +NODE *last_node(NODE *node); +//void dump_tree(NODE *start, char level); + + static void finish(int sig); @@ -86,7 +113,7 @@ read_fds = master_fds; - result = select(fdmax + 1, &read_fds, NULL, NULL, &timeout); + result = select(fdmax + 1, &read_fds, NULL, NULL, &timeout); // Check select() result if (result > 0) @@ -211,7 +238,9 @@ MENU *m_main; WINDOW *w_menu = NULL; ITEM **menu_items; - int mrows, mcols; + TREE *flowTree; + NODE *flowNode; + int mrows, mcols, line=1; int c; char *menuItem; int done = 0; @@ -219,7 +248,7 @@ int mySock; struct sockaddr_in serverAddr; // Server address struct ipc_packet_t myPacket; - int nbytes; + int nbytes,x,y; signal(SIGINT, finish); @@ -312,11 +341,9 @@ // Set our main screen wbkgdset(w_main, COLOR_PAIR(2) | ' '); fill_window(w_main); - windowTitle(w_main,3,2,0,2,"BWM Monitor v"PACKAGE_VERSION" - Copyright (c) 2003-2005 Linux Based Systems Design"); + windowTitle(w_main,3,2,0,2,"BWM Monitor v"PACKAGE_VERSION" - Copyright (c) 2003-2004 Linux Based Systems Design"); refresh(); - - - + // Send a LIST packet away to get a flow list myPacket.pktType = IPC_PACKET_LIST_FLOWS; @@ -325,10 +352,15 @@ fprintf(stderr,"bwm_monitor: Failed to write to socket: %s\n",strerror(errno)); exit(1); } - - c = 0; + + getmaxyx(w_main, y, x); + flowTree = new_tree(y-2, x/2, 1, x/4); +// if (flowTree && flowTree->window) +// keypad(flowTree->window,TRUE); + +// c = 0; // Allocate memory for menu items - menu_items = (ITEM **) malloc((numFlows + 1) * sizeof(ITEM *)); +// menu_items = (ITEM **) malloc((numFlows + 1) * sizeof(ITEM *)); do { // Grab pavket @@ -338,13 +370,78 @@ if (myPacket.pktType == IPC_PACKET_LIST_ITEM) { // And add memory item - menu_items[c] = new_item(strdup(myPacket.u.statusData.flowName),""); - c++; +// menu_items[c] = new_item(strdup(myPacket.u.statusData.flowName),""); +// c++; + flowNode = new_node(flowTree, myPacket.u.statusData.flowName, + myPacket.u.statusData.parentName, NODE_EXPAND); + if (flowNode != NULL) + flowTree->selnode = flowNode; } } } while (myPacket.pktType == IPC_PACKET_LIST_ITEM); + flowTree->selnode = flowTree->nodes; + draw_tree(flowTree, flowTree->selnode); +// wrefresh(flowTree->window); +// dump_tree(flowTree->nodes, 0); + while (!done) + { +// if (flowTree->window) +// keypad(flowTree->window,TRUE); + switch (getch()) + { + case 'q': + done = 1; + break; + case KEY_UP: + if (flowTree->selnode->prio) + { + flowTree->selnode = flowTree->selnode->prio; + while (flowTree->selnode->flags&NODE_EXPAND && + flowTree->selnode->childs) + flowTree->selnode = last_node(flowTree->selnode->childs); + } + else if (flowTree->selnode->parent) + { + flowTree->selnode = flowTree->selnode->parent; + } + draw_tree(flowTree, flowTree->selnode); + /* we dont need colorful, do we? */ + /* + wmove(flowTree->window, 0, 1); + waddch(flowTree->window, '('); + waddch(flowTree->window, ACS_UARROW); + waddch(flowTree->window, ')'); + wrefresh(flowTree->window); + */ + break; + case KEY_DOWN: + if (flowTree->selnode->flags&NODE_EXPAND && + flowTree->selnode->childs) + flowTree->selnode = flowTree->selnode->childs; + else if (flowTree->selnode->next) + flowTree->selnode = flowTree->selnode->next; + else if (flowTree->selnode->parent) + { + NODE *p = flowTree->selnode->parent; + while (p && !p->next) + p = p->parent; + if (p) flowTree->selnode = p->next; + } + draw_tree(flowTree, flowTree->selnode); + /* + wmove(flowTree->window, 0, 1); + waddch(flowTree->window, '('); + waddch(flowTree->window, ACS_DARROW); + waddch(flowTree->window, ')'); + wrefresh(flowTree->window); + */ + break; + } + refresh(); + } +/* // Don't forget our quit item menu_items[numFlows] = new_item(strdup("Quit"),""); @@ -397,7 +494,7 @@ free_item(menu_items[c]); // free(menu_items); free_menu(m_main); - +*/ shutdown(mySock,SHUT_RDWR); close(mySock); @@ -414,3 +511,184 @@ exit(0); } + +/******************************************************************************/ +// create new tree +TREE *new_tree(int l, int c, int y, int x) +{ + TREE *tree = (TREE *) malloc (sizeof(TREE)); + if (tree != NULL) + { + tree->nodes = NULL; + tree->x = x; + tree->y = y; + tree->c = c; + tree->l = l; + if (has_colors()) + wbkgdset(tree->window, COLOR_PAIR(5) | ' '); + else + wbkgdset(tree->window, A_BOLD | ' '); + tree->window = newwin(tree->l, tree->c, tree->y, tree->x); + } + return tree; +} + +NODE *search_node(NODE *node, const char *name) +{ + if (node == NULL) + return NULL; + while (node && strcasecmp(node->name, name)!=0) + { + NODE *pnode = search_node(node->childs, name); + if ( pnode != NULL ) + return pnode; + node = node->next; + } + if (node && strcasecmp(node->name, name)==0) + return node; + return NULL; +} + +NODE *last_node(NODE *node) +{ + if (node == NULL) + return NULL; + while (node->next != NULL) + node = node->next; + return node; +} + +// create new tree node +NODE *new_node(TREE *tree, char *name, char *parent, char flag) +{ + NODE *node, *lnode=NULL, *pnode = NULL; + + if (!name || !strlen(name)) + return NULL; + + node = (NODE *) malloc(sizeof(NODE)); + if ( node != NULL ) + { + node->name = strdup(name); + if (!node->name) + { + free(node); + node = NULL; + } + else + { + if (parent && strlen(parent)) + { + NODE *p = tree->selnode ? tree->selnode : NULL; + while (p && strcasecmp(p->name, parent)!=0) + p = p->parent; + pnode = p ? p : search_node(tree->nodes, parent); + } + if (pnode!=NULL) + if (!pnode->childs) + pnode->childs = node; + else + lnode = last_node(pnode->childs); + else + lnode = last_node(tree->nodes); + node->next = NULL; + node->prio = lnode; + node->parent = pnode; + node->childs = NULL; + node->flags = flag; + if (lnode) + lnode->next = node; + if (!tree->nodes) + tree->nodes = node; + tree->selnode = node; + } + } + return node; +} + +void draw_tree(TREE *tree, NODE *start) +{ + int y, x, l, i, level=0; + NODE *p = start->parent; + + if (!tree->window) return; + setTextAttr(tree->window,WA_BOLD); + //wbkgdset(tree->window, COLOR_PAIR(5) | ' '); + fill_window(tree->window); + box(tree->window, 0, 0); + + // get start node level + while (p) { level++; p = p->parent; } + + getmaxyx(tree->window, y, x); + for (i=1; i<y-1 && start; i++) + { + wmove(tree->window, i, level*3 + 1); + if (start->parent) + { + NODE *p = start->parent; + l = level; + while (p && l>0) + { + if (p->next) + { + wmove(tree->window, i, (l-1)*3 + 1); + waddch(tree->window, ACS_VLINE); + } + p = p->parent; l--; + } + wmove(tree->window, i, level*3 + 1); + + if (!start->next) + waddch(tree->window, ACS_LLCORNER); + else + waddch(tree->window, ACS_LTEE); + } + else if (!start->next) + waddch(tree->window, ACS_LLCORNER); + else if (!start->prio) + waddch(tree->window, ACS_ULCORNER); + else + waddch(tree->window, ACS_LTEE); + waddch(tree->window, ACS_HLINE); + mvwprintw(tree->window, i, level*3 + 4, "%s", start->name); + if (start->flags&NODE_EXPAND && start->childs) + { + start = start->childs; level++; + } + else if (start->next) + start = start->next; + else if (start->parent) + { + while (start && !start->next) + { + start = start->parent; level--; + } + if (start && start->next) + start = start->next; + } + else start = NULL; + } + wrefresh(tree->window); +} + +#if 0 +void dump_tree(NODE *start, char level) +{ + while (start) + { + char i = 0; + FILE *debug = fopen("debug.txt", "a+"); + if (debug) + { + for (i = 0; i<level; i++) + fprintf(debug, "\t"); + fprintf(debug, "%s\n", start->name); + fclose(debug); + } + dump_tree(start->childs, level+1); + start = start->next; + } +} +#endif + Modified: branch/bwm_monitor-treeview/bwmd/flowControl.c =================================================================== --- trunk/bwmd/flowControl.c 2005-01-12 18:05:22 UTC (rev 39) +++ branch/bwm_monitor-treeview/bwmd/flowControl.c 2005-01-12 18:38:02 UTC (rev 40) @@ -207,6 +207,13 @@ // Construct the status packet and send it away strncpy(statusPacket.flowName,tmpFlow->flowName,MAX_REFLEN); + // If we have a parent, get its name + if (tmpFlow->parent) + strncpy(statusPacket.parentName, + tmpFlow->parent->flowName,MAX_REFLEN); + else // If not, strlen == 0 + memset(statusPacket.parentName,'\0',MAX_REFLEN); + // Setup all our counters statusPacket.maxQueueSize = tmpFlow->maxQueueSize; statusPacket.maxQueueLen = tmpFlow->maxQueueLen; statusPacket.maxRate = tmpFlow->maxRate; @@ -250,6 +257,8 @@ // Construct the status packet and send it away strncpy(statusPacket.flowName,tmpGroup->groupName,MAX_REFLEN); + // Groups don't have parents + memset(statusPacket.parentName,'\0',MAX_REFLEN); statusPacket.maxQueueSize = -1; statusPacket.maxQueueLen = -1; statusPacket.maxRate = -1; @@ -425,6 +434,12 @@ myPacket.pktType = IPC_PACKET_LIST_ITEM; strcpy(myPacket.u.statusData.flowName,tmpFlow->flowName); + // If we have a parent, get its name + if (tmpFlow->parent) + strncpy(myPacket.u.statusData.parentName, + tmpFlow->parent->flowName,MAX_REFLEN); + else // If not, strlen == 0 + memset(myPacket.u.statusData.parentName,'\0',MAX_REFLEN); sendall(tmpClient->fd, &myPacket, sizeof(struct ipc_packet_t)); flowItem = g_list_next(flowItem); } @@ -437,6 +452,8 @@ myPacket.pktType = IPC_PACKET_LIST_ITEM; strcpy(myPacket.u.statusData.flowName,tmpGroup->groupName); + // Groups don't have parents + memset(myPacket.u.statusData.parentName,'\0',MAX_REFLEN); sendall(tmpClient->fd, &myPacket, sizeof(struct ipc_packet_t)); groupItem = g_list_next(groupItem); } Modified: branch/bwm_monitor-treeview/include/flowControl.h =================================================================== --- trunk/include/flowControl.h 2005-01-12 18:05:22 UTC (rev 39) +++ branch/bwm_monitor-treeview/include/flowControl.h 2005-01-12 18:38:02 UTC (rev 40) @@ -71,6 +71,7 @@ struct status_packet_t { char flowName[MAX_REFLEN]; + char parentName[MAX_REFLEN]; long int maxQueueSize; long int maxQueueLen; long int maxRate; |