home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somd / cpp / animal / testsvr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  10.2 KB  |  346 lines

  1. //
  2. //   COMPONENT_NAME: somx
  3. //
  4. //   ORIGINS: 27
  5. //
  6. //
  7. //   10H9767, 10H9769  (C) COPYRIGHT International Business Machines Corp. 1992,1994
  8. //   All Rights Reserved
  9. //   Licensed Materials - Property of IBM
  10. //   US Government Users Restricted Rights - Use, duplication or
  11. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  12. //
  13.  
  14. /* %Z% %I% %W% %G% %U% [%H% %T%] */
  15. /*
  16.  *
  17.  * DISCLAIMER OF WARRANTIES.
  18.  * The following [enclosed] code is sample code created by IBM
  19.  * Corporation. This sample code is not part of any standard or IBM
  20.  * product and is provided to you solely for the purpose of assisting
  21.  * you in the development of your applications.  The code is provided
  22.  * "AS IS". IBM MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
  23.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24.  * FOR A PARTICULAR PURPOSE, REGARDING THE FUNCTION OR PERFORMANCE OF
  25.  * THIS CODE.  IBM shall not be liable for any damages arising out of
  26.  * your use of the sample code, even if they have been advised of the
  27.  * possibility of such damages.
  28.  *
  29.  * DISTRIBUTION.
  30.  * This sample code can be freely distributed, copied, altered, and
  31.  * incorporated into other software, provided that it bears the above
  32.  * Copyright notice and DISCLAIMER intact.
  33.  */
  34.  
  35.  
  36. /*
  37.  *  This is a generic server which can use the shared server or persistent
  38.  *  server activation policy.  It can also be threaded or non-threaded.
  39.  *
  40.  *  Syntax:  testsvr <implid> | -a <alias>
  41.  */
  42.  
  43.  
  44. #ifndef _ALL_SOURCE
  45. #define _ALL_SOURCE
  46. #endif
  47.  
  48. #include <windows.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <signal.h>
  52. #include <somd.xh>
  53. #include <somoa.xh>
  54. #include <implrep.xh>
  55.  
  56. #include "nlsutil.h"
  57. #include "testsvr.h"
  58.  
  59. /***************************************************
  60. Messages for NLS support
  61. ****************************************************/
  62. static char *nlsmsgs[ENDNLSID-STARTNLSID+1];
  63. #define GetNlsMessage(id) nlsmsgs[id-STARTNLSID]
  64. #define SetNlsMessage(id, str) nlsmsgs[id-STARTNLSID] = (str);
  65.  
  66. /**********************************************************
  67. 18953: NLS support
  68. This procedure must be called first to initialize all the messages
  69. to be used by irdump. These messages are obtained from the resource file.
  70. ****************************************************************/
  71. static void InitNlsMsgs(){
  72.  
  73. SetNlsMessage(FindImpldefFailId, NlsMsgAlloc(FindImpldefFailId));
  74. SetNlsMessage(FindImpldefaliasFailId, NlsMsgAlloc(FindImpldefaliasFailId));
  75. SetNlsMessage(InvalidArgumentId ,NlsMsgAlloc(InvalidArgumentId));
  76. SetNlsMessage(InvalidNumArgsId ,NlsMsgAlloc(InvalidNumArgsId));
  77. SetNlsMessage(ImplReadyFailId ,NlsMsgAlloc(ImplReadyFailId));
  78. SetNlsMessage(NoThreadId, NlsMsgAlloc(NoThreadId));
  79. SetNlsMessage(LoopFailId, NlsMsgAlloc(LoopFailId));
  80. }
  81.  
  82. #define WM_SOMDSVR  (WM_USER+0)
  83. #define checkEv(ev) ((ev)->_major != NO_EXCEPTION)
  84. #define MAX_ARGS 25
  85. #define ARG_LENGTH 80
  86.  
  87. long APIENTRY WndProc(HWND, UINT, UINT, LONG);
  88. long          ProcessInputArgs(LPSTR, long *, char **, long);
  89. long          testsvrProcess(HWND);
  90. void          cleanup(Environment *, long);
  91.  
  92. DWORD WINAPI  request_loop_thread(LPVOID);
  93. static HANDLE gThreadId;
  94. static BOOL   gShutdown;
  95.  
  96. char **Argv = NULL;
  97. long Argc = 1;
  98. char buffer[100] = "";
  99. static char szAppName[] = "Testsvr";
  100.  
  101.  
  102. Environment ev = {(enum exception_type) 0, NULL, NULL, NULL};
  103.  
  104. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  105.                     LPSTR lpszCmdLine, int nCmdShow)
  106. {
  107.     HWND hwnd;
  108.     MSG nlsmsg;
  109.     WNDCLASS wndclass;
  110.     long i;
  111.  
  112.     InitNlsMsgs();
  113.  
  114.     somEnvironmentNew();
  115.  
  116.     if (!hPrevInstance) {
  117.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  118.         wndclass.lpfnWndProc = WndProc;
  119.         wndclass.cbClsExtra = 0;
  120.         wndclass.cbWndExtra = 0;
  121.         wndclass.hInstance = hInstance;
  122.         wndclass.hIcon = LoadIcon(hInstance, szAppName);
  123.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  124.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  125.         wndclass.lpszMenuName = NULL;
  126.         wndclass.lpszClassName = szAppName;
  127.         RegisterClass(&wndclass);
  128.     }
  129.  
  130.     hwnd = CreateWindow(szAppName, szAppName,
  131.                         WS_OVERLAPPEDWINDOW | WS_MINIMIZE,
  132.                         CW_USEDEFAULT, CW_USEDEFAULT,
  133.                         CW_USEDEFAULT, CW_USEDEFAULT,
  134.                         NULL, NULL, hInstance, NULL);
  135.  
  136.     ShowWindow(hwnd, SW_SHOW);
  137.  
  138.  
  139.     Argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
  140.     for (i = 0; i < MAX_ARGS; ++i)
  141.         Argv[i] = (char *) calloc(ARG_LENGTH, sizeof(char *));
  142.  
  143.     ProcessInputArgs(lpszCmdLine, &Argc, Argv, MAX_ARGS);
  144.  
  145.     /* Start the server with Windows user message */
  146.     PostMessage(hwnd, WM_SOMDSVR, 0, 0);
  147.  
  148.     /* Windows message loop */
  149.     while (GetMessage(&nlsmsg, NULL, 0, 0)) {
  150.         TranslateMessage(&nlsmsg);
  151.         DispatchMessage(&nlsmsg);
  152.     }
  153.  
  154.     return nlsmsg.wParam;
  155.  
  156. }                                      /* end main */
  157.  
  158. long APIENTRY WndProc(HWND hwnd, UINT message, UINT wParam,
  159.                       LONG lParam)
  160. {
  161.     switch (message) {
  162.         case WM_SOMDSVR:
  163.             testsvrProcess(hwnd);
  164.             return 0;
  165.  
  166.         case WM_DESTROY:
  167.             gShutdown = TRUE;
  168.             if (gThreadId)
  169.               Sleep(0);
  170.             cleanup(0,0);
  171.             PostQuitMessage(0);
  172.             return 0;
  173.     }
  174.     return DefWindowProc(hwnd, message, wParam, lParam);
  175. }
  176.  
  177.  
  178. long testsvrProcess(HWND hwnd)
  179. {
  180.     ImplId implid;
  181.     Environment ev;
  182.     char *alias;
  183.     char buf[75] = "DSOM Server - ";
  184.  
  185.     /* initialization */
  186.     SOM_InitEnvironment(&ev);
  187.     SOMD_Init(&ev);
  188.     SOMD_SOMOAObject = (SOMOA *) new SOMOA;
  189.  
  190.     /* Parse arguments */
  191.     switch (Argc) {
  192.  
  193.         case 2:
  194.             implid = Argv[1];
  195.             /* get ImplementationDef from Implementation Repository */
  196.             SOMD_ImplDefObject = SOMD_ImplRepObject->find_impldef(&ev, implid);
  197.             if ((SOMD_ImplDefObject == NULL) || checkEv(&ev)) {
  198.                 wsprintf(buffer, GetNlsMessage(FindImpldefFailId),
  199.                          (LPSTR) somExceptionId(&ev));
  200.                 MessageBox(hwnd, buffer, "TstServer", MB_ICONSTOP);
  201.                 cleanup(&ev,1);
  202.             }
  203.             alias = SOMD_ImplDefObject->_get_impl_alias(&ev);
  204.             strcat(buf, alias);
  205.             SetWindowText(hwnd, buf);
  206.             break;
  207.  
  208.         case 3:
  209.             /* alias passed  (possibly) */
  210.             /* check for a valid option */
  211.             if ((Argv[1][0] == '-') &&
  212.                 ((Argv[1][1] == 'a') || (Argv[1][1] == 'A'))) {
  213.                 alias = Argv[2];
  214.                 strcat(buf, alias);
  215.                 SetWindowText(hwnd, buf);
  216.                 /* get ImplementationDef from input alias */
  217.                 SOMD_ImplDefObject =
  218.                     SOMD_ImplRepObject->find_impldef_by_alias(&ev, alias);
  219.                 if ((SOMD_ImplDefObject == NULL) || checkEv(&ev)) {
  220.                     wsprintf(buffer, GetNlsMessage(FindImpldefaliasFailId),
  221.                              (LPSTR) somExceptionId(&ev));
  222.                     MessageBox(hwnd, buffer, "TstServer", MB_ICONSTOP);
  223.                     cleanup(&ev,1);
  224.                 }
  225.             }
  226.             else {
  227.                 MessageBox(hwnd, GetNlsMessage(InvalidArgumentId), "TstServer", MB_ICONSTOP);
  228.                 cleanup(&ev,1);
  229.             }
  230.             break;
  231.  
  232.  
  233.         default:
  234.             MessageBox(hwnd, GetNlsMessage(InvalidNumArgsId), "TstServer",
  235.                        MB_ICONSTOP);
  236.             cleanup(&ev,1);
  237.             break;
  238.  
  239.     }                                  /* switch */
  240.  
  241.     /* implementation now ready to process requests */
  242.     SOMD_SOMOAObject->impl_is_ready(&ev, SOMD_ImplDefObject);
  243.     if (checkEv(&ev)) {
  244.         wsprintf(GetNlsMessage(ImplReadyFailId),
  245.                  (LPSTR) somExceptionId(&ev));
  246.         MessageBox(hwnd, buffer, "TstServer", MB_ICONSTOP);
  247.         cleanup(&ev,1);
  248.     }
  249.  
  250.     /*
  251.     **  kick off a thread to process the messages so control can
  252.     **  can go back top our message loop
  253.     */
  254.     if ((gThreadId = CreateThread(NULL, /* security descriptor */
  255.                                   0L, /* stack size - 0 means use default */
  256.                                   request_loop_thread, /* thread function */
  257.                                   (LPVOID)hwnd, /* function parameters */
  258.                                   0, /* startup flags */
  259.                                   (LPDWORD)&gThreadId)) == NULL) {
  260.        MessageBox(hwnd, GetNlsMessage(NoThreadId), "TstServer", MB_ICONSTOP);
  261.        cleanup(&ev, 1);
  262.     }
  263.  
  264.     SOM_UninitEnvironment(&ev);
  265.     return 0;
  266. }
  267.  
  268. long ProcessInputArgs(LPSTR lpszCmdLine, long *Argc, char **Argv, long
  269.                        argSize)
  270. {
  271.  
  272.     char *c;
  273.     long i;
  274.  
  275.     *Argc = 1;                         /* lpszCmdLine does not argv[0] */
  276.  
  277.     /* lpszCmdLine has command line arguments delimited by blanks */
  278.  
  279.     c = strtok((char *) lpszCmdLine, " ");
  280.  
  281.     for (i = 0; i < argSize; ++i) {
  282.         if (c == NULL)
  283.             break;
  284.         strcpy((char *) Argv[*Argc], (char *) c);
  285.         c = strtok(NULL, " ");
  286.         *Argc += 1;
  287.     }
  288.     return *Argc;
  289. }
  290.  
  291.  
  292. /*
  293.  *  Frees local and global DSOM objects.
  294.  */
  295.  
  296. void cleanup(Environment * ev, long rc)
  297. {
  298.   Environment  lev;
  299.   Environment *pev = &lev;
  300.  
  301.   if (!ev)
  302.     SOM_InitEnvironment(pev);
  303.   else
  304.     pev = ev;
  305.  
  306.   if (SOMD_SOMOAObject && SOMD_ImplDefObject)
  307.      SOMD_SOMOAObject->deactivate_impl(pev, SOMD_ImplDefObject);
  308.  
  309.   SOMD_Uninit(pev);
  310.   SOM_UninitEnvironment(pev);
  311.   somEnvironmentEnd();
  312.  
  313.   if (ev) exit((int)rc);
  314. }
  315.  
  316.  
  317. /*
  318. ** thread for the request loop.
  319. */
  320.  
  321. DWORD WINAPI
  322. request_loop_thread(LPVOID params)
  323. {
  324.   HANDLE       hwnd = (HANDLE)params;
  325.   Environment  ev;
  326.   char         lbuffer[64];
  327.  
  328.   SOM_InitEnvironment(&ev);
  329.  
  330.   while (!gShutdown) {
  331.     /* process requests until deactivation */
  332.     (void) SOMD_SOMOAObject->execute_request_loop(&ev, SOMD_WAIT);
  333.     if (checkEv(&ev)) {
  334.         wsprintf(lbuffer, GetNlsMessage(LoopFailId),
  335.                  (LPSTR) somExceptionId(&ev));
  336.         MessageBox(hwnd, lbuffer, "TstServer", MB_ICONSTOP);
  337.         cleanup(&ev,1);
  338.     }
  339.   }
  340.  
  341.   SOM_UninitEnvironment(&ev);
  342.  
  343.   return(0L);
  344.   
  345. }
  346.