home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / srev13h.zip / events.rxx < prev    next >
Text File  |  1999-10-31  |  9KB  |  279 lines

  1. /* the sre-http event monitor daemon */
  2. parse arg efile,icheck,serverport,servername
  3.  
  4. mytid=dostid()
  5. call pmprintf(' SRE-http Events: thread, file, frequency='||mytid', 'efile', 'icheck)
  6.  
  7. oo=syssleep(60)  /* sleep for 60 seconds (avoid startup problems */
  8. esem='\SEM32\SREF_'serverport'_EVENT_CHECK'
  9. oofbar:                 
  10. foo=eventsem_create(esem,'P')
  11. if foo<>0 & foo<>285 then do
  12.   call pmprintf(' SRE-http Events: ERROR, problem with semaphore 'esem' ('foo)
  13.   exit
  14. end
  15. towait=icheck*60*1000           /* minutes to wait */
  16.  
  17. /* endless loop --- wake up, check events, back to sleep */
  18. do forever              
  19.  
  20.   foo=eventsem_wait(esem,towait)
  21.  
  22.   if foo<>640 & foo<>0 then do
  23.      call pmprintf(' SRE-http Events: WARNING, bad status with semaphore 'esem' ('foo)
  24.      foo=eventsem_close(esem)
  25.      foo=eventsem_create(esem)
  26.   end
  27.   foo=eventsem_reset(esem)              /* ignore multiple postings */       
  28.  
  29.   verbose=value('SREF_VERBOSE',,'os2environment')
  30.   if verbose>2 then call pmprintf(' SRE-http Events: checking event file 'efile)
  31.  
  32.   istat=get_file(efile)
  33.   if istat=0 then do
  34.      oo=syssleep(icheck*60)
  35.      iterate
  36.   end
  37.  
  38.   if verbose>3 then do
  39.    do mm=1 to events.0
  40.      call pmprintf(' SRE-http Event# 'mm ' 'events.!type.mm' = ' events.!type2.mm)
  41.      if events.!type3.mm<>''  then call pmprintf('   ...  'events.!type3.mm )
  42.      call pmprintf('     'events.!action.mm ' == 'events.!action2.mm)
  43.    end
  44.   end
  45.  
  46. /* now check the events */
  47.   do mm=1 to events.0
  48.     aaction=events.!action.mm
  49.     atype=events.!type.mm
  50.     doretain=events.!retain.mm
  51.     if aaction=0 | atype=0 | atype='' | aaction='' then iterate /* bad event */
  52.  
  53.     if verbose>3 then call pmprintf('  SRE-http Events: checking 'atype', 'aaction)
  54.  
  55.     select
  56.       when atype='S' then do          /* semaphore */
  57.         asem=events.!type2.mm
  58.         oof=eventsem_query(asem)
  59.         if oof<= 0 then iterate    /* bad sem, or not posted */
  60.       end
  61.       when atype='F' then do
  62.         afile=stream(strip(events.!type2.mm),'c','query exists')
  63.         if afile='' then iterate        /* no such file */
  64.       end
  65.       when atype='P' then do
  66.         aprocfile=events.!type2.mm
  67.         tproc='SRE_'||serverport||'_EVENT_A'
  68.         if macroquery(tproc)<>'' then foo=macrodrop(tproc)
  69.         foo=macroadd(tproc,aprocfile,'B')
  70.         if foo<>1 then do
  71.            call pmprintf('  SRE-http Events: WARNING: unable to use proc 'aprocfile)
  72.            iterate
  73.         end
  74.         interpret 'istat='tproc'('events.!type3.mm')'
  75.         if istat<>1 then iterate
  76.       end
  77.       otherwise iterate
  78.     end
  79.  
  80.     if verbose>1 then call pmprintf('  SRE-http Events: doing #'mm': 'atype', 'aaction)
  81.  
  82. /* if here, this event has occured. Take an action */
  83.    aaction=events.!action.mm 
  84.    select
  85.       when aaction='SUSPEND' then do
  86.           foo=value('SRE_SUSPEND',10,'os2environment')
  87.       end
  88.       when aaction='RESUME' then do
  89.           foo=value('SRE_SUSPEND',0,'os2environment')
  90.       end
  91.  
  92.       when  aaction='S' then  do
  93.           foo=value('SRE_SHUTDOWN',10,'os2environment')
  94.           foo=get_request(servername,serverport,'!ping')
  95.       end
  96.       when aaction='R' then do
  97.          foo=value('SREF_REDO',1,'os2environment')  
  98.       end
  99.       when aaction="I" then do
  100.          interpret events.!action2.mm
  101.       end
  102.       when aaction='E' then do
  103.          address cmd events.!action2.mm
  104.       end
  105.       when aaction='C' then do
  106.         parse var events.!action2.mm  aprocfile arg
  107.         tproc='SRE_'||serverport||'_EVENT_B'
  108.         if macroquery(tproc)<>'' then foo=macrodrop(tproc)
  109.         foo=macroadd(tproc,aprocfile,'B')
  110.         if foo<>1 then do
  111.            call pmprintf('  SRE-http Events: WARNING: unable to perform proc 'aprocfile)
  112.            iterate
  113.         end
  114.         interpret 'istat='tproc'('arg')'
  115.         if istat<>1 then iterate
  116.       end
  117.       otherwise nop
  118.    end
  119.  
  120. /* clear semaphore or file? */
  121.    if doretain<>1 then do  
  122.       if atype='S' then foo=eventsem_reset(asem)
  123.       if atype='F' then foo=sysfiledelete(afile)
  124.    end
  125.  
  126.   end                   /*  checking events */
  127.  
  128. /* done checking events, wait on esem */
  129.  
  130. end
  131.  
  132.  
  133.  
  134. /*********************/
  135. /* read the efile into events. array
  136. Structure of efile:
  137.   Event description are seperated by blank lines.
  138.   Lines beginning with ; are comments, and are ignored.
  139.   Event descriptions have two fields, a TYPE and an ACTION field.
  140.  
  141. type: FILE xxx, SEMAPHORE xxx, or PROCEDURE xx
  142. action: RESET, SHUTDOWN, INTERPRET zzz, CALL zzz arglist , EXEC zzz
  143.  
  144. Example:
  145.    Type: SEMAPHORE \SEM32\SHUTDOWN_1
  146.    Action: shutdown
  147.  
  148.    Type: F e:\goserve\flags\flag.1
  149.    Action: Call e:\goserve\flagdo.cmd  1
  150.  
  151.    Type: Procedure check_time
  152.    Action: interpret atime=time('n') ;
  153.             adate=date('n') ;
  154.             call pmprintf("Today's date is: "||atime||' '||adate) 
  155. */
  156.  
  157. get_file:procedure expose events.
  158. parse arg efile
  159. events.=0
  160.  
  161. ikk=stream(efile,'c','query size')
  162. if ikk='' | ikk=0 then do
  163.  call pmprintf('  SRE-http Events: WARNING: bad event file: 'efile
  164.  return 0
  165. end
  166.  
  167. stuff=charin(efile,1,ikk)
  168. foo=stream(efile,'c','close')
  169.  
  170. do until stuff=''   /* read entire file */
  171. /* remove comments and blank lines preceeding a block */
  172.   do until stuff=''
  173.     parse var stuff a1 '0d0a'x stuff
  174.     a1=strip(a1)
  175.     if abbrev(a1,';')=1 | a1='' then iterate
  176.     stuff=a1||'0d0a'x||stuff
  177.     leave
  178.   end
  179.  
  180. /* now at beginning of a block */
  181.   if stuff='' then  leave               /* no more events specified */
  182.  
  183. /* read lines until next blank line */
  184.   ii=events.0+1
  185.   do until stuff=''
  186.     parse var stuff a1 '0d0a'x stuff ;a1=strip(a1)
  187.     if abbrev(a1,';')=1 then iterate
  188.     if a1='' then leave
  189.     ta1=translate(a1)
  190.     select
  191.       when abbrev(ta1,'TYPE:')=1 then do
  192.         parse var a1 . ':' atype events.!type2.ii events.!type3.ii
  193.         atype=translate(atype)
  194.         select
  195.           when wordpos(atype,'S SEMAPHORE SEM')>0 then do
  196.               atype='S'
  197.               if abbrev(translate(strip(events.!type2.ii)),'\SEM32\')=0 then do
  198.                 call pmprintf('  SRE-http Events: WARNING: bad Semaphore TYPE: 'a1)
  199.                 atype=0
  200.               end
  201.           end                   
  202.           when wordpos(atype,'F FILE')>0 then atype='F'
  203.           when wordpos(atype,'P PROCEDURE PROC')>0 then atype='P'
  204.  
  205.           otherwise do
  206.                 call pmprintf('  SRE-http Events: WARNING: bad TYPE: 'a1)
  207.                 atype=0
  208.           end
  209.         end
  210.         events.!type.ii=atype
  211.       end
  212.  
  213.       when abbrev(ta1,'RETAIN:')=1 then  do
  214.          parse var ta1 . ':' aa
  215.          if wordpos(strip(aa),'1 YES Y')>0 then events.!retain.ii=1
  216.       end
  217.  
  218.       when abbrev(ta1,'ACTION:')=1 then do
  219.          parse var a1 . ':' aaction events.!action2.ii
  220.          aaction=translate(aaction)
  221.          select
  222.            when wordpos(aaction,'RESUME')>0 then aaction='RESUME'
  223.            when wordpos(aaction,'SUSPEND')>0 then aaction='SUSPEND'
  224.            when wordpos(aaction,'SHUTDOWN S SHUT')>0 then aaction='S'
  225.            when wordpos(aaction,'RESET R')>0 then aaction='R'
  226.            when wordpos(aaction,'CALL C')>0 then aaction='C'
  227.            when wordpos(aaction,'EXECUTE EXEC E')>0 then aaction='E'
  228.            when wordpos(aaction,'INTERPRET INTER I INTERP')>0 then aaction='I'
  229.            otherwise do 
  230.                 call pmprintf('  SRE-http Events: WARNING: bad ACTION: 'a1)
  231.                 aaction=0
  232.           end
  233.          end
  234.          events.!action.ii=aaction
  235.       end
  236.       otherwise do
  237.         if events.!action.ii<>'' then do
  238.             events.!action2.ii=events.!action2.ii' 'a1
  239.         end
  240.         else do     
  241.            call pmprintf('  SRE-http Events: WARNING: Bad line ignore = 'a1)
  242.         end
  243.       end               /* otherwise */
  244.     end                /* select */
  245.   end           /* stuff='' */
  246.    if events.!type.ii<>0 then events.0=ii  /* if action found, then augment counter */
  247.  
  248. end             /* get next block */
  249.         
  250. return 1
  251.  
  252. /***********/
  253. /* hit me */
  254. get_request:procedure  
  255. crlf='0d0a'x
  256.  
  257. parse arg servername,sport,message
  258. crlf    ='0d0a'x 
  259. family  ='AF_INET'
  260. httpport=sport
  261. rc=sockgethostbyname(servername, 'serv.0'); dotserver=serv.0addr  
  262. if rc=0 then return 0
  263. gosaddr.0family=family ;gosaddr.0port=httpport;gosaddr.0addr =dotserver
  264. gosock = SockSocket(family, 'SOCK_STREAM', 'IPPROTO_TCP')
  265. message='GET /'request' HTTP/1.0'crlf
  266. message=message||crlf
  267. rc = SockConnect(gosock,'gosaddr.0')
  268. if rc<0 then return 0
  269. rc = SockSend(gosock, message)
  270. do r=1 by 1
  271.   rc = SockRecv(gosock, 'response', 1000)
  272.   got=got||response
  273.   if rc<=0 then leave
  274.  end r
  275. rc = SockClose(gosock)
  276. return length(got)
  277.  
  278.  
  279.