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

mosquitto_pub: split main loop. #1473

Merged
merged 1 commit into from Nov 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
mosquitto_pub: split main loop.
Main loop has two different logics inside: one for stdin input,
second for all other cases. Exit loop condition is also different
for both variants. This commit splits these two variants into
two separate loops.

Signed-off-by: Marek Wodzinski <[email protected]>
  • Loading branch information
majekw committed Oct 5, 2019
commit ede9f2a51d753ab06c3d3ad0860c75f215fa6471
30 changes: 12 additions & 18 deletions client/pub_client.c
Expand Up @@ -223,7 +223,7 @@ int pub_shared_loop(struct mosquitto *mosq)
{
int read_len;
int pos;
int rc, rc2;
int rc;
char *buf2;
int buf_len_actual;
int mode;
Expand All @@ -239,20 +239,17 @@ int pub_shared_loop(struct mosquitto *mosq)
if(mode == MSGMODE_STDIN_LINE){
mosquitto_loop_start(mosq);
stdin_finished = false;
}

do{
if(mode == MSGMODE_STDIN_LINE){
do{
if(status == STATUS_CONNACK_RECVD){
pos = 0;
read_len = line_buf_len;
while(status == STATUS_CONNACK_RECVD && fgets(&line_buf[pos], read_len, stdin)){
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);
if(rc2){
err_printf(&cfg, "Error: Publish returned %d, disconnecting.\n", rc2);
rc = my_publish(mosq, &mid_sent, cfg.topic, buf_len_actual-1, line_buf, cfg.qos, cfg.retain);
if(rc){
err_printf(&cfg, "Error: Publish returned %d, disconnecting.\n", rc);
mosquitto_disconnect_v5(mosq, MQTT_RC_DISCONNECT_WITH_WILL_MSG, cfg.disconnect_props);
}
break;
Expand Down Expand Up @@ -297,11 +294,13 @@ int pub_shared_loop(struct mosquitto *mosq)
nanosleep(&ts, NULL);
#endif
}
rc = MOSQ_ERR_SUCCESS;
}else{
}while(stdin_finished == false);
mosquitto_loop_stop(mosq, false);
}else{
do{
rc = mosquitto_loop(mosq, loop_delay, 1);
if(ready_for_repeat && check_repeat_time()){
rc = 0;
rc = MOSQ_ERR_SUCCESS;
switch(cfg.pub_mode){
case MSGMODE_CMD:
case MSGMODE_FILE:
Expand All @@ -311,19 +310,14 @@ int pub_shared_loop(struct mosquitto *mosq)
case MSGMODE_NULL:
rc = my_publish(mosq, &mid_sent, cfg.topic, 0, NULL, cfg.qos, cfg.retain);
break;
case MSGMODE_STDIN_LINE:
break;
}
if(rc){
err_printf(&cfg, "Error sending repeat publish: %s", mosquitto_strerror(rc));
}
}
}
}while(rc == MOSQ_ERR_SUCCESS && stdin_finished == false);

if(mode == MSGMODE_STDIN_LINE){
mosquitto_loop_stop(mosq, false);
}while(rc == MOSQ_ERR_SUCCESS);
}

if(status == STATUS_DISCONNECTED){
return MOSQ_ERR_SUCCESS;
}else{
Expand Down