home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / ods / gateway / gateway.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  4.0 KB  |  148 lines

  1. // This program is an example of an Open Data Services application. It accepts
  2. // requests from clients and then passes those requests on to an SQL server.
  3. // The results from the SQL server are then sent back to the client.
  4.  
  5. #include    <stdlib.h>
  6. #include    <stdio.h>
  7. #include    <string.h>
  8. #include    <windows.h>
  9. #include    <sqlfront.h>
  10. #include    <sqldb.h>
  11. #include    <srv.h>
  12.  
  13. // Globals
  14. //
  15. DBCHAR *SrvrName = "";  // Default remote server name
  16. DBCHAR *RegistryName = "Gateway";   // Default registry name
  17.  
  18. #define EXIT_OK 0
  19. #define EXIT_ERROR 1
  20.  
  21. // function prototypes
  22. //
  23. void getargs(int argc, char **argv);
  24.  
  25. void main(int argc, char **argv);
  26.  
  27. int SRVAPI init_server(SRV_SERVER *server);
  28.  
  29. void SRVAPI set_remote_server_name(char *name);
  30.  
  31. int chk_err(SRV_SERVER *server, SRV_PROC *srvproc, int srverror,
  32.             BYTE severity, BYTE state, int oserrnum, DBCHAR *errtext,
  33.             int errtextlen, DBCHAR *oserrtext, int oserrtextlen);
  34.  
  35. void main(argc, argv)
  36. int argc;
  37. char *argv[];
  38. {
  39.     SRV_CONFIG *config; // The configuration structure
  40.     SRV_SERVER *server; // The service process
  41.     int exitcode = EXIT_ERROR;
  42.  
  43.     // Read any command line arguments.
  44.     //
  45.     getargs(argc, argv);
  46.  
  47.     // Send the name retrieved to the gateway's DLL module
  48.     //
  49.     set_remote_server_name(SrvrName);
  50.  
  51.     // Allocate a configuration structure that is used to initialize
  52.     // the Open Data Services application
  53.     //
  54.     config = srv_config_alloc();
  55.  
  56.     // Allow 20 connections at a time.
  57.     //
  58.     srv_config(config, (DBINT)SRV_CONNECTIONS, "100", SRV_NULLTERM);
  59.  
  60.     // Set the log file.
  61.     //
  62.     srv_config(config, (DBINT)SRV_LOGFILE, "gateway.log", SRV_NULLTERM);
  63.  
  64.     // The gateway will not convert data source strings in order to allow
  65.     // SQL Server and DB-libraray to coordinate on the codepage as usual.
  66.     //
  67.     srv_config(config, (DBINT)SRV_ANSI_CODEPAGE, "TRUE", SRV_NULLTERM);
  68.  
  69.     // Install the Open Data Services error handler.
  70.     //
  71.     srv_errhandle(chk_err);
  72.  
  73.     // Initialize the gateway and save the server handle
  74.     // so it can be used in later functions.
  75.     //
  76.     server = srv_init(config, RegistryName, SRV_NULLTERM);
  77.     if (server == NULL) {
  78.         printf("Unable to initialize Gateway.\n");
  79.         ExitProcess(exitcode);
  80.     }
  81.  
  82.     // When starting the gateway, initialize the remote server structure.
  83.     // This is done in the init_server() function.
  84.     // All the other event handlers are also defined in the init_server()
  85.     // function.
  86.     //
  87.     srv_handle(server, (DBINT)SRV_START, init_server);
  88.  
  89.     // Now everything's ready to go with our gateway, so we
  90.     // start it and keep it going until we get a stop request.
  91.     //
  92.     srv_log(server, FALSE, " ", SRV_NULLTERM);  // insert blank line
  93.     srv_log(server, TRUE, "Gateway Starting", SRV_NULLTERM);
  94.  
  95.     printf("\nGateway Starting\n");
  96.  
  97.     srv_run(server);
  98.  
  99. }
  100.  
  101. // GETARGS
  102. //    Read the command line arguments.
  103. //
  104. // Parameters:
  105. //     argc - int (from "main" entry)
  106. //     argv - pointer to array of char pointers (from "main" entry)
  107. //
  108. // Returns:
  109. //     VOID
  110. //
  111. void getargs(int argc, char *argv[])
  112. {
  113.  
  114.     int i;
  115.     char *ptr;
  116.     int exitcode = EXIT_ERROR;
  117.  
  118.     for (i = 1; i < argc; ++i) {
  119.         if (argv[i][0] != '-' && argv[i][0] != '/') {
  120.             printf(
  121.             "Usage: gateway -S<remote server name> [-R<registry key name>]\n");
  122.                 
  123.             ExitProcess(exitcode);
  124.         }
  125.         ptr = argv[i];
  126.         switch (ptr[1]) {
  127.  
  128.         case 'S':
  129.             if (strlen(ptr + 2) > 0) {
  130.                 SrvrName = ptr + 2;
  131.             }
  132.             break;
  133.  
  134.         case 'R':
  135.             if (strlen(ptr + 2) > 0) {
  136.                 RegistryName = ptr + 2;
  137.             }
  138.             break;
  139.  
  140.         default:
  141.             printf(
  142.             "Usage: gateway -S<remote server name> [-R<registry key name>]\n");
  143.                 
  144.             ExitProcess(exitcode);
  145.         }
  146.     }
  147. }
  148.