home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / KERNEL.C < prev    next >
C/C++ Source or Header  |  2000-07-13  |  7KB  |  189 lines

  1. /******************************************************************************
  2. KERNEL.C     Custom kernel function for TOP.
  3.  
  4.     Copyright 1993 - 2000 Paul J. Sidorsky
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License, version 2, as
  8.     published by the Free Software Foundation.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. This module contains the custom kernel function for TOP as well as the handler
  20. for time warnings.
  21. ******************************************************************************/
  22.  
  23. #include "top.h"
  24.  
  25. /* top_kernel() - Custom kernel.
  26.    Parameters:  None.
  27.    Returns:  Nothing.
  28.    Notes:  This function is called by the door kit's own kernel.  The door
  29.            kit's kernel is executed every few seconds and does things like
  30.            subtract the user's time and check for carrier.  TOP hooks a
  31.            custom kernel function to perform some time-sensitive tasks
  32.            specific to TOP.
  33. */
  34. void top_kernel(void)
  35. {
  36. time_t curtime; /* Current time, in UNIX format. */
  37. XINT kd; /* Counter. */
  38. unsigned char tbyte; /* Temporary byte buffer. */
  39. node_idx_typ tnode; /* Temporary node index data. */
  40. long tcred; /* Temporary RA credit holder. */
  41.  
  42. /* Get the current time. */
  43. curtime = time(NULL);
  44.  
  45. /* Support for RA's credit system. */
  46. if (cfg.usecredits != 0)
  47.     {
  48.     /* Execute once a minute. */
  49.     if (curtime >= lastkertime + 60)
  50.         {
  51.         /* Deduct the configured number of credits. */
  52.         tcred = (((curtime - lastkertime) / 60) * (long) abs(cfg.usecredits));
  53.         /* The user is out of time if the value to be deducted exceeds
  54.            the number of credits the user currently has. */
  55.         if (tcred >= od_control.user_credit)
  56.             {
  57.             /* Evict the user. */
  58.             od_control.user_credit = 0;
  59.             top_output(OUT_SCREEN, getlang("OutOfCredits"));
  60.             od_log_write(top_output(OUT_STRING, getlang("LogOutOfCredits")));
  61.             od_exit(9, FALSE);
  62.             }
  63.         /* Decrease the user's credit count. */
  64.         od_control.user_credit -= tcred;
  65.         /* Update the last kernel execution time. */
  66.         lastkertime = curtime;
  67.         }
  68.     }
  69.  
  70. /* Crash protection.  Only executed while the node is "logged in" to TOP,
  71.    i.e. in the node index. */
  72. if (cfg.crashprotdelay > 0 && isregistered)
  73.     {
  74.     /* Check every 10 seconds.  This is why there is a minimum of 10 on
  75.        the setting for CrashProtDelay. */
  76.     if ((curtime - node->lastaccess) >= (cfg.crashprotdelay - 10))
  77.         {
  78.         /* Keep this node alive by writing the current time to the node
  79.            index. */
  80.         node->lastaccess = time(NULL);
  81.         save_node_data(od_control.od_node, node);
  82.  
  83.         /* Check each node to make sure it's still alive. */
  84.         // Reading the entire index into a dynamic array of node_idx_typs
  85.         // and then scanning should drastically help the speed here...
  86.         for (kd = 0; kd < MAXNODES; kd++)
  87.             {
  88.             /* Only check if the node is active. */
  89.             if (!get_node_data(kd, &tnode) && tnode.handle[0] &&
  90.                 kd != od_control.od_node)
  91.                 {
  92.                 /* The node is assumed to be dead if the at least
  93.                    CrashProtDelay seconds have elapsed since the node
  94.                    last checked in. */
  95.                 if ((curtime - tnode.lastaccess) >= cfg.crashprotdelay)
  96.                     { // Err!!!!!!!!!!
  97.                     /* Delete the node from the index. */
  98.                     lseek(nidx2fil, kd, SEEK_SET);
  99.                     rec_locking(REC_LOCK, nidx2fil, kd, 1);
  100.                     tbyte = 0;
  101.                     write(nidx2fil, &tbyte, 1);
  102.                     rec_locking(REC_UNLOCK, nidx2fil, kd, 1);
  103.                     memset(&tnode, 0, sizeof(node_idx_typ));
  104.                     save_node_data(kd, &tnode);
  105.  
  106.                     /* Tell all other nodes that we just killed a node
  107.                        with Crash Protection, which will cause the other
  108.                        nodes on this channel to recognize the node's
  109.                        deactivation. */
  110.                     // Should maybe be sent globally...
  111.                     itoa(kd, outnum[0], 10);
  112.                     dispatch_message(MSG_KILLCRASH, outnum[0],
  113.                                      -1, 0, 1);
  114.                     od_log_write(top_output(OUT_STRING,
  115.                                             getlang("LogFoundCrashed"),
  116.                                             outnum[0]));
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.     }
  122.  
  123. /* Check high-level censor warnings. */
  124. if (cfg.censortimerhigh > 0 && censorwarningshigh > 0)
  125.     {
  126.     /* Deduct a warning if enough time has elapsed without an offence. */
  127.     if (curtime - lastcensorwarnhigh > cfg.censortimerhigh)
  128.         {
  129.         censorwarningshigh--;
  130.         lastcensorwarnhigh = time(NULL);
  131.         }
  132.     }
  133. /* Check low-level censor warnings. */
  134. if (cfg.censortimerlow > 0 && censorwarningslow > 0)
  135.     {
  136.     /* Deduct a warning if enough time has elapsed without an offence. */
  137.     if (curtime - lastcensorwarnlow > cfg.censortimerlow)
  138.         {
  139.         censorwarningslow--;
  140.         lastcensorwarnlow = time(NULL);
  141.         }
  142.     }
  143.  
  144. #ifdef __OS2__
  145. /* Keep us alive if we're using Maximus MCP. */
  146. if (cfg.bbstype == BBS_MAX3 && hpMCP != 0)
  147.     {
  148.     /* Send a ping every minute. */
  149.     if (curtime % 60L == 0L)
  150.         {
  151.         McpSendMsg(hpMCP, PMSG_PING, NULL, 0);
  152.         }
  153.     }
  154. #endif
  155.  
  156. }
  157.  
  158. /* sendtimemsgs() - Handles door-kit time warning messages.
  159.    Parameters:  string - Message being sent from the door kit.
  160.    Returns:  Nothing.
  161.    Notes:  Called by the door kit when needed.
  162. */
  163. void sendtimemsgs(char *string)
  164.     {
  165.  
  166.     /* Strip any extra spaces. */
  167.     trim_string(string, string, 0);
  168.  
  169.     /* Time messages are blocked if TOP is in the process_messages()
  170.        function.  Remember that since this function is called by the
  171.        door kit it can be called when any od_ function is called. */
  172.     if (blocktimemsgs)
  173.         {
  174.         /* Just display the message to the screen if blocking is on.  It
  175.            may not look pretty but at least it will get through. */
  176.         top_output(OUT_SCREEN, getlang("TimeMsgForcePrefix"), string);
  177.         return;
  178.         }
  179.  
  180.     /* Send the message like a normal TOP message, which looks much
  181.        cleaner. */
  182.     dispatch_message(MSG_GENERIC,
  183.                      top_output(OUT_STRINGNF, getlang("TimeMsgPrefix"),
  184.                                 string),
  185.                      od_control.od_node, 1, 0);
  186.  
  187.     }
  188.  
  189.