diff --git a/daemon/commands/ptime.cc b/daemon/commands/ptime.cc index e1456a62d..579a4ff1d 100644 --- a/daemon/commands/ptime.cc +++ b/daemon/commands/ptime.cc @@ -2,24 +2,59 @@ using namespace std; -PtimeCommand::PtimeCommand() : - DaemonCommand("ptime", "ptime ", "Set the if ms is defined, otherwise return the ptime.") { +class PtimeCommandPrivate { +public: + void outputPtime(Daemon *app, ostringstream &ost, int ms); +}; + +void PtimeCommandPrivate::outputPtime(Daemon* app, ostringstream& ost, int ms) { + ost << "Value: " << ms << "\n"; } + +PtimeCommand::PtimeCommand() : + DaemonCommand("ptime", "ptime [up|down] ", "Set the upload or download ptime if ms is defined, otherwise return the current value of the ptime."), + d(new PtimeCommandPrivate()) { +} + +PtimeCommand::~PtimeCommand() +{ + delete d; +} + void PtimeCommand::exec(Daemon *app, const char *args) { + string direction; int ms; - int ret = sscanf(args, "%d", &ms); - if (ret <= 0) { - ostringstream ostr; - ms = linphone_core_get_upload_ptime(app->getCore()); - ostr << "Ptime: " << ms << "\n"; - app->sendResponse(Response(ostr.str().c_str(), Response::Ok)); - } else if (ret == 1) { - ostringstream ostr; - linphone_core_set_upload_ptime(app->getCore(), ms); - ms = linphone_core_get_upload_ptime(app->getCore()); - ostr << "Ptime: " << ms << "\n"; - app->sendResponse(Response(ostr.str().c_str(), Response::Ok)); + istringstream ist(args); + ist >> direction; + if (ist.fail()) { + app->sendResponse(Response("Missing/Incorrect parameter(s).", Response::Error)); } else { - app->sendResponse(Response("Missing/Incorrect parameter(s).")); + if (direction.compare("up") == 0) { + if (!ist.eof()) { + ist >> ms; + if (ist.fail()) { + app->sendResponse(Response("Incorrect ms parameter.", Response::Error)); + } + linphone_core_set_upload_ptime(app->getCore(), ms); + } + ms = linphone_core_get_upload_ptime(app->getCore()); + ostringstream ost; + d->outputPtime(app, ost, ms); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (direction.compare("down") == 0) { + if (!ist.eof()) { + ist >> ms; + if (ist.fail()) { + app->sendResponse(Response("Incorrect ms parameter.", Response::Error)); + } + linphone_core_set_download_ptime(app->getCore(), ms); + } + ms = linphone_core_get_download_ptime(app->getCore()); + ostringstream ost; + d->outputPtime(app, ost, ms); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else { + app->sendResponse(Response("Missing/Incorrect parameter(s).", Response::Error)); + } } } diff --git a/daemon/commands/ptime.h b/daemon/commands/ptime.h index e07140296..fec6b49a3 100644 --- a/daemon/commands/ptime.h +++ b/daemon/commands/ptime.h @@ -3,10 +3,15 @@ #include "../daemon.h" +class PtimeCommandPrivate; + class PtimeCommand: public DaemonCommand { public: PtimeCommand(); + ~PtimeCommand(); virtual void exec(Daemon *app, const char *args); +private: + PtimeCommandPrivate *d; }; #endif //COMMAND_PTIME_H_