home *** CD-ROM | disk | FTP | other *** search
/ Network Support Encyclopedia 96-1 / novell-nsepro-1996-1-cd2.iso / download / netware / an401x.exe / JOBSERV.C < prev    next >
C/C++ Source or Header  |  1993-11-07  |  7KB  |  236 lines

  1. /*
  2.  ████████████████████████████████████████████████████████████████████████████
  3.  █                                                                          █
  4.  █ jobserv.c                                                                █
  5.  █                                                                          █
  6.  █ service a DOS command line job that has been put in the job queue by     █
  7.  █ doit.exe                                                                 █
  8.  █                                                                          █
  9.  ████████████████████████████████████████████████████████████████████████████
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <io.h>
  15. #include <mem.h>
  16. #include <conio.h>
  17. #include <string.h>
  18. #include <nwcalls.h>
  19. #include <nwerror.h>
  20. #include "..\doit.h"
  21.  
  22. #define NWDOS
  23.  
  24. #define ESC 0x1B
  25.  
  26. #ifndef SUCCESSFUL
  27. #define SUCCESSFUL 0x0000
  28. #endif
  29.  
  30. int            done=0,
  31.             gotone;
  32. NWCCODE             cCode;
  33. NWOBJ_ID        oID;
  34. DWORD               queueID;
  35. NWFILE_HANDLE       fileHandle;
  36. NWCONN_HANDLE        connID;
  37. char                command[181],
  38.             line[80],
  39.             ch;
  40. WORD                jobType;
  41. NWQueueJobStruct    jobStructure;
  42. NWOBJ_TYPE        objectType;
  43. char NWFAR        userName[48];
  44. NWHOLD_AMOUNT        holdAmount;
  45.  
  46. void process_job(void);
  47.  
  48. void main()
  49. {
  50.     cCode = NWCallsInit(NULL, NULL);
  51.     if (cCode == SUCCESSFUL)
  52.         printf("Call to NWCallsInit successful\n");
  53.     else {
  54.         printf("Call to NWCallsInit failed\n");
  55.         exit(-1);
  56.     }
  57.  
  58.     /* get the connection ID of the default server */
  59.     cCode = NWGetDefaultConnectionID(&connID);
  60.     if (cCode == SUCCESSFUL)
  61.         printf("Call to NWGetDefaultConnectionID successful\n");
  62.     else {
  63.         printf("Call to NWGetDefaultConnectionID failed, cCode = %X\n", cCode);
  64.         exit(-1);
  65.     }
  66.  
  67.     /* check to see if the default server has had the job server
  68.        object installed on it
  69.        If so, assume that queue directory exists, queue object was created, etc. */
  70.     cCode = NWScanObject(connID, JOBSERV_NAME, OT_DOIT, &oID, NULL, NULL, NULL, NULL, NULL);
  71.     if (cCode != SUCCESSFUL) {
  72.         printf("Job server has not been installed on this file server.\n");
  73.         exit(-1);
  74.     }
  75.  
  76.     /* log out as who you were */
  77.     cCode = NWLogoutFromFileServer(connID);
  78.     if (cCode != SUCCESSFUL) {
  79.         printf("Call to NWLogoutFromFileServer failed, cCode = %X\n", cCode);
  80.         exit(-1);
  81.     }
  82.  
  83.     /* log in as the job server object */
  84.     cCode = NWLoginToFileServer(connID, JOBSERV_NAME, OT_DOIT, JOBSERV_PWORD);
  85.     if (cCode != SUCCESSFUL) {
  86.         printf("Call to NWLoginToFileServer failed, cCode = %X\n", cCode);
  87.         exit(-1);
  88.     }
  89.  
  90.     /* get the bindery object ID of the queue we will be servicing */
  91.     cCode = NWGetObjectID(connID, JOBSERV_NAME, OT_DOIT_Q, &queueID);
  92.     if (cCode != SUCCESSFUL) {
  93.         printf("Call to NWGetObjectID failed, cCode = %X\n", cCode);
  94.         exit(-1);
  95.     }
  96.  
  97.     /* attach to the queue we are going to service */
  98.     cCode = NWAttachQueueServerToQueue(connID, queueID);
  99.     if (cCode != SUCCESSFUL) {
  100.         printf("Call to NWAttachQueueServerToQueue failed, cCode = %X\n", cCode);
  101.         exit(-1);
  102.     }
  103.  
  104.     /* display prompt */
  105.     memset(line, '-', 79);
  106.     clrscr();
  107.     printf("Job server example program\n");
  108.     printf("Press ESC to exit\n");
  109.     gotoxy(1,5);
  110.     printf("%s\n", line);
  111.     printf("> \n");
  112.     printf("%s\n", line);
  113.  
  114.     /* continue requesting jobs from the queue until user presses ESC */
  115.     while (!(done)) {
  116.         do {
  117.             /* first, look for priority 1 */
  118.             jobType = DOIT_JOB_PRIORITY_1;
  119.  
  120.             /* see if there are any jobs to be serviced */
  121.             cCode = NWServiceQueueJob2(connID, queueID, jobType, &jobStructure, &fileHandle);
  122.             if (cCode != SUCCESSFUL) {
  123.                 jobType = DOIT_JOB_PRIORITY_2;
  124.                 cCode = NWServiceQueueJob2(connID, queueID, jobType, &jobStructure, &fileHandle);
  125.                 if (cCode != SUCCESSFUL) {
  126.                     gotoxy(1,4);
  127.                     printf("No jobs in queue");
  128.                 }
  129.             }
  130.  
  131.             /* if there is a job then process it, otherwise keep asking */
  132.             if (!(cCode)) {
  133.                 /* submit a hold on the user's account */
  134.                 /* first get his name and object type */
  135.                 cCode = NWGetObjectName(connID, jobStructure.clientID, userName, &objectType);
  136.                 if (cCode != SUCCESSFUL) {
  137.                     printf("Unable to get name of user submitting job\n");
  138.                     exit(-1);
  139.                 }
  140.  
  141.                 /* then submit the hold; charge more for a high-priority job */
  142.                 if (jobStructure.jobType == DOIT_JOB_PRIORITY_1)
  143.                     holdAmount = 2;
  144.                 else
  145.                     holdAmount = 1;
  146.  
  147.                 cCode = NWSubmitAccountHold(connID, objectType, userName, holdAmount);
  148.                 if (cCode != SUCCESSFUL)
  149.                     printf("Unable to submit account hold\n");
  150.  
  151.                 /* assume the rights of the client who submitted the job */
  152.                 cCode = NWChangeToClientRights2(connID, queueID, jobStructure.jobNumber);
  153.                 if (cCode != SUCCESSFUL) {
  154.                     printf("Call to NWChangeToClientRights2 failed, cCode = %X\n", cCode);
  155.                     exit(-1);
  156.                 }
  157.  
  158.                 /* process the job */
  159.                 process_job();
  160.  
  161.                 /* redisplay prompt */
  162.                 gotoxy(1,1);
  163.                 printf("Job server example program\n");
  164.                 printf("Press ESC to exit\n");
  165.                 gotoxy(1,5);
  166.                 printf("%s\n", line);
  167.                 printf("> \n");
  168.                 printf("%s\n", line);
  169.  
  170.                 /* clear the command string */
  171.                 memset(command,0,181);
  172.  
  173.                 /* set flag saying there is a job to service */
  174.                 gotone = 1;
  175.             }
  176.  
  177.             /* check to see if ESC has been hit */
  178.             if (kbhit()) {
  179.                 ch = getch();
  180.                 if (ch == ESC) done = 1;
  181.             }
  182.  
  183.         /* loop until done or until we have a job */
  184.         } while ((!(gotone)) && (!(done)));
  185.  
  186.         if (gotone) {
  187.             /* tell QMS we've successfully serviced the queue job */
  188.             cCode = NWFinishServicingQueueJob2(connID, queueID, jobStructure.jobNumber, fileHandle);
  189.             if (cCode != SUCCESSFUL) {
  190.                 gotoxy(1,3);
  191.                 printf("Call to NWFinishServicingQueueJob2 failed, cCode = %X", cCode);
  192.                 exit(-1);
  193.             }
  194.         }
  195.  
  196.         /* submit account charge and cancel hold; do not submit an account note */
  197.         cCode = NWSubmitAccountCharge(connID, objectType, userName, OT_DOIT, holdAmount, holdAmount, NULL, NULL);
  198.         if (cCode != SUCCESSFUL)
  199.             printf("Unable to submit account charge\n");
  200.  
  201.         /* continue looking for commands */
  202.         gotone = 0;
  203.     }
  204.  
  205.     /* if the user hit ESC, then detach from the queue and logout */
  206.     /* the workstation will remain connected and default to the LOGIN directory */
  207.  
  208.     /* detach from the queue */
  209.     gotoxy(1,24);
  210.     cCode = NWDetachQueueServerFromQueue(connID, queueID);
  211.     if (cCode == SUCCESSFUL)
  212.         printf("Call to NWDetachQueueServerFromQueue successful\n");
  213.     else {
  214.         printf("Call to NWDetachQueueServerFromQueue failed, cCode = %X\n", cCode);
  215.     }
  216.  
  217.     /* logout from the file server */
  218.     cCode = NWLogoutFromFileServer(connID);
  219.     if (cCode == SUCCESSFUL)
  220.         printf("Call to NWLogoutFromFileServer successful\n");
  221.     else
  222.         printf("Call to NWLogoutFromFileServer failed, cCode = %X\n", cCode);
  223. }
  224.  
  225. void process_job(void)
  226. {
  227.     /* process the job by reading the file and sending the command to */
  228.     /* system() */
  229.     clrscr();
  230.     read(fileHandle, command, 181);
  231.     gotoxy(3,6);
  232.     printf("%s\n",command);
  233.     gotoxy(1,8);
  234.     system(command);
  235. }
  236.