home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / dnet / dnet2.3.2 / amiga / client / loadav.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  7.6 KB  |  352 lines

  1.  
  2. /*
  3.  *  LOADAV.C
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  LOADAV  [frequency]     Connect to remote UNIX system and display
  8.  *                load average.  Default is every 5 minutes.
  9.  *
  10.  *  frequency in seconds, default is every 60 seconds
  11.  *
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16.  
  17. #ifdef DEBUG
  18. #define IFDEBUG(foo)  foo
  19. #else
  20. #define IFDEBUG(foo)  
  21. #endif
  22.  
  23. #ifndef __GNUC__
  24. #include <local/deemu.h>
  25. #else
  26. #include "deemu.h"
  27. #endif
  28.  
  29. #ifndef __GNUC__
  30. int __stdargs CXBRK(void);
  31. #endif
  32.  
  33. short Deemu[] = {
  34.     DMSTRT, 0, 0,
  35.     DMNW  , 0, 10, 2, 2, -80, 40, 0xFFFF,
  36.     DMEND , 0, 0
  37. };
  38.  
  39. #define DMNWOFF 4
  40.  
  41. #define NA  0
  42. #define GWIDTH    (sizeof(Graph)/sizeof(Graph[0]))
  43. #define GDEPTH    (sizeof(GMap)/sizeof(GMap[0]))
  44. #define GMASK    (GWIDTH-1)
  45.  
  46. uword    GMax[] = { 2*256, 50 };
  47. uword    GIncr[]= { 2*256, 20 };
  48. uword    GMap[2] = { 0, 1 };
  49. uword    Graph[1024][GDEPTH];    /*  5Min,#users.       */
  50. uword    Gi;
  51.  
  52. ubyte    Scr[128];
  53. ubyte    Initial;
  54.  
  55. ubyte Title[128];
  56.  
  57. extern void updatewindow();
  58.  
  59. NW Nw = {
  60.     0, 0, 320, 50, -1, -1,
  61.     NEWSIZE|CLOSEWINDOW,
  62.     WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|NOCAREREFRESH,
  63.     NULL, NULL, Title, NULL, NULL,
  64.     32, 18, -1, -1, WBENCHSCREEN
  65. };
  66.  
  67. WIN *Win;
  68. RP  *Rp;
  69.  
  70. #ifndef __GNUC__
  71. extern int Enable_Abort;
  72. #endif
  73.  
  74. struct IntuitionBase *IntuitionBase;
  75. struct GfxBase *GfxBase;
  76.  
  77. void clearwindow ARGS((void));
  78. void updatewindow ARGS((char *));
  79. int main ARGS((int, char **));
  80.  
  81. #ifndef __GNUC__
  82. int
  83. brk()
  84. {
  85.     return(0);
  86. }
  87. #endif
  88.  
  89. int
  90. main(ac,av)
  91. char *av[];
  92. {
  93.     void *chan = NULL;
  94.     short numsecs = 60;
  95.     long imask, tmask, dmask, mask;
  96.     char notdone = 1;
  97.     char *host = NULL;
  98.     PORT *TimPort = CreatePort(NULL, 0);
  99.     IOT Iot;
  100.  
  101. #ifndef __GNUC__
  102.     onbreak(brk);
  103. #endif
  104.     sprintf(Title, "LoadAv V%s%s", VERSION, LOADAV_VERSION);
  105.     {
  106.     short i;
  107.     for (i = 1; i < ac; ++i) {
  108.         if (strncmp(av[i], "-N", 2) == 0) {
  109.         if(strlen(av[i]) > 2)
  110.             host = av[i]+2;
  111.         else
  112.             fprintf(stderr, "%s: host needed!\n");
  113.         continue;
  114.         }
  115.         numsecs = atoi(av[i]);
  116.     }
  117.     }
  118.  
  119.  
  120.     Iot.tr_node.io_Device = NULL;
  121.  
  122.     if (OpenDevice("timer.device", UNIT_VBLANK, (IOR *)&Iot, 0))
  123.     goto fail;
  124.     Iot.tr_node.io_Command = TR_ADDREQUEST;
  125.     Iot.tr_node.io_Message.mn_ReplyPort = TimPort;
  126.     Iot.tr_time.tv_micro = 1;
  127.     Iot.tr_time.tv_secs  = 0;
  128.     SendIO((IOR *)&Iot);
  129.  
  130. #if !defined( LATTICE) && !defined( __GNUC__)
  131.     Enable_Abort = 0;
  132. #endif
  133.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  134.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  135.  
  136.     chan = DOpen(host, PORT_LOADAV, 25, 25);
  137.     IFDEBUG(printf("DOpen() done\n"));
  138.     if (chan == NULL) {
  139.     puts("DOpen failed to connect!\n");
  140.     IFDEBUG(sleep(1);)
  141.     goto fail;
  142.     }
  143.     InitDeemuNW(Deemu+DMNWOFF, &Nw);
  144.     Win = OpenWindow(&Nw);
  145.     if (Win == NULL) {
  146.     puts("Unable to open window");
  147.     goto fail;
  148.     }
  149.     IFDEBUG(printf("Window opened...\n"));
  150.     Rp = Win->RPort;
  151.     imask   = 1 << Win->UserPort->mp_SigBit;
  152.     dmask   = 1 << ((PORT *)chan)->mp_SigBit;
  153.     tmask   = 1 << TimPort->mp_SigBit;
  154.  
  155.     IFDEBUG(printf("signal masks: i %d, d %d, t %d\n", imask, dmask, tmask));
  156.  
  157.  
  158.     clearwindow();
  159.     IFDEBUG(printf("window cleared...\n"));
  160.  
  161.     while (notdone) {
  162.     mask = Wait(imask|dmask|tmask|SIGBREAKF_CTRL_C);
  163.         IFDEBUG(printf("signal(s) gotten:%d\n", mask));
  164.     if (mask & SIGBREAKF_CTRL_C){
  165.         notdone = 0;
  166.     }
  167.     if (mask & imask) {
  168.         IMESS *im;
  169.         while (im = (IMESS *)GetMsg(Win->UserPort)) {
  170.         IFDEBUG(printf("UserPort message:%d\n", im->Class));
  171.         switch(im->Class) {
  172.         case NEWSIZE:
  173.             clearwindow();
  174.             break;
  175.         case CLOSEWINDOW:
  176.             notdone = 0;
  177.             break;
  178.         }
  179.         ReplyMsg((MSG *)im);
  180.         }
  181.     }
  182.     if (mask & dmask) {
  183.         char dummy;
  184.         if ((dummy = DNRead(chan, &dummy, 1)) != 0)
  185.         notdone = 0;
  186.         IFDEBUG(printf("DNRead resulted %d \n", dummy));
  187.     }
  188.     if (mask & tmask) {      
  189.         char len = 0;
  190.  
  191.         IFDEBUG(printf("tmask\n"));
  192.  
  193.         if (GetMsg(TimPort)) {
  194.         Iot.tr_time.tv_micro = 0;
  195.         Iot.tr_time.tv_secs  = numsecs;
  196.         SendIO((IOR *)&Iot);
  197.         if (DWrite(chan, &len, 1) == 1 && DRead(chan, &len, 1) == 1) {
  198.             if (len < sizeof(Title) && DRead(chan, Title, len) == len) {
  199.             Title[len] = 0;
  200.             IFDEBUG(printf("updatewindow() \n"));
  201.             updatewindow(Title);
  202.             IFDEBUG(printf("SettingWindowTitle(%s) \n", Title));
  203.             SetWindowTitles(Win, Title, (UBYTE *)-1);
  204.             }
  205.         } else {
  206.             notdone = 0;
  207.         }
  208.         }
  209.     }
  210.     }
  211.  
  212.  
  213. fail:
  214.     IFDEBUG(printf("Shutting things down...\n"));
  215.     if (Iot.tr_node.io_Device) {
  216.     AbortIO((IOR *)&Iot);
  217.     WaitIO((IOR *)&Iot);
  218.     CloseDevice((IOR *)&Iot);
  219.     }
  220.     DeletePort(TimPort);
  221.     IFDEBUG(printf("TimerPort deleted...\n"));
  222.     if (Win){
  223.     CloseWindow(Win);
  224.     IFDEBUG(printf("Window closed...\n"));
  225.     }
  226.     if (chan){
  227.     DClose(chan);
  228.     IFDEBUG(printf("channel DClosed()...\n"));
  229.     }
  230.     if (IntuitionBase)
  231.     CloseLibrary((LIB *)IntuitionBase);
  232.     if (GfxBase)
  233.     CloseLibrary((LIB *)GfxBase);
  234.     IFDEBUG(printf("libraries closed()...\n"));
  235. }
  236.  
  237. /*
  238.  *  Graphics routines.    ************************************************
  239.  */
  240.  
  241. short WOx, WOy, Ww, Wh;
  242.  
  243. void
  244. clearwindow()
  245. {
  246.     short i, j, d;
  247.  
  248.     WOx = Win->BorderLeft;
  249.     WOy = Win->BorderTop;
  250.     Ww    = Win->Width - Win->BorderRight - Win->BorderLeft;   /* drawaable width of window */
  251.     Wh    = Win->Height- Win->BorderTop    - Win->BorderBottom; /* drawable height of window */
  252.  
  253.     SetAPen(Rp, 0);   /* set pen to draw 'clear' */
  254.     RectFill(Rp, WOx, WOy, Ww + WOx, Wh + WOy);  /* fill rectangle */
  255.     IFDEBUG(printf("RectFill() to clear window done\n"));
  256.     WOx += 2; WOy += 2; Ww -= 4; Wh -= 4;   /* shrink in a few pixels for an internal border ? */
  257.  
  258.     if(Ww < 0) Ww = 0; 
  259.     if(Wh < 0) Wh = 0; 
  260.  
  261.     /*
  262.      *    Redraw the graph.  Scale values relative to GMax[?] and Wh.
  263.      *
  264.      *    ypos = (WOy + Wh) - (Wh * value / GMax[d])
  265.      *
  266.      */
  267.  
  268.     for (d = 0; d < GDEPTH; ++d) {
  269.     char move = 1;
  270.     SetAPen(Rp, (d & 1) ? 3 : 1);
  271.     for (i = Ww - 1, j = Gi - 1; i >= 0; --i, --j) {
  272.         uword value = Wh * Graph[j&GMASK][d] / GMax[GMap[d]];
  273.         (move) ?
  274.         Move(Rp, WOx + i, WOy + Wh - value - 1) :
  275.         Draw(Rp, WOx + i, WOy + Wh - value - 1)
  276.         ;
  277.         move = 0;
  278.     }
  279.     }
  280.     IFDEBUG(printf("clearwindow() done\n"));
  281. }
  282.  
  283. /*
  284.  *  5:44pm up 2 days, 22:30, 37 users, load average: 5.98, 7.93, 7.97
  285.  *              7 mins,
  286.  */
  287.  
  288. void
  289. updatewindow(str)
  290. char *str;
  291. {
  292.     short d;
  293.     char refresh = 0;
  294.     uword ary[GDEPTH];
  295.  
  296.     IFDEBUG(printf("starting updatewindow(%s)\n", str));
  297.  
  298.     {
  299.     long nusers, i1, f1, i5, f5;
  300.     char *ptr = str;
  301.  
  302.     while (strncmp(ptr, "users", 5) && *ptr)
  303.         ++ptr;
  304.     while (*--ptr == ' ' && ptr != str)
  305.         --ptr;
  306.     while (*--ptr != ' ' && ptr != str)
  307.         --ptr;
  308.     nusers = atoi(ptr+1);
  309.     while (strncmp(ptr, "load", 4) && *ptr)
  310.         ++ptr;
  311.     sscanf(ptr, "load average: %ld.%ld, %ld.%ld,",
  312.         &i1, &f1, &i5, &f5
  313.     );
  314.     IFDEBUG(printf("Got loads: %d users, load: %d.%d %d.%d\n", nusers , i1, f1, i5,f5));
  315.     /*
  316.     ary[0] = (i1 << 8) | ((f1 << 8) / 100);
  317.     */
  318.     ary[0] = (i5 << 8) | ((f5 << 8) / 100);
  319.     ary[1] = nusers;
  320.     }
  321.     IFDEBUG(printf("Drawing new load window\n"));
  322.     for (d = 0; d < GDEPTH; ++d) {
  323.     while (ary[d] > GMax[GMap[d]] && ary[d] < 65000) {
  324.         GMax[GMap[d]] += GIncr[GMap[d]];
  325.         refresh = 1;
  326.     }
  327.     Graph[Gi][d] = ary[d];
  328.     }
  329.     if (!Initial) {
  330.     short i;
  331.     Initial = 1;
  332.     for (i = 0; i < GWIDTH; ++i) {
  333.         for (d = 0; d < GDEPTH; ++d)
  334.         Graph[i][d] = Graph[Gi][d];
  335.     }
  336.     }
  337.     Gi = (Gi + 1) & GMASK;
  338.     if (refresh) {
  339.     clearwindow();
  340.     return;
  341.     }
  342.     ScrollRaster(Rp, 1, 0, WOx, WOy, WOx + Ww - 1, WOy + Wh - 1);
  343.     for (d = 0; d < GDEPTH; ++d) {
  344.     uword value1 = Wh * Graph[(Gi-2)&GMASK][d] / GMax[GMap[d]];
  345.     uword value2 = Wh * Graph[(Gi-1)&GMASK][d] / GMax[GMap[d]];
  346.     SetAPen(Rp, (d & 1) ? 3 : 1);
  347.     Move(Rp, Ww + WOx - 2, WOy + Wh - value1 - 1);
  348.     Draw(Rp, Ww + WOx - 1, WOy + Wh - value2 - 1);
  349.     }
  350. }
  351.  
  352.