diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/ipa-stream-client.c | 37 | ||||
-rw-r--r-- | examples/ipa-stream-server.c | 39 | ||||
-rw-r--r-- | examples/osmux-test-input.c | 15 | ||||
-rw-r--r-- | examples/osmux-test-output.c | 12 | ||||
-rw-r--r-- | examples/stream-client.c | 82 | ||||
-rw-r--r-- | examples/stream-server.c | 84 |
6 files changed, 162 insertions, 107 deletions
diff --git a/examples/ipa-stream-client.c b/examples/ipa-stream-client.c index 0c9c589..b58370e 100644 --- a/examples/ipa-stream-client.c +++ b/examples/ipa-stream-client.c @@ -74,7 +74,7 @@ static int connect_cb(struct osmo_stream_cli *conn) char *ptr; int x; - msg = osmo_ipa_msg_alloc(0); + msg = osmo_ipa_ext_msg_alloc(0); if (msg == NULL) { LOGP(DLINP, LOGL_ERROR, "cannot alloc msg\n"); return -1; @@ -93,8 +93,7 @@ static int connect_cb(struct osmo_stream_cli *conn) msg_sent->num = i; llist_add(&msg_sent->head, &msg_sent_list); - osmo_ipa_msg_push_header(msg, IPAC_PROTO_OSMO); - + osmo_ipa_msg_push_headers(msg, osmo_ipa_msgb_cb_proto(msg), osmo_ipa_msgb_cb_proto_ext(msg)); osmo_stream_cli_send(conn, msg); LOGP(DIPATEST, LOGL_DEBUG, "enqueueing msg %d of " @@ -103,30 +102,26 @@ static int connect_cb(struct osmo_stream_cli *conn) return 0; } -static int read_cb(struct osmo_stream_cli *conn) +static int read_cb(struct osmo_stream_cli *conn, int res, struct msgb *msg) { - struct msgb *msg; + int num; + struct msg_sent *cur, *tmp, *found = NULL; - LOGP(DIPATEST, LOGL_DEBUG, "received message from stream\n"); + LOGP(DIPATEST, LOGL_DEBUG, "received message from stream (payload len=%d)\n", msgb_length(msg)); - 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"); + if (res <= 0) { + LOGP(DIPATEST, LOGL_ERROR, "Event with no data! %d\n", res); + msgb_free(msg); return 0; } + if (osmo_ipa_process_msg(msg) < 0) { LOGP(DIPATEST, LOGL_ERROR, "bad IPA message\n"); + msgb_free(msg); return 0; } - int num; - struct msg_sent *cur, *tmp, *found = NULL; - - num = ntohl(*((int *)(msg->data + sizeof(struct ipa_head)))); + num = osmo_load32be(msg->data); LOGP(DLINP, LOGL_DEBUG, "received msg number %d\n", num); llist_for_each_entry_safe(cur, tmp, &msg_sent_list, head) { @@ -149,6 +144,7 @@ static int read_cb(struct osmo_stream_cli *conn) LOGP(DLINP, LOGL_ERROR, "message %d not found!\n", num); } + msgb_free(msg); return 0; } @@ -169,7 +165,7 @@ int main(int argc, char *argv[]) tall_test = talloc_named_const(NULL, 1, "osmo_stream_client_test"); msgb_talloc_ctx_init(tall_test, 0); osmo_init_logging2(tall_test, &osmo_stream_client_test_log_info); - log_set_log_level(osmo_stderr_target, LOGL_NOTICE); + log_set_log_level(osmo_stderr_target, LOGL_DEBUG); /* * initialize stream client. @@ -180,11 +176,12 @@ int main(int argc, char *argv[]) fprintf(stderr, "cannot create client\n"); exit(EXIT_FAILURE); } + osmo_stream_cli_set_name(conn, "ipa_test_client"); osmo_stream_cli_set_addr(conn, "127.0.0.1"); 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); @@ -193,6 +190,8 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + osmo_stream_cli_set_segmentation_cb(conn, osmo_ipa_segmentation_cb); + LOGP(DIPATEST, LOGL_NOTICE, "Entering main loop\n"); while(1) { diff --git a/examples/ipa-stream-server.c b/examples/ipa-stream-server.c index c311697..e87eab4 100644 --- a/examples/ipa-stream-server.c +++ b/examples/ipa-stream-server.c @@ -1,4 +1,5 @@ /* IPA stream srv example */ +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -46,29 +47,18 @@ void sighandler(int foo) exit(EXIT_SUCCESS); } -int read_cb(struct osmo_stream_srv *conn) +int read_cb(struct osmo_stream_srv *conn, int res, struct msgb *msg) { - struct msgb *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); + if (res <= 0) { + LOGP(DSTREAMTEST, LOGL_ERROR, "cannot receive message (%d)\n", res); msgb_free(msg); - return 0; - } - if (osmo_ipa_process_msg(msg) < 0) { - LOGP(DSTREAMTEST, LOGL_ERROR, "Bad IPA message\n"); - msgb_free(msg); - return 0; + osmo_stream_srv_destroy(conn); + return -EBADF; } + LOGP(DSTREAMTEST, LOGL_DEBUG, "received message from stream (payload len=%d)\n", msgb_length(msg)); + + osmo_ipa_msg_push_headers(msg, osmo_ipa_msgb_cb_proto(msg), osmo_ipa_msgb_cb_proto_ext(msg)); osmo_stream_srv_send(conn, msg); return 0; } @@ -83,17 +73,20 @@ static int accept_cb(struct osmo_stream_srv_link *srv, int fd) { 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, srv, fd, NULL); if (conn == NULL) { LOGP(DSTREAMTEST, LOGL_ERROR, "error while creating connection\n"); return -1; } + osmo_stream_srv_set_name(conn, "ipa_srv"); + osmo_stream_srv_set_read_cb(conn, read_cb); + osmo_stream_srv_set_closed_cb(conn, close_cb); + osmo_stream_srv_set_segmentation_cb(conn, osmo_ipa_segmentation_cb); return 0; } @@ -103,7 +96,7 @@ int main(void) tall_test = talloc_named_const(NULL, 1, "osmo_stream_srv_test"); msgb_talloc_ctx_init(tall_test, 0); osmo_init_logging2(tall_test, &osmo_stream_srv_test_log_info); - log_set_log_level(osmo_stderr_target, LOGL_NOTICE); + log_set_log_level(osmo_stderr_target, LOGL_DEBUG); /* * initialize stream srv. diff --git a/examples/osmux-test-input.c b/examples/osmux-test-input.c index 57e199c..d91ee15 100644 --- a/examples/osmux-test-input.c +++ b/examples/osmux-test-input.c @@ -83,11 +83,7 @@ static void osmux_deliver(struct msgb *batch_msg, void *data) * This is the input handle for osmux. It stores the last osmux sequence that * has been used and the deliver function that sends the osmux batch. */ -struct osmux_in_handle h_input = { - .osmux_seq = 0, /* sequence number to start OSmux message from */ - .batch_factor = 4, /* batch up to 4 RTP messages */ - .deliver = osmux_deliver, -}; +struct osmux_in_handle *h_input; #define MAX_CONCURRENT_CALLS 8 @@ -165,9 +161,9 @@ int read_cb(struct osmo_dgram *conn) if (ccid < 0) register_ccid(rtph->ssrc); - while ((ret = osmux_xfrm_input(&h_input, msg, ccid)) > 0) { + while ((ret = osmux_xfrm_input(h_input, msg, ccid)) > 0) { /* batch full, deliver it */ - osmux_xfrm_input_deliver(&h_input); + osmux_xfrm_input_deliver(h_input); } if (ret == -1) printf("something is wrong\n"); @@ -217,7 +213,10 @@ int main(int argc, char *argv[]) /* * initialize OSMUX handlers. */ - osmux_xfrm_input_init(&h_input); + h_input = osmux_xfrm_input_alloc(tall_test); + osmux_xfrm_input_set_initial_seqnum(h_input, 0); + osmux_xfrm_input_set_batch_factor(h_input, 4); + osmux_xfrm_input_set_deliver_cb(h_input, osmux_deliver, NULL); /* * initialize datagram server. diff --git a/examples/osmux-test-output.c b/examples/osmux-test-output.c index 0ab03a7..3f0e5a2 100644 --- a/examples/osmux-test-output.c +++ b/examples/osmux-test-output.c @@ -42,7 +42,7 @@ static struct osmo_rtp_handle *rtp; * This is the output handle for osmux, it stores last RTP sequence and * timestamp that has been used. There should be one per circuit ID. */ -static struct osmux_out_handle h_output; +static struct osmux_out_handle *h_output; static int fd; @@ -107,7 +107,7 @@ int read_cb(struct osmo_dgram *conn) msg->len, buf); while((osmuxh = osmux_xfrm_output_pull(msg)) != NULL) - osmux_xfrm_output_sched(&h_output, osmuxh); + osmux_xfrm_output_sched(h_output, osmuxh); return 0; } @@ -117,7 +117,7 @@ void sighandler(int foo) LOGP(DOSMUX_TEST, LOGL_NOTICE, "closing OSMUX.\n"); osmo_dgram_close(conn); osmo_dgram_destroy(conn); - osmux_xfrm_output_flush(&h_output); + talloc_free(h_output); osmo_rtp_handle_free(rtp); amr_close(); exit(EXIT_SUCCESS); @@ -155,8 +155,10 @@ int main(int argc, char *argv[]) /* * initialize OSMUX handlers. */ - osmux_xfrm_output_init(&h_output, random()); - osmux_xfrm_output_set_tx_cb(&h_output, tx_cb, NULL); + h_output = osmux_xfrm_output_alloc(NULL); + osmux_xfrm_output_set_rtp_ssrc(h_output, random()); + osmux_xfrm_output_set_rtp_pl_type(h_output, 98); + osmux_xfrm_output_set_tx_cb(h_output, tx_cb, NULL); /* * initialize datagram server. */ diff --git a/examples/stream-client.c b/examples/stream-client.c index 428402e..6d20263 100644 --- a/examples/stream-client.c +++ b/examples/stream-client.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <netinet/in.h> #include <osmocom/core/select.h> #include <osmocom/core/talloc.h> @@ -31,10 +32,13 @@ const struct log_info osmo_stream_cli_test_log_info = { static struct osmo_stream_cli *conn; +static bool quit = false; + void sighandler(int foo) { LOGP(DSTREAMTEST, LOGL_NOTICE, "closing stream.\n"); - exit(EXIT_SUCCESS); + quit = true; + signal(SIGINT, SIG_DFL); } static int connect_cb(struct osmo_stream_cli *conn) @@ -50,27 +54,18 @@ static int disconnect_cb(struct osmo_stream_cli *conn) return 0; } -static int read_cb(struct osmo_stream_cli *conn) +static int read_cb(struct osmo_stream_cli *conn, int res, 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"); + if (res < 0) { + LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot receive message (res = %d)\n", res); + msgb_free(msg); 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; @@ -98,23 +93,48 @@ static int kbd_cb(struct osmo_fd *fd, unsigned int what) ptr = msgb_put(msg, ret); memcpy(ptr, buf, ret); - osmo_stream_cli_send(conn, msg); + LOGP(DSTREAMTEST, LOGL_NOTICE, "sending %d bytes message: %s\n", msg->len, msgb_hexdump(msg)); - LOGP(DSTREAMTEST, LOGL_NOTICE, "sent %d bytes message: %s\n", msg->len, msgb_hexdump(msg)); + osmo_stream_cli_send(conn, msg); return 0; } -int main(void) +static void signal_handler(int signum) +{ + switch (signum) { + case SIGUSR1: + talloc_report_full(tall_test, stdout); + break; + } +} + +int main(int argc, char **argv) { struct osmo_fd *kbd_ofd; - int rc; + bool use_sctp = false; + const char *use_remote_addr = "127.0.0.1"; + int opt, rc; + + while ((opt = getopt(argc, argv, "sr:")) != -1) { + switch (opt) { + case 's': + use_sctp = true; + break; + case 'r': + use_remote_addr = optarg; + break; + default: + exit(0); + } + } + + signal(SIGUSR1, &signal_handler); tall_test = talloc_named_const(NULL, 1, "osmo_stream_cli_test"); msgb_talloc_ctx_init(tall_test, 0); osmo_init_logging2(tall_test, &osmo_stream_cli_test_log_info); - log_set_log_level(osmo_stderr_target, 1); - log_set_category_filter(osmo_stderr_target, DLINP, 0, LOGL_INFO); + log_set_log_level(osmo_stderr_target, LOGL_DEBUG); /* * initialize stream cli. @@ -125,14 +145,18 @@ int main(void) fprintf(stderr, "cannot create cli\n"); exit(EXIT_FAILURE); } - osmo_stream_cli_set_addr(conn, "127.0.0.1"); + osmo_stream_cli_set_name(conn, "stream_client"); + osmo_stream_cli_set_addr(conn, use_remote_addr); osmo_stream_cli_set_port(conn, 10000); + if (use_sctp) + osmo_stream_cli_set_proto(conn, IPPROTO_SCTP); 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"); + rc = osmo_stream_cli_open(conn); + if (rc < 0) { + fprintf(stderr, "cannot open cli: %d\n", rc); exit(EXIT_FAILURE); } @@ -148,9 +172,15 @@ int main(void) exit(EXIT_FAILURE); } + signal(SIGINT, sighandler); + LOGP(DSTREAMTEST, LOGL_NOTICE, "Entering main loop\n"); - while(1) { + while (!quit) { osmo_select_main(0); } + + osmo_fd_unregister(kbd_ofd); + + osmo_stream_cli_destroy(conn); } diff --git a/examples/stream-server.c b/examples/stream-server.c index e4ca480..5295c2b 100644 --- a/examples/stream-server.c +++ b/examples/stream-server.c @@ -3,6 +3,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <errno.h> #include <osmocom/core/select.h> #include <osmocom/core/socket.h> @@ -35,36 +36,30 @@ const struct log_info osmo_stream_srv_test_log_info = { static struct osmo_stream_srv_link *srv; static struct osmo_stream_srv *conn; +bool quit = false; void sighandler(int foo) { LOGP(DSTREAMTEST, LOGL_NOTICE, "closing STREAMSERVER.\n"); - exit(EXIT_SUCCESS); + quit = true; + signal(SIGINT, SIG_DFL); } -int read_cb(struct osmo_stream_srv *conn) +int read_cb(struct osmo_stream_srv *conn, int res, 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)); + if (res <= 0) { + if (res < 0) + LOGPC(DSTREAMTEST, LOGL_ERROR, "cannot receive message: %s\n", strerror(-res)); else LOGPC(DSTREAMTEST, LOGL_ERROR, "client closed connection\n"); + msgb_free(msg); osmo_stream_srv_destroy(conn); - } else - LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d (%d) bytes: %s\n", bytes, msg->len, msgb_hexdump(msg)); + return -EBADF; + } + + LOGPC(DSTREAMTEST, LOGL_NOTICE, "got %d bytes: %s\n", msg->len, msgb_hexdump(msg)); msgb_free(msg); return 0; @@ -72,6 +67,7 @@ int read_cb(struct osmo_stream_srv *conn) static int close_cb(struct osmo_stream_srv *dummy) { + LOGPC(DSTREAMTEST, LOGL_ERROR, "client closed connection\n"); conn = NULL; return 0; } @@ -86,13 +82,15 @@ static int accept_cb(struct osmo_stream_srv_link *srv, int fd) return -1; } - conn = osmo_stream_srv_create(tall_test, srv, fd, read_cb, - close_cb, NULL); + conn = osmo_stream_srv_create2(tall_test, srv, fd, NULL); if (conn == NULL) { LOGP(DSTREAMTEST, LOGL_ERROR, "error while creating connection\n"); return -1; } + osmo_stream_srv_set_name(conn, "stream_server"); + 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); @@ -127,20 +125,46 @@ static int kbd_cb(struct osmo_fd *fd, unsigned int what) memcpy(ptr, buf, ret); osmo_stream_srv_send(conn, msg); - LOGP(DSTREAMTEST, LOGL_NOTICE, "message of %d bytes sent\n", msg->len); + LOGP(DSTREAMTEST, LOGL_NOTICE, "message of %d bytes sent\n", ret); return 0; } -int main(void) +static void signal_handler(int signum) +{ + switch (signum) { + case SIGUSR1: + talloc_report(tall_test, stderr); + break; + } +} + +int main(int argc, char **argv) { struct osmo_fd *kbd_ofd; + bool use_sctp = false; + const char *use_local_addr = "127.0.0.1"; + int opt; + + while ((opt = getopt(argc, argv, "sl:")) != -1) { + switch (opt) { + case 's': + use_sctp = true; + break; + case 'l': + use_local_addr = optarg; + break; + default: + exit(0); + } + } + + signal(SIGUSR1, &signal_handler); tall_test = talloc_named_const(NULL, 1, "osmo_stream_srv_test"); msgb_talloc_ctx_init(tall_test, 0); osmo_init_logging2(tall_test, &osmo_stream_srv_test_log_info); - log_set_log_level(osmo_stderr_target, 1); - log_set_category_filter(osmo_stderr_target, DLINP, 0, LOGL_INFO); + log_set_log_level(osmo_stderr_target, LOGL_DEBUG); /* * initialize stream srv. @@ -151,8 +175,10 @@ int main(void) fprintf(stderr, "cannot create server link\n"); exit(EXIT_FAILURE); } - osmo_stream_srv_link_set_addr(srv, "127.0.0.1"); + osmo_stream_srv_link_set_addr(srv, use_local_addr); osmo_stream_srv_link_set_port(srv, 10000); + if (use_sctp) + osmo_stream_srv_link_set_proto(srv, IPPROTO_SCTP); osmo_stream_srv_link_set_accept_cb(srv, accept_cb); if (osmo_stream_srv_link_open(srv) < 0) { @@ -168,9 +194,15 @@ int main(void) osmo_fd_setup(kbd_ofd, STDIN_FILENO, OSMO_FD_READ, kbd_cb, srv, 0); osmo_fd_register(kbd_ofd); + signal(SIGINT, sighandler); + LOGP(DSTREAMTEST, LOGL_NOTICE, "Entering main loop on %s\n", osmo_stream_srv_link_get_sockname(srv)); - while(1) { + while (!quit) { osmo_select_main(0); } + + osmo_fd_unregister(kbd_ofd); + + osmo_stream_srv_link_destroy(srv); } |