comparison pingstats.c @ 3:5a977ccbc7a9 default tip

Empty changelog
author darius
date Sat, 06 Dec 1997 05:41:29 +0000
parents
children
comparison
equal deleted inserted replaced
2:fba0b6e6cdc7 3:5a977ccbc7a9
1 /* $Id: pingstats.c,v 1.1.1.1 1997/12/06 05:41:29 darius Exp $ */
2
3 /*
4 * pingstats.c (mostly taken from stats.c)
5 */
6 #include "copyright.h"
7
8 #include <stdio.h>
9 #include <math.h>
10 #include "Wlib.h"
11 #include "defs.h"
12 #include "struct.h"
13 #include "data.h"
14 #include "proto.h"
15
16 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
17
18 #define BX_OFF() ((textWidth + 1) * W_Textwidth + S_IBORDER)
19 #define BY_OFF(line) ((line) * (W_Textheight + S_IBORDER) + S_IBORDER)
20 #define TX_OFF(len) ((textWidth - len) * W_Textwidth + S_IBORDER)
21 #define TY_OFF(line) BY_OFF(line)
22
23 /* right side labels */
24 #define TEXT_WIDTH (5*W_Textwidth + 2*STAT_BORDER)
25 #define STAT_WIDTH (260 + TEXT_WIDTH)
26 #define STAT_HEIGHT BY_OFF(NUM_SLIDERS)
27 #define STAT_BORDER 2
28 #define S_IBORDER 5
29 #define STAT_X 422
30 #define STAT_Y 13
31
32 #define SL_WID \
33 (STAT_WIDTH -TEXT_WIDTH - 2 * S_IBORDER - (textWidth + 1) * W_Textwidth)
34 #define SL_HEI (W_Textheight)
35
36 #define NUM_ELS(a) (sizeof (a) / sizeof (*(a)))
37 #define NUM_SLIDERS NUM_ELS(sliders)
38
39 typedef struct slider {
40 char *label;
41 int min, max;
42 int green, yellow;
43 int label_length;
44 int diff;
45 int *var;
46 int lastVal;
47 } SLIDER;
48
49 typedef struct record {
50 int *data;
51 int last_value;
52 } RECORD;
53
54 static SLIDER sliders[] = {
55 {"round trip time", 0, 500, 100, 200},
56 {"average r.t. time", 0, 500, 100, 200},
57 {"lag (st. dev.)", 0, 100, 20, 50},
58 {"%pack in loss", 0, 50, 10, 20},
59 {"%pack out loss", 0, 50, 10, 20},
60 {"tot %pack loss in", 0, 50, 5, 10},
61 {"tot %pack loss out", 0, 50, 5, 10},
62 };
63
64 static int textWidth = 0;
65 static int initialized = 0;
66
67 /* prototypes */
68 static void box P((int filled, int x, int y, int wid, int hei, W_Color color));
69 static void text P((int value, int y));
70
71 /* externals from ping.c (didn't feel like cluttering up data.c with them) */
72 extern int ping_iloss_sc; /* inc % loss 0--100, server to client */
73 extern int ping_iloss_cs; /* inc % loss 0--100, client to server */
74 extern int ping_tloss_sc; /* total % loss 0--100, server to client */
75 extern int ping_tloss_cs; /* total % loss 0--100, client to server */
76 extern int ping_lag; /* delay in ms of last ping */
77 extern int ping_av; /* average rt */
78 extern int ping_sd; /* standard deviation */
79
80 int
81 pStatsHeight()
82 {
83 return STAT_HEIGHT;
84 }
85
86 int
87 pStatsWidth()
88 {
89 return STAT_WIDTH;
90 }
91
92 void
93 initPStats()
94 {
95 int i;
96
97 if (initialized)
98 return;
99 initialized = 1;
100 sliders[0].var = (int *) &ping_lag;
101 sliders[1].var = (int *) &ping_av;
102 sliders[2].var = (int *) &ping_sd;
103 sliders[3].var = (int *) &ping_iloss_sc;
104 sliders[4].var = (int *) &ping_iloss_cs;
105 sliders[5].var = (int *) &ping_tloss_sc;
106 sliders[6].var = (int *) &ping_tloss_cs;
107
108 /* adjust */
109 if (ping_av > 0) {
110 sliders[0].max = MAX(ping_av * 2, 200);
111 sliders[1].max = MAX(ping_av * 2, 200);
112 }
113 for (i = 0; i < NUM_SLIDERS; i++) {
114 sliders[i].label_length = strlen(sliders[i].label);
115 textWidth = MAX(textWidth, sliders[i].label_length);
116 sliders[i].diff = sliders[i].max - sliders[i].min;
117 sliders[i].lastVal = 0;
118 }
119 }
120
121 void
122 redrawPStats()
123 {
124 int i;
125
126 W_ClearWindow(pStats);
127 initPStats();
128 for (i = 0; i < NUM_SLIDERS; i++) {
129 sliders[i].lastVal = 0;
130 }
131 for (i = 0; i < NUM_SLIDERS; i++) {
132 W_WriteText(pStats, TX_OFF(sliders[i].label_length), TY_OFF(i),
133 textColor, sliders[i].label, sliders[i].label_length,
134 W_RegularFont);
135 box(0, BX_OFF() - 1, BY_OFF(i) - 1, SL_WID + 2, SL_HEI + 2, borderColor);
136 sliders[i].lastVal = 0;
137 }
138 }
139
140 void
141 updatePStats()
142 {
143 int i, value, diff, old_x, new_x;
144 W_Color color;
145 SLIDER *s;
146
147 /* do the average and standard deviation calculations */
148 initPStats();
149
150 for (i = 0; i < NUM_SLIDERS; i++) {
151 s = &sliders[i];
152 value = *(s->var);
153 /* update decimal values at the right */
154 text(*(s->var), BY_OFF(i));
155
156 if (value < s->min)
157 value = s->min;
158 else if (value > s->max)
159 value = s->max;
160 if (value == s->lastVal)
161 continue;
162 diff = value - s->lastVal;
163 if (diff < 0) { /* bar decreasing */
164 old_x = s->lastVal * SL_WID / s->diff;
165 new_x = value * SL_WID / s->diff;
166 box(1, BX_OFF() + new_x, BY_OFF(i), old_x - new_x, SL_HEI, backColor);
167
168 if (s->lastVal > s->green && value <= s->green)
169 box(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, gColor);
170 else if (s->lastVal > s->yellow && value <= s->yellow)
171 box(1, BX_OFF(), BY_OFF(i), new_x, SL_HEI, yColor);
172 } else { /* bar increasing */
173 if (s->lastVal <= s->yellow && value > s->yellow) {
174 color = rColor;
175 s->lastVal = 0;
176 } else if (s->lastVal <= s->green && value > s->green) {
177 color = yColor;
178 s->lastVal = 0;
179 } else if (value > s->yellow)
180 color = rColor;
181 else if (value > s->green)
182 color = yColor;
183 else
184 color = gColor;
185
186 old_x = s->lastVal * SL_WID / s->diff;
187 new_x = value * SL_WID / s->diff;
188 box(1, BX_OFF() + old_x, BY_OFF(i), new_x - old_x, SL_HEI, color);
189 }
190 s->lastVal = value;
191 }
192 }
193
194 static
195 void
196 box(filled, x, y, wid, hei, color)
197 int filled, x, y, wid, hei;
198 W_Color color;
199 {
200 if (wid == 0)
201 return;
202
203 if (filled) {
204 /* XFIX */
205 W_FillArea(pStats, x, y, wid + 1, hei + 1, color);
206 return;
207 }
208 W_MakeLine(pStats, x, y, x + wid, y, color);
209 W_MakeLine(pStats, x + wid, y, x + wid, y + hei, color);
210 W_MakeLine(pStats, x + wid, y + hei, x, y + hei, color);
211 W_MakeLine(pStats, x, y + hei, x, y, color);
212 }
213
214 static
215 void
216 text(value, y)
217 int value, y;
218 {
219 char buf[6];
220 sprintf(buf, "(%3d)", value); /* fix */
221
222 W_WriteText(pStats, STAT_WIDTH - TEXT_WIDTH, y, textColor,
223 buf, 5, W_RegularFont);
224 }