home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IDDETSRV.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  27KB  |  438 lines

  1. #ifndef _IDDETSRV_
  2. #define _IDDETSRV_
  3. /*******************************************************************************
  4. * FILE NAME: iddetsrv.hpp                                                      *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     IDDETopicServer          - Defines a DDE Server on a single topic.       *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   Licensed Materials - Property of IBM                                       *
  12. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  13. *   All Rights Reserved                                                        *
  14. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  15. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  16. *                                                                              *
  17. *******************************************************************************/
  18. #ifndef _ISTRING_
  19.   #include <istring.hpp>
  20. #endif
  21. #ifndef _IHANDLER_
  22.   #include <ihandler.hpp>
  23. #endif
  24. #ifndef _IHANDLE_
  25.   #include <ihandle.hpp>
  26. #endif
  27. #ifndef _IDDEEVT_
  28.   #include <iddeevt.hpp>
  29. #endif
  30.  
  31. /*----------------------------------------------------------------------------*/
  32. /* Align classes on four byte boundary.                                       */
  33. /*----------------------------------------------------------------------------*/
  34. #pragma pack(4)
  35.  
  36. // Forward declarations for other classes:
  37. class IObjectWindow;
  38. class IFrameWindow;
  39. class IThread;
  40. class IDDEServerConversationSet;
  41. class IDDEServerHotLinkItemSet;
  42. class IDDEFormatSet;
  43.  
  44. class IDDETopicServer : protected IHandler {
  45. typedef IHandler
  46.   Inherited;
  47. /*******************************************************************************
  48. * The IDDETopicServer class adds dynamic data exchange (DDE) server function   *
  49. * to an application.                                                           *
  50. *                                                                              *
  51. * An instance of this class is created for each topic supported by a DDE       *
  52. * server application.  All window, shared memory, and atom table processing is *
  53. * managed by the class.                                                        *
  54. *                                                                              *
  55. * This class uses a window to communicate.  Therefore, window message          *
  56. * processing must occur.  This means that ICurrentThread::processMsgs must     *
  57. * be invoked.  There are several ways for this to occur.  Normally, this is    *
  58. * accomplished by invoking IApplication::current().run().                      *
  59. *                                                                              *
  60. * NOTE: Applications using this class must be compiled with the multithreaded  *
  61. *       library.                                                               *
  62. *                                                                              *
  63. * Example:                                                                     *
  64. *                                                                              *
  65. * This example creates an IDDETopicServer subclass instance as required to     *
  66. * provide an implementation for the 'requestData()' callback function.         *
  67. *                                                                              *
  68. *   class MyDDETopicServer : public IDDETopicServer {                          *
  69. *   public:                                                                    *
  70. *   virtual Boolean                                                            *
  71. *     requestData ( unsigned long conversationId,                              *
  72. *                   IDDERequestDataEvent& event)                               *
  73. *     {                                                                        *
  74. *        IString item = event.item();                                          *
  75. *        IString format = event.format();                                      *
  76. *        IString myData = itemData(item, format);  // some function to         *
  77. *        if (myData.size())                        // retrieve the data for    *
  78. *        {                                         // this item and format     *
  79. *           event.setData(myData);                                             *
  80. *           return true;                                                       *
  81. *        }                                                                     *
  82. *        return false;        // return false if unable to provide data        *
  83. *     }                                                                        *
  84. *   };  // MyDDETopicServer                                                    *
  85. *                                                                              *
  86. *   myTopicServer = new MyDDETopicServer( "Quote Server",                      *
  87. *                                         "Famous Quotes");                    *
  88. *                                                                              *
  89. *******************************************************************************/
  90. public:
  91. /*------------------------- Constructor ----------------------------------------
  92. | There is one way to construct instances of this class.  The constructor
  93. | takes two required arguments and two optional arguments:
  94. |                                                                              |
  95. |    - The name of the application the topic server belongs to                 |
  96. |
  97. |    - The name of the topic the topic server supports                         |
  98. |
  99. |    - The third argument is optional, but highly recommended if the
  100. |      application has a main frame window.  IDDETopicServer creates an
  101. |      instance of IFrameWindow, which must be destructed in order for the
  102. |      application to end normally.  Specifying this argument guarantees
  103. |      that this window is destructed when the main frame window is closed.
  104. |      The alternative is to ensure that all instances of IDDETopicServer
  105. |      are deleted prior to attempting to end the application.                                                                 |
  106. |
  107. |    - The fourth argument is optional, but also highly recommended if the          |
  108. |      application must do any extensive processing, or intends to interact
  109. |      with the end user during any of the callback functions.  Specifying
  110. |      true allows IDDETopicServer to create a secondary thread to process
  111. |      incoming events.  This prevents problems with window message
  112. |      processing since the thread is created without a message queue.  If
  113. |      false is specified, no secondary thread will be created and care must
  114. |      be taken to return promptly from all callback functions.                                                                   |
  115. ------------------------------------------------------------------------------*/
  116.   IDDETopicServer  ( const char*   applicationName,
  117.                      const char*   supportedTopic,
  118.                      IFrameWindow* owner = 0,
  119.                      Boolean       useEventThread = true );
  120.  
  121. virtual
  122.   ~IDDETopicServer ( );
  123.  
  124. /*------------------------------- Transactions ---------------------------------
  125. | Use the following functions to communicate with client applications:         |
  126. |                                                                              |
  127. |   beginConversation - This is provided along with the serverHandle           |
  128. |                       function to allow a topic server to get in             |
  129. |                       conversation with a client without engaging in the     |
  130. |                       normal conversation initialization.                    |
  131. |                                                                              |
  132. |                       This function sets the window handle associated with   |
  133. |                       the client conversation.  To initiate a conversation   |
  134. |                       in this manner, the client and server application      |
  135. |                       must have their own method for exchanging their        |
  136. |                       window handles.                                        |
  137. |   endConversation   - Ends a conversation with a DDE client.                 |
  138. |   hotLinkUpdate     - Sends either data or a change notification for items   |
  139. |                       whose data has changed to all clients who have an      |
  140. |                       active hot link for this item.                         |
  141. |                       IDDEServer::requestHotLinkData is called for each      |
  142. |                       format that has an active hot link and requires data.  |
  143. |                       The number of hot links for which data or a            |
  144. |                       notification is sent is returned to the caller.  If    |
  145. |                       there is an outstanding acknowledgement for a hot      |
  146. |                       link, the notification will not be sent to the client  |
  147. |                       to which the hot link belongs.  When the               |
  148. |                       acknowledgement is satisfied, a notification will be   |
  149. |                       sent to the client with the most current data if it    |
  150. |                       is a data hot link.                                    |
  151. ------------------------------------------------------------------------------*/
  152. virtual IDDETopicServer
  153.  &beginConversation ( const IWindowHandle& clientHandle ),
  154.  &endConversation   ( unsigned long conversationId );
  155.  
  156. unsigned long
  157.   hotLinkUpdate ( const char* item );
  158.  
  159. /*-------------------------------- Callbacks -----------------------------------
  160. | The following functions are called to notify the server of transactions      |
  161. | initiated by a client.  If true is specified for the useEventThread          |
  162. | argument of the IDDETopicServer constructor, all of these callbacks except   |
  163. | for acceptConversation and in some cases requestHotLinkData will be invoked  |
  164. | on a secondary thread:                                                       |
  165. |                                                                              |
  166. |   requestData         - Informs the server that a client is requesting data  |
  167. |                         for an item in a specified format.  If the request   |
  168. |                         is for an item and format the application supports,  |
  169. |                         it should provide the data using the setData         |
  170. |                         function of the IDDERequestDataEvent instance and    |
  171. |                         return true.  (The application can also request an   |
  172. |                         acknowledgement from the client when it has          |
  173. |                         received the data using the requestAck function of   |
  174. |                         IDDERequestDataEvent.)  If the application cannot    |
  175. |                         provide the data, it can indicate one of several     |
  176. |                         reasons using the appropriate functions of           |
  177. |                         IDDERequestDataEvent and return false.  This will    |
  178. |                         cause the topic server to send the client a negative |
  179. |                         acknowledgement.  This function is pure virtual and  |
  180. |                         must be overridden.                                  |
  181. |   pokeData            - Informs the server that a client is requesting it to |
  182. |                         set an item to the new value provided by the client. |
  183. |                         If the application is able to honor the request, it  |
  184. |                         should return true and the topic server will send    |
  185. |                         the client a positive acknowledgment.  If the        |
  186. |                         application is unable to honor the request, it       |
  187. |                         should use any appropriate functions of the          |
  188. |                         IDDEPokeEvent instance to indicate the reason and    |
  189. |                         return false.  This will cause the topic server to   |
  190. |                         send a negative acknowledgment to the client.  The   |
  191. |                         default behavior is to return false.                 |
  192. |   beginHotLink        - Informs the server that a client is requesting a     |
  193. |                         hot link on a particular item and format.  If the    |
  194. |                         application supports hot links for this item and     |
  195. |                         format, it should return true and the topic server   |
  196. |                         will send the client a positive acknowledgement.     |
  197. |                         The hotLinkUpdate function is provided in            |
  198. |                         IDDETopicServer for sending either data or a         |
  199. |                         notification when the item's value changes.  If      |
  200. |                         the application does not support this hot link       |
  201. |                         request, it should use any appropriate functions of  |
  202. |                         the IDDEServerHotLinkEvent instance to indicate      |
  203. |                         the reason and return false.  This will cause the    |
  204. |                         topic server to send a negative acknowledgement to   |
  205. |                         the client.  The default behavior is to return       |
  206. |                         false.                                               |
  207. |   executeCommands     - Informs the server that a client is requesting the   |
  208. |                         application to execute a string of one or more       |
  209. |                         commands.  If the application supports the request   |
  210. |                         and successfully executes the commands, it should    |
  211. |                         return true and the topic server will send the       |
  212. |                         client a positive acknowledgement.  If the           |
  213. |                         application cannot honor this request, it should use |
  214. |                         any appropriate functions of the IDDEExecuteEvent    |
  215. |                         instance to indicate the reason and return false.    |
  216. |                         This will cause the topic server to send a negative  |
  217. |                         acknowledgement to the client.  The default behavior |
  218. |                         is to return false.                                  |
  219. |   acceptConversation  - Informs the server that a client is asking to begin  |
  220. |                         a conversation.  The topic server will call this     |
  221. |                         function if the application and topic match,         |
  222. |                         ignoring mismatches due to different cases.  The     |
  223. |                         application should return promptly from this         |
  224. |                         function because the request is sent, not posted,    |
  225. |                         by the client application.  Therefore, this          |
  226. |                         function will always be invoked in the main thread.  |
  227. |                                                                              |
  228. |                         The application should return true if the            |
  229. |                         conversation is accepted.  (The application can      |
  230. |                         indicate it enforces case sensitivity by using the   |
  231. |                         setCaseSensitive function of the IDDEBeginEvent      |
  232. |                         instance.)  If the conversation request is           |
  233. |                         rejected, the application should return false and    |
  234. |                         the topic server will not accept the conversation.   |
  235. |                         The default behavior is to return true.              |
  236. |   requestHotLinkData  - Informs the server that data is required for a       |
  237. |                         an item in a specified format.  This function is     |
  238. |                         called once for each format that has an active hot   |
  239. |                         link that requires data when the server invokes      |
  240. |                         the hotLinkUpdate function for an item.  The         |
  241. |                         server invokes the hotLinkUpdate function when an    |
  242. |                         item with active hot links changes.                  |
  243. |                                                                              |
  244. |                         There is no need for this function to invoke the     |
  245. |                         requestAck function of IDDERequestDataEvent          |
  246. |                         because the topic server will do this                |
  247. |                         automatically for all hot links that have pacing     |
  248. |                         active.  (The server can request an                  |
  249. |                         acknowledgement even if pacing is not active.)       |
  250. |                         For hot links that have pacing active and an         |
  251. |                         outstanding acknowledgment, the update is not        |
  252. |                         sent.                                                |
  253. |                                                                              |
  254. |                         When the acknowledgement is received, the topic      |
  255. |                         server will request the latest update if the data    |
  256. |                         has changed while the acknowledgement was            |
  257. |                         outstanding.  In these cases, if true is specified   |
  258. |                         for the useEventThread argument of the               |
  259. |                         IDDETopicServer constructor, this function will be   |
  260. |                         invoked on a secondary thread; otherwise, it is      |
  261. |                         invoked in the main thread.                          |
  262. |                         NOTE:  The hotLinkUpdate and endConversation         |
  263. |                                functions must not be invoked from this       |
  264. |                                function. Otherwise, a dead-lock can occur.   |
  265. |   hotLinkEnded        - Informs the server that a client has ended one or    |
  266. |                         more hot links.  If the format is a 0-length string, |
  267. |                         all hot links on the specified item are ended.  If   |
  268. |                         the item is a 0-length string, all hot links with    |
  269. |                         this client are ended.                               |
  270. |   acknowledged        - Informs the server that a client has acknowledged    |
  271. |                         the receipt of data or notification of changed hot   |
  272. |                         link data.                                           |
  273. |                         NOTE:  The hotLinkUpdate and endConversation         |
  274. |                                functions must not be invoked from this       |
  275. |                                function.  Otherwise, a dead-lock can occur.  |
  276. |   conversationEnded  -  Notifies the server that the conversation is ending  |
  277. |                         or ended.  The conversation end can be initiated by  |
  278. |                         either the client or the server and can also be      |
  279. |                         caused by an error condition in the                  |
  280. |                         IDDEClientConversation.                              |
  281. ------------------------------------------------------------------------------*/
  282. virtual Boolean
  283.   requestData        ( unsigned long           conversationId,
  284.                        IDDERequestDataEvent&   event ) = 0,
  285.   pokeData           ( unsigned long           conversationId,
  286.                        IDDEPokeEvent&          event ),
  287.   beginHotLink       ( unsigned long           conversationId,
  288.                        IDDEServerHotLinkEvent& event ),
  289.   executeCommands    ( unsigned long           conversationId,
  290.                        IDDEExecuteEvent&       event ),
  291.   acceptConversation ( unsigned long           conversationId,
  292.                        IDDEBeginEvent&         event );
  293.  
  294. virtual void
  295.   requestHotLinkData  ( IDDERequestDataEvent&       event ),
  296.   hotLinkEnded        ( unsigned long               conversationId,
  297.                         IDDEEvent&                  event ),
  298.   acknowledged        ( unsigned long               conversationId,
  299.                         IDDEServerAcknowledgeEvent& event ),
  300.   conversationEnded   ( unsigned long               conversationId,
  301.                         IDDEEndEvent&               event );
  302.  
  303. /*-------------------------------- Accessors -----------------------------------
  304. | Use the following functions to get the accessible attributes                 |
  305. | of instances of this class:                                                  |
  306. |                                                                              |
  307. |   conversationCount - Returns the number of conversations in which the       |
  308. |                       topic server is currently engaged.                     |
  309. |   hotLinkCount      - Returns the number of hot links in which the topic     |
  310. |                       server is currently engaged.                           |
  311. |   application       - Returns the name of the server application.            |
  312. |   topic             - Returns the name of the supported topic.               |
  313. |   serverHandle      - Returns the window handle of the topic server.  This   |
  314. |                       is provided along with the beginConversation           |
  315. |                       function to allow a topic server to get in             |
  316. |                       conversation with a client without engaging in the     |
  317. |                       normal conversation initialization.  Only a client is  |
  318. |                       normally allowed to initiate a conversation.           |
  319. ------------------------------------------------------------------------------*/
  320. unsigned long
  321.   conversationCount ( ) const,
  322.   hotLinkCount      ( ) const;
  323.  
  324. IString
  325.   application ( ) const,
  326.   topic       ( ) const;
  327.  
  328. IWindowHandle
  329.   serverHandle ( ) const;
  330.  
  331. protected:
  332. /*-------------------------------- Overrides -----------------------------------
  333. | This class overrides the following inherited function, typically there is no |
  334. | need for a subclass to override this function:                               |
  335. |                                                                              |
  336. |   dispatchHandlerEvent - This function is overridden to process              |
  337. |                          DDE specific messages sent to the topic server.     |
  338. ------------------------------------------------------------------------------*/
  339. virtual Boolean
  340.   dispatchHandlerEvent ( IEvent& event );
  341.  
  342. /*--------------------- Implementation Handler Functions -----------------------
  343. | The dispatchHandlerEvent function calls the following functions to process   |
  344. | DDE-specific messages sent to the topic server.  Typically, there is no      |
  345. | need to override these functions:                                            |
  346. |                                                                              |
  347. |   handleAck       - Handles acknowledgements from client applications.       |
  348. |   handleAdvise    - Handles beginHotLink requests from client applications.  |
  349. |   handleExecute   - Handles executeCommands requests from client             |
  350. |                     applications.                                            |
  351. |   handleInitiate  - Handles begin requests from client applications.         |
  352. |   handlePoke      - Handles pokeData requests from client applications.      |
  353. |   handleRequest   - Handles requestData requests from client applications.   |
  354. |   handleTerminate - Handles end requests from client applications.           |
  355. |   handleUnadvise  - Handles hotLinkEnded requests from client applications.  |
  356. ------------------------------------------------------------------------------*/
  357. virtual void
  358.   handleAck       ( const IEvent& ackEvent ),
  359.   handleAdvise    ( const IEvent& adviseEvent ),
  360.   handleExecute   ( const IEvent& executeEvent ),
  361.   handleInitiate  ( const IEvent& initiateEvent ),
  362.   handlePoke      ( const IEvent& pokeEvent ),
  363.   handleRequest   ( const IEvent& requestEvent ),
  364.   handleTerminate ( const IEvent& terminateEvent ),
  365.   handleUnadvise  ( const IEvent& unadviseEvent );
  366.  
  367. /*-------------------- Implementation Hot Link Function ------------------------
  368. | The following function processes hot link activity.  Typically, there is no  |
  369. | need to override this function:                                              |
  370. |                                                                              |
  371. |   removeLink - Called by the handleUnadvise function to update hot link      |
  372. |                information when a client ends a hot link.                    |
  373. ------------------------------------------------------------------------------*/
  374. virtual Boolean
  375.   removeLink ( IString       item,
  376.                IString       format,
  377.                unsigned long conversationId );
  378.  
  379. private:
  380. /*--------------------------------- PRIVATE ----------------------------------*/
  381.   IDDETopicServer ( const IDDETopicServer& aTopicServer );
  382. IDDETopicServer&
  383.   operator=( const IDDETopicServer& aTopicServer );
  384. IDDEServerHotLinkItemSet
  385.  &hotLinkItems ( ) const;
  386. IDDEServerConversationSet
  387.  &closedConversations ( ) const,
  388.  &conversations       ( ) const;
  389. IDDEFormatSet
  390.  &formats ( ) const;
  391. unsigned long
  392.   queueHandle ( ) const;
  393. void
  394.   dispatchEventFromQueue ( );
  395. void
  396.  *buildDDEStruct ( const char*    itemName,
  397.                    const char*    dataFormat,
  398.                    unsigned short status,
  399.                    const void*    xferData,
  400.                    unsigned long  dataLength );
  401.  
  402. unsigned long
  403.  ulClQHandle;
  404. IThread
  405.  *pThreadCl;
  406. Boolean
  407.   fClHdrActive,
  408.   fClPostMsgFail;
  409. IWindowHandle
  410.   wndhClServer;
  411. IObjectWindow
  412.  *pwndClServer;
  413. IString
  414.   strClTopic,
  415.   strClApplication;
  416. IDDEServerConversationSet
  417.  *pConvSetCl;
  418. IDDEServerHotLinkItemSet
  419.  *pHLItemSetCl;
  420. IDDEServerConversationSet
  421.  *pClsdConvSetCl;
  422. IDDEFormatSet
  423.  *pFormatSetCl;
  424.  
  425. }; // IDDETopicServer
  426.  
  427. /*----------------------------------------------------------------------------*/
  428. /* Resume compiler default packing.                                           */
  429. /*----------------------------------------------------------------------------*/
  430. #pragma pack()
  431.  
  432. /*--------------------------------- INLINES ----------------------------------*/
  433. #ifndef I_NO_INLINES
  434.   #include <iddetsrv.inl>
  435. #endif
  436.  
  437. #endif /* _IDDETSRV_ */
  438.