浏览代码

ZOOKEEPER-3640: Implement "batch mode" in cli_mt

Batch mode never was implemented in `cli_mt`.  This patch seems to work, but:

1. There may be a cleaner way of waiting for the completion;
2. ~~`nanosleep` is POSIX; the Windows path should probably use `Sleep`~~ (DONE).

symat: Comments welcome.

Author: Damien Diederen <dd@crosstwine.com>

Reviewers: andor@apache.org

Closes #1173 from ztzg/ZOOKEEPER-3640-implement-batch-mode-in-cli-mt
Damien Diederen 5 年之前
父节点
当前提交
d7bc7b135f
共有 1 个文件被更改,包括 23 次插入2 次删除
  1. 23 2
      zookeeper-client/zookeeper-client-c/src/cli.c

+ 23 - 2
zookeeper-client/zookeeper-client-c/src/cli.c

@@ -736,6 +736,19 @@ int handleBatchMode(const char* arg, const char** buf) {
     return 1;
 }
 
+#ifdef THREADED
+static void millisleep(int ms) {
+#ifdef WIN32
+    Sleep(ms);
+#else /* !WIN32 */
+    struct timespec ts;
+    ts.tv_sec = ms / 1000;
+    ts.tv_nsec = (ms % 1000) * 1000000; // to nanoseconds
+    nanosleep(&ts, NULL);
+#endif /* WIN32 */
+}
+#endif /* THREADED */
+
 int main(int argc, char **argv) {
     static struct option long_options[] = {
             {"host",     required_argument, NULL, 'h'}, //hostPort
@@ -896,9 +909,17 @@ int main(int argc, char **argv) {
 #endif
 
 #ifdef THREADED
+    if (batchMode) {
+        processline(cmd);
+    }
     while(!shutdownThisThing) {
-        int rc;
-        int len = sizeof(buffer) - bufoff -1;
+        int rc, len;
+        if (batchMode) {
+            // We are just waiting for the asynchronous command to complete.
+            millisleep(10);
+            continue;
+        }
+        len = sizeof(buffer) - bufoff -1;
         if (len <= 0) {
             fprintf(stderr, "Can't handle lines that long!\n");
             exit(2);