home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DBMIPC.ZIP / WORKER.SQC < prev   
Text File  |  1993-04-12  |  8KB  |  241 lines

  1. #define INCL_DOS
  2. #include <os2.h>
  3. #include "master.h"
  4. #include <sqlenv.h>
  5. static HFILE hError=0;      /* allows for multiple writes to this file although the code doesn't do it */
  6.  
  7. /* this routine is necessary for error handling since this process runs in
  8.    the background and is incapable of writing to stdout/stderr
  9.    It will write to the indicated file, the string passed in
  10.    This program only uses the database name as the file name. That file
  11.    is placed in the current directory
  12. */
  13. static void ErrorLog(char *fname,char * infl)
  14. {
  15. ULONG oAction;
  16.  
  17.    DosBeep( 540,300);
  18.   if (hError==0)
  19.   {
  20.    if (DosOpen(fname,&hError,&oAction,0L,FILE_NORMAL,
  21.            OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  22.            OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | OPEN_FLAGS_WRITE_THROUGH,
  23.            0L))
  24.            DosBeep( 540,300);
  25.   }
  26.   if (hError!=0)
  27.   {
  28.     ULONG  byteswr;
  29.     ULONG  bytesout;
  30.     APIRET rc;
  31.      bytesout= strlen(infl);
  32.      rc =DosWrite(hError,infl,bytesout,&byteswr) ;
  33.      if (rc != 0 || bytesout != byteswr)
  34.            DosBeep( 540,300);
  35.    }
  36. }
  37. main(argc, argv, envp)
  38.    int argc;
  39.    char *argv[];
  40. {
  41.   struct sqlca sqlca;
  42.   int myindex;                     /* index into shared memory as passed in */
  43.   ULONG postct;                    /* for use with DosResetEventSem */
  44.   APIRET rc;                       /* OS/2 return codes */
  45.   char work[100];
  46.   /* sql stuff */
  47.   EXEC SQL BEGIN DECLARE SECTION;
  48.   long key;
  49.   long value;
  50.   EXEC SQL END   DECLARE SECTION;
  51.  
  52.   HEV mysem;                     /* handle for my semaphore */
  53.   HEV mastersem;                 /* handle for master.exe's semaphore */
  54.   BOOL BFinished=FALSE;
  55.   PTIB    threadptr;             /* hold thread information after DosGetInfoblocks */
  56.   PPIB    procptr;              /* hold process information after DosGetInfoblocks */
  57.  struct _commarea * SharedMem;  /* shared memory pointer */
  58.  struct _commarea * MyPieceofSharedMem;  /* points within SharedMem to my stuff */
  59.  
  60.   struct _updatestuff  * stuff;    /* work pointer when I  want to use input area as */
  61.                                    /* something other than a string */
  62.   if (argc !=2)
  63.   {
  64.        ErrorLog("unknown","worker not invoked with a valid option");
  65.        DosExit(EXIT_PROCESS,8);
  66.   }
  67.   /* I should be passed my index as an argument, convert to binary
  68.      and validate */
  69.   myindex=atoi(argv[1]);
  70.   if (myindex > DBNAMECOUNT-1 || myindex < 0)
  71.   {
  72.        sprintf(work,"worker invoked with invalid option %ld",myindex);
  73.        ErrorLog("unknown",work);
  74.        DosExit(EXIT_PROCESS,8);
  75.  
  76.   }
  77.   /* connect to the shared memory created by master.exe */
  78.  
  79.   rc=DosGetNamedSharedMem((void *)&SharedMem,SHARED_MEM_NAME,PAG_READ|PAG_WRITE);
  80.   if (rc)
  81.   {
  82.        sprintf(work,"unable to connect to shared memory rc= %ld",rc);
  83.        ErrorLog(dbNames[myindex],work);
  84.        DosExit(EXIT_PROCESS,8);
  85.  
  86.   }
  87.        DosGetInfoBlocks(&threadptr,&procptr);
  88.   MyPieceofSharedMem =SharedMem+myindex;
  89.   /* open event semaphore created by master process note mastersem must be zero
  90.      on entry or "invalid parameter" will be returned */
  91.  mastersem = 0L;
  92.   rc=DosOpenEventSem(MASTER_SEM_NAME,&mastersem);
  93.   if (rc)
  94.   {
  95.        DosFreeMem(SharedMem);
  96.        sprintf(work,"unable to open master semaphore rc= %ld",rc);
  97.        ErrorLog(dbNames[myindex],work);
  98.        DosExit(EXIT_PROCESS,8);
  99.  
  100.   }
  101.   /* create my semaphore name based on the database and then open it. It
  102.      will have been created by master.exe */
  103.   sprintf(work,"\\SEM32\\%s",dbNames[myindex]);
  104.   mysem=0L;
  105.   rc=DosOpenEventSem(work,&mysem);
  106.   if (rc)
  107.   {
  108.        MyPieceofSharedMem->result=ITSBAD;
  109.        DosPostEventSem(mastersem);
  110.        DosCloseEventSem(mastersem);
  111.        DosFreeMem(SharedMem);
  112.        sprintf(work,"unable to open my semaphore rc= %ld",rc);
  113.        ErrorLog(dbNames[myindex],work);
  114.        DosExit(EXIT_PROCESS,8);
  115.  
  116.   }
  117.   /* start using the database.. can use EXEC SQL CONNECT if this is DB2/2 */
  118.  
  119.   sqlestrd(dbNames[myindex],SQL_USE_SHR,&sqlca);
  120.   if (SQLCODE != 0)
  121.   {
  122.        MyPieceofSharedMem->result=ITSBAD;
  123.        DosPostEventSem(mastersem);
  124.        DosCloseEventSem(mastersem);
  125.        DosFreeMem(SharedMem);
  126.        sprintf(work,"unable to connect to database rc= %ld",SQLCODE);
  127.        ErrorLog(dbNames[myindex],work);
  128.        DosExit(EXIT_PROCESS,8);
  129.   }
  130.  
  131.   /* flag all is well in shared memory and post the master  */
  132.  
  133.   MyPieceofSharedMem->result=ITSOK;
  134.   stuff = (struct _updatestuff  *)(MyPieceofSharedMem->inarea);
  135.   rc=DosPostEventSem(mastersem);
  136.   if (rc)
  137.   {
  138.        sprintf(work,"unable to post master semaphore rc= %ld",rc);
  139.        ErrorLog(dbNames[myindex],work);
  140.        goto ERROREXIT;
  141.   }
  142.   /* loop until I'm told to die */
  143.   while (!BFinished)
  144.   {
  145.       /* wait for action */
  146.  
  147.       rc=DosWaitEventSem(mysem,SEM_INDEFINITE_WAIT);
  148.       if (rc)
  149.       {
  150.            sprintf(work,"unable to wait on worker semaphore rc= %ld",rc);
  151.            ErrorLog(dbNames[myindex],work);
  152.            goto ERROREXIT;
  153.       }
  154.       switch (MyPieceofSharedMem->requestcode) {
  155.        case REQTERM:
  156.             BFinished=TRUE;
  157.             sprintf(work,"PID %ld Terminating\n",procptr->pib_ulpid);
  158.             strcpy(MyPieceofSharedMem->outarea,work);
  159.             MyPieceofSharedMem->result=ITSOK;
  160.           break;
  161.        case REQBEEP:
  162.             DosBeep( (myindex*200)+330,300);
  163.             sprintf(work,"PID %ld has beeped\n",procptr->pib_ulpid);
  164.             strcpy(MyPieceofSharedMem->outarea,work);
  165.             MyPieceofSharedMem->result=ITSAWARNING;
  166.           break;
  167.        case REQUPDATE:
  168.            key=  stuff->key;
  169.            value= stuff->value;
  170.            EXEC SQL UPDATE DEMOTABLE SET INFO=:value WHERE TABKEY=:key;
  171.            if (SQLCODE==0)
  172.            {
  173.               EXEC SQL COMMIT WORK;
  174.               if (SQLCODE!=0)
  175.               {
  176.                sprintf(MyPieceofSharedMem->outarea,
  177.                     "Commit of update failed sqlcode = %ld",SQLCODE);
  178.                MyPieceofSharedMem->result=ITSBAD;
  179.  
  180.               }
  181.               else
  182.               {
  183.                sprintf(MyPieceofSharedMem->outarea,
  184.                  "Update of DEMOTABLE in db <%s> successful\n",dbNames[myindex]);
  185.                MyPieceofSharedMem->result=ITSOK;
  186.               }
  187.            }
  188.            else
  189.            {
  190.             sprintf(MyPieceofSharedMem->outarea,"Update failed sqlcode = %ld",SQLCODE);
  191.             MyPieceofSharedMem->result=ITSBAD;
  192.            }
  193.           break;
  194.        case REQINSERT:
  195.            key=  stuff->key;
  196.            value= stuff->value;
  197.            EXEC SQL INSERT INTO DEMOTABLE VALUES (:key,:value);
  198.            if (SQLCODE==0)
  199.            {
  200.               EXEC SQL COMMIT WORK;
  201.               if (SQLCODE!=0)
  202.               {
  203.                sprintf(MyPieceofSharedMem->outarea,
  204.                     "Commit of insert failed sqlcode = %ld",SQLCODE);
  205.                MyPieceofSharedMem->result=ITSBAD;
  206.  
  207.               }
  208.               else
  209.               {
  210.                sprintf(MyPieceofSharedMem->outarea,
  211.                  "Insert into DEMOTABLE in db <%s> successful\n",dbNames[myindex]);
  212.                MyPieceofSharedMem->result=ITSOK;
  213.               }
  214.            }
  215.            else
  216.            {
  217.             sprintf(MyPieceofSharedMem->outarea,"Insert failed sqlcode = %ld",SQLCODE);
  218.             MyPieceofSharedMem->result=ITSBAD;
  219.            }
  220.           break;
  221.        default:
  222.           break;
  223.        } /* endswitch */
  224.       DosResetEventSem(mysem,&postct);
  225.       rc=DosPostEventSem(mastersem);
  226.       if (rc)
  227.       {
  228.            sprintf(work,"unable to post master semaphore rc= %ld",rc);
  229.            ErrorLog(dbNames[myindex],work);
  230.            goto ERROREXIT;
  231.       }
  232.  
  233.   }
  234. ERROREXIT:
  235.        sqlestpd(&sqlca);
  236.        DosFreeMem(SharedMem);
  237.        DosCloseEventSem(mastersem);
  238.        DosCloseEventSem(mysem);
  239.        DosExit(EXIT_PROCESS,8);
  240. }
  241.