Skip to content

Commit

Permalink
Use JSON parser to get properties from the session response
Browse files Browse the repository at this point in the history
  • Loading branch information
fador committed Feb 7, 2016
1 parent 03ee0d2 commit 19dd2ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "tools.h"

#include "threadpool.h"
#include "json.h"


void ThreadPool::taskValidateUser(ThreadTask *task)
Expand Down Expand Up @@ -117,13 +118,41 @@ void ThreadPool::taskValidateUser(ThreadTask *task)

} while (len > 0 || BIO_should_retry(web));


// Make sure we have the right HTTP code
findResult = output.find("200 OK");
if (findResult == std::string::npos) goto cleanup;
findResult = output.find("\"id\":\"");
findResult = output.find("\r\n\r\n");
if (findResult == std::string::npos) goto cleanup;

task->user->setUUID(output.substr(findResult+6, 32), false);
{
// Parse the JSON
std::string input_json = output.substr(findResult);
int32_t jsonlen = input_json.length();
JSON_Val value((uint8_t*)input_json.c_str(), jsonlen, JSON_Val::JSON_COMPOUND);

// Grab the values
if (value["id"] != nullptr && value["id"]->getType() == JSON_Val::JSON_STRING &&
value["name"] != nullptr && value["name"]->getType() == JSON_Val::JSON_STRING &&
value["properties"] != nullptr && value["properties"]->getType() == JSON_Val::JSON_LIST)
{
task->user->setUUID(*value["id"]->getString(), false);
//std::cout << "name: " << *value["name"]->getString() << std::endl;
for(JSON_Val *var : *value["properties"]->getList())
{
if ((*var)["name"] != nullptr && (*var)["name"]->getType() == JSON_Val::JSON_STRING &&
(*var)["value"] != nullptr && (*var)["value"]->getType() == JSON_Val::JSON_STRING &&
(*var)["signature"] != nullptr && (*var)["signature"]->getType() == JSON_Val::JSON_STRING)
{
task->user->properties.push_back(UserProperty(*(*var)["name"]->getString(),
*(*var)["value"]->getString(),
*(*var)["signature"]->getString()));
}
}
} else {
goto cleanup;
}

}

//std::cout << output << std::endl;

Expand Down
13 changes: 13 additions & 0 deletions src/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@

bool client_write(User *user);



struct UserProperty {
UserProperty() {};
UserProperty(std::string _name, std::string _value, std::string _signature):
name(_name), value(_value), signature(_signature) {};
std::string name;
std::string value;
std::string signature;
};

struct position
{
double x;
Expand Down Expand Up @@ -248,6 +259,8 @@ class User
return true;
}

std::vector<UserProperty> properties;

private:
event* m_readEvent;
event* m_writeEvent;
Expand Down

0 comments on commit 19dd2ca

Please sign in to comment.