WatchUtil.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef WATCH_UTIL_H_
  19. #define WATCH_UTIL_H_
  20. #include <sys/select.h>
  21. #include <cstring>
  22. #include <list>
  23. using namespace std;
  24. #include "CollectionUtil.h"
  25. #include "ThreadingUtil.h"
  26. using namespace Util;
  27. #ifdef THREADED
  28. static void yield(zhandle_t *zh, int i)
  29. {
  30. sleep(i);
  31. }
  32. #else
  33. static void yield(zhandle_t *zh, int seconds)
  34. {
  35. int fd;
  36. int interest;
  37. int events;
  38. struct timeval tv;
  39. time_t expires = time(0) + seconds;
  40. time_t timeLeft = seconds;
  41. fd_set rfds, wfds, efds;
  42. FD_ZERO(&rfds);
  43. FD_ZERO(&wfds);
  44. FD_ZERO(&efds);
  45. while(timeLeft >= 0) {
  46. zookeeper_interest(zh, &fd, &interest, &tv);
  47. if (fd != -1) {
  48. if (interest&ZOOKEEPER_READ) {
  49. FD_SET(fd, &rfds);
  50. } else {
  51. FD_CLR(fd, &rfds);
  52. }
  53. if (interest&ZOOKEEPER_WRITE) {
  54. FD_SET(fd, &wfds);
  55. } else {
  56. FD_CLR(fd, &wfds);
  57. }
  58. } else {
  59. fd = 0;
  60. }
  61. FD_SET(0, &rfds);
  62. if (tv.tv_sec > timeLeft) {
  63. tv.tv_sec = timeLeft;
  64. }
  65. select(fd+1, &rfds, &wfds, &efds, &tv);
  66. timeLeft = expires - time(0);
  67. events = 0;
  68. if (FD_ISSET(fd, &rfds)) {
  69. events |= ZOOKEEPER_READ;
  70. }
  71. if (FD_ISSET(fd, &wfds)) {
  72. events |= ZOOKEEPER_WRITE;
  73. }
  74. zookeeper_process(zh, events);
  75. }
  76. }
  77. #endif
  78. typedef struct evt {
  79. string path;
  80. int type;
  81. } evt_t;
  82. typedef struct watchCtx {
  83. private:
  84. list<evt_t> events;
  85. watchCtx(const watchCtx&);
  86. watchCtx& operator=(const watchCtx&);
  87. public:
  88. bool connected;
  89. zhandle_t *zh;
  90. Mutex mutex;
  91. watchCtx() {
  92. connected = false;
  93. zh = 0;
  94. }
  95. ~watchCtx() {
  96. if (zh) {
  97. zookeeper_close(zh);
  98. zh = 0;
  99. }
  100. }
  101. evt_t getEvent() {
  102. evt_t evt;
  103. mutex.acquire();
  104. CPPUNIT_ASSERT( events.size() > 0);
  105. evt = events.front();
  106. events.pop_front();
  107. mutex.release();
  108. return evt;
  109. }
  110. int countEvents() {
  111. int count;
  112. mutex.acquire();
  113. count = events.size();
  114. mutex.release();
  115. return count;
  116. }
  117. void putEvent(evt_t evt) {
  118. mutex.acquire();
  119. events.push_back(evt);
  120. mutex.release();
  121. }
  122. bool waitForConnected(zhandle_t *zh) {
  123. time_t expires = time(0) + 10;
  124. while(!connected && time(0) < expires) {
  125. yield(zh, 1);
  126. }
  127. return connected;
  128. }
  129. bool waitForDisconnected(zhandle_t *zh) {
  130. time_t expires = time(0) + 15;
  131. while(connected && time(0) < expires) {
  132. yield(zh, 1);
  133. }
  134. return !connected;
  135. }
  136. } watchctx_t;
  137. #endif /*WATCH_UTIL_H_*/