WatchUtil.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. int rc;
  40. time_t expires = time(0) + seconds;
  41. time_t timeLeft = seconds;
  42. fd_set rfds, wfds, efds;
  43. FD_ZERO(&rfds);
  44. FD_ZERO(&wfds);
  45. FD_ZERO(&efds);
  46. while(timeLeft >= 0) {
  47. zookeeper_interest(zh, &fd, &interest, &tv);
  48. if (fd != -1) {
  49. if (interest&ZOOKEEPER_READ) {
  50. FD_SET(fd, &rfds);
  51. } else {
  52. FD_CLR(fd, &rfds);
  53. }
  54. if (interest&ZOOKEEPER_WRITE) {
  55. FD_SET(fd, &wfds);
  56. } else {
  57. FD_CLR(fd, &wfds);
  58. }
  59. } else {
  60. fd = 0;
  61. }
  62. FD_SET(0, &rfds);
  63. if (tv.tv_sec > timeLeft) {
  64. tv.tv_sec = timeLeft;
  65. }
  66. rc = select(fd+1, &rfds, &wfds, &efds, &tv);
  67. timeLeft = expires - time(0);
  68. events = 0;
  69. if (FD_ISSET(fd, &rfds)) {
  70. events |= ZOOKEEPER_READ;
  71. }
  72. if (FD_ISSET(fd, &wfds)) {
  73. events |= ZOOKEEPER_WRITE;
  74. }
  75. zookeeper_process(zh, events);
  76. }
  77. }
  78. #endif
  79. typedef struct evt {
  80. string path;
  81. int type;
  82. } evt_t;
  83. typedef struct watchCtx {
  84. private:
  85. list<evt_t> events;
  86. watchCtx(const watchCtx&);
  87. watchCtx& operator=(const watchCtx&);
  88. public:
  89. bool connected;
  90. zhandle_t *zh;
  91. Mutex mutex;
  92. watchCtx() {
  93. connected = false;
  94. zh = 0;
  95. }
  96. ~watchCtx() {
  97. if (zh) {
  98. zookeeper_close(zh);
  99. zh = 0;
  100. }
  101. }
  102. evt_t getEvent() {
  103. evt_t evt;
  104. mutex.acquire();
  105. CPPUNIT_ASSERT( events.size() > 0);
  106. evt = events.front();
  107. events.pop_front();
  108. mutex.release();
  109. return evt;
  110. }
  111. int countEvents() {
  112. int count;
  113. mutex.acquire();
  114. count = events.size();
  115. mutex.release();
  116. return count;
  117. }
  118. void putEvent(evt_t evt) {
  119. mutex.acquire();
  120. events.push_back(evt);
  121. mutex.release();
  122. }
  123. bool waitForConnected(zhandle_t *zh) {
  124. time_t expires = time(0) + 10;
  125. while(!connected && time(0) < expires) {
  126. yield(zh, 1);
  127. }
  128. return connected;
  129. }
  130. bool waitForDisconnected(zhandle_t *zh) {
  131. time_t expires = time(0) + 15;
  132. while(connected && time(0) < expires) {
  133. yield(zh, 1);
  134. }
  135. return !connected;
  136. }
  137. } watchctx_t;
  138. #endif /*WATCH_UTIL_H_*/