Improve register-status command to be able to list all registrations.

This commit is contained in:
Ghislain MARY 2012-08-28 16:28:26 +02:00
parent 813826d913
commit 7a16503fca

View file

@ -2,22 +2,68 @@
using namespace std;
RegisterStatusCommand::RegisterStatusCommand() :
DaemonCommand("register-status", "register-status <register id>", "Return status of a register.") {
class RegisterStatusResponse : public Response {
public:
RegisterStatusResponse();
RegisterStatusResponse(int id, const LinphoneProxyConfig *cfg);
void append(int id, const LinphoneProxyConfig *cfg);
};
RegisterStatusResponse::RegisterStatusResponse() {
}
RegisterStatusResponse::RegisterStatusResponse(int id, const LinphoneProxyConfig *cfg) {
append(id, cfg);
}
void RegisterStatusResponse::append(int id, const LinphoneProxyConfig* cfg) {
ostringstream ost;
ost << getBody();
if (ost.tellp() > 0) {
ost << "\n";
}
ost << "Id: " << id << "\n";
ost << "State: " << linphone_registration_state_to_string(linphone_proxy_config_get_state(cfg)) << "\n";
setBody(ost.str().c_str());
}
RegisterStatusCommand::RegisterStatusCommand() :
DaemonCommand("register-status", "register-status <register_id|ALL>", "Return status of a registration or of all registrations.") {
}
void RegisterStatusCommand::exec(Daemon *app, const char *args) {
LinphoneProxyConfig *cfg = NULL;
string param;
int pid;
if (sscanf(args, "%i", &pid) == 1) {
cfg = app->findProxy(pid);
if (cfg == NULL) {
app->sendResponse(Response("No register with such id."));
return;
}
} else {
app->sendResponse(Response("Missing/Incorrect parameter(s)."));
istringstream ist(args);
ist >> param;
if (ist.fail()) {
app->sendResponse(Response("Missing parameter.", Response::Error));
return;
}
app->sendResponse(Response(linphone_registration_state_to_string(linphone_proxy_config_get_state(cfg)), Response::Ok));
if (param.compare("ALL") == 0) {
RegisterStatusResponse response;
for (int i = 1; i <= app->maxProxyId(); i++) {
cfg = app->findProxy(i);
if (cfg != NULL) {
response.append(i, cfg);
}
}
app->sendResponse(response);
} else {
ist.str(param);
ist.seekg(0);
ist >> pid;
if (ist.fail()) {
app->sendResponse(Response("Incorrect parameter.", Response::Error));
return;
}
cfg = app->findProxy(pid);
if (cfg == NULL) {
app->sendResponse(Response("No register with such id.", Response::Error));
return;
}
app->sendResponse(RegisterStatusResponse(pid, cfg));
}
}