08.events.cpp 4.5 KB

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