Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_sse.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_str.h"
28#include "nilorea/n_log.h"
29#include "nilorea/n_network.h"
30
31#include <getopt.h>
32#include <string.h>
33
35#define MAX_EVENTS 3
36
38static int event_count = 0;
40static N_SSE_CONN* g_conn = NULL;
41
42void usage(void) {
43 fprintf(stderr,
44 " -V 'log level' : set the log level (LOG_NULL, LOG_NOTICE, LOG_INFO, LOG_ERR, LOG_DEBUG)\n"
45 " -h : help\n");
46}
47
48#ifdef HAVE_OPENSSL
53static void on_sse_event(N_SSE_EVENT* event, N_SSE_CONN* conn, void* user_data) {
54 (void)user_data;
55 g_conn = conn;
57 fprintf(stdout, "SSE event %d: type=%s data=%s id=%s\n",
59 (event->event && event->event->data) ? event->event->data : "(default)",
60 (event->data && event->data->data) ? event->data->data : "(empty)",
61 (event->id && event->id->data) ? event->id->data : "(none)");
62
63 if (event_count >= MAX_EVENTS) {
64 n_sse_stop(conn);
65 }
66}
67#endif
68
69int main(int argc, char* argv[]) {
70 int log_level = LOG_ERR;
71 int getoptret = 0;
72
73 while ((getoptret = getopt(argc, argv, "hV:")) != EOF) {
74 switch (getoptret) {
75 case 'V':
76 if (!strncmp("LOG_NULL", optarg, 8)) {
78 } else if (!strncmp("LOG_NOTICE", optarg, 10)) {
80 } else if (!strncmp("LOG_INFO", optarg, 8)) {
82 } else if (!strncmp("LOG_ERR", optarg, 7)) {
84 } else if (!strncmp("LOG_DEBUG", optarg, 9)) {
86 } else {
87 fprintf(stderr, "%s is not a valid log level.\n", optarg);
88 exit(1);
89 }
90 break;
91 case 'h':
92 default:
93 usage();
94 exit(1);
95 }
96 }
98
99#ifdef HAVE_OPENSSL
100 n_log(LOG_INFO, "Connecting to SSE endpoint https://sse.dev/test ...");
101
102 g_conn = n_sse_connect("sse.dev", "443", "/test", 1, on_sse_event, NULL);
103 if (!g_conn) {
104 fprintf(stdout, "SSE server unreachable, skipping\n");
105 exit(0);
106 }
107
108 fprintf(stdout, "SSE example completed, received %d events\n", event_count);
110 n_log(LOG_INFO, "SSE example completed successfully");
111#else
112 (void)log_level;
113 fprintf(stdout, "OpenSSL not available, skipping SSE test\n");
114#endif
115
116 exit(0);
117}
static void usage(void)
int main(void)
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
static N_SSE_CONN * g_conn
pointer to connection for callback to stop
static int event_count
counter for received events
static void on_sse_event(N_SSE_EVENT *event, N_SSE_CONN *conn, void *user_data)
SSE event callback.
#define MAX_EVENTS
maximum number of events to receive before stopping
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
char * data
the string
Definition n_str.h:62
N_STR * event
event type (or NULL for default)
Definition n_network.h:714
N_STR * data
event data
Definition n_network.h:715
N_STR * id
last event ID (or NULL)
Definition n_network.h:716
void n_sse_stop(N_SSE_CONN *conn)
Signal the SSE connection to stop reading.
Definition n_network.c:5085
void n_sse_conn_free(N_SSE_CONN **conn)
Free an SSE connection structure.
Definition n_network.c:5094
N_SSE_CONN * n_sse_connect(const char *host, const char *port, const char *path, int use_ssl, void(*on_event)(N_SSE_EVENT *, N_SSE_CONN *, void *), void *user_data)
Connect to an SSE endpoint and start reading events.
Definition n_network.c:5179
SSE connection handle.
Definition n_network.h:723
SSE event received from server.
Definition n_network.h:713
Generic log system.
Network Engine.
N_STR and string function declaration.