aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ipa-stream-server.c
blob: 5dfd4c993dd43e1e27e018f38a769a9e4c46b097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* IPA stream server example */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>

#include <osmocom/core/select.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/application.h>

#include <osmocom/netif/stream.h>
#include <osmocom/netif/ipa.h>

static void *tall_test;

#define DSTREAMTEST 0

struct log_info_cat osmo_stream_server_test_cat[] = {
	[DSTREAMTEST] = {
		.name = "DSTREAMTEST",
		.description = "STREAMSERVER-mode test",
		.color = "\033[1;35m",
		.enabled = 1, .loglevel = LOGL_NOTICE,
	},
};

const struct log_info osmo_stream_server_test_log_info = {
	.filter_fn = NULL,
	.cat = osmo_stream_server_test_cat,
	.num_cat = ARRAY_SIZE(osmo_stream_server_test_cat),
};

static struct osmo_stream_server_link *server;
static struct osmo_stream_server_conn *conn;

void sighandler(int foo)
{
	LOGP(DSTREAMTEST, LOGL_NOTICE, "closing STREAMSERVER.\n");
	exit(EXIT_SUCCESS);
}

int read_cb(struct osmo_stream_server_conn *conn)
{
	struct msgb *msg;
	struct osmo_fd *ofd = osmo_stream_server_conn_get_ofd(conn);

	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_ipa_msg_recv(ofd->fd, msg) <= 0) {
		LOGP(DSTREAMTEST, LOGL_ERROR, "cannot receive message\n");
		osmo_stream_server_conn_destroy(conn);
		msgb_free(msg);
		return 0;
	}
	osmo_stream_server_conn_send(conn, msg);
	return 0;
}

static int close_cb(struct osmo_stream_server_conn *dummy)
{
	conn = NULL;
	return 0;
}

static int accept_cb(struct osmo_stream_server_link *server, int fd)
{
	if (conn != NULL) {
		LOGP(DSTREAMTEST, LOGL_ERROR, "Sorry, this example only "
			"support one client simultaneously\n");
		return -1;
	}

	conn = osmo_stream_server_conn_create(tall_test, server, fd,
					      read_cb, close_cb, NULL);
	if (conn == NULL) {
		LOGP(DSTREAMTEST, LOGL_ERROR,
			"error while creating connection\n");
		return -1;
	}

	return 0;
}

int main(void)
{
	tall_test = talloc_named_const(NULL, 1, "osmo_stream_server_test");

	osmo_init_logging(&osmo_stream_server_test_log_info);
	log_set_log_level(osmo_stderr_target, LOGL_NOTICE);

	/*
	 * initialize stream server.
	 */

	server = osmo_stream_server_link_create(tall_test);
	if (server == NULL) {
		fprintf(stderr, "cannot create client\n");
		exit(EXIT_FAILURE);
	}
	osmo_stream_server_link_set_addr(server, "127.0.0.1");
	osmo_stream_server_link_set_port(server, 10000);
	osmo_stream_server_link_set_accept_cb(server, accept_cb);

	int on = 1, ret;
	struct osmo_fd *ofd = osmo_stream_server_link_get_ofd(server);

	if (osmo_stream_server_link_open(server) < 0) {
		fprintf(stderr, "cannot open client\n");
		exit(EXIT_FAILURE);
	}

	ret = setsockopt(ofd->fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
	if (ret < 0) {
		LOGP(DSTREAMTEST, LOGL_ERROR, "cannot disable Nagle\n");
		exit(EXIT_FAILURE);
	}

	LOGP(DSTREAMTEST, LOGL_NOTICE, "Entering main loop\n");

	while(1) {
		osmo_select_main(0);
	}
}