]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - fwp/mngr/gui.c
Restored screen on manager exit
[frescor/fwp.git] / fwp / mngr / gui.c
1 #include "gui.h"
2 #include "fwp_foreach.h"
3 #include <ncurses.h>
4 #include <fwp_contract.h>
5 #include "fwp_admctrl.h"
6
7 void gui_init(void)
8 {
9         initscr();
10         cbreak();
11         noecho();
12 }
13
14 static char *num_with_unit(char *buf, unsigned num, const char *units[])
15 {
16         const int div[] = {100,10,1};
17         unsigned order = 0;
18         int d;
19
20         while (num > 999 && units[(order+3)/3] != NULL) {
21                 num /= 10;
22                 order++;
23         }
24
25         d = div[(order+2)%3];
26         if (d == 1)
27                 sprintf(buf, "%4d %s", num, units[(order+2)/3]);
28         else
29                 sprintf(buf, "%d.%.*d %s", num/d, (d==10) ? 1 : 2, num%d, units[(order+2)/3]);
30         return buf;
31 }
32
33 char *bandwidth_to_text(char *buf, unsigned bandwidth_bps)
34 {
35         const char *unit[] = {"bps", "kbps", "Mbps", NULL};
36         return num_with_unit(buf, bandwidth_bps, unit);
37 }
38
39 char *usec_to_text(char *buf, unsigned usec)
40 {
41         const char *unit[] = {"us", "ms", "s", NULL};
42         return num_with_unit(buf, usec, unit);
43 }
44
45 #define addfield(width, title, format, ...)                             \
46         move(y, x);                                                     \
47         x+=width;                                                       \
48         if (cd == NULL) addstr(title);                                  \
49         else {                                                          \
50                 snprintf(str, sizeof(str), format, __VA_ARGS__);        \
51                 addstr(str);                                            \
52         }
53
54 static void print_contract(int y, fwp_contract_data_t *cd)
55 {
56         const char *ac_ids[] = { [FWP_AC_VO]="VO", [FWP_AC_VI]="VI", [FWP_AC_BE]="BE", [FWP_AC_BK]="BK" };
57         struct fwp_contract *c = &cd->contract;
58         char str[200], s1[20];
59         int x = 0;
60         
61         addfield(4,  "ID", "%d", cd->id);
62         addfield(11, "BW", "%s", bandwidth_to_text(s1, (long long)1000*1000*8*c->budget/c->period_usec));
63         addfield(11, "Budget", "%d bytes", c->budget);
64         addfield(11, "Period", "%s", usec_to_text(s1, c->period_usec));
65         addfield(11, "Deadline", "%s", usec_to_text(s1, c->deadline_usec));
66         addfield(3,  "AC", "%s", ac_ids[cd->vres_params.ac_id]);
67 }
68
69 void gui_print_status()
70 {
71         fwp_contract_data_t *cd;
72         struct foreach_contract fec;
73         int y;
74         char str[200];
75
76         clear();                /* Repaint the screen completely to
77                                  * delete error messages */
78         mvaddstr(0, 0, "FWP Manager");
79         sprintf(str, "Reserved utilization: %d%%", fwp_reserved_utilization/100);
80         mvaddstr(2, 0, str);
81
82         /* Print header */
83         y=4;
84         print_contract(y, NULL);
85         y++;
86
87         /* Print contracts */
88         fec.contdata_new = NULL;
89         for (cd=foreach_contract_begin(&fec);
90              cd;
91              cd=foreach_contract_next(&fec)) {
92                 print_contract(y, cd);
93                 y++;
94         }
95         refresh();
96 }
97
98 void gui_end(void)
99 {
100         endwin();
101 }