Add MOSQ_EVT_CLIENT_OFFLINE.
This allows plugins to know when a client with a non-zero session expiry interval has gone offline.pull/2756/merge
parent
086a361bcc
commit
92c1899278
@ -0,0 +1,51 @@
|
||||
/*
|
||||
Copyright (c) 2023 Roger Light <roger@atchoo.org>
|
||||
Copyright (c) 2023 Cedalo Gmbh
|
||||
|
||||
All rights reserved. This program and the accompanying materials
|
||||
are made available under the terms of the Eclipse Public License 2.0
|
||||
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
|
||||
The Eclipse Public License is available at
|
||||
https://www.eclipse.org/legal/epl-2.0/
|
||||
and the Eclipse Distribution License is available at
|
||||
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
|
||||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
||||
|
||||
Contributors:
|
||||
Roger Light - initial implementation and documentation.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "mosquitto_broker_internal.h"
|
||||
#include "utlist.h"
|
||||
|
||||
|
||||
static void plugin__handle_client_offline_single(struct mosquitto__security_options *opts, struct mosquitto *context, int reason)
|
||||
{
|
||||
struct mosquitto_evt_client_offline event_data;
|
||||
struct mosquitto__callback *cb_base, *cb_next;
|
||||
|
||||
if(context->id == NULL) return;
|
||||
|
||||
memset(&event_data, 0, sizeof(event_data));
|
||||
event_data.client = context;
|
||||
event_data.reason = reason;
|
||||
DL_FOREACH_SAFE(opts->plugin_callbacks.client_offline, cb_base, cb_next){
|
||||
cb_base->cb(MOSQ_EVT_CLIENT_OFFLINE, &event_data, cb_base->userdata);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void plugin__handle_client_offline(struct mosquitto *context, int reason)
|
||||
{
|
||||
/* Global plugins */
|
||||
plugin__handle_client_offline_single(&db.config->security_options, context, reason);
|
||||
|
||||
/* Per listener plugins */
|
||||
if(db.config->per_listener_settings && context->listener){
|
||||
plugin__handle_client_offline_single(context->listener->security_options, context, reason);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from mosq_test_helper import *
|
||||
|
||||
def write_config(filename, port):
|
||||
with open(filename, 'w') as f:
|
||||
f.write("listener %d\n" % (port))
|
||||
f.write("plugin c/plugin_evt_client_offline.so\n")
|
||||
f.write("allow_anonymous true\n")
|
||||
|
||||
|
||||
def do_test():
|
||||
rc = 1
|
||||
connect_packet = mosq_test.gen_connect("plugin-evt-subscribe", proto_ver=4, clean_session=False)
|
||||
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=4)
|
||||
|
||||
publish_packet = mosq_test.gen_publish("evt/client/offline", qos=0, payload="plugin-evt-subscribe")
|
||||
|
||||
port = mosq_test.get_port()
|
||||
conf_file = os.path.basename(__file__).replace('.py', '.conf')
|
||||
write_config(conf_file, port)
|
||||
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port, use_conf=True)
|
||||
|
||||
try:
|
||||
sub_sock = mosq_test.sub_helper(port, '#')
|
||||
|
||||
sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
|
||||
sock.close()
|
||||
|
||||
mosq_test.expect_packet(sub_sock, "publish", publish_packet)
|
||||
rc = 0
|
||||
|
||||
sub_sock.close()
|
||||
except mosq_test.TestError:
|
||||
pass
|
||||
finally:
|
||||
os.remove(conf_file)
|
||||
broker.terminate()
|
||||
if mosq_test.wait_for_subprocess(broker):
|
||||
print("broker not terminated")
|
||||
if rc == 0: rc=1
|
||||
(stdo, stde) = broker.communicate()
|
||||
if rc:
|
||||
print(stde.decode('utf-8'))
|
||||
exit(rc)
|
||||
|
||||
|
||||
do_test()
|
@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <mosquitto.h>
|
||||
#include <mosquitto_broker.h>
|
||||
#include <mosquitto_plugin.h>
|
||||
|
||||
MOSQUITTO_PLUGIN_DECLARE_VERSION(5);
|
||||
|
||||
static mosquitto_plugin_id_t *plg_id;
|
||||
|
||||
int callback_client_offline(int event, void *event_data, void *user_data)
|
||||
{
|
||||
struct mosquitto_evt_client_offline *ed = event_data;
|
||||
const char *clientid;
|
||||
|
||||
(void)user_data;
|
||||
|
||||
if(event != MOSQ_EVT_CLIENT_OFFLINE){
|
||||
abort();
|
||||
}
|
||||
clientid = mosquitto_client_id(ed->client);
|
||||
mosquitto_broker_publish_copy(NULL, "evt/client/offline", strlen(clientid), clientid, 0, false, NULL);
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *opts, int opt_count)
|
||||
{
|
||||
(void)user_data;
|
||||
(void)opts;
|
||||
(void)opt_count;
|
||||
|
||||
plg_id = identifier;
|
||||
|
||||
mosquitto_callback_register(plg_id, MOSQ_EVT_CLIENT_OFFLINE, callback_client_offline, NULL, NULL);
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
}
|
||||
|
||||
int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *opts, int opt_count)
|
||||
{
|
||||
(void)user_data;
|
||||
(void)opts;
|
||||
(void)opt_count;
|
||||
|
||||
mosquitto_callback_unregister(plg_id, MOSQ_EVT_CLIENT_OFFLINE, callback_client_offline, NULL);
|
||||
|
||||
return MOSQ_ERR_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue