home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DBMIPC.ZIP / WORKER.C < prev    next >
C/C++ Source or Header  |  1993-04-12  |  11KB  |  354 lines

  1. static unsigned char sqla_program_id[40] = 
  2. {111,65,65,66,65,69,67,67,78,69,87,84,79,78,32,32,87,79,82,75,
  3. 69,82,32,32,108,65,103,104,85,77,69,74,48,32,32,32,32,32,32,32};
  4.  
  5.  
  6. /* Operating System Control Parameters */
  7. #ifndef SQL_API_RC
  8. #define SQL_STRUCTURE struct
  9. #define SQL_API_RC int
  10. #define SQL_POINTER
  11. #ifdef  SQL16TO32
  12. #define SQL_API_FN far pascal _loadds
  13. #else
  14. #define SQL_API_FN _System
  15. #endif
  16. #endif
  17.  
  18. struct sqlca;
  19. struct sqlda;
  20. SQL_API_RC SQL_API_FN  sqlaaloc(unsigned short,
  21.                                 unsigned short,
  22.                                 unsigned short,
  23.                                 void *);
  24. SQL_API_RC SQL_API_FN  sqlacall(unsigned short,
  25.                                 unsigned short,
  26.                                 unsigned short,
  27.                                 unsigned short,
  28.                                 void *);
  29. SQL_API_RC SQL_API_FN  sqladloc(unsigned short,
  30.                                 void *);
  31. SQL_API_RC SQL_API_FN  sqlasets(unsigned short,
  32.                                 void *,
  33.                                 void *);
  34. SQL_API_RC SQL_API_FN  sqlasetv(unsigned short,
  35.                                 unsigned short,
  36.                                 unsigned short,
  37.                                 unsigned short,
  38.                                 void *,
  39.                                 void *,
  40.                                 void *);
  41. SQL_API_RC SQL_API_FN  sqlastop(void *);
  42. SQL_API_RC SQL_API_FN  sqlastrt(void *,
  43.                                 void *,
  44.                                 struct sqlca *);
  45. SQL_API_RC SQL_API_FN  sqlausda(unsigned short,
  46.                                 struct sqlda *,
  47.                                 void *);
  48. SQL_API_RC SQL_API_FN  sqlestrd_api(unsigned char *,
  49.                                     unsigned char *,
  50.                                     unsigned char,
  51.                                     struct sqlca *);
  52. SQL_API_RC SQL_API_FN  sqlestpd_api(struct sqlca *);
  53. #line 1 "D:\\dale\\stuff\\worker.sqc"
  54. #define INCL_DOS
  55. #include <os2.h>
  56. #include "master.h"
  57. #include <sqlenv.h>
  58. static HFILE hError=0;      /* allows for multiple writes to this file although the code doesn't do it */
  59.  
  60. /* this routine is necessary for error handling since this process runs in
  61.    the background and is incapable of writing to stdout/stderr
  62.    It will write to the indicated file, the string passed in
  63.    This program only uses the database name as the file name. That file
  64.    is placed in the current directory
  65. */
  66. static void ErrorLog(char *fname,char * infl)
  67. {
  68. ULONG oAction;
  69.  
  70.    DosBeep( 540,300);
  71.   if (hError==0)
  72.   {
  73.    if (DosOpen(fname,&hError,&oAction,0L,FILE_NORMAL,
  74.            OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
  75.            OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE | OPEN_FLAGS_WRITE_THROUGH,
  76.            0L))
  77.            DosBeep( 540,300);
  78.   }
  79.   if (hError!=0)
  80.   {
  81.     ULONG  byteswr;
  82.     ULONG  bytesout;
  83.     APIRET rc;
  84.      bytesout= strlen(infl);
  85.      rc =DosWrite(hError,infl,bytesout,&byteswr) ;
  86.      if (rc != 0 || bytesout != byteswr)
  87.            DosBeep( 540,300);
  88.    }
  89. }
  90. main(argc, argv, envp)
  91.    int argc;
  92.    char *argv[];
  93. {
  94.   struct sqlca sqlca;
  95.   int myindex;                     /* index into shared memory as passed in */
  96.   ULONG postct;                    /* for use with DosResetEventSem */
  97.   APIRET rc;                       /* OS/2 return codes */
  98.   char work[100];
  99.   /* sql stuff */
  100.   
  101. /*
  102. EXEC SQL BEGIN DECLARE SECTION;
  103. */
  104. #line 47 "D:\\dale\\stuff\\worker.sqc"
  105.  
  106.   long key;
  107.   long value;
  108.   
  109. /*
  110. EXEC SQL END   DECLARE SECTION;
  111. */
  112. #line 50 "D:\\dale\\stuff\\worker.sqc"
  113.  
  114.  
  115.   HEV mysem;                     /* handle for my semaphore */
  116.   HEV mastersem;                 /* handle for master.exe's semaphore */
  117.   BOOL BFinished=FALSE;
  118.   PTIB    threadptr;             /* hold thread information after DosGetInfoblocks */
  119.   PPIB    procptr;              /* hold process information after DosGetInfoblocks */
  120.  struct _commarea * SharedMem;  /* shared memory pointer */
  121.  struct _commarea * MyPieceofSharedMem;  /* points within SharedMem to my stuff */
  122.  
  123.   struct _updatestuff  * stuff;    /* work pointer when I  want to use input area as */
  124.                                    /* something other than a string */
  125.   if (argc !=2)
  126.   {
  127.        ErrorLog("unknown","worker not invoked with a valid option");
  128.        DosExit(EXIT_PROCESS,8);
  129.   }
  130.   /* I should be passed my index as an argument, convert to binary
  131.      and validate */
  132.   myindex=atoi(argv[1]);
  133.   if (myindex > DBNAMECOUNT-1 || myindex < 0)
  134.   {
  135.        sprintf(work,"worker invoked with invalid option %ld",myindex);
  136.        ErrorLog("unknown",work);
  137.        DosExit(EXIT_PROCESS,8);
  138.  
  139.   }
  140.   /* connect to the shared memory created by master.exe */
  141.  
  142.   rc=DosGetNamedSharedMem((void *)&SharedMem,SHARED_MEM_NAME,PAG_READ|PAG_WRITE);
  143.   if (rc)
  144.   {
  145.        sprintf(work,"unable to connect to shared memory rc= %ld",rc);
  146.        ErrorLog(dbNames[myindex],work);
  147.        DosExit(EXIT_PROCESS,8);
  148.  
  149.   }
  150.        DosGetInfoBlocks(&threadptr,&procptr);
  151.   MyPieceofSharedMem =SharedMem+myindex;
  152.   /* open event semaphore created by master process note mastersem must be zero
  153.      on entry or "invalid parameter" will be returned */
  154.  mastersem = 0L;
  155.   rc=DosOpenEventSem(MASTER_SEM_NAME,&mastersem);
  156.   if (rc)
  157.   {
  158.        DosFreeMem(SharedMem);
  159.        sprintf(work,"unable to open master semaphore rc= %ld",rc);
  160.        ErrorLog(dbNames[myindex],work);
  161.        DosExit(EXIT_PROCESS,8);
  162.  
  163.   }
  164.   /* create my semaphore name based on the database and then open it. It
  165.      will have been created by master.exe */
  166.   sprintf(work,"\\SEM32\\%s",dbNames[myindex]);
  167.   mysem=0L;
  168.   rc=DosOpenEventSem(work,&mysem);
  169.   if (rc)
  170.   {
  171.        MyPieceofSharedMem->result=ITSBAD;
  172.        DosPostEventSem(mastersem);
  173.        DosCloseEventSem(mastersem);
  174.        DosFreeMem(SharedMem);
  175.        sprintf(work,"unable to open my semaphore rc= %ld",rc);
  176.        ErrorLog(dbNames[myindex],work);
  177.        DosExit(EXIT_PROCESS,8);
  178.  
  179.   }
  180.   /* start using the database.. can use EXEC SQL CONNECT if this is DB2/2 */
  181.  
  182.   sqlestrd(dbNames[myindex],SQL_USE_SHR,&sqlca);
  183.   if (SQLCODE != 0)
  184.   {
  185.        MyPieceofSharedMem->result=ITSBAD;
  186.        DosPostEventSem(mastersem);
  187.        DosCloseEventSem(mastersem);
  188.        DosFreeMem(SharedMem);
  189.        sprintf(work,"unable to connect to database rc= %ld",SQLCODE);
  190.        ErrorLog(dbNames[myindex],work);
  191.        DosExit(EXIT_PROCESS,8);
  192.   }
  193.  
  194.   /* flag all is well in shared memory and post the master  */
  195.  
  196.   MyPieceofSharedMem->result=ITSOK;
  197.   stuff = (struct _updatestuff  *)(MyPieceofSharedMem->inarea);
  198.   rc=DosPostEventSem(mastersem);
  199.   if (rc)
  200.   {
  201.        sprintf(work,"unable to post master semaphore rc= %ld",rc);
  202.        ErrorLog(dbNames[myindex],work);
  203.        goto ERROREXIT;
  204.   }
  205.   /* loop until I'm told to die */
  206.   while (!BFinished)
  207.   {
  208.       /* wait for action */
  209.  
  210.       rc=DosWaitEventSem(mysem,SEM_INDEFINITE_WAIT);
  211.       if (rc)
  212.       {
  213.            sprintf(work,"unable to wait on worker semaphore rc= %ld",rc);
  214.            ErrorLog(dbNames[myindex],work);
  215.            goto ERROREXIT;
  216.       }
  217.       switch (MyPieceofSharedMem->requestcode) {
  218.        case REQTERM:
  219.             BFinished=TRUE;
  220.             sprintf(work,"PID %ld Terminating\n",procptr->pib_ulpid);
  221.             strcpy(MyPieceofSharedMem->outarea,work);
  222.             MyPieceofSharedMem->result=ITSOK;
  223.           break;
  224.        case REQBEEP:
  225.             DosBeep( (myindex*200)+330,300);
  226.             sprintf(work,"PID %ld has beeped\n",procptr->pib_ulpid);
  227.             strcpy(MyPieceofSharedMem->outarea,work);
  228.             MyPieceofSharedMem->result=ITSAWARNING;
  229.           break;
  230.        case REQUPDATE:
  231.            key=  stuff->key;
  232.            value= stuff->value;
  233.            
  234. /*
  235. EXEC SQL UPDATE DEMOTABLE SET INFO=:value WHERE TABKEY=:key;
  236. */
  237.  
  238. {
  239.   sqlastrt(sqla_program_id,0L,&sqlca);
  240.   sqlaaloc(1,2,1,0L);
  241.     sqlasetv(1,0,496,4,&value,0L,0L);
  242.     sqlasetv(1,1,496,4,&key,0L,0L);
  243.   sqlacall((unsigned short)24,1,1,0,0L);
  244.   sqlastop(0L);
  245. }
  246. #line 170 "D:\\dale\\stuff\\worker.sqc"
  247.  
  248.            if (SQLCODE==0)
  249.            {
  250.               
  251. /*
  252. EXEC SQL COMMIT WORK;
  253. */
  254.  
  255. {
  256.   sqlastrt(sqla_program_id,0L,&sqlca);
  257.   sqlacall((unsigned short)21,0,0,0,0L);
  258.   sqlastop(0L);
  259. }
  260. #line 173 "D:\\dale\\stuff\\worker.sqc"
  261.  
  262.               if (SQLCODE!=0)
  263.               {
  264.                sprintf(MyPieceofSharedMem->outarea,
  265.                     "Commit of update failed sqlcode = %ld",SQLCODE);
  266.                MyPieceofSharedMem->result=ITSBAD;
  267.  
  268.               }
  269.               else
  270.               {
  271.                sprintf(MyPieceofSharedMem->outarea,
  272.                  "Update of DEMOTABLE in db <%s> successful\n",dbNames[myindex]);
  273.                MyPieceofSharedMem->result=ITSOK;
  274.               }
  275.            }
  276.            else
  277.            {
  278.             sprintf(MyPieceofSharedMem->outarea,"Update failed sqlcode = %ld",SQLCODE);
  279.             MyPieceofSharedMem->result=ITSBAD;
  280.            }
  281.           break;
  282.        case REQINSERT:
  283.            key=  stuff->key;
  284.            value= stuff->value;
  285.            
  286. /*
  287. EXEC SQL INSERT INTO DEMOTABLE VALUES (:key,:value);
  288. */
  289.  
  290. {
  291.   sqlastrt(sqla_program_id,0L,&sqlca);
  292.   sqlaaloc(1,2,2,0L);
  293.     sqlasetv(1,0,496,4,&key,0L,0L);
  294.     sqlasetv(1,1,496,4,&value,0L,0L);
  295.   sqlacall((unsigned short)24,2,1,0,0L);
  296.   sqlastop(0L);
  297. }
  298. #line 197 "D:\\dale\\stuff\\worker.sqc"
  299.  
  300.            if (SQLCODE==0)
  301.            {
  302.               
  303. /*
  304. EXEC SQL COMMIT WORK;
  305. */
  306.  
  307. {
  308.   sqlastrt(sqla_program_id,0L,&sqlca);
  309.   sqlacall((unsigned short)21,0,0,0,0L);
  310.   sqlastop(0L);
  311. }
  312. #line 200 "D:\\dale\\stuff\\worker.sqc"
  313.  
  314.               if (SQLCODE!=0)
  315.               {
  316.                sprintf(MyPieceofSharedMem->outarea,
  317.                     "Commit of insert failed sqlcode = %ld",SQLCODE);
  318.                MyPieceofSharedMem->result=ITSBAD;
  319.  
  320.               }
  321.               else
  322.               {
  323.                sprintf(MyPieceofSharedMem->outarea,
  324.                  "Insert into DEMOTABLE in db <%s> successful\n",dbNames[myindex]);
  325.                MyPieceofSharedMem->result=ITSOK;
  326.               }
  327.            }
  328.            else
  329.            {
  330.             sprintf(MyPieceofSharedMem->outarea,"Insert failed sqlcode = %ld",SQLCODE);
  331.             MyPieceofSharedMem->result=ITSBAD;
  332.            }
  333.           break;
  334.        default:
  335.           break;
  336.        } /* endswitch */
  337.       DosResetEventSem(mysem,&postct);
  338.       rc=DosPostEventSem(mastersem);
  339.       if (rc)
  340.       {
  341.            sprintf(work,"unable to post master semaphore rc= %ld",rc);
  342.            ErrorLog(dbNames[myindex],work);
  343.            goto ERROREXIT;
  344.       }
  345.  
  346.   }
  347. ERROREXIT:
  348.        sqlestpd(&sqlca);
  349.        DosFreeMem(SharedMem);
  350.        DosCloseEventSem(mastersem);
  351.        DosCloseEventSem(mysem);
  352.        DosExit(EXIT_PROCESS,8);
  353. }
  354.