Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token cache #68

Merged
merged 3 commits into from
Jul 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,39 @@ find_package(Kodi REQUIRED)
find_package(kodiplatform REQUIRED)
find_package(p8-platform REQUIRED)
find_package(LibXml2 REQUIRED)
find_package(TinyXML REQUIRED)
find_package(JsonCpp REQUIRED)

include_directories(${kodiplatform_INCLUDE_DIRS}
${p8-platform_INCLUDE_DIRS}
${KODI_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/lib
${LIBXML2_INCLUDE_DIR}
${TINYXML_INCLUDE_DIRS}
${JSONCPP_INCLUDE_DIRS})
include_directories(
${kodiplatform_INCLUDE_DIRS}
${p8-platform_INCLUDE_DIRS}
${KODI_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/lib
${LIBXML2_INCLUDE_DIR}
${JSONCPP_INCLUDE_DIRS})

add_subdirectory(lib/libstalkerclient)

set(DEPLIBS ${p8-platform_LIBRARIES} stalkerclient ${LIBXML2_LIBRARIES} ${TINYXML_LIBRARIES} ${JSONCPP_LIBRARIES})

if(WIN32)
list(APPEND DEPLIBS ws2_32)
endif()

set(STALKER_SOURCES src/client.cpp
src/CWatchdog.cpp
src/HTTPSocket.cpp
src/SAPI.cpp
src/SData.cpp
src/Utils.cpp
src/XMLTV.cpp)
set(DEPLIBS
${p8-platform_LIBRARIES}
stalkerclient
${LIBXML2_LIBRARIES}
${JSONCPP_LIBRARIES})

set(BASE_SOURCES
src/base/Cache.cpp)

set(SOURCES
src/client.cpp
src/CWatchdog.cpp
src/HTTPSocket.cpp
src/SAPI.cpp
src/SData.cpp
src/Utils.cpp
src/XMLTV.cpp)

set(STALKER_SOURCES
${BASE_SOURCES}
${SOURCES})

build_addon(pvr.stalker STALKER DEPLIBS)

Expand Down
27 changes: 0 additions & 27 deletions FindTinyXML.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Priority: extra
Maintainer: Nobody <nobody@kodi.tv>
Build-Depends: debhelper (>= 9.0.0), cmake, kodi-pvr-dev,
libkodiplatform-dev (>= 16.0.0), kodi-addon-dev,
libjsoncpp-dev, libtinyxml-dev, libxml2-dev
zlib1g-dev, libxml2-dev, libjsoncpp-dev
Standards-Version: 3.9.4
Section: libs

Expand Down
2 changes: 1 addition & 1 deletion pvr.stalker/addon.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon
id="pvr.stalker"
version="2.5.1"
version="2.6.0"
name="Stalker Client"
provider-name="Jamal Edey">
<requires>
Expand Down
4 changes: 4 additions & 0 deletions pvr.stalker/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
2.6.0 (04-07-2016)
- added per portal token caching

2.5.1 (18-06-2016)
- updated language files from Transifex

2.5.0 (20-05-2016)
- [XMLTV] replaced tinyxml with libxml2

Expand Down
142 changes: 88 additions & 54 deletions src/SData.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015 Jamal Edey
* Copyright (C) 2015, 2016 Jamal Edey
* http://www.kenshisoft.com/
*
* This program is free software; you can redistribute it and/or
Expand All @@ -24,7 +24,6 @@

#include <cmath>

#include "tinyxml.h"
#include "p8-platform/util/StringUtils.h"
#include "p8-platform/util/timeutils.h"
#include "p8-platform/util/util.h"
Expand All @@ -47,7 +46,7 @@
using namespace ADDON;
using namespace P8PLATFORM;

SData::SData(void)
SData::SData(void) : Base::Cache()
{
m_bInitedApi = false;
m_bTokenManuallySet = false;
Expand Down Expand Up @@ -120,36 +119,50 @@ bool SData::LoadCache()
{
XBMC->Log(LOG_DEBUG, "%s", __FUNCTION__);

std::string strCacheFile;
TiXmlDocument doc;
TiXmlElement *pRootElement = NULL;
TiXmlElement *pTokenElement = NULL;
std::string cacheFile;
xmlDocPtr doc = NULL;
xmlNodePtr rootNode = NULL;
xmlNodePtr node = NULL;
xmlNodePtr portalsNode = NULL;
xmlNodePtr portalNode = NULL;
std::string portalNum = Utils::ToString(g_iActivePortal);

strCacheFile = Utils::GetFilePath("cache.xml");
cacheFile = Utils::GetFilePath("cache.xml");

if (!doc.LoadFile(strCacheFile)) {
XBMC->Log(LOG_ERROR, "%s: failed to load: \"%s\"", __FUNCTION__, strCacheFile.c_str());
if (!Open(cacheFile, doc, rootNode, "cache")) {
xmlFreeDoc(doc);
return false;
}

pRootElement = doc.RootElement();
if (strcmp(pRootElement->Value(), "cache") != 0) {
XBMC->Log(LOG_ERROR, "%s: invalid xml doc. root element 'cache' not found", __FUNCTION__);
return false;
}

if (!m_bTokenManuallySet) {
pTokenElement = pRootElement->FirstChildElement("token");
if (!pTokenElement || !pTokenElement->GetText()) {
XBMC->Log(LOG_DEBUG, "%s: 'token' element not found", __FUNCTION__);
} else {
SC_STR_SET(m_identity.token, pTokenElement->GetText());
portalsNode = FindNodeByName(rootNode->children, (const xmlChar *) "portals");
if (!portalsNode) {
XBMC->Log(LOG_DEBUG, "%s: 'portals' element not found", __FUNCTION__);
} else {
xmlChar *num = NULL;
bool found = false;
for (node = portalsNode->children; node; node = node->next) {
if (!xmlStrcmp(node->name, (const xmlChar *) "portal")) {
num = xmlGetProp(node, (const xmlChar *) "num");
if (num && !xmlStrcmp(num, (const xmlChar *) portalNum.c_str())) {
portalNode = node;
found = true;
}
xmlFree(num);
if (found) break;
}
}
if (portalNode) {
std::string val;
if (!m_bTokenManuallySet) {
FindAndGetNodeValue(portalNode, (const xmlChar *) "token", val);
SC_STR_SET(m_identity.token, val.c_str());

XBMC->Log(LOG_DEBUG, "%s: token=%s", __FUNCTION__, m_identity.token);
XBMC->Log(LOG_DEBUG, "%s: token=%s", __FUNCTION__, m_identity.token);
}
}
}

doc.Clear();
xmlFreeDoc(doc);

return true;
}
Expand All @@ -158,46 +171,67 @@ bool SData::SaveCache()
{
XBMC->Log(LOG_DEBUG, "%s", __FUNCTION__);

std::string strCacheFile;
bool bFailed(false);
TiXmlDocument doc;
TiXmlElement *pRootElement = NULL;
TiXmlElement *pTokenElement = NULL;

strCacheFile = Utils::GetFilePath("cache.xml");

if ((bFailed = !doc.LoadFile(strCacheFile))) {
XBMC->Log(LOG_ERROR, "%s: failed to load \"%s\"", __FUNCTION__, strCacheFile.c_str());
} else {
pRootElement = doc.RootElement();
if (!pRootElement || strcmp(pRootElement->Value(), "cache") != 0) {
XBMC->Log(LOG_ERROR, "%s: invalid xml doc. root element 'cache' not found", __FUNCTION__);
bFailed = true;
std::string cacheFile;
bool ret = false;
xmlDocPtr doc = NULL;
xmlNodePtr rootNode = NULL;
xmlNodePtr node = NULL;
xmlNodePtr portalsNode = NULL;
xmlNodePtr portalNode = NULL;
std::string portalNum = Utils::ToString(g_iActivePortal);

cacheFile = Utils::GetFilePath("cache.xml");

ret = Open(cacheFile, doc, rootNode, "cache");
if (!ret) {
if (!doc) {
doc = xmlNewDoc((const xmlChar *) XML_DEFAULT_VERSION);
}
if (rootNode) {
xmlUnlinkNode(rootNode);
xmlFreeNode(rootNode);
}
rootNode = xmlNewDocNode(doc, NULL, (const xmlChar *) "cache", NULL);
xmlDocSetRootElement(doc, rootNode);
}

if (bFailed) {
XBMC->Log(LOG_DEBUG, "%s: creating root element 'cache'", __FUNCTION__);
portalsNode = FindNodeByName(rootNode->children, (const xmlChar *) "portals");
if (!portalsNode) {
portalsNode = xmlNewChild(rootNode, NULL, (const xmlChar *) "portals", NULL);
}

pRootElement = new TiXmlElement("cache");
doc.LinkEndChild(pRootElement);
xmlChar *num = NULL;
for (node = portalsNode->children; node; node = node->next) {
if (!xmlStrcmp(node->name, (const xmlChar *) "portal")) {
num = xmlGetProp(node, (const xmlChar *) "num");
if (!num || !xmlStrlen(num) || portalNode) {
xmlNodePtr tmp = node;
node = tmp->prev;
xmlUnlinkNode(tmp);
xmlFreeNode(tmp);
} else if (num && !xmlStrcmp(num, (const xmlChar *) portalNum.c_str())) {
portalNode = node;
}
xmlFree(num);
}
}
if (!portalNode) {
portalNode = xmlNewChild(portalsNode, NULL, (const xmlChar *) "portal", NULL);
xmlNewProp(portalNode, (const xmlChar *) "num", (const xmlChar *) portalNum.c_str());
}

pTokenElement = pRootElement->FirstChildElement("token");
if (!pTokenElement) {
pTokenElement = new TiXmlElement("token");
pRootElement->LinkEndChild(pTokenElement);
if (!m_bTokenManuallySet) {
FindAndSetNodeValue(portalNode, (const xmlChar *) "token", (const xmlChar *) m_identity.token);
}
pTokenElement->Clear();
if (m_profile.store_auth_data_on_stb)
pTokenElement->LinkEndChild(new TiXmlText(m_identity.token));

if (!doc.SaveFile(strCacheFile)) {
XBMC->Log(LOG_ERROR, "%s: failed to save \"%s\"", __FUNCTION__, strCacheFile.c_str());
return false;
ret = xmlSaveFormatFileEnc(cacheFile.c_str(), doc, xmlGetCharEncodingName(XML_CHAR_ENCODING_UTF8), 1) >= 0;
if (!ret) {
XBMC->Log(LOG_ERROR, "%s: failed to save cache file", __FUNCTION__);
}

return true;
xmlFreeDoc(doc);

return ret;
}

SError SData::InitAPI()
Expand Down
5 changes: 3 additions & 2 deletions src/SData.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

/*
* Copyright (C) 2015 Jamal Edey
* Copyright (C) 2015, 2016 Jamal Edey
* http://www.kenshisoft.com/
*
* This program is free software; you can redistribute it and/or
Expand All @@ -28,6 +28,7 @@

#include "libstalkerclient/identity.h"
#include "libstalkerclient/stb.h"
#include "base/Cache.h"
#include "client.h"
#include "CWatchdog.h"
#include "XMLTV.h"
Expand Down Expand Up @@ -68,7 +69,7 @@ struct SChannel
bool bUseLoadBalancing;
};

class SData
class SData : Base::Cache
{
public:
SData(void);
Expand Down
Loading