home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Connectivity / GateKeeper-2.1 / InactivityTimer.m < prev    next >
Encoding:
Text File  |  1997-04-14  |  8.8 KB  |  315 lines

  1. //*****************************************************************************
  2. //
  3. //    InactivityTimer.h.      
  4. //
  5. //    Tracks and determines whether inactivity timeout is warranted, also 
  6. //    controls pppStats.  Works in conjunction with StaticSpaceITimerView
  7. //    class (subclass of spaceView)
  8. //
  9. //        by    Felipe A. Rodriguez        
  10. //
  11. //    This code is supplied "as is" the author makes no warranty as to its 
  12. //    suitability for any purpose.  This code is free and may be distributed 
  13. //    in accordance with the terms of the:
  14. //        
  15. //            GNU GENERAL PUBLIC LICENSE
  16. //            Version 2, June 1991
  17. //            copyright (C) 1989, 1991 Free Software Foundation, Inc.
  18. //             675 Mass Ave, Cambridge, MA 02139, USA
  19. //
  20. //*****************************************************************************
  21.  
  22.  
  23. #import "InactivityTimer.h"
  24. #import "CommandScroll.h"
  25. #import "Coordinator.h"
  26.  
  27. #import "pppstats.h"
  28.  
  29.  
  30.             // used in producing timed events
  31.     static DPSTimedEntry mailIconAniTag;
  32. static void ITAnimate();
  33.  
  34.  
  35. @implementation InactivityTimer
  36.  
  37. //************************************************************************
  38. //
  39. //         designated initializer
  40. //
  41. //************************************************************************
  42.  
  43. - init
  44. {
  45.     [super init];
  46.     posNI = NO;                                // set interactive session ivar
  47.     [self resetInactivityTimer];            // set or reset ivars
  48.     imageList = [[List alloc] init];    // produce an array of objects
  49.     [imageList insertObject: [NXImage findImageNamed: "nomail"] at: 0];
  50.     [imageList insertObject: [NXImage findImageNamed: "newmail1"] at: 1];
  51.     [imageList insertObject: [NXImage findImageNamed: "newmail2"] at: 2];
  52.                 
  53.     return self;
  54. }
  55. //************************************************************************
  56. //
  57. //         test for inactivity timeout
  58. //
  59. //************************************************************************
  60.  
  61. - inactivityTimeout
  62. {
  63. struct pppIO *pppStat;
  64.                                             // Inactivity timeout algorithm
  65.     if(pppstats)                            // if pppstats is enabled
  66.         {
  67.         pppStat = intpr();
  68.         if((pppStat->inPkts <= iTimeThresh)&&(pppStat->outPkts <= iTimeThresh))
  69.             iPeriodCntr++;                    // cntr counts # of consecutive 
  70.         else                                // 0 ppp i/o intervals
  71.             iPeriodCntr = 0;                // reset cntr if link is active
  72.         iBaseCntr++;                        // bCntr counts from time link up
  73.  
  74.         wasHidden =    [NXApp isHidden];        // is the app hidden?
  75.                     // if interface is inactive for > n min or non-interactive     
  76.                     // session in progress and timeout "ON" and this is a 
  77.                     // possibly non-interactive session.
  78.         if((iPeriodCntr > (timeout - 1) || ((iBaseCntr < 4) && 
  79.                     (((pppStat->inPkts == 0) && (pppStat->outPkts == 0))) && 
  80.                             (preTimeout == YES) && (posNI == YES)))
  81.                                     && (timeout > 0))
  82.             {
  83.             [NXApp activateSelf:YES];
  84.                             // ppp shutdown imminent, get users attention
  85.             if(strcmp(NXGetDefaultValue([NXApp appName],"sound"), "YES") == 0) 
  86.                 [[[Sound findSoundFor: "Gong"] play: nil] free];
  87.             if(!ITPanel) 
  88.                 [NXApp loadNibSection:"InactivityPanel.nib" owner:self
  89.                                             withNames:NO fromZone:[self zone]];
  90.             if([[NXApp delegate] mailInQueue])
  91.                 [self setMailIconAnim];
  92.             [ITPanel center];
  93.             [ITPanel makeKeyAndOrderFront:self];
  94.             }
  95.         }
  96.         
  97.     return self;
  98. }
  99. //*****************************************************************************
  100. //
  101. //         set inactivity timeout Ivar and write its value to ddb 
  102. //
  103. //*****************************************************************************
  104.  
  105. - setTimeout:(int)minTillTimeout 
  106. {
  107. char buf[8];
  108.  
  109.     timeout = minTillTimeout;
  110.     sprintf(buf,"%d",(int)timeout);
  111.     if(!NXWriteDefault([NXApp appName], "iTimeout", buf))
  112.         [[NXApp delegate] showAlert:"ddbWriteError"];
  113.  
  114.     return self; 
  115. }
  116. //************************************************************************
  117. //
  118. //         reset our Ivars so that we're ready for next ppp session
  119. //
  120. //************************************************************************
  121.  
  122. - resetInactivityTimer
  123. {
  124.     iPeriodCntr = 0;                // reset inactivity timeout cntr
  125.     iBaseCntr = 0;                    // reset inactivity timeout base counter
  126.                                     // turn pre timeout on or off
  127.     if(strcmp(NXGetDefaultValue([NXApp appName],"preTimeout"), "YES") == 0) 
  128.         preTimeout = YES;
  129.     else
  130.         preTimeout = NO;
  131.                     // setup inactivity timeout threshold var from ddb value
  132.     iTimeThresh = atoi(NXGetDefaultValue([NXApp appName], "iTimeThreshold"));
  133.                         // setup inactivity timeout Instance var from ddb value
  134.     timeout = atoi(NXGetDefaultValue([NXApp appName], "iTimeout"));
  135.         
  136.     return self;
  137. }
  138. //*****************************************************************************
  139. //
  140. //         set when upcoming session is possibly not interactive, only DNS 
  141. //            triggered sessions are given this status
  142. //
  143. //*****************************************************************************
  144.  
  145. - setPosNonInter:(BOOL)pni 
  146. {
  147.     posNI = pni;                // DNS triggered session, possibly     
  148.                                     // non-interactive if YES
  149.     return self; 
  150. }
  151. //*****************************************************************************
  152. //
  153. //         return whether session is possibly a DNS triggered non-interactive  
  154. //
  155. //*****************************************************************************
  156.  
  157. - (BOOL)posNonInter; 
  158. {
  159.     return posNI; 
  160. }
  161. //************************************************************************
  162. //
  163. //         enable pppstats
  164. //
  165. //************************************************************************
  166.  
  167. - pppstats:aView
  168. {
  169.     commandView = aView;
  170.     if(!pppstats)
  171.         {                                    // display pppstats?
  172.         if(strcmp(NXGetDefaultValue([NXApp appName], "DispPPP"),"YES") == 0)
  173.             {
  174.             openSocket(aView);        // open IP datagram socket, pass a view
  175.             intpr();
  176.             }
  177.         else
  178.             openSocket(nil);        // open IP datagram socket, w/o view
  179.         pppstats = YES;
  180.         }                
  181.     [self resetInactivityTimer];        
  182.  
  183.     return self;
  184. }
  185. //************************************************************************
  186. //
  187. //         disable pppstats
  188. //
  189. //************************************************************************
  190.  
  191. - pppstatsReset
  192. {
  193.     pppstats = NO;    
  194.         
  195.     return self;
  196. }
  197. //*****************************************************************************
  198. //
  199. //         cancel target -- do not end ppp session 
  200. //
  201. //*****************************************************************************
  202.  
  203. - cancel:sender
  204. {
  205.     posNI = NO;                     
  206.     [sView removeTimedEntry];
  207.     [self removeTimedEntry];
  208.     [ITPanel close];
  209.     ITPanel = nil;            
  210.     if(wasHidden)                // if app was hidden prior to cursor in, hide
  211.         [NXApp hide:self];
  212.     iPeriodCntr = 0;
  213.  
  214.     return self;
  215. }
  216. //*****************************************************************************
  217. //
  218. //         continue target -- end ppp session 
  219. //
  220. //*****************************************************************************
  221.  
  222. - continue:sender
  223. {
  224.     [sView removeTimedEntry];
  225.     [self removeTimedEntry];
  226.     [ITPanel close];
  227.     ITPanel = nil;            
  228.                     // unlink if unlink menu item is enabled + pppstats
  229.     if((strcmp([[[NXApp mainMenu] findCellWithTag:2] title], 
  230.                     [[NXApp delegate] localString:"Disconnect"]) == 0) && 
  231.                             [[[NXApp mainMenu] findCellWithTag:2] isEnabled])
  232.         {
  233.         if((iPeriodCntr > (timeout - 1)) && commandView)    
  234.             [commandView appendStringUseFixedFont:[[NXApp delegate]localString:
  235.                     "Dropping ppp Link due to inactivity timeout\n"]];
  236.         else
  237.             [commandView appendStringUseFixedFont:[[NXApp delegate]localString:
  238.                     "Dropping ppp Link because sesion is non-interactive\n"]];
  239.         [[NXApp delegate] UnLink:self];    
  240.         if(wasHidden)        // if app was hidden prior to cursor in, hide
  241.             [NXApp hide:self];
  242.         }
  243.  
  244.     return self;
  245. }
  246. //************************************************************************
  247. //
  248. //         set mail icon animation into motion
  249. //
  250. //************************************************************************
  251.  
  252. - setMailIconAnim
  253. {
  254.     if(!mailIconAniTag)  
  255.         mailIconAniTag = DPSAddTimedEntry(        // register function Animate
  256.             0.3,                                 // to be called every period of         
  257.             (DPSTimedEntryProc)ITAnimate,         // arg0
  258.             (id)self, 
  259.             NX_BASETHRESHOLD);
  260.  
  261.     return self;
  262. }
  263. //************************************************************************
  264. //
  265. //         icon animation
  266. //
  267. //************************************************************************
  268.  
  269. - _animate
  270. {
  271. static int Index = 0;
  272. static BOOL reverse = NO;
  273.  
  274.     [mailView setImage:[imageList objectAt:Index]]; 
  275.     if(Index >= 2)                        // reverse anim seq
  276.         reverse = YES;
  277.     if(Index <= 0)                        // reverse anim seq
  278.         reverse = NO;
  279.     if(reverse)
  280.         Index--;
  281.     else
  282.         Index++;
  283.             
  284.     return self;
  285. }
  286. //************************************************************************
  287. //
  288. //         remove the timed entry when connection is made or we exit
  289. //
  290. //************************************************************************
  291.  
  292. - removeTimedEntry
  293. {
  294.     if (mailIconAniTag)  
  295.         DPSRemoveTimedEntry (mailIconAniTag);
  296.     mailIconAniTag = 0;
  297.  
  298.     return self;
  299. }
  300.  
  301. @end
  302.  
  303. //************************************************************************
  304. //
  305. //        This fucntion is registered by DPSaddtimedentry.
  306. //        It is subsequently called every period t as registered  
  307. //        in arg 0 of DPSaddtimedentry.
  308. //
  309. //************************************************************************
  310.  
  311. static void ITAnimate(DPSTimedEntry time_tag, double now, id self)
  312. {
  313.     [self _animate];
  314. }
  315.