daniel has submitted this change. ( 
https://gerrit.osmocom.org/c/libosmo-netif/+/33194 )

Change subject: examples: Use new stream API in {ipa-,}stream-{client,server}
......................................................................

examples: Use new stream API in {ipa-,}stream-{client,server}

Change-Id: I97a9979199c816686b32080534627f6f033e009e
---
M examples/ipa-stream-client.c
M examples/ipa-stream-server.c
M examples/stream-client.c
M examples/stream-server.c
4 files changed, 29 insertions(+), 75 deletions(-)

Approvals:
  Jenkins Builder: Verified
  pespin: Looks good to me, but someone else must approve
  laforge: Looks good to me, approved




diff --git a/examples/ipa-stream-client.c b/examples/ipa-stream-client.c
index 0c9c589..6cd212f 100644
--- a/examples/ipa-stream-client.c
+++ b/examples/ipa-stream-client.c
@@ -103,21 +103,10 @@
        return 0;
 }

-static int read_cb(struct osmo_stream_cli *conn)
+static int read_cb(struct osmo_stream_cli *conn, struct msgb *msg)
 {
-       struct msgb *msg;
+       LOGP(DIPATEST, LOGL_DEBUG, "received message from stream (len=%d)\n", 
msgb_length(msg));

-       LOGP(DIPATEST, LOGL_DEBUG, "received message from stream\n");
-
-       msg = osmo_ipa_msg_alloc(0);
-       if (msg == NULL) {
-               LOGP(DIPATEST, LOGL_ERROR, "cannot allocate message\n");
-               return 0;
-       }
-       if (osmo_stream_cli_recv(conn, msg) <= 0) {
-               LOGP(DIPATEST, LOGL_ERROR, "cannot receive message\n");
-               return 0;
-       }
        if (osmo_ipa_process_msg(msg) < 0) {
                LOGP(DIPATEST, LOGL_ERROR, "bad IPA message\n");
                return 0;
@@ -175,7 +164,7 @@
         * initialize stream client.
         */

-       conn = osmo_stream_cli_create(tall_test);
+       conn = osmo_stream_cli_create2(tall_test, "ipa_test_client");
        if (conn == NULL) {
                fprintf(stderr, "cannot create client\n");
                exit(EXIT_FAILURE);
@@ -184,7 +173,7 @@
        osmo_stream_cli_set_port(conn, 10000);
        osmo_stream_cli_set_connect_cb(conn, connect_cb);
        osmo_stream_cli_set_disconnect_cb(conn, disconnect_cb);
-       osmo_stream_cli_set_read_cb(conn, read_cb);
+       osmo_stream_cli_set_read_cb2(conn, read_cb);
        osmo_stream_cli_set_data(conn, &num_msgs);
        osmo_stream_cli_set_nodelay(conn, true);

diff --git a/examples/ipa-stream-server.c b/examples/ipa-stream-server.c
index 1ca1aaf..b84f2c9 100644
--- a/examples/ipa-stream-server.c
+++ b/examples/ipa-stream-server.c
@@ -47,23 +47,10 @@
        exit(EXIT_SUCCESS);
 }

-int read_cb(struct osmo_stream_srv *conn)
+int read_cb(struct osmo_stream_srv *conn, struct msgb *msg)
 {
-       struct msgb *msg;
+       LOGP(DSTREAMTEST, LOGL_DEBUG, "received message from stream 
(len=%d)\n", msgb_length(msg));

-       LOGP(DSTREAMTEST, LOGL_DEBUG, "received message from stream\n");
-
-       msg = osmo_ipa_msg_alloc(0);
-       if (msg == NULL) {
-               LOGP(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n");
-               return 0;
-       }
-       if (osmo_stream_srv_recv(conn, msg) <= 0) {
-               LOGP(DSTREAMTEST, LOGL_ERROR, "cannot receive message\n");
-               osmo_stream_srv_destroy(conn);
-               msgb_free(msg);
-               return -EBADF;
-       }
        if (osmo_ipa_process_msg(msg) < 0) {
                LOGP(DSTREAMTEST, LOGL_ERROR, "Bad IPA message\n");
                msgb_free(msg);
@@ -84,17 +71,18 @@
 {
        if (conn != NULL) {
                LOGP(DSTREAMTEST, LOGL_ERROR, "Sorry, this example only "
-                       "support one client simultaneously\n");
+                       "supports one client simultaneously\n");
                return -1;
        }

-       conn = osmo_stream_srv_create(tall_test, srv, fd,
-                                      read_cb, close_cb, NULL);
+       conn = osmo_stream_srv_create2(tall_test, "ipa_srv", srv, fd, NULL);
        if (conn == NULL) {
                LOGP(DSTREAMTEST, LOGL_ERROR,
                        "error while creating connection\n");
                return -1;
        }
+       osmo_stream_srv_set_read_cb(conn, read_cb);
+       osmo_stream_srv_set_closed_cb(conn, close_cb);

        return 0;
 }
diff --git a/examples/stream-client.c b/examples/stream-client.c
index 428402e..93fbdd3 100644
--- a/examples/stream-client.c
+++ b/examples/stream-client.c
@@ -50,27 +50,11 @@
        return 0;
 }

-static int read_cb(struct osmo_stream_cli *conn)
+static int read_cb(struct osmo_stream_cli *conn, struct msgb *msg)
 {
-       int bytes;
-       struct msgb *msg;
-
        LOGP(DSTREAMTEST, LOGL_NOTICE, "receiving message from stream... ");

-       msg = msgb_alloc(1024, "STREAMCLIENT/test");
-       if (msg == NULL) {
-               LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n");
-               return 0;
-       }
-
-       bytes = osmo_stream_cli_recv(conn, msg);
-
-       if (bytes < 0) {
-               LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot receive message\n");
-               return 0;
-       }
-
-       LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d (%d) bytes: %s\n", bytes, 
msg->len, msgb_hexdump(msg));
+       LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d bytes: %s\n", msg->len, 
msgb_hexdump(msg));

        msgb_free(msg);
        return 0;
@@ -120,7 +104,7 @@
         * initialize stream cli.
         */

-       conn = osmo_stream_cli_create(tall_test);
+       conn = osmo_stream_cli_create2(tall_test, "stream_client");
        if (conn == NULL) {
                fprintf(stderr, "cannot create cli\n");
                exit(EXIT_FAILURE);
@@ -129,7 +113,7 @@
        osmo_stream_cli_set_port(conn, 10000);
        osmo_stream_cli_set_connect_cb(conn, connect_cb);
        osmo_stream_cli_set_disconnect_cb(conn, disconnect_cb);
-       osmo_stream_cli_set_read_cb(conn, read_cb);
+       osmo_stream_cli_set_read_cb2(conn, read_cb);

        if (osmo_stream_cli_open(conn) < 0) {
                fprintf(stderr, "cannot open cli\n");
diff --git a/examples/stream-server.c b/examples/stream-server.c
index e4ca480..9e077f0 100644
--- a/examples/stream-server.c
+++ b/examples/stream-server.c
@@ -42,29 +42,11 @@
        exit(EXIT_SUCCESS);
 }

-int read_cb(struct osmo_stream_srv *conn)
+int read_cb(struct osmo_stream_srv *conn, struct msgb *msg)
 {
-       int bytes;
-       struct msgb *msg;
-
        LOGP(DSTREAMTEST, LOGL_NOTICE, "receiving message from stream... ");

-       msg = msgb_alloc(1024, "STREAMSERVER/test");
-       if (msg == NULL) {
-               LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot allocate message\n");
-               return 0;
-       }
-
-       bytes = osmo_stream_srv_recv(conn, msg);
-
-       if (bytes <= 0) {
-               if (bytes < 0)
-                       LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot receive message: 
%s\n", strerror(-bytes));
-               else
-                       LOGPC(DSTREAMTEST, LOGL_ERROR, "client closed 
connection\n");
-               osmo_stream_srv_destroy(conn);
-       } else
-               LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d (%d) bytes: %s\n", 
bytes, msg->len, msgb_hexdump(msg));
+       LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d bytes: %s\n", msg->len, 
msgb_hexdump(msg));

        msgb_free(msg);
        return 0;
@@ -72,6 +54,7 @@

 static int close_cb(struct osmo_stream_srv *dummy)
 {
+       LOGPC(DSTREAMTEST, LOGL_ERROR, "client closed connection\n");
        conn = NULL;
        return 0;
 }
@@ -86,13 +69,14 @@
                return -1;
        }

-       conn = osmo_stream_srv_create(tall_test, srv, fd, read_cb,
-                                        close_cb, NULL);
+       conn = osmo_stream_srv_create2(tall_test, "stream_server", srv, fd, 
NULL);
        if (conn == NULL) {
                LOGP(DSTREAMTEST, LOGL_ERROR,
                        "error while creating connection\n");
                return -1;
        }
+       osmo_stream_srv_set_read_cb(conn, read_cb);
+       osmo_stream_srv_set_closed_cb(conn, close_cb);

        osmo_sock_get_name_buf(buf, OSMO_SOCK_NAME_MAXLEN, fd);
        LOGP(DSTREAMTEST, LOGL_NOTICE, "accepted client: %s\n", buf);

--
To view, visit https://gerrit.osmocom.org/c/libosmo-netif/+/33194
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmo-netif
Gerrit-Branch: master
Gerrit-Change-Id: I97a9979199c816686b32080534627f6f033e009e
Gerrit-Change-Number: 33194
Gerrit-PatchSet: 14
Gerrit-Owner: arehbein <[email protected]>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: daniel <[email protected]>
Gerrit-Reviewer: laforge <[email protected]>
Gerrit-Reviewer: pespin <[email protected]>
Gerrit-MessageType: merged

Reply via email to