Skip to content

Commit f63f81e

Browse files
committed
MT#61856 control_ng: substitute switch lookup
With the newly introduced `ng_command_find()` lookup func. Add the local `ng_command_def` and maintain via it. Check whether: - the handler has been defined - the handler gets properly selected - the command is defined (known) Accordingly update the opmode of the command context. By default define as `OP_OTHER` always. For the case when no handler has been defined, introduce the error reply "No handler found". Accordingly trigger selected handler after the command was found. The main payoff here is that the command processing flow becomes quite transparent and easy to maintain: - parse command string - lookup command definition - set opmode from table - call uniform handler - shared error/success reply handling for all commands Deprecate strhash based `__csh_lookup()`/`CSH_LOOKUP()`. Accordingly adap the daemons's and tests Makefile, remove the strhash objects with the normal control_ng one. Additionally: introduce the special `resultstr = "pong"` for the ping command case, because all other commands have "ok". Change-Id: Ifff7d7f61ae4d25fd220460f4485d794c9a7cbd5
1 parent cc55764 commit f63f81e

3 files changed

Lines changed: 28 additions & 139 deletions

File tree

daemon/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ CFLAGS += $(CFLAGS_MQTT)
7575
LDLIBS += $(LDLIBS_MQTT)
7676

7777
SRCS := main.c kernel.c helpers.c control_tcp.c call.c control_udp.c redis.c \
78-
cookie_cache.c udp_listener.c control_ng_flags_parser.c control_ng.strhash.c sdp.strhash.c stun.c rtcp.c \
78+
cookie_cache.c udp_listener.c control_ng_flags_parser.c control_ng.c sdp.strhash.c stun.c rtcp.c \
7979
crypto.c rtp.c call_interfaces.c dtls.c log.c cli.strhash.c graphite.c ice.c \
8080
media_socket.c homer.c recording.c statistics.c cdr.c ssrc.c iptables.c tcp_listener.c \
8181
codec.c load.c dtmf.c timerthread.c media_player.c jitter_buffer.c t38.c websocket.c \

daemon/control_ng.c

Lines changed: 25 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons
732732
struct ng_buffer **ngbufp)
733733
{
734734
str cmd = STR_NULL;
735+
const struct ng_command_def *cmd_def;
735736
const char *errstr, *resultstr;
736737
GString *log_str;
737738
int64_t cmd_start, cmd_stop, cmd_process_time = {0};
@@ -817,144 +818,32 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons
817818
// start command timer
818819
cmd_start = now_us();
819820

820-
switch (__csh_lookup(&cmd)) {
821-
case CSH_LOOKUP("ping"):
822-
resultstr = "pong";
823-
command_ctx.opmode = OP_PING;
824-
break;
825-
case CSH_LOOKUP("offer"):
826-
command_ctx.opmode = OP_OFFER;
827-
errstr = call_offer_ng(&command_ctx, addr);
828-
break;
829-
case CSH_LOOKUP("answer"):
830-
command_ctx.opmode = OP_ANSWER;
831-
errstr = call_answer_ng(&command_ctx);
832-
break;
833-
case CSH_LOOKUP("delete"):
834-
command_ctx.opmode = OP_DELETE;
835-
errstr = call_delete_ng(&command_ctx);
836-
break;
837-
case CSH_LOOKUP("query"):
838-
command_ctx.opmode = OP_QUERY;
839-
errstr = call_query_ng(&command_ctx);
840-
break;
841-
case CSH_LOOKUP("list"):
842-
command_ctx.opmode = OP_LIST;
843-
errstr = call_list_ng(&command_ctx);
844-
break;
845-
case CSH_LOOKUP("start recording"):
846-
command_ctx.opmode = OP_START_RECORDING;
847-
errstr = call_start_recording_ng(&command_ctx);
848-
break;
849-
case CSH_LOOKUP("stop recording"):
850-
command_ctx.opmode = OP_STOP_RECORDING;
851-
errstr = call_stop_recording_ng(&command_ctx);
852-
break;
853-
case CSH_LOOKUP("pause recording"):
854-
command_ctx.opmode = OP_PAUSE_RECORDING;
855-
errstr = call_pause_recording_ng(&command_ctx);
856-
break;
857-
case CSH_LOOKUP("start forwarding"):
858-
command_ctx.opmode = OP_START_FORWARDING;
859-
errstr = call_start_forwarding_ng(&command_ctx);
860-
break;
861-
case CSH_LOOKUP("stop forwarding"):
862-
command_ctx.opmode = OP_STOP_FORWARDING;
863-
errstr = call_stop_forwarding_ng(&command_ctx);
864-
break;
865-
case CSH_LOOKUP("block DTMF"):
866-
command_ctx.opmode = OP_BLOCK_DTMF;
867-
errstr = call_block_dtmf_ng(&command_ctx);
868-
break;
869-
case CSH_LOOKUP("unblock DTMF"):
870-
command_ctx.opmode = OP_UNBLOCK_DTMF;
871-
errstr = call_unblock_dtmf_ng(&command_ctx);
872-
break;
873-
case CSH_LOOKUP("block media"):
874-
command_ctx.opmode = OP_BLOCK_MEDIA;
875-
errstr = call_block_media_ng(&command_ctx);
876-
break;
877-
case CSH_LOOKUP("unblock media"):
878-
command_ctx.opmode = OP_UNBLOCK_MEDIA;
879-
errstr = call_unblock_media_ng(&command_ctx);
880-
break;
881-
case CSH_LOOKUP("silence media"):
882-
command_ctx.opmode = OP_SILENCE_MEDIA;
883-
errstr = call_silence_media_ng(&command_ctx);
884-
break;
885-
case CSH_LOOKUP("unsilence media"):
886-
command_ctx.opmode = OP_UNSILENCE_MEDIA;
887-
errstr = call_unsilence_media_ng(&command_ctx);
888-
break;
889-
case CSH_LOOKUP("play media"):
890-
command_ctx.opmode = OP_PLAY_MEDIA;
891-
errstr = call_play_media_ng(&command_ctx);
892-
break;
893-
case CSH_LOOKUP("stop media"):
894-
command_ctx.opmode = OP_STOP_MEDIA;
895-
errstr = call_stop_media_ng(&command_ctx);
896-
break;
897-
case CSH_LOOKUP("play DTMF"):
898-
command_ctx.opmode = OP_PLAY_DTMF;
899-
errstr = call_play_dtmf_ng(&command_ctx);
900-
break;
901-
case CSH_LOOKUP("statistics"):
902-
command_ctx.opmode = OP_STATISTICS;
903-
errstr = statistics_ng(&command_ctx);
904-
break;
905-
case CSH_LOOKUP("publish"):
906-
command_ctx.opmode = OP_PUBLISH;
907-
errstr = call_publish_ng(&command_ctx, addr);
908-
break;
909-
case CSH_LOOKUP("subscribe request"):
910-
command_ctx.opmode = OP_SUBSCRIBE_REQ;
911-
errstr = call_subscribe_request_ng(&command_ctx);
912-
break;
913-
case CSH_LOOKUP("subscribe answer"):
914-
command_ctx.opmode = OP_SUBSCRIBE_ANS;
915-
errstr = call_subscribe_answer_ng(&command_ctx);
916-
break;
917-
case CSH_LOOKUP("unsubscribe"):
918-
command_ctx.opmode = OP_UNSUBSCRIBE;
919-
errstr = call_unsubscribe_ng(&command_ctx);
920-
break;
921-
case CSH_LOOKUP("inject start"):
922-
command_ctx.opmode = OP_INJECT_START;
923-
errstr = call_inject_start_ng(&command_ctx);
924-
break;
925-
case CSH_LOOKUP("inject stop"):
926-
command_ctx.opmode = OP_INJECT_STOP;
927-
errstr = call_inject_stop_ng(&command_ctx);
928-
break;
929-
case CSH_LOOKUP("connect"):
930-
command_ctx.opmode = OP_CONNECT;
931-
errstr = call_connect_ng(&command_ctx);
932-
break;
933-
case CSH_LOOKUP("cli"):
934-
case CSH_LOOKUP("CLI"):
935-
command_ctx.opmode = OP_CLI;
936-
errstr = cli_ng(&command_ctx);
937-
break;
938-
case CSH_LOOKUP("transform"):
939-
command_ctx.opmode = OP_TRANSFORM;
940-
errstr = call_transform_ng(&command_ctx);
941-
break;
942-
case CSH_LOOKUP("create"):
943-
command_ctx.opmode = OP_CREATE;
944-
errstr = call_create_ng(&command_ctx);
945-
break;
946-
case CSH_LOOKUP("create answer"):
947-
command_ctx.opmode = OP_CREATE_ANSWER;
948-
errstr = call_create_answer_ng(&command_ctx);
949-
break;
950-
case CSH_LOOKUP("mesh"):
951-
command_ctx.opmode = OP_MESH;
952-
errstr = call_mesh_ng(&command_ctx);
953-
break;
954-
default:
955-
errstr = "Unrecognized command";
821+
command_ctx.opmode = OP_OTHER;
822+
cmd_def = ng_command_find(&cmd);
823+
824+
/* undefined command */
825+
if (!cmd_def) {
826+
errstr = "Unrecognized command";
827+
goto err_send;
956828
}
957829

830+
/* No handler found */
831+
if (!cmd_def->handler && !cmd_def->addr_handler) {
832+
errstr = "No command handler";
833+
goto err_send;
834+
}
835+
836+
command_ctx.opmode = cmd_def->opmode;
837+
/* ping has special response string */
838+
if (cmd_def->opmode == OP_PING)
839+
resultstr = "pong";
840+
841+
/* properly select the handler to be used (single-/double-parameter) */
842+
if (cmd_def->addr_handler)
843+
errstr = cmd_def->addr_handler(&command_ctx, addr);
844+
else
845+
errstr = cmd_def->handler(&command_ctx);
846+
958847
CH(homer_fill_values, hctx, &callid, command_ctx.opmode);
959848
CH(homer_trace_msg_in, hctx, data);
960849

t/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ test-stats: test-stats.o \
347347
../daemon/cli.strhash.o \
348348
../daemon/codec.o \
349349
../daemon/control_ng_flags_parser.o \
350-
../daemon/control_ng.strhash.o \
350+
../daemon/control_ng.o \
351351
../daemon/cookie_cache.o \
352352
../daemon/crypto.o \
353353
../daemon/dtls.o \
@@ -407,7 +407,7 @@ test-transcode: test-transcode.o \
407407
../daemon/cli.strhash.o \
408408
../daemon/codec.o \
409409
../daemon/control_ng_flags_parser.o \
410-
../daemon/control_ng.strhash.o \
410+
../daemon/control_ng.o \
411411
../daemon/cookie_cache.o \
412412
../daemon/crypto.o \
413413
../daemon/dtls.o \

0 commit comments

Comments
 (0)