home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somd / cpp / event / consumer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  14.3 KB  |  395 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.    CONSUMER.C -- DSOM Sample Program
  37.   ----------------------------------------*/
  38.  
  39. #include <windows.h>
  40. #include <string.h>
  41. #include <malloc.h>
  42. #include <stdio.h>
  43. #include <errno.h>
  44.  
  45. #include <somd.xh>
  46. #include <orb.xh>
  47.  
  48. #define WIN32_USE_CLASS_RESOLVE_MACROS
  49. #include "eventcom.xh"
  50. #include "eventch.xh"
  51. #undef WIN32_USE_CLASS_RESOLVE_MACROS
  52.  
  53. /************************************************************
  54. 19982
  55. Given file name, return a file name with the prefix for
  56. the temporary directory . Return NULL if insufficient buffer space.
  57. Note: This implementation returns pointer to a static variable.
  58. *************************************************************/
  59. #define TEMP_FILE_NAME_SIZE 256
  60. static char tempFileName[TEMP_FILE_NAME_SIZE];
  61. char *TmpFileName(char *fname){
  62. int len;
  63.     len = GetEnvironmentVariable("TEMP", tempFileName, TEMP_FILE_NAME_SIZE);
  64.     if ( len == 0 || (len+strlen(fname)+1 > TEMP_FILE_NAME_SIZE) )
  65.         return NULL;
  66.     strcat(tempFileName,"\\");
  67.     strcat(tempFileName, fname);
  68.     return tempFileName;
  69. }
  70.  
  71. long APIENTRY WndProc (HWND, UINT, UINT, LONG) ;
  72. void WinSleep(unsigned long);
  73. void printEv(Environment *ev);
  74.  
  75. #define checkEv(ev) ((ev)->_major != NO_EXCEPTION)
  76.  
  77. struct {
  78.      long style ;
  79.      char *text ;
  80. }
  81. button[] = {
  82.      BS_PUSHBUTTON, "Poll the event channel", /* 18953:NLS in InitNlsMsgs()*/
  83.      BS_PUSHBUTTON, "Terminate the consumer",
  84. } ;
  85.  
  86. /**********************************************************
  87. 18953: NLS support
  88. ***********************************************************/
  89. #include "consumer.h"
  90. #include "nlsutil.h"
  91. /***************************************************
  92. Messages for NLS support
  93. ****************************************************/
  94. static char *nlsmsgs[ENDNLSID-STARTNLSID+1];
  95. #define GetNlsMessage(id) nlsmsgs[id-STARTNLSID]
  96. #define SetNlsMessage(id, str) nlsmsgs[id-STARTNLSID] = (str);
  97.  
  98. /**********************************************************
  99. 18953: NLS support
  100. This procedure must be called first to initialize all the messages
  101. to be used by the sample. These messages are obtained from the resource file.
  102. ****************************************************************/
  103. static void InitNlsMsgs(){
  104. SetNlsMessage(ObjrefErrorId , NlsMsgAlloc(ObjrefErrorId ));
  105. SetNlsMessage(CreateErrorId, NlsMsgAlloc(CreateErrorId ));
  106. SetNlsMessage(CurrentValueId, NlsMsgAlloc(CurrentValueId ));
  107. SetNlsMessage(ErrorOccurId , NlsMsgAlloc(ErrorOccurId ));
  108. SetNlsMessage(MinorCodeId , NlsMsgAlloc(MinorCodeId ));
  109. SetNlsMessage(CompletionCodeId, NlsMsgAlloc(CompletionCodeId ));
  110. SetNlsMessage(ExceptionId, NlsMsgAlloc(ExceptionId));
  111. SetNlsMessage(DisconnectId, NlsMsgAlloc(DisconnectId));
  112. SetNlsMessage(YesId, NlsMsgAlloc(YesId ));
  113. SetNlsMessage(NoId, NlsMsgAlloc(NoId));
  114. SetNlsMessage(MaybeId, NlsMsgAlloc(MaybeId ));
  115. SetNlsMessage(PollId, NlsMsgAlloc(PollId));
  116. SetNlsMessage(TerminateId, NlsMsgAlloc(TerminateId));
  117.  
  118. button[0].text = GetNlsMessage(PollId);
  119. button[1].text = GetNlsMessage(TerminateId);
  120. }
  121.  
  122. #define NUM (sizeof button / sizeof button [0])
  123.  
  124. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  125.                     LPSTR lpszCmdLine, int nCmdShow)
  126.      {
  127.      static char szAppName[] = "consumer" ;
  128.      HWND        hwnd ;
  129.      MSG         msg ;
  130.      WNDCLASS    wndclass ;
  131.  
  132.      InitNlsMsgs();
  133.  
  134.      somEnvironmentNew();
  135.  
  136.      if (!hPrevInstance)
  137.           {
  138.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  139.           wndclass.lpfnWndProc   = WndProc ;
  140.           wndclass.cbClsExtra    = 0 ;
  141.           wndclass.cbWndExtra    = 0 ;
  142.           wndclass.hInstance     = hInstance ;
  143.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  144.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  145.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  146.           wndclass.lpszMenuName  = NULL ;
  147.           wndclass.lpszClassName = szAppName ;
  148.  
  149.           RegisterClass (&wndclass) ;
  150.           }
  151.  
  152.      hwnd = CreateWindow (szAppName, "DSOM Event Sample -- Consumer",
  153.                           WS_OVERLAPPEDWINDOW,
  154.                           CW_USEDEFAULT, CW_USEDEFAULT,
  155.                           CW_USEDEFAULT, CW_USEDEFAULT,
  156.                           NULL, NULL, hInstance, NULL) ;
  157.  
  158.      ShowWindow (hwnd, nCmdShow) ;
  159.      UpdateWindow (hwnd) ;
  160.  
  161.      while (GetMessage (&msg, NULL, 0, 0))
  162.           {
  163.           TranslateMessage (&msg) ;
  164.           DispatchMessage (&msg) ;
  165.           }
  166.      return msg.wParam ;
  167.      }
  168.  
  169. long APIENTRY WndProc (HWND hwnd, UINT message, UINT wParam,
  170.                        LONG lParam)
  171.      {
  172.      static HWND  hwndButton [NUM] ;
  173.      static int   buttonx [NUM] = { 10, 10 };
  174.      static int   buttony [NUM] = { 9, 12};
  175.      static int   cxChar, cyChar ;
  176.      int i;
  177.      HDC          hdc ;
  178.      TEXTMETRIC   tm ;
  179.      static HCURSOR hCursor ;
  180.      PAINTSTRUCT  ps ;
  181.      char buffer[100];
  182.  
  183.      static Environment  NEAR                    ev;
  184.      static EventChannelAdmin_EventChannel       *evchObj;
  185.      static EventComm_Supplier                   *supp_if;
  186.      static EventChannelAdmin_ConsumerAdmin      *cons_admin;
  187.      static EventComm_EventConnection            *evcon_if;
  188.      static boolean                              has_event;
  189.      static any     NEAR                         notify;
  190.      FILE                                       *fileref;
  191.      string                                      *stringref;
  192.      char *fname; /* 19982 */
  193.  
  194.      switch (message)
  195.           {
  196.           case WM_CREATE:
  197.  
  198.                 /* local and DSOM initialization */
  199.                 SOM_InitEnvironment(&ev);
  200.                 SOMD_Init(&ev);
  201.                 EventComm_EventConnectionNewClass(0,0);
  202.                 EventComm_ConsumerNewClass(0,0);
  203.                 EventComm_SupplierNewClass(0,0);
  204.                 EventChannelAdmin_ConsumerAdminNewClass(0,0);
  205.                 EventChannelAdmin_SupplierAdminNewClass(0,0);
  206.                 EventChannelAdmin_EventChannelNewClass(0,0);
  207.                 evchObj = NULL;
  208.  
  209.                 fname = TmpFileName("event.rep"); /* 19982 */
  210.                 if (/* 19982*/ fname == NULL ||
  211.                    !(fileref = fopen(fname /* 19982 "event.rep" */,"r"))) {
  212.                         MessageBox ((HWND)NULL,
  213.                         "Unable to find object reference string in event.rep.",
  214.                                "File Open Error", MB_ICONEXCLAMATION | MB_OK);
  215.                         SendMessage(hwnd, WM_CLOSE, 0, 0L);
  216.             SOMD_Uninit(&ev);
  217.                         return 0 ;
  218.                 }
  219.                 stringref = (string *)SOMMalloc(512);
  220.                 fscanf(fileref,"%s",stringref);
  221.                 fclose(fileref);
  222.                 evchObj = (EventChannelAdmin_EventChannel *)(SOMD_ORBObject->string_to_object(&ev,(char *)stringref));
  223.                 if (!evchObj) {
  224.                         wsprintf(buffer, GetNlsMessage(ObjrefErrorId),
  225.                                     stringref);
  226.                         MessageBox ((HWND)NULL,  buffer, GetNlsMessage(CreateErrorId),
  227.                                     MB_ICONEXCLAMATION | MB_OK);
  228.                         SendMessage(hwnd, WM_CLOSE, 0, 0L);
  229.             SOMD_Uninit(&ev);
  230.                         return 0 ;
  231.                 }
  232.                 SOMFree(stringref);
  233.  
  234.                 cons_admin = evchObj->for_consumers(&ev);
  235.                 evcon_if = (EventComm_EventConnection *)(SOMD_ObjectMgr->somdNewObject(&ev,"EventComm::EventConnection", NULL));
  236.                 if (checkEv(&ev))  {
  237.                                 printEv(&ev);
  238.                                 SendMessage(hwnd, WM_CLOSE, 0, 0L);
  239.                     SOMD_Uninit(&ev);
  240.                                 return 0;
  241.                         }
  242.                 supp_if = cons_admin->add_pull_consumer(&ev, evcon_if);
  243.                 has_event = FALSE;
  244.  
  245.                /* Display button selections: */
  246.                hdc = GetDC (hwnd) ;
  247.                GetTextMetrics (hdc, &tm) ;
  248.                cxChar = tm.tmAveCharWidth ;
  249.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  250.                ReleaseDC (hwnd, hdc) ;
  251.                for (i = 0 ; i < NUM ; i++)
  252.                     hwndButton [i] =
  253.                         CreateWindow ("button",
  254.                                       button[i].text,
  255.                                       WS_CHILD | WS_VISIBLE | button[i].style,
  256.                                       buttonx[i] * cxChar,
  257.                                       cyChar * buttony[i],
  258.                                       40 * cxChar,
  259.                                       7 * cyChar / 4,
  260.                                       hwnd,
  261.                                       (HMENU)i,
  262.                                       ((LPCREATESTRUCT)lParam)->hInstance,
  263.                                       NULL);
  264.                return 0 ;
  265.  
  266.           case WM_PAINT:
  267.  
  268.                 hdc = BeginPaint (hwnd, &ps) ;
  269.                 GetTextMetrics (hdc, &tm) ;
  270.                 cyChar = tm.tmHeight + tm.tmExternalLeading;
  271.                 cxChar = tm.tmAveCharWidth;
  272.  
  273.                 if (has_event) {
  274.                         wsprintf(buffer, GetNlsMessage(CurrentValueId),
  275.                                 *((long *)notify._value));
  276.                         TextOut (hdc, cxChar*10, 4*cyChar,
  277.                                 (LPSTR)buffer, strlen(buffer));
  278.                 }
  279.  
  280.                 EndPaint (hwnd, &ps) ;
  281.                 return 0 ;
  282.  
  283.           case WM_COMMAND:
  284.           case WM_DRAWITEM:
  285.  
  286.                 ValidateRect (hwnd, NULL) ;
  287.                 switch (wParam)
  288.                         {
  289.                         case 0: /* poll event channel */
  290.  
  291.                                 hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  292.                                 ShowCursor(TRUE);
  293.  
  294.                                 if (has_event) {
  295.                                     TypeCode_free(notify._type, &ev);
  296.                                     has_event = FALSE;
  297.                                 }
  298.                                 while(!has_event) {
  299.                                         has_event= supp_if->try_pull(&ev, ¬ify);
  300.                                         if (checkEv(&ev))  {
  301.                                            printEv(&ev);
  302.                                            SendMessage(hwnd, WM_CLOSE, 0, 0L);
  303.                                SOMD_Uninit(&ev);
  304.                                            return 0;
  305.                                         }
  306.                                         if (!has_event)
  307.                                            WinSleep((unsigned long)1000);
  308.                                 }
  309.  
  310.                                 ShowCursor(FALSE);
  311.                                 SetCursor(hCursor);
  312.                                 InvalidateRect(hwnd, NULL, TRUE);
  313.                                 break;
  314.  
  315.                         case 1: /* quit */
  316.  
  317.                                 SendMessage(hwnd, WM_CLOSE, 0, 0L);
  318.                     SOMD_Uninit(&ev);
  319.                                 break;
  320.                 }
  321.                 return 0;
  322.  
  323.           case WM_DESTROY:
  324.  
  325.                 if (evchObj)
  326.                   SOMD_ObjectMgr->somdReleaseObject(&ev, evchObj);
  327.                 /* DSOM uninitialization: */
  328.                 SOMD_Uninit(&ev);
  329.                 SOM_UninitEnvironment(&ev);
  330.  
  331.                 PostQuitMessage (0) ;
  332.                 return 0 ;
  333.           }
  334.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  335.      }
  336.  
  337.  
  338. /*
  339.  *  Prints exception information.
  340.  */
  341.  
  342. void printEv(Environment *ev)
  343. {
  344.   char *exId, *bp;
  345.   char buffer[200];
  346.   StExcep *params;
  347.   Disconnected *discon;
  348.  
  349.   exId = somExceptionId(ev);
  350.   if (!exId) exId = "None";
  351.   bp = buffer;
  352.   wsprintf(bp, GetNlsMessage(ErrorOccurId), (LPSTR) exId);
  353.   bp += strlen(bp);
  354.  
  355.   switch(ev->_major)
  356.     {
  357.     case SYSTEM_EXCEPTION:
  358.         params = (StExcep *)somExceptionValue(ev);
  359.         wsprintf(bp, GetNlsMessage(MinorCodeId), (params ? params->minor : 0));
  360.         bp += strlen(bp);
  361.         wsprintf(bp, GetNlsMessage(CompletionCodeId),
  362.                 (LPSTR) (params ? (params->completed == YES ? GetNlsMessage(YesId) :
  363.                 params->completed == NO ? GetNlsMessage(NoId): GetNlsMessage(MaybeId)) : GetNlsMessage(YesId)));
  364.         break;
  365.  
  366.     case USER_EXCEPTION:
  367.         if (strcmp(exId, ex_Disconnected) == 0)
  368.             discon = (Disconnected *) somExceptionValue(ev);
  369.             wsprintf(bp, GetNlsMessage(DisconnectId), (LPSTR) discon->Reason);
  370.         break;
  371.   }
  372.   MessageBox ((HWND)NULL, buffer, (LPSTR) GetNlsMessage(ExceptionId),
  373.                                 MB_ICONEXCLAMATION | MB_OK);
  374.   somdExceptionFree(ev);
  375.   return;
  376. }
  377.  
  378. void WinSleep(unsigned long msec)
  379. {
  380.  
  381.    DWORD dwStartTime = GetCurrentTime();
  382.    MSG   msg;
  383.  
  384.    do
  385.    {
  386.       while ( PeekMessage( (MSG FAR*)&msg, (HWND)NULL, 0, 0, PM_REMOVE) )
  387.       {
  388.          TranslateMessage( &msg );
  389.          DispatchMessage( &msg );
  390.       }
  391.    } while ( (GetCurrentTime() - dwStartTime) < msec );
  392.  
  393.    return;
  394. }
  395.