Browse code

dlgs: rpc commands to get records by filter

Daniel-Constantin Mierla authored on 09/07/2020 08:50:29
Showing 1 changed files
... ...
@@ -1033,6 +1033,36 @@ static void dlgs_rpc_stats(rpc_t *rpc, void *ctx)
1033 1033
 	} while(i<2);
1034 1034
 }
1035 1035
 
1036
+/*
1037
+ * RPC command to add a dlg item to rpc result
1038
+ */
1039
+static int dlgs_rpc_add_item(rpc_t *rpc, void *ctx, dlgs_item_t *it, int n,
1040
+		int mode)
1041
+{
1042
+	void *th;
1043
+
1044
+	if (rpc->add(ctx, "{", &th) < 0) {
1045
+		rpc->fault(ctx, 500, "Internal error creating rpc");
1046
+		return -1;
1047
+	}
1048
+	if(rpc->struct_add(th, "dSSSSSSSuuu",
1049
+					"count", n,
1050
+					"src", &it->src,
1051
+					"dst", &it->dst,
1052
+					"data", &it->data,
1053
+					"ruid", &it->ruid,
1054
+					"callid", &it->callid,
1055
+					"ftag", &it->ftag,
1056
+					"ttag", &it->ttag,
1057
+					"ts_init", (unsigned int)it->ts_init,
1058
+					"ts_answer", (unsigned int)it->ts_answer,
1059
+					"state", it->state)<0) {
1060
+		rpc->fault(ctx, 500, "Internal error creating item");
1061
+		return -1;
1062
+	}
1063
+	return 0;
1064
+}
1065
+
1036 1066
 static const char *dlgs_rpc_list_doc[2] = {
1037 1067
 	"List the dlgs records",
1038 1068
 	0
... ...
@@ -1047,7 +1077,6 @@ static void dlgs_rpc_list(rpc_t *rpc, void *ctx)
1047 1077
 	dlgs_item_t *it;
1048 1078
 	int n = 0;
1049 1079
 	int i;
1050
-	void *th;
1051 1080
 
1052 1081
 	if(_dlgs_htb == NULL) {
1053 1082
 		return;
... ...
@@ -1057,25 +1086,9 @@ static void dlgs_rpc_list(rpc_t *rpc, void *ctx)
1057 1086
 		lock_get(&_dlgs_htb->slots[i].lock);
1058 1087
 		it = _dlgs_htb->slots[i].first;
1059 1088
 		while(it) {
1060
-			if (rpc->add(ctx, "{", &th) < 0) {
1061
-				lock_release(&_dlgs_htb->slots[i].lock);
1062
-				rpc->fault(ctx, 500, "Internal error creating rpc");
1063
-				return;
1064
-			}
1065
-			if(rpc->struct_add(th, "dSSSSSSSuuu",
1066
-							"count", ++n,
1067
-							"src", &it->src,
1068
-							"dst", &it->dst,
1069
-							"data", &it->data,
1070
-							"ruid", &it->ruid,
1071
-							"callid", &it->callid,
1072
-							"ftag", &it->ftag,
1073
-							"ttag", &it->ttag,
1074
-							"ts_init", (unsigned int)it->ts_init,
1075
-							"ts_answer", (unsigned int)it->ts_answer,
1076
-							"state", it->state)<0) {
1089
+			n++;
1090
+			if(dlgs_rpc_add_item(rpc, ctx, it, n, 0) < 0) {
1077 1091
 				lock_release(&_dlgs_htb->slots[i].lock);
1078
-				rpc->fault(ctx, 500, "Internal error creating item");
1079 1092
 				return;
1080 1093
 			}
1081 1094
 			it = it->next;
... ...
@@ -1085,7 +1098,7 @@ static void dlgs_rpc_list(rpc_t *rpc, void *ctx)
1085 1098
 }
1086 1099
 
1087 1100
 static const char *dlgs_rpc_briefing_doc[2] = {
1088
-	"Briefinf the dlgs records",
1101
+	"Briefing the dlgs records",
1089 1102
 	0
1090 1103
 };
1091 1104
 
... ...
@@ -1129,6 +1142,88 @@ static void dlgs_rpc_briefing(rpc_t *rpc, void *ctx)
1129 1142
 	}
1130 1143
 }
1131 1144
 
1145
+/*
1146
+ * Helper to get dlgs records by filter
1147
+ */
1148
+static void dlgs_rpc_get_limit(rpc_t *rpc, void *ctx, int limit)
1149
+{
1150
+	dlgs_item_t *it = NULL;
1151
+	int n = 0;
1152
+	int i = 0;
1153
+	str vfield = STR_NULL;
1154
+	str vop = STR_NULL;
1155
+	str vdata = STR_NULL;
1156
+	int tfield = 0;
1157
+	int top = 0;
1158
+
1159
+	if(_dlgs_htb == NULL) {
1160
+		return;
1161
+	}
1162
+	n = rpc->scan(ctx, "SSS", &vfield, &vop, &vdata);
1163
+	if(n < 3) {
1164
+		rpc->fault(ctx, 500, "Invalid Parameters");
1165
+		return;
1166
+	}
1167
+
1168
+	if(dlgs_parse_field(&vfield, &tfield)<0) {
1169
+		rpc->fault(ctx, 500, "Invalid Field");
1170
+		return;
1171
+	}
1172
+
1173
+	if(dlgs_parse_op(&vop, &top)<0) {
1174
+		rpc->fault(ctx, 500, "Invalid Operator");
1175
+		return;
1176
+	}
1177
+
1178
+	n = 0;
1179
+	for(i = 0; i < _dlgs_htb->htsize; i++) {
1180
+		lock_get(&_dlgs_htb->slots[i].lock);
1181
+		it = _dlgs_htb->slots[i].first;
1182
+		while(it) {
1183
+			if(dlgs_match_field(it, tfield, top, &vdata, NULL)==0) {
1184
+				n++;
1185
+				if(dlgs_rpc_add_item(rpc, ctx, it, n, 0) < 0) {
1186
+					lock_release(&_dlgs_htb->slots[i].lock);
1187
+					return;
1188
+				}
1189
+				if(limit!=0 && limit==n) {
1190
+					/* finished by limit */
1191
+					lock_release(&_dlgs_htb->slots[i].lock);
1192
+					return;
1193
+				}
1194
+			}
1195
+			it = it->next;
1196
+		}
1197
+		lock_release(&_dlgs_htb->slots[i].lock);
1198
+	}
1199
+}
1200
+
1201
+static const char *dlgs_rpc_get_doc[2] = {
1202
+	"Get the first dlgs record by filter",
1203
+	0
1204
+};
1205
+
1206
+/*
1207
+ * RPC command to get first dlgs record by filter
1208
+ */
1209
+static void dlgs_rpc_get(rpc_t *rpc, void *ctx)
1210
+{
1211
+	dlgs_rpc_get_limit(rpc, ctx, 1);
1212
+}
1213
+
1214
+static const char *dlgs_rpc_getall_doc[2] = {
1215
+	"Get the all dlgs records by filter",
1216
+	0
1217
+};
1218
+
1219
+/*
1220
+ * RPC command to get all dlgs records by filter
1221
+ */
1222
+static void dlgs_rpc_getall(rpc_t *rpc, void *ctx)
1223
+{
1224
+	dlgs_rpc_get_limit(rpc, ctx, 0);
1225
+}
1226
+
1132 1227
 /* clang-format off */
1133 1228
 rpc_export_t dlgs_rpc_cmds[] = {
1134 1229
 	{"dlgs.stats", dlgs_rpc_stats,
... ...
@@ -1137,6 +1232,10 @@ rpc_export_t dlgs_rpc_cmds[] = {
1137 1232
 		dlgs_rpc_list_doc, RET_ARRAY},
1138 1233
 	{"dlgs.briefing",  dlgs_rpc_briefing,
1139 1234
 		dlgs_rpc_briefing_doc, RET_ARRAY},
1235
+	{"dlgs.get",  dlgs_rpc_get,
1236
+		dlgs_rpc_get_doc, RET_ARRAY},
1237
+	{"dlgs.getall",  dlgs_rpc_getall,
1238
+		dlgs_rpc_getall_doc, RET_ARRAY},
1140 1239
 
1141 1240
 	{0, 0, 0, 0}
1142 1241
 };