improved the login in the interaction between terminal api client and main

This commit is contained in:
csoler 2017-07-08 17:38:09 +02:00
parent 7ee527ecbd
commit decbd3514d
5 changed files with 148 additions and 92 deletions

View file

@ -87,10 +87,8 @@ private:
namespace resource_api {
TerminalApiClient::TerminalApiClient(ApiServer *api):
mApiServer(api)
TerminalApiClient::TerminalApiClient(ApiServer *api): mApiServer(api)
{
start("resapi terminal");
}
TerminalApiClient::~TerminalApiClient()
@ -98,13 +96,6 @@ TerminalApiClient::~TerminalApiClient()
fullstop();
}
struct AccountInfo
{
std::string name ;
std::string location ;
RsPeerId ssl_id ;
};
void TerminalApiClient::data_tick()
{
// values in milliseconds
@ -228,44 +219,7 @@ void TerminalApiClient::data_tick()
if(!ask_for_password && edge && runstate == "waiting_account_select")
{
JsonStream reqs;
JsonStream resps;
Request req(reqs);
std::stringstream ss;
Response resp(resps, ss);
req.mPath.push("locations");
req.mPath.push("control");
reqs.switchToDeserialisation();
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
waitForResponse(id);
resps.switchToDeserialisation();
if(!resps.hasMore())
std::cout << "Error: No Accounts. Use the Qt-GUI or the webinterface to create an account." << std::endl;
int i = 0;
accounts.clear();
while(resps.hasMore())
{
std::string id;
std::string name;
std::string location;
resps.getStreamToMember()
<< makeKeyValueReference("id", id)
<< makeKeyValueReference("name", name)
<< makeKeyValueReference("location", location);
AccountInfo info ;
info.location = location ;
info.name = name ;
info.ssl_id = RsPeerId(id) ;
accounts.push_back(info);
i++;
}
readAvailableAccounts(accounts) ;
account_number_size = (int)ceil(log(accounts.size())/log(10.0f)) ;
for(uint32_t i=0;i<accounts.size();++i)
@ -292,19 +246,7 @@ void TerminalApiClient::data_tick()
std::cout << std::endl << "Selected account: " << accounts[selected_account_number].name << " (" << accounts[selected_account_number].location << ") SSL id: " << accounts[selected_account_number].ssl_id << std::endl;
std::string acc_ssl_id = accounts[selected_account_number].ssl_id.toStdString();
JsonStream reqs;
JsonStream resps;
Request req(reqs);
std::stringstream ss;
Response resp(resps, ss);
req.mPath.push("login");
req.mPath.push("control");
reqs << makeKeyValueReference("id", acc_ssl_id);
reqs.switchToDeserialisation();
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
waitForResponse(id);
sendSelectedAccount(acc_ssl_id) ;
inbuf.clear();
}
@ -330,33 +272,107 @@ void TerminalApiClient::data_tick()
}
if(ask_for_password && enter_was_pressed && !inbuf.empty())
{
std::cout << "TerminalApiClient: got a password" << std::endl;
JsonStream reqs;
JsonStream resps;
Request req(reqs);
std::stringstream ss;
Response resp(resps, ss);
{
std::cout << "TerminalApiClient: got a password" << std::endl;
req.mPath.push("password");
req.mPath.push("control");
reqs << makeKeyValueReference("password", inbuf);
reqs.switchToDeserialisation();
// Send passwd to api server
sendPassword(inbuf) ;
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
waitForResponse(id);
inbuf.clear();
// clears buffer
inbuf.clear();
}
}
}
void TerminalApiClient::waitForResponse(ApiServer::RequestId id)
void TerminalApiClient::waitForResponse(ApiServer::RequestId id) const
{
while(!mApiServer->isRequestDone(id))
usleep(20*1000);
}
void TerminalApiClient::sendPassword(const std::string& passwd) const
{
JsonStream reqs;
JsonStream resps;
Request req(reqs);
std::stringstream ss;
Response resp(resps, ss);
req.mPath.push("password");
req.mPath.push("control");
std::string pass(passwd) ;
reqs << makeKeyValueReference("password", pass);
reqs.switchToDeserialisation();
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
waitForResponse(id);
}
void TerminalApiClient::sendSelectedAccount(const std::string& ssl_id) const
{
JsonStream reqs;
JsonStream resps;
Request req(reqs);
std::stringstream ss;
Response resp(resps, ss);
std::string acc_ssl_id(ssl_id) ;
req.mPath.push("login");
req.mPath.push("control");
reqs << makeKeyValueReference("id", acc_ssl_id);
reqs.switchToDeserialisation();
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
waitForResponse(id);
}
void TerminalApiClient::readAvailableAccounts(std::vector<AccountInfo>& accounts) const
{
JsonStream reqs;
JsonStream resps;
Request req(reqs);
std::stringstream ss;
Response resp(resps, ss);
req.mPath.push("locations");
req.mPath.push("control");
reqs.switchToDeserialisation();
ApiServer::RequestId id = mApiServer->handleRequest(req, resp);
waitForResponse(id);
resps.switchToDeserialisation();
if(!resps.hasMore())
std::cout << "Error: No Accounts. Use the Qt-GUI or the webinterface to create an account." << std::endl;
int i = 0;
accounts.clear();
while(resps.hasMore())
{
std::string id;
std::string name;
std::string location;
resps.getStreamToMember()
<< makeKeyValueReference("id", id)
<< makeKeyValueReference("name", name)
<< makeKeyValueReference("location", location);
AccountInfo info ;
info.location = location ;
info.name = name ;
info.ssl_id = RsPeerId(id) ;
accounts.push_back(info);
i++;
}
}
bool TerminalApiClient::isTokenValid(StateToken runstate_state_token)
{
JsonStream reqs;

View file

@ -8,7 +8,7 @@ namespace resource_api {
// - account selection
// - login
// - shutdown
class TerminalApiClient: private RsTickingThread{
class TerminalApiClient: public RsTickingThread{
public:
// zero setup: create an instance of this class and destroy it when not needed anymore
// no need to call start or stop or something
@ -19,9 +19,24 @@ protected:
// from RsThread
virtual void data_tick(); /* called once the thread is started. Should be overloaded by subclasses. */
private:
void waitForResponse(ApiServer::RequestId id);
struct AccountInfo
{
std::string name ;
std::string location ;
RsPeerId ssl_id ;
};
void waitForResponse(ApiServer::RequestId id) const;
bool isTokenValid(StateToken st);
ApiServer* mApiServer;
// Methods to talk to the ApiServer
void sendPassword(const std::string& passwd) const;
void sendSelectedAccount(const std::string& ssl_id) const;
void readAvailableAccounts(std::vector<AccountInfo>& accounts) const;
void getRunningState() const ;
};
} // namespace resource_api

View file

@ -100,9 +100,25 @@ int main(int argc, char **argv)
}
resource_api::TerminalApiClient tac(&api);
tac.start();
while(ctrl_mod.processShouldExit() == false)
{
usleep(20*1000);
usleep(200*1000);
if(!tac.isRunning())
{
if(!RsInit::isLocationRunning())
{
std::cerr << "Terminal API client stopped but location not set ! Relaunching." ;
tac.start();
}
else if(RsInit::isLocationRunning())
{
std::cerr << "Terminal API client running but location already set ! Stopping it." ;
tac.shutdown();
}
}
}
if(httpd)