home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / DCPSTATS.C < prev    next >
C/C++ Source or Header  |  1993-09-20  |  8KB  |  228 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    File transfer information for UUPC/extended                     */
  3. /*                                                                    */
  4. /*    Copyright (c) 1991, Andrew H. Derbyshire                        */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*
  16.  *       $Id: dcpstats.c 1.6 1993/09/20 04:41:54 ahd Exp $
  17.  *
  18.  *       $Log: dcpstats.c $
  19.  * Revision 1.6  1993/09/20  04:41:54  ahd
  20.  * OS/2 2.x support
  21.  *
  22.  * Revision 1.5  1993/04/11  00:34:11  ahd
  23.  * Global edits for year, TEXT, etc.
  24.  *
  25.  * Revision 1.4  1993/03/06  23:04:54  ahd
  26.  * Lock host status file before updating
  27.  *
  28.  * Revision 1.3  1992/12/30  13:17:12  ahd
  29.  * Windows/NT changes
  30.  *
  31.  */
  32.  
  33. /*--------------------------------------------------------------------*/
  34. /*                       standard include files                       */
  35. /*--------------------------------------------------------------------*/
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <time.h>
  40. #include <stdlib.h>
  41.  
  42. /*--------------------------------------------------------------------*/
  43. /*                    UUPC/extended include files                     */
  44. /*--------------------------------------------------------------------*/
  45.  
  46. #include "lib.h"
  47. #include "dcp.h"
  48. #include "dcpstats.h"
  49. #include "stater.h"
  50. #include "hlib.h"
  51. #include "hostable.h"
  52. #include "hostatus.h"
  53. #include "security.h"
  54. #include "timestmp.h"
  55. #include "ssleep.h"
  56. #include "lock.h"
  57.  
  58. /*--------------------------------------------------------------------*/
  59. /*                          Global variables                          */
  60. /*--------------------------------------------------------------------*/
  61.  
  62. currentfile();
  63.  
  64. /*--------------------------------------------------------------------*/
  65. /*    d c s t a t s                                                   */
  66. /*                                                                    */
  67. /*    Report transmission information for a connection                */
  68. /*--------------------------------------------------------------------*/
  69.  
  70. void dcstats(void)
  71. {
  72.    if (hostp == BADHOST)
  73.    {
  74.       printmsg(0,"dcstats: host structure pointer is NULL");
  75.       panic();
  76.    }
  77.  
  78.    if (!equal(rmtname , hostp->hostname))
  79.       return;
  80.  
  81.    if (remote_stats.lconnect > 0)
  82.    {
  83.       time_t connected;
  84.       unsigned long bytes;
  85.       unsigned long bps;
  86.  
  87.       HostStatus();
  88.  
  89.       connected = time(NULL) - remote_stats.lconnect;
  90.       remote_stats.connect += connected;
  91.       bytes = remote_stats.bsent + remote_stats.breceived;
  92.  
  93.       if ( connected > 0 )
  94.          bps = bytes / (long unsigned) connected;
  95.       else
  96.          bps = 0;
  97.  
  98.       printmsg(0,"%ld files sent, %ld files received, "
  99.                  "%ld bytes sent, %ld bytes received",
  100.             remote_stats.fsent, remote_stats.freceived ,
  101.             remote_stats.bsent, remote_stats.breceived);
  102.       printmsg(0, "%ld packets transferred, %ld errors, "
  103.                   "connection time %ld:%02ld, %ld bytes/second",
  104.             (long) remote_stats.packets,
  105.             (long) remote_stats.errors,
  106.             (long) connected / 60L, (long) connected % 60L, bps);
  107.  
  108.    } /*if */
  109.    else
  110.       printmsg(0,"Unable to print information for host %s (%s)",
  111.             rmtname,
  112.             hostp->hostname);
  113.  
  114.    if (remote_stats.lconnect > hostp->hstats->lconnect)
  115.       hostp->hstats->lconnect = remote_stats.lconnect;
  116.  
  117.    if (remote_stats.ltime > hostp->hstats->ltime)
  118.       hostp->hstats->lconnect = remote_stats.lconnect;
  119.  
  120.    hostp->hstats->connect   += remote_stats.connect;
  121.    hostp->hstats->calls     += remote_stats.calls;
  122.    hostp->hstats->fsent     += remote_stats.fsent;
  123.    hostp->hstats->freceived += remote_stats.freceived;
  124.    hostp->hstats->bsent     += remote_stats.bsent;
  125.    hostp->hstats->breceived += remote_stats.breceived;
  126.    hostp->hstats->errors    += remote_stats.errors;
  127.    hostp->hstats->packets   += remote_stats.packets;
  128.  
  129. } /* dcstats */
  130.  
  131. /*--------------------------------------------------------------------*/
  132. /*    d c u p d a t e                                                 */
  133. /*                                                                    */
  134. /*    Update the status of all known hosts                            */
  135. /*--------------------------------------------------------------------*/
  136.  
  137. void dcupdate( void )
  138. {
  139.    boolean firsthost = TRUE;
  140.    struct HostTable *host;
  141.    FILE *stream;
  142.    char fname[FILENAME_MAX];
  143.    long size;
  144.    unsigned short len1 = strlen(compilep );
  145.    unsigned short len2 = strlen(compilev );
  146.    boolean gotlock;
  147.    short retries = 30;
  148.    LOCKSTACK savelock;
  149.  
  150.    mkfilename( fname, E_confdir, DCSTATUS );
  151.  
  152. /*--------------------------------------------------------------------*/
  153. /*            Save lock status, then lock host status file            */
  154. /*--------------------------------------------------------------------*/
  155.  
  156.    PushLock( &savelock );
  157.  
  158.    do {
  159.       gotlock = LockSystem( "*status", B_UUSTAT );
  160.       if ( ! gotlock )
  161.          ssleep(2);
  162.    } while ( ! gotlock && retries-- );
  163.  
  164.    if ( ! gotlock )
  165.    {
  166.       printmsg(0,"Cannot obtain lock for %s", fname );
  167.       PopLock( &savelock );
  168.       return;
  169.    }
  170.  
  171. /*--------------------------------------------------------------------*/
  172. /*                  Old previous status as required                   */
  173. /*--------------------------------------------------------------------*/
  174.  
  175.    HostStatus();              /* Get new data, if needed          */
  176.  
  177.    filebkup( fname );      /* Rename the file if desired       */
  178.  
  179.    if ((stream  = FOPEN(fname, "w", BINARY_MODE)) == NULL)
  180.    {
  181.       printerr( fname );
  182.       return;
  183.    }
  184.  
  185.    fwrite( &len1, sizeof len1, 1, stream );
  186.    fwrite( &len2, sizeof len2, 1, stream );
  187.    fwrite( compilep , 1, len1, stream);
  188.    fwrite( compilev , 1, len2, stream);
  189.    fwrite( &start_stats , sizeof start_stats , 1,  stream);
  190.  
  191.    while  ((host = nexthost( firsthost )) != BADHOST)
  192.    {
  193.       len1 = strlen( host->hostname );
  194.       len2 = sizeof *(host->hstats);
  195.  
  196.       firsthost = FALSE;
  197.  
  198.       fwrite( &len1, sizeof len1, 1, stream );
  199.       fwrite( &len2, sizeof len2, 1, stream );
  200.       fwrite( host->hostname , sizeof hostp->hostname[0], len1, stream);
  201.       host->hstats->save_hstatus = ( host->hstatus == called ) ?
  202.                                     succeeded : host->hstatus;
  203.       fwrite( host->hstats , len2, 1,  stream);
  204.    }
  205.  
  206. /*--------------------------------------------------------------------*/
  207. /*         Make we sure got end of file and not an I/O error          */
  208. /*--------------------------------------------------------------------*/
  209.  
  210.    if (ferror( stream ))
  211.    {
  212.       printerr( fname );
  213.       clearerr( stream );
  214.    }
  215.  
  216.    fclose( stream );
  217.  
  218.    hstatus_age = stater( fname , &size );
  219.  
  220. /*--------------------------------------------------------------------*/
  221. /*                      Restore locks and return                      */
  222. /*--------------------------------------------------------------------*/
  223.  
  224.    UnlockSystem( );
  225.    PopLock( &savelock );
  226.  
  227. } /* dcupdate */
  228.