2
0

08.events.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include <atomic>
  29. #ifndef WIN32
  30. #include <unistd.h>
  31. #else
  32. #include <windows.h>
  33. #endif
  34. static IMaster* master = fb_get_master_interface();
  35. namespace
  36. {
  37. // This class encapsulates single event handling
  38. class Event : public IEventCallbackImpl<Event, ThrowStatusWrapper>
  39. {
  40. public:
  41. Event(IAttachment* aAttachment, const char* name)
  42. : refCounter(0),
  43. attachment(aAttachment),
  44. counter(0),
  45. status(master->getStatus()),
  46. events(NULL),
  47. first(true)
  48. {
  49. eveLen = isc_event_block(&eveBuffer, &eveResult, 1, name);
  50. events = attachment->queEvents(&status, this, eveLen, eveBuffer);
  51. }
  52. void process(int pass)
  53. {
  54. if (!events)
  55. return;
  56. ISC_ULONG tot = 0;
  57. if (counter)
  58. {
  59. isc_event_counts(&tot, eveLen, eveBuffer, eveResult);
  60. events->release();
  61. events = NULL;
  62. counter = 0;
  63. events = attachment->queEvents(&status, this, eveLen, eveBuffer);
  64. }
  65. if (tot && !first)
  66. printf("Event count on pass %d is %d\n", pass, tot);
  67. else
  68. printf("Pass %d - no events\n", pass);
  69. first = false;
  70. }
  71. // IEventCallback implementation
  72. void eventCallbackFunction(unsigned int length, const ISC_UCHAR* data)
  73. {
  74. memcpy(eveResult, data, length);
  75. ++counter;
  76. if (!first)
  77. printf("AST called\n");
  78. }
  79. // refCounted implementation
  80. virtual void addRef()
  81. {
  82. ++refCounter;
  83. }
  84. virtual int release()
  85. {
  86. if (--refCounter == 0)
  87. {
  88. delete this;
  89. return 0;
  90. }
  91. else
  92. return 1;
  93. }
  94. private:
  95. ~Event()
  96. {
  97. if (events)
  98. events->release();
  99. if (eveBuffer)
  100. isc_free((char*) eveBuffer);
  101. if (eveResult)
  102. isc_free((char*) eveResult);
  103. status.dispose();
  104. }
  105. std::atomic_int refCounter;
  106. IAttachment* attachment;
  107. volatile int counter;
  108. ThrowStatusWrapper status;
  109. IEvents* events;
  110. unsigned char* eveBuffer;
  111. unsigned char* eveResult;
  112. unsigned eveLen;
  113. bool first;
  114. };
  115. }
  116. int main()
  117. {
  118. int rc = 0;
  119. // set default password if none specified in environment
  120. setenv("ISC_USER", "sysdba", 0);
  121. setenv("ISC_PASSWORD", "masterkey", 0);
  122. // With ThrowStatusWrapper passed as status interface FbException will be thrown on error
  123. ThrowStatusWrapper status(master->getStatus());
  124. // Declare pointers to required interfaces
  125. IProvider* prov = master->getDispatcher();
  126. IAttachment* att = NULL;
  127. ITransaction* tra = NULL;
  128. Event* event = NULL;
  129. try
  130. {
  131. // attach database
  132. att = prov->attachDatabase(&status, "employee", 0, NULL);
  133. // register an event
  134. event = new Event(att, "EVENT1");
  135. event->addRef();
  136. const char cmdBlock[] = "execute block as begin post_event 'EVENT1'; end";
  137. for (int i = 0; i < 3; ++i)
  138. {
  139. #ifndef WIN32
  140. sleep(1); // sec
  141. #else
  142. Sleep(1000); // msec
  143. #endif
  144. event->process(i);
  145. tra = att->startTransaction(&status, 0, NULL);
  146. att->execute(&status, tra, 0, cmdBlock, SAMPLES_DIALECT,
  147. NULL, NULL, NULL, NULL);
  148. tra->commit(&status);
  149. tra = NULL;
  150. }
  151. // cleanup
  152. event->release();
  153. event = NULL;
  154. att->detach(&status);
  155. att = NULL;
  156. }
  157. catch (const FbException& error)
  158. {
  159. // handle error
  160. rc = 1;
  161. char buf[256];
  162. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  163. fprintf(stderr, "%s\n", buf);
  164. }
  165. // release interfaces after error caught
  166. if (event)
  167. event->release();
  168. if (tra)
  169. tra->release();
  170. if (att)
  171. att->release();
  172. // generic cleanup
  173. prov->release();
  174. status.dispose();
  175. return rc;
  176. }