Refresh

This website github.com/eclipse/mosquitto/pull/1508/files is currently offline. Cloudflare's Always Online™ shows a snapshot of this web page from the Internet Archive's Wayback Machine. To check for the live version, click Refresh.

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow client to publish on many topics #1508

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client/client_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ int cfg_add_topic(struct mosq_config *cfg, int type, char *topic, const char *ar
fprintf(stderr, "Error: Malformed UTF-8 in %s argument.\n\n", arg);
return 1;
}
if(type == CLIENT_PUB || type == CLIENT_RR){
if(type == CLIENT_RR){
if(mosquitto_pub_topic_check(topic) == MOSQ_ERR_INVAL){
fprintf(stderr, "Error: Invalid publish topic '%s', does it contain '+' or '#'?\n", topic);
return 1;
Expand All @@ -427,13 +427,15 @@ int cfg_add_topic(struct mosq_config *cfg, int type, char *topic, const char *ar
fprintf(stderr, "Error: Invalid subscription topic '%s', are all '+' and '#' wildcards correct?\n", topic);
return 1;
}
cfg->topic = strdup(topic);
cfg->topic_count++;
cfg->topics = realloc(cfg->topics, cfg->topic_count*sizeof(char *));
if(!cfg->topics){
err_printf(cfg, "Error: Out of memory.\n");
return 1;
}
cfg->topics[cfg->topic_count-1] = strdup(topic);
cfg->current_topic = cfg->topics + cfg->topic_count - 1;
}
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions client/client_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct mosq_config {
# endif
#endif
bool clean_session;
char **current_topic;
char **topics; /* sub */
int topic_count; /* sub */
bool exit_after_sub; /* sub */
Expand Down
14 changes: 9 additions & 5 deletions client/pub_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flag
case MSGMODE_CMD:
case MSGMODE_FILE:
case MSGMODE_STDIN_FILE:
rc = my_publish(mosq, &mid_sent, cfg.topic, cfg.msglen, cfg.message, cfg.qos, cfg.retain);
rc = my_publish(mosq, &mid_sent, *cfg.current_topic, cfg.msglen, cfg.message, cfg.qos, cfg.retain);
break;
case MSGMODE_NULL:
rc = my_publish(mosq, &mid_sent, cfg.topic, 0, NULL, cfg.qos, cfg.retain);
rc = my_publish(mosq, &mid_sent, *cfg.current_topic, 0, NULL, cfg.qos, cfg.retain);
break;
case MSGMODE_STDIN_LINE:
status = STATUS_CONNACK_RECVD;
Expand Down Expand Up @@ -193,6 +193,10 @@ void my_publish_callback(struct mosquitto *mosq, void *obj, int mid, int reason_
}
publish_count++;

++cfg.current_topic;
cfg.current_topic = (cfg.topics + cfg.topic_count == cfg.current_topic) ? cfg.topics : cfg.current_topic;


if(cfg.pub_mode == MSGMODE_STDIN_LINE){
if(mid == last_mid){
mosquitto_disconnect_v5(mosq, 0, cfg.disconnect_props);
Expand Down Expand Up @@ -250,7 +254,7 @@ int pub_shared_loop(struct mosquitto *mosq)
buf_len_actual = strlen(line_buf);
if(line_buf[buf_len_actual-1] == '\n'){
line_buf[buf_len_actual-1] = '\0';
rc2 = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain);
rc2 = my_publish(mosq, &mid_sent, *cfg.current_topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain);
if(rc2){
err_printf(&cfg, "Error: Publish returned %d, disconnecting.\n", rc2);
mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props);
Expand Down Expand Up @@ -306,10 +310,10 @@ int pub_shared_loop(struct mosquitto *mosq)
case MSGMODE_CMD:
case MSGMODE_FILE:
case MSGMODE_STDIN_FILE:
rc = my_publish(mosq, &mid_sent, cfg.topic, cfg.msglen, cfg.message, cfg.qos, cfg.retain);
rc = my_publish(mosq, &mid_sent, *cfg.current_topic, cfg.msglen, cfg.message, cfg.qos, cfg.retain);
break;
case MSGMODE_NULL:
rc = my_publish(mosq, &mid_sent, cfg.topic, 0, NULL, cfg.qos, cfg.retain);
rc = my_publish(mosq, &mid_sent, *cfg.current_topic, 0, NULL, cfg.qos, cfg.retain);
break;
case MSGMODE_STDIN_LINE:
break;
Expand Down