[ovs-dev] [PATCH 1/3] stream: Generalize stream_open_block().

Ben Pfaff blp at nicira.com
Thu Mar 18 19:59:32 UTC 2010


This change makes it possible to separate opening a stream from blocking on
connection completion.  This avoids some code redundancy in an upcoming
commit.
---
 lib/stream.c         |   16 ++++++++++++----
 lib/stream.h         |    4 ++--
 ovsdb/ovsdb-client.c |    2 +-
 tests/test-jsonrpc.c |    4 ++--
 tests/test-ovsdb.c   |    2 +-
 5 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/lib/stream.c b/lib/stream.c
index db6ec61..4420f83 100644
--- a/lib/stream.c
+++ b/lib/stream.c
@@ -185,13 +185,21 @@ stream_open(const char *name, struct stream **streamp)
     return EAFNOSUPPORT;
 }
 
+/* Blocks until a previously started stream connection attempt succeeds or
+ * fails.  'error' should be the value returned by stream_open() and 'streamp'
+ * should point to the stream pointer set by stream_open().  Returns 0 if
+ * successful, otherwise a positive errno value other than EAGAIN or
+ * EINPROGRESS.  If successful, leaves '*streamp' untouched; on error, closes
+ * '*streamp' and sets '*streamp' to null.
+ *
+ * Typical usage:
+ *   error = stream_open_block(stream_open("tcp:1.2.3.4:5", &stream), &stream);
+ */
 int
-stream_open_block(const char *name, struct stream **streamp)
+stream_open_block(int error, struct stream **streamp)
 {
-    struct stream *stream;
-    int error;
+    struct stream *stream = *streamp;
 
-    error = stream_open(name, &stream);
     while (error == EAGAIN) {
         stream_run(stream);
         stream_run_wait(stream);
diff --git a/lib/stream.h b/lib/stream.h
index e7eef36..3c61792 100644
--- a/lib/stream.h
+++ b/lib/stream.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 Nicira Networks.
+ * Copyright (c) 2009, 2010 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ void stream_usage(const char *name, bool active, bool passive, bool bootstrap);
 
 /* Bidirectional byte streams. */
 int stream_open(const char *name, struct stream **);
-int stream_open_block(const char *name, struct stream **);
+int stream_open_block(int error, struct stream **);
 void stream_close(struct stream *);
 const char *stream_get_name(const struct stream *);
 uint32_t stream_get_remote_ip(const struct stream *);
diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index da7faf6..a17c5aa 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -224,7 +224,7 @@ open_jsonrpc(const char *server)
     struct stream *stream;
     int error;
 
-    error = stream_open_block(server, &stream);
+    error = stream_open_block(stream_open(server, &stream), &stream);
     if (error == EAFNOSUPPORT) {
         struct pstream *pstream;
 
diff --git a/tests/test-jsonrpc.c b/tests/test-jsonrpc.c
index 06b1cf4..f760b3c 100644
--- a/tests/test-jsonrpc.c
+++ b/tests/test-jsonrpc.c
@@ -274,7 +274,7 @@ do_request(int argc OVS_UNUSED, char *argv[])
         ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
     }
 
-    error = stream_open_block(argv[1], &stream);
+    error = stream_open_block(stream_open(argv[1], &stream), &stream);
     if (error) {
         ovs_fatal(error, "could not open \"%s\"", argv[1]);
     }
@@ -313,7 +313,7 @@ do_notify(int argc OVS_UNUSED, char *argv[])
         ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
     }
 
-    error = stream_open_block(argv[1], &stream);
+    error = stream_open_block(stream_open(argv[1], &stream), &stream);
     if (error) {
         ovs_fatal(error, "could not open \"%s\"", argv[1]);
     }
diff --git a/tests/test-ovsdb.c b/tests/test-ovsdb.c
index b2ab4c6..8cb6c94 100644
--- a/tests/test-ovsdb.c
+++ b/tests/test-ovsdb.c
@@ -1764,7 +1764,7 @@ do_idl(int argc, char *argv[])
     if (argc > 2) {
         struct stream *stream;
 
-        error = stream_open_block(argv[1], &stream);
+        error = stream_open_block(stream_open(argv[1], &stream), &stream);
         if (error) {
             ovs_fatal(error, "failed to connect to \"%s\"", argv[1]);
         }
-- 
1.6.6.1





More information about the dev mailing list