home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Src / MTAconsole / auto.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  8.2 KB  |  451 lines

  1. /* auto.c:routines for auto things */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Src/MTAconsole/RCS/auto.c,v 6.0 1991/12/18 20:26:48 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Src/MTAconsole/RCS/auto.c,v 6.0 1991/12/18 20:26:48 jpo Rel $
  9.  *
  10.  * $Log: auto.c,v $
  11.  * Revision 6.0  1991/12/18  20:26:48  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include    "console.h"
  19.  
  20. unsigned long     timeoutFor = 300000,
  21.         inactiveFor = 600000,
  22.         connectFor,
  23.         connectInit = 300000,
  24.         connectInc = 60000,
  25.         connectMax = 600000;
  26. int    backoff    = TRUE;
  27. caddr_t  retryMask = (caddr_t) XtInputWriteMask;
  28. XtIntervalId    refreshId = 0;
  29. XtIntervalId    refreshStatusId = 0;
  30. XtIntervalId    connectId = 0;
  31. XtIntervalId    inactiveId = 0;
  32. extern XtAppContext    appContext;
  33. extern Display        *disp;
  34. extern Widget        top;
  35. extern char        *hostname;
  36. extern void         ChangeMode();
  37. extern int    autoReconnect,
  38.         userConnected,
  39.         autoRefresh;
  40. extern State    connectState;
  41.  
  42. static int InitRefreshStatusTimeOut(), TermRefreshStatusTimeOut();
  43.  
  44. char    *top_of_refresh_list();
  45.  
  46. /* ARGSUSED */
  47. ConnectTimeOut(data, id)
  48. caddr_t    data;    /* NOT USED */
  49. XtIntervalId    *id;
  50. {
  51.     /* try connect */
  52.     connectId = 0;
  53.     if (connectState == notconnected && autoReconnect == TRUE) {
  54.         construct_event(connect);
  55.     }
  56. }
  57.  
  58. InitConnectTimeOut()
  59. {
  60.     if (connectId != 0) XtRemoveTimeOut(connectId);
  61.     if (connectState == notconnected && autoReconnect == TRUE) {
  62.         connectFor = connectInit;
  63.         connectId = XtAppAddTimeOut(appContext,
  64.                         (userConnected == TRUE) ? connectFor : 500,
  65.                         ConnectTimeOut,    
  66.                         NULL);
  67.     }
  68. }
  69.  
  70. TermConnectTimeOut()
  71. {
  72.     if (connectId != 0) {
  73.         XtRemoveTimeOut(connectId);
  74.         connectId = 0;
  75.     }
  76. }
  77.  
  78. /*   */
  79. extern Mode    mode;
  80. extern int    errorUp;
  81. extern Widget    error;
  82.  
  83. /* ARGSUSED */
  84. Inactive(data, id)
  85. caddr_t        data;
  86. XtIntervalId    *id;
  87. {
  88.     inactiveId = 0;
  89.     if (mode == control && connectState == connected)
  90.         ChangeMode((Widget)NULL, (caddr_t) NULL, (caddr_t) NULL);
  91. }
  92.  
  93.  
  94. ResetInactiveTimeout()
  95. {
  96.     if (errorUp > 0) {
  97.         errorUp--;
  98.         if (errorUp == 0)
  99.             XtSetMappedWhenManaged(error, False);
  100.     }
  101.     if (inactiveId != 0) XtRemoveTimeOut(inactiveId);
  102.     if (mode == control && connectState == connected)
  103.         inactiveId = XtAppAddTimeOut(appContext,
  104.                          inactiveFor,
  105.                          Inactive,
  106.                          NULL);
  107.     else
  108.         inactiveId = 0;
  109. }
  110.  
  111. /*   */
  112. extern int compat;
  113. extern Widget statistics;
  114. /* ARGSUSED */
  115. RefreshTimeOut(data,id)
  116. caddr_t        data;    
  117. XtIntervalId    *id;
  118. {
  119.     refreshId = 0;
  120.     if (connectState == connected ) {
  121.         construct_event(chanread);
  122.         if (autoRefresh == TRUE) 
  123.             refreshId = XtAppAddTimeOut(appContext,
  124.                             timeoutFor,
  125.                             RefreshTimeOut,
  126.                             NULL);
  127.     }
  128. }
  129.         
  130. InitRefreshTimeOut(tx)
  131. unsigned long    tx;
  132. {
  133.     if (refreshId != 0) XtRemoveTimeOut(refreshId);
  134.     if (autoRefresh == TRUE && connectState == connected) {
  135.         refreshId = XtAppAddTimeOut(appContext,
  136.                         (tx == 0) ? timeoutFor : tx,
  137.                         RefreshTimeOut,
  138.                         NULL);
  139.         if (!compat)
  140.             InitRefreshStatusTimeOut(tx);
  141.     } else 
  142.         refreshId = 0;
  143. }
  144.  
  145. TermRefreshTimeOut()
  146. {
  147.     if (refreshId != 0){
  148.         XtRemoveTimeOut(refreshId);
  149.         refreshId = 0;
  150.     }
  151.     TermRefreshStatusTimeOut();
  152. }
  153.  
  154. /*   */
  155.  
  156. /* ARGSUSED */
  157. static RefreshStatusTimeOut(data,id)
  158. caddr_t        data;    
  159. XtIntervalId    *id;
  160. {
  161.     refreshStatusId = 0;
  162.     if (connectState == connected ) {
  163.         my_invoke(qmgrStatus, (char **) NULL);
  164.         refreshStatusId = XtAppAddTimeOut(appContext,
  165.                           timeoutFor / 2,
  166.                           RefreshStatusTimeOut,
  167.                           NULL);
  168.     }
  169. }
  170.         
  171. static InitRefreshStatusTimeOut(tx)
  172. unsigned long    tx;
  173. {
  174.     if (refreshStatusId != 0) XtRemoveTimeOut(refreshStatusId);
  175.     if (connectState == connected)
  176.         refreshStatusId = XtAppAddTimeOut(appContext,
  177.                         (tx == 0) ? (timeoutFor/2) : tx / 2,
  178.                         RefreshStatusTimeOut,
  179.                         NULL);
  180.     else 
  181.         refreshStatusId = 0;
  182. }
  183.  
  184. static TermRefreshStatusTimeOut()
  185. {
  186.     if (refreshStatusId != 0){
  187.         XtRemoveTimeOut(refreshStatusId);
  188.         refreshStatusId = 0;
  189.     }
  190. }
  191.  
  192. /*   */
  193.  
  194. int construct_event(op)
  195. Operations    op;
  196. {
  197.   XClientMessageEvent    event;
  198.  
  199.   event.type = ClientMessage;
  200.   event.send_event = True;
  201.   event.display = disp;
  202.   event.window = XtWindow(top);
  203.   event.format = 8;
  204.   event.data.b[0] = (char) op;
  205.       
  206.   XSendEvent(disp,
  207.          XtWindow(top),
  208.          False,
  209.          NoEventMask,
  210.          (XEvent *) &event);
  211. /*  if (result == 0)
  212.       temp = result;*/
  213. }
  214.  
  215. int construct_refresh_event(widget)
  216. Widget    widget;
  217. {
  218.   XClientMessageEvent    event;
  219.  
  220.   event.type = ResizeRequest;
  221.   event.send_event = True;
  222.   event.display = disp;
  223.   event.window = XtWindow(widget);
  224.       
  225.   XSendEvent(disp,
  226.          XtWindow(widget),
  227.          False,
  228.          VisibilityChangeMask,
  229.          (XEvent *)&event);
  230. /*  XSync(XtDisplay(widget), False);*/
  231. }
  232.  
  233. /* ARGSUSED */
  234. void client_msg_handler(w, client_data, event)
  235. Widget    w;
  236. caddr_t    client_data;
  237. XClientMessageEvent    *event;
  238. {
  239.     char    *name;
  240.     extern char    *msginfo_args[];
  241.     if (event -> type == ClientMessage &&
  242.         connectState == connected) {
  243.         switch ((Operations) event -> data.b[0]) {
  244.             case chanread:
  245.             if (!compat)
  246.                 my_invoke(qmgrStatus, (char **) NULL);
  247.             my_invoke(chanread, (char **) NULL);
  248.             break;
  249.             case mtaread:
  250.             if ((name = top_of_refresh_list()) != NULLCP) {
  251.                 my_invoke(mtaread, &name);
  252.                 construct_event(mtaread);
  253.             }
  254.  
  255.             break;
  256.             case readchannelmtamessage:
  257.             my_invoke(readchannelmtamessage, msginfo_args);
  258.             break;
  259.             
  260.             default:
  261.             break;
  262.         }
  263.  
  264.     } else if (event -> type == ClientMessage &&
  265.            connectState == notconnected &&
  266.            autoReconnect == TRUE &&
  267.            (Operations) event -> data.b[0] == connect) {
  268.         XSync(disp, 0);
  269.         Connect(hostname);
  270.         if (connectState == notconnected) {
  271.             if (backoff == TRUE && connectFor < connectMax)
  272.                 connectFor += connectInc;
  273.         }
  274.     }
  275. }
  276.  
  277. /*   */
  278. /* refresh list stuff */
  279. typedef struct mta_refresh_struct {
  280.     char    *name;
  281.     struct mta_refresh_struct *next;
  282. } Mta_refresh_struct;
  283.  
  284. struct mta_refresh_struct    *mta_refresh_list = NULL;
  285.  
  286. Mta_refresh_struct    *new_mta_refresh_struct(name)
  287. char    *name;
  288. {
  289.     Mta_refresh_struct * ret =
  290.         (Mta_refresh_struct *) calloc(1, sizeof (Mta_refresh_struct));
  291.     ret -> name = strdup(name);
  292.     return ret;
  293. }
  294.  
  295. free_mta_refresh_list(list)
  296. Mta_refresh_struct    *list;
  297. {
  298.     if (list == NULL)
  299.         return;
  300.  
  301.     if (list -> next != NULL)
  302.         free_mta_refresh_list(list -> next);
  303.  
  304.     if (list -> name != NULLCP)
  305.         free(list -> name);
  306.  
  307.     free((char *) list);
  308. }
  309.  
  310. clear_mta_refresh_list()
  311. {
  312.  
  313.     free_mta_refresh_list(mta_refresh_list) ;
  314.     mta_refresh_list = NULL;
  315. }
  316.  
  317. add_mta_refresh_list(name)
  318. char    *name;
  319. {
  320.     Mta_refresh_struct    *ix = mta_refresh_list;
  321.  
  322.     if (mta_refresh_list == NULL)
  323.         mta_refresh_list = new_mta_refresh_struct(name);
  324.     else {
  325.         while (ix -> next != NULL)
  326.             ix = ix -> next;
  327.         ix -> next = new_mta_refresh_struct(name);
  328.     }
  329. }
  330.  
  331. char    *top_of_refresh_list()
  332. {
  333.     if (mta_refresh_list != NULL)
  334.         return mta_refresh_list -> name;
  335.     return NULLCP;
  336. }
  337.  
  338. char    *pop_from_mta_refresh_list()
  339. {
  340.     char    *ret = NULL;
  341.     Mta_refresh_struct    *temp;
  342.  
  343.     if ((temp = mta_refresh_list) != NULL) {
  344.         ret = temp -> name;
  345.         mta_refresh_list = mta_refresh_list -> next;
  346.         free((char *) temp);
  347.     }
  348.  
  349.     return ret;
  350. }
  351.  
  352. /*   */
  353. XtInputId    retryId = 0,
  354.         assocId = 0;
  355.  
  356. /* ARGSUSED */
  357. ConnectRetry(data, fd, id)
  358. caddr_t        data;
  359. int        *fd;
  360. XtInputId    *id;
  361. {
  362.     int    result;
  363.     int    myfd = *fd;
  364.  
  365.  
  366.     XtRemoveInput(*id);
  367.     retryId = 0;
  368.     switch( result = acsap_retry(myfd)) {
  369. #ifdef    CONNECTING_1
  370.         case CONNECTING_1:
  371.         case CONNECTING_2:
  372.         Connecting(result);
  373.         break;
  374. #else
  375.         case OK:
  376.         retryMask = (caddr_t) XtInputReadMask;
  377.         Connecting(result);
  378.         break;
  379. #endif
  380.         case DONE:
  381.         Connected();
  382.         break;
  383.         case NOTOK:
  384.         default:
  385.         NotConnected();
  386.         break;
  387.     }
  388. }
  389.  
  390. TermConnectRetry()
  391. {
  392.     if (retryId != 0) {
  393.         XtRemoveInput(retryId);
  394.         retryId = 0;
  395.     }
  396. }
  397.  
  398. InitConnectRetry(sd, res)
  399. int    sd,
  400.     res;
  401. {
  402.     XSync(disp, 0);
  403. #ifdef CONNECTING_1
  404.     if (res == CONNECTING_1)
  405.         retryMask = (caddr_t) XtInputWriteMask;
  406.     else
  407.         retryMask = (caddr_t) XtInputReadMask;
  408. #endif
  409.  
  410.     retryId = XtAppAddInput(appContext,
  411.                 sd,
  412.                 retryMask,
  413.                 ConnectRetry,
  414.                 NULL);
  415. }
  416.  
  417.  
  418.  
  419. /*   */
  420.  
  421. /* ARGSUSED */ 
  422. Listen(data, fd, id)
  423. caddr_t        data;    /* unused */
  424. int        *fd;    /* where to read from */
  425. XtInputId    *id;    /* unused */
  426. {
  427.     if (ros_work(*fd) == NOTOK) 
  428.         return;
  429. }
  430.  
  431. InitListen(fd)
  432. int    fd;
  433. {
  434.     /* set Listen to listen on fd */
  435.     if (assocId != 0) XtRemoveInput(assocId);
  436.     assocId = XtAppAddInput(appContext, fd, 
  437.                 XtInputReadMask,
  438.                 Listen,
  439.                 NULL);
  440. }
  441.  
  442. /* ARGSUSED */ 
  443. TermListen()
  444. {
  445.     /* unset Listen which is listening on fd */
  446.     if (assocId != 0) {
  447.         XtRemoveInput(assocId);
  448.         assocId = 0;
  449.     }
  450. }
  451.