📄 draw.c
字号:
#ifndef lint#ifdef sccsstatic char sccsid[] = "@(#)draw.c 1.1 92/07/30 Copyr 1985 Sun Micro";#endif#endif/* * Copyright (c) 1985 by Sun Microsystems, Inc. */#include <stdio.h>#include <rpc/rpc.h>#include <suntool/sunview.h>#include <suntool/panel.h>#include <suntool/canvas.h>#include <rpcsvc/ether.h>#include "traffic.h"#define MAXINT 0x7fffffff#define MININT 0x80000000#define NSITES 1000int debug;/* * global variables */static struct etherhmem *(*dsthtablep)[MAXSPLIT][HASHSIZE];static struct etherhmem *(*dsthtable1p)[MAXSPLIT][HASHSIZE];static struct etherhmem *(*dstdeltahtablep)[MAXSPLIT][HASHSIZE];static struct etherhmem *(*srchtablep)[MAXSPLIT][HASHSIZE];static struct etherhmem *(*srchtable1p)[MAXSPLIT][HASHSIZE];static struct etherhmem *(*srcdeltahtablep)[MAXSPLIT][HASHSIZE];#define dsthtable (*dsthtablep)#define dsthtable1 (*dsthtable1p)#define dstdeltahtable (*dstdeltahtablep)#define srchtable (*srchtablep)#define srchtable1 (*srchtable1p)#define srcdeltahtable (*srcdeltahtablep)static unsigned long bucket[MAXSPLIT][NBUCKETS/2];static unsigned long bucket1[MAXSPLIT][NBUCKETS/2];static int prevbar[MAXSPLIT][MAXBARS];static int prevbar1[MAXSPLIT][MAXBARS];static unsigned long prototable[MAXSPLIT][NPROTOS];static unsigned long prototable1[MAXSPLIT][NPROTOS];static int deltacnt[MAXSPLIT];static struct timeval deltatime[MAXSPLIT];static int first[MAXSPLIT];static int lasttm[MAXSPLIT]; /* last time bar1 updated */static int oldbcast[MAXSPLIT];static int pos[MAXSPLIT];static int savepktcnt[MAXSPLIT];static int savepktcnt1[MAXSPLIT];static struct timeval savetime[MAXSPLIT];static struct timeval savetime1[MAXSPLIT];static struct rank rank[NSITES], rank1[NSITES];static struct etherstat etherstat;static struct etheraddrs ea;/* * procedure variables */int traf_compar();struct timeval timersub();/* * pixrects */static short traf_gray80_data[] = {#include <images/square_80.pr>};mpr_static(traf_gray80_patch, 15, 15, 1, traf_gray80_data);static short traf_gray25_data[] = {#include <images/square_25.pr>};mpr_static(traf_gray25_patch, 16, 16, 1, traf_gray25_data);struct pixrect *proof_pr1 = &traf_gray80_patch;struct pixrect *proof_pr = &traf_gray25_patch;/* malloc space for arrays */trf_allocate(){ dsthtablep = (struct etherhmem * (*)[MAXSPLIT][HASHSIZE]) malloc(MAXSPLIT*HASHSIZE*sizeof(char *)); dsthtable1p = (struct etherhmem * (*)[MAXSPLIT][HASHSIZE]) malloc(MAXSPLIT*HASHSIZE*sizeof(char *)); dstdeltahtablep = (struct etherhmem * (*)[MAXSPLIT][HASHSIZE]) malloc(MAXSPLIT*HASHSIZE*sizeof(char *)); srchtablep = (struct etherhmem * (*) [MAXSPLIT][HASHSIZE]) malloc(MAXSPLIT*HASHSIZE*sizeof(char *)); srchtable1p = (struct etherhmem * (*) [MAXSPLIT][HASHSIZE]) malloc(MAXSPLIT*HASHSIZE*sizeof(char *)); srcdeltahtablep = (struct etherhmem * (*) [MAXSPLIT][HASHSIZE]) malloc(MAXSPLIT*HASHSIZE*sizeof(char *));}draw(k){ int i, j; struct etherstat *ep; struct timeval tm; if (!running) return; ep = ðerstat; switch (mode[k]) { case DISPLAY_LOAD: { int wd, ht, diff, left, go, step; static struct timeval oldtime[MAXSPLIT]; static int oldht[MAXSPLIT]; static unsigned oldbytes[MAXSPLIT]; bzero(ðerstat, sizeof(etherstat)); if (mycallrpc(host, ETHERSTATPROG, ETHERSTATVERS, ETHERSTATPROC_GETDATA,xdr_void, 0, xdr_etherstat, ðerstat)) return; diff = 1000*(ep->e_time.tv_sec - oldtime[k].tv_sec); diff += (ep->e_time.tv_usec - oldtime[k].tv_usec)/1000; if (diff == 0) break; ht = (8*(ep->e_bytes - oldbytes[k]))/(10*diff); if (ht > 999) ht = 999; /* add 1 so won't rest on bottom of graph */ ht = TOPGAP + curht[k] - (ht*curht[k])/1000 - 1; wd = tswrect[k].r_width; if (first[k]) { go = 0; first[k] = 0; } else { go = 1; } step = (int)panel_get_value(speed_item[k]); if (pos[k] + step < wd) { if (go) pw_vector(pixwin[k], pos[k], oldht[k], pos[k] + step, ht, PIX_SRC, 1); } else if (pos[k] < wd) { pw_vector(pixwin[k], pos[k], oldht[k], wd-1, ht, PIX_SRC, 1); } else { left = LEFTMARGIN*marginfontwd; lock(k); pw_copy(pixwin[k], left, TOPGAP, wd, curht[k] + 1, PIX_SRC, pixwin[k], /*curht[k] + debug, PIX_SRC, pixwin[k], */ left + step, TOPGAP); pw_writebackground(pixwin[k], wd-step, TOPGAP, step, curht[k]+1, PIX_CLR); pw_vector(pixwin[k], wd-1-step, oldht[k], wd-1, ht, PIX_SRC, 1); if (gridon[k]) drawgridstub(k, step); unlock(k); } pos[k] += step; oldht[k] = ht; oldtime[k] = ep->e_time; oldbytes[k] = ep->e_bytes; break; } case DISPLAY_SIZE: bzero(ðerstat, sizeof(etherstat)); if (mycallrpc(host, ETHERSTATPROG, ETHERSTATVERS, ETHERSTATPROC_GETDATA,xdr_void, 0, xdr_etherstat, ðerstat)) return; for (i = 0; i < NBUCKETS/2; i++) bucket[k][i] = (ep->e_size[2*i] + ep->e_size[2*i+1]) - bucket[k][i]; gettimeofday(&tm, 0); if (tm.tv_sec > lasttm[k] + timeout1[k]) { for (i = 0; i < NBUCKETS/2; i++) bucket1[k][i] = ep->e_size[2*i] + ep->e_size[2*i+1] - bucket1[k][i]; lasttm[k] = tm.tv_sec; drawbars1(etherstat.e_packets-savepktcnt1[k], timersub(etherstat.e_time, savetime1[k]), k, bucket1[k], sizeof(bucket1[k][0]), NBUCKETS/2); savepktcnt1[k] = etherstat.e_packets; savetime1[k] = etherstat.e_time; for (i = 0; i < NBUCKETS/2; i++) bucket1[k][i] = ep->e_size[2*i] + ep->e_size[2*i+1]; } drawbars(etherstat.e_packets-savepktcnt[k], timersub(etherstat.e_time, savetime[k]), k, bucket[k], sizeof(bucket[k][0]), NBUCKETS/2); for (i = 0; i < NBUCKETS/2; i++) bucket[k][i] = ep->e_size[2*i] + ep->e_size[2*i+1]; savepktcnt[k] = etherstat.e_packets; savetime[k] = etherstat.e_time; break; case DISPLAY_PROTO: bzero(ðerstat, sizeof(etherstat)); if (mycallrpc(host, ETHERSTATPROG, ETHERSTATVERS, ETHERSTATPROC_GETDATA,xdr_void, 0, xdr_etherstat, ðerstat)) return; for (i = 0; i < NPROTOS; i++) prototable[k][i] = ep->e_proto[i] - prototable[k][i]; gettimeofday(&tm, 0); if (tm.tv_sec > lasttm[k] + timeout1[k]) { for (i = 0; i < NPROTOS; i++) prototable1[k][i] = ep->e_proto[i] - prototable1[k][i]; lasttm[k] = tm.tv_sec; drawbars1(etherstat.e_packets - savepktcnt1[k], timersub(etherstat.e_time, savetime1[k]), k, prototable1[k], sizeof(prototable1[k][0]), NPROTOS); savetime1[k] = etherstat.e_time; savepktcnt1[k] = etherstat.e_packets; for (i = 0; i < NPROTOS; i++) prototable1[k][i] = ep->e_proto[i]; } drawbars(etherstat.e_packets-savepktcnt[k], timersub(etherstat.e_time, savetime[k]), k, prototable[k], sizeof(prototable[k][0]), NPROTOS); for (i = 0; i < NPROTOS; i++) prototable[k][i] = ep->e_proto[i]; savepktcnt[k] = etherstat.e_packets; savetime[k] = etherstat.e_time; break; case DISPLAY_SRC: bzero(&ea, sizeof(ea)); if (mycallrpc(host, ETHERSTATPROG, ETHERSTATVERS, ETHERSTATPROC_GETSRCDATA,xdr_void, 0, xdr_etheraddrs, &ea)) return; j = loadrank(rank1, srchtable[k]); qsort(rank1, j, sizeof(rank1[0]), traf_compar); drawnames(k, rank, rank1); drawbars(ea.e_packets - savepktcnt[k], timersub(ea.e_time, savetime[k]), k, rank, sizeof(rank[0]), NUMNAMES); gettimeofday(&tm, 0); if (tm.tv_sec > lasttm[k] + timeout1[k]) { lasttm[k] = tm.tv_sec; loadrank1(rank, srchtable1[k], srcdeltahtable[k]); drawbars1(ea.e_packets - savepktcnt1[k], timersub(ea.e_time, savetime1[k]), k, rank, sizeof(rank[0]), NUMNAMES); deltacnt[k] = ea.e_packets - savepktcnt1[k]; deltatime[k] = timersub(ea.e_time, savetime1[k]); savepktcnt1[k] = ea.e_packets; savetime1[k] = ea.e_time; updatetable(srchtable1[k]); } else { loadfromdelta(rank, srcdeltahtable[k]); drawbars1(deltacnt[k], deltatime[k], k, rank, sizeof(rank[0]), NUMNAMES); } savepktcnt[k] = ea.e_packets; savetime[k] = ea.e_time; updatetable(srchtable[k]); freeargs(xdr_etheraddrs, &ea); break; case DISPLAY_DST: bzero(&ea, sizeof(ea)); if (mycallrpc(host, ETHERSTATPROG, ETHERSTATVERS, ETHERSTATPROC_GETDSTDATA,xdr_void, 0, xdr_etheraddrs, &ea)) return; j = loadrank(rank1, dsthtable[k]); rank1[j].addr = 0; rank1[j].cnt = ea.e_bcast - oldbcast[k]; j++; oldbcast[k] = ea.e_bcast; qsort(rank1, j, sizeof(rank1[0]), traf_compar); drawnames(k, rank, rank1); drawbars(ea.e_packets - savepktcnt[k], timersub(ea.e_time, savetime[k]), k, rank, sizeof(rank[0]), NUMNAMES); gettimeofday(&tm, 0); if (tm.tv_sec > lasttm[k] + timeout1[k]) { lasttm[k] = tm.tv_sec; loadrank1(rank, dsthtable1[k], dstdeltahtable[k]); drawbars1(ea.e_packets - savepktcnt1[k], timersub(ea.e_time, savetime1[k]), k, rank, sizeof(rank[0]), NUMNAMES); deltacnt[k] = ea.e_packets - savepktcnt1[k]; deltatime[k] = timersub(ea.e_time, savetime1[k]); savetime1[k] = ea.e_time; savepktcnt1[k] = ea.e_packets; updatetable(dsthtable1[k]); } else { loadfromdelta(rank, dstdeltahtable[k]); drawbars1(deltacnt[k], deltatime[k], k, rank, sizeof(rank[0]), NUMNAMES); } savepktcnt[k] = ea.e_packets; savetime[k] = ea.e_time; updatetable(dsthtable[k]); freeargs(xdr_etheraddrs, &ea); break; }}/* * divide graph into cnt equal parts, and imagine a dotted line * drawn to divide the parts. Then BETWEEN is space between * dotted line and leftmost bar, OVERLAP is space between leftbar * and right one, right edge of right bar is flush with dotted line. */#define BETWEEN 15#define OVERLAP 10/* * draws leftmost bar: assumes right bar has value * given by prevbar1 */drawbars(pkts, tm, k, p, offset, cnt) struct timeval tm; char p[];{ int ht, wd, i, j, j1, z, w, left; double time; left = LEFTMARGIN*marginfontwd; wd = (tswrect[k].r_width - left)/cnt; nopkts(pkts == 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -