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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include "nilorea/n_str.h"
29#include "nilorea/n_log.h"
30#include "nilorea/n_network.h"
31
32#include <getopt.h>
33#include <string.h>
34
36#define MAX_EVENTS 3
37
39static int event_count = 0;
41static N_SSE_CONN* g_conn = NULL;
42
43void usage(void) {
44 fprintf(stderr,
45 " -V 'log level' : set the log level (LOG_NULL, LOG_NOTICE, LOG_INFO, LOG_ERR, LOG_DEBUG)\n"
46 " -h : help\n");
47}
48
49#ifdef HAVE_OPENSSL
54static void on_sse_event(N_SSE_EVENT* event, N_SSE_CONN* conn, void* user_data) {
55 (void)user_data;
56 g_conn = conn;
58 fprintf(stdout, "SSE event %d: type=%s data=%s id=%s\n",
60 (event->event && event->event->data) ? event->event->data : "(default)",
61 (event->data && event->data->data) ? event->data->data : "(empty)",
62 (event->id && event->id->data) ? event->id->data : "(none)");
63
64 if (event_count >= MAX_EVENTS) {
65 n_sse_stop(conn);
66 }
67}
68#endif
69
70int main(int argc, char* argv[]) {
71 int log_level = LOG_ERR;
72 int getoptret = 0;
73
74 while ((getoptret = getopt(argc, argv, "hV:")) != EOF) {
75 switch (getoptret) {
76 case 'V':
77 if (!strncmp("LOG_NULL", optarg, 8)) {
79 } else if (!strncmp("LOG_NOTICE", optarg, 10)) {
81 } else if (!strncmp("LOG_INFO", optarg, 8)) {
83 } else if (!strncmp("LOG_ERR", optarg, 7)) {
85 } else if (!strncmp("LOG_DEBUG", optarg, 9)) {
87 } else {
88 fprintf(stderr, "%s is not a valid log level.\n", optarg);
89 exit(1);
90 }
91 break;
92 case 'h':
93 default:
94 usage();
95 exit(1);
96 }
97 }
99
100#ifdef HAVE_OPENSSL
101 n_log(LOG_INFO, "Connecting to SSE endpoint https://sse.dev/test ...");
102
103 g_conn = n_sse_connect("sse.dev", "443", "/test", 1, NULL, on_sse_event, NULL);
104 if (!g_conn) {
105 fprintf(stdout, "SSE server unreachable, skipping\n");
106 exit(0);
107 }
108
109 fprintf(stdout, "SSE example completed, received %d events\n", event_count);
111 n_log(LOG_INFO, "SSE example completed successfully");
112#else
113 (void)log_level;
114 fprintf(stdout, "OpenSSL not available, skipping SSE test\n");
115#endif
116
117 exit(0);
118}
static void usage(void)
int main(void)
int getoptret
Definition ex_fluid.c:59
int log_level
Definition ex_fluid.c:60
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:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_NULL
no log output
Definition n_log.h:46
#define LOG_INFO
informational
Definition n_log.h:82
char * data
the string
Definition n_str.h:63
N_STR * event
event type (or NULL for default)
Definition n_network.h:1003
N_STR * data
event data
Definition n_network.h:1004
N_STR * id
last event ID (or NULL)
Definition n_network.h:1005
void n_sse_stop(N_SSE_CONN *conn)
Signal the SSE connection to stop reading.
Definition n_network.c:6617
void n_sse_conn_free(N_SSE_CONN **conn)
Free an SSE connection structure.
Definition n_network.c:6626
N_SSE_CONN * n_sse_connect(const char *host, const char *port, const char *path, int use_ssl, const char *user_agent, 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:6711
SSE connection handle.
Definition n_network.h:1012
SSE event received from server.
Definition n_network.h:1002
Generic log system.
Network Engine.
N_STR and string function declaration.