Skip to content

Commit

Permalink
update network connection oriented mode check
Browse files Browse the repository at this point in the history
As it's not possible to parse token of not nugu usage types,
so when token parsing failure, it determine to connection oriented mode.

Signed-off-by: kimhyungrok <[email protected]>
  • Loading branch information
kimhyungrok authored and webispy committed Jun 12, 2024
1 parent 262925f commit 863a8e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/core/network_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,32 +215,10 @@ bool NetworkManager::setToken(const std::string& token)

nugu_dbg("JWT payload: %s", json_text);

/* find the 'device:S.I.D.' from the scope */
NJson::Reader reader;
NJson::Value root;

if (!reader.parse(json_text, root)) {
nugu_error("Payload parsing error");
g_free(json_text);
return false;
}
bool is_connection_oriented = isConnectionOriented(json_text);

g_free(json_text);

bool is_connection_oriented = false;

NJson::Value scope = root["scope"];
if (scope.isArray()) {
NJson::ArrayIndex scope_len = scope.size();
for (NJson::ArrayIndex i = 0; i < scope_len; ++i) {
nugu_dbg("scope[%d] = %s", i, scope[i].asCString());
if (scope[i].asString() == "device:S.I.D.") {
is_connection_oriented = true;
break;
}
}
}

if (nugu_network_manager_set_token(token.c_str()) < 0) {
nugu_error("network set token failed");
return false;
Expand All @@ -257,6 +235,35 @@ bool NetworkManager::setToken(const std::string& token)
return true;
}

bool NetworkManager::isConnectionOriented(const char* json_text)
{
NJson::Reader reader;
NJson::Value root;

/*
* because it's not possible to parse another type token currently,
* it decide to connection oriented mode, when fail to parse.
*/
if (!reader.parse(json_text, root)) {
nugu_error("Payload parsing error");
return true;
}

/* find the 'device:S.I.D.' from the scope */
NJson::Value scope = root["scope"];
if (scope.isArray()) {
NJson::ArrayIndex scope_len = scope.size();
for (NJson::ArrayIndex i = 0; i < scope_len; ++i) {
nugu_dbg("scope[%d] = %s", i, scope[i].asCString());
if (scope[i].asString() == "device:S.I.D.") {
return true;
}
}
}

return false;
}

bool NetworkManager::setRegistryUrl(const std::string& url)
{
if (nugu_network_manager_set_registry_url(url.c_str()) < 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/network_manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public:
bool setUserAgent(const std::string& app_version, const std::string& additional_info) override;

private:
bool isConnectionOriented(const char* json_text);

std::vector<INetworkManagerListener*> listeners;
};

Expand Down

0 comments on commit 863a8e0

Please sign in to comment.