08.events.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 08.events.cpp
  4. * DESCRIPTION: A sample of working with events.
  5. *
  6. * Example for the following interfaces:
  7. * IEvents - returned by queEvents(), used to cancel events monitoring
  8. * IEventCallback - it's callback is invoked when event happens
  9. *
  10. * The contents of this file are subject to the Mozilla Public License Version
  11. * 1.1 (the "License"); you may not use this file except in compliance with
  12. * the License. You may obtain a copy of the License at
  13. * http://www.mozilla.org/MPL/
  14. *
  15. * Software distributed under the License is distributed on an "AS IS" basis,
  16. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17. * for the specific language governing rights and limitations under the
  18. * License.
  19. *
  20. * The Initial Developer of the Original Code is Adriano dos Santos Fernandes.
  21. * Portions created by the Initial Developer are Copyright (C) 2011 the Initial Developer.
  22. * All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Alexander Peshkov
  26. */
  27. #include "ifaceExamples.h"
  28. #ifndef WIN32
  29. #include <unistd.h>
  30. #else
  31. #include <windows.h>
  32. #endif
  33. static IMaster* master = fb_get_master_interface();
  34. namespace
  35. {
  36. // This class encapsulates single event handling
  37. class Event : public IEventCallbackImpl<Event, ThrowStatusWrapper>
  38. {
  39. public:
  40. Event(IAttachment* aAttachment, const char* name)
  41. : refCounter(0),
  42. attachment(aAttachment),
  43. counter(0),
  44. status(master->getStatus()),
  45. events(NULL),
  46. first(true)
  47. {
  48. eveLen = isc_event_block(&eveBuffer, &eveResult, 1, name);
  49. events = attachment->queEvents(&status, this, eveLen, eveBuffer);
  50. }
  51. void process(int pass)
  52. {
  53. if (!events)
  54. return;
  55. ISC_ULONG tot = 0;
  56. if (counter)
  57. {
  58. isc_event_counts(&tot, eveLen, eveBuffer, eveResult);
  59. events->release();
  60. events = NULL;
  61. counter = 0;
  62. events = attachment->queEvents(&status, this, eveLen, eveBuffer);
  63. }
  64. if (tot && !first)
  65. printf("Event count on pass %d is %d\n", pass, tot);
  66. else
  67. printf("Pass %d - no events\n", pass);
  68. first = false;
  69. }
  70. // IEventCallback implementation
  71. void eventCallbackFunction(unsigned int length, const ISC_UCHAR* data)
  72. {
  73. memcpy(eveResult, data, length);
  74. ++counter;
  75. if (!first)
  76. printf("AST called\n");
  77. }
  78. // refCounted implementation
  79. virtual void addRef()
  80. {
  81. ++refCounter;
  82. }
  83. virtual int release()
  84. {
  85. if (--refCounter == 0)
  86. {
  87. delete this;
  88. return 0;
  89. }
  90. else
  91. return 1;
  92. }
  93. private:
  94. ~Event()
  95. {
  96. if (events)
  97. events->release();
  98. if (eveBuffer)
  99. isc_free((char*) eveBuffer);
  100. if (eveResult)
  101. isc_free((char*) eveResult);
  102. status.dispose();
  103. }
  104. FbSampleAtomic refCounter;
  105. IAttachment* attachment;
  106. volatile int counter;
  107. ThrowStatusWrapper status;
  108. IEvents* events;
  109. unsigned char* eveBuffer;
  110. unsigned char* eveResult;
  111. unsigned eveLen;
  112. bool first;
  113. };
  114. }
  115. int main()
  116. {
  117. int rc = 0;
  118. // set default password if none specified in environment
  119. setenv("ISC_USER", "sysdba", 0);
  120. setenv("ISC_PASSWORD", "masterkey", 0);
  121. // With ThrowStatusWrapper passed as status interface FbException will be thrown on error
  122. ThrowStatusWrapper status(master->getStatus());
  123. // Declare pointers to required interfaces
  124. IProvider* prov = master->getDispatcher();
  125. IAttachment* att = NULL;
  126. ITransaction* tra = NULL;
  127. Event* event = NULL;
  128. try
  129. {
  130. // attach database
  131. att = prov->attachDatabase(&status, "employee", 0, NULL);
  132. // register an event
  133. event = new Event(att, "EVENT1");
  134. event->addRef();
  135. const char cmdBlock[] = "execute block as begin post_event 'EVENT1'; end";
  136. for (int i = 0; i < 3; ++i)
  137. {
  138. #ifndef WIN32
  139. sleep(1); // sec
  140. #else
  141. Sleep(1000); // msec
  142. #endif
  143. event->process(i);
  144. tra = att->startTransaction(&status, 0, NULL);
  145. att->execute(&status, tra, 0, cmdBlock, SAMPLES_DIALECT,
  146. NULL, NULL, NULL, NULL);
  147. tra->commit(&status);
  148. tra = NULL;
  149. }
  150. // cleanup
  151. event->release();
  152. event = NULL;
  153. att->detach(&status);
  154. att = NULL;
  155. }
  156. catch (const FbException& error)
  157. {
  158. // handle error
  159. rc = 1;
  160. char buf[256];
  161. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  162. fprintf(stderr, "%s\n", buf);
  163. }
  164. // release interfaces after error caught
  165. if (event)
  166. event->release();
  167. if (tra)
  168. tra->release();
  169. if (att)
  170. att->release();
  171. // generic cleanup
  172. prov->release();
  173. status.dispose();
  174. return rc;
  175. }