summaryrefslogtreecommitdiff
path: root/Plexservice.cpp
diff options
context:
space:
mode:
authorchriszero <zerov83@gmail.com>2015-02-27 17:22:11 +0100
committerchriszero <zerov83@gmail.com>2015-02-27 17:22:11 +0100
commitd6339ad957b31ec4810777ef4c63f6e42a66d2c2 (patch)
tree739365b1cd296fe24efdd1433f4288ece9be6206 /Plexservice.cpp
parent793fa10c35ef60ea7370fef5167854d38c65df65 (diff)
downloadvdr-plugin-plex-d6339ad957b31ec4810777ef4c63f6e42a66d2c2.tar.gz
vdr-plugin-plex-d6339ad957b31ec4810777ef4c63f6e42a66d2c2.tar.bz2
Optimized plexservice
Diffstat (limited to 'Plexservice.cpp')
-rw-r--r--Plexservice.cpp81
1 files changed, 26 insertions, 55 deletions
diff --git a/Plexservice.cpp b/Plexservice.cpp
index 5bee008..dc01721 100644
--- a/Plexservice.cpp
+++ b/Plexservice.cpp
@@ -11,30 +11,6 @@ Plexservice::Plexservice(PlexServer *server)
pServer = server;
}
-Plexservice::~Plexservice()
-{
-}
-
-Poco::Net::HTTPClientSession* Plexservice::GetHttpSession(bool createNew)
-{
- try {
- if(pServer == 0) {
- return NULL;
- }
- if (m_pPlexSession == 0 || createNew) {
- if (createNew) {
- //delete m_pPlexSession;
- }
- m_pPlexSession = new Poco::Net::HTTPClientSession(pServer->GetIpAdress(), pServer->GetPort());
- m_pPlexSession->setKeepAlive(true);
- }
- } catch(Poco::Exception &exc) {
- esyslog("[plex]Exception in %s s%", __func__, exc.displayText().c_str() );
- m_pPlexSession = 0;
- }
- return m_pPlexSession;
-}
-
std::string Plexservice::GetMyPlexToken()
{
static bool done;
@@ -73,7 +49,7 @@ std::string Plexservice::GetMyPlexToken()
esyslog("[plex] plex.tv Login failed, check you creditials.");
}
done = true;
- plexSession.detachSocket();
+ plexSession.abort();
} catch (Poco::Exception &exc) {
esyslog("[plex]Exception in %s s%", __func__, exc.displayText().c_str() );
done = true;
@@ -87,21 +63,17 @@ void Plexservice::Authenticate()
{
if(!Config::GetInstance().UsePlexAccount) return;
try {
- if(GetHttpSession(true)) {
- std::string token = GetMyPlexToken();
- Poco::Net::HTTPRequest *pRequest = CreateRequest("/?X-Plex-Token=" + token);
-
- m_pPlexSession->sendRequest(*pRequest);
- Poco::Net::HTTPResponse response;
- /*std::istream &rs = */
- m_pPlexSession->receiveResponse(response);
-
- // TODO: process response
- //Poco::StreamCopier::copyStream(rs, std::cout);
- delete pRequest;
- } else {
- esyslog("[plex] %s HttpSession is NULL", __func__);
- }
+ std::string token = GetMyPlexToken();
+ auto pRequest = CreateRequest("/?X-Plex-Token=" + token);
+
+ Poco::Net::HTTPClientSession session(pServer->GetIpAdress(), pServer->GetPort());
+ session.sendRequest(*pRequest);
+ Poco::Net::HTTPResponse response;
+ /*std::istream &rs = */
+ session.receiveResponse(response);
+ session.abort();
+ // TODO: process response
+ //Poco::StreamCopier::copyStream(rs, std::cout);
} catch (Poco::Exception &exc) {
esyslog("[plex]Exception in %s s%", __func__, exc.displayText().c_str() );
}
@@ -114,9 +86,7 @@ PlexServer* Plexservice::GetServer()
std::shared_ptr<MediaContainer> Plexservice::GetSection(std::string section, bool putOnStack)
{
- if(GetHttpSession()) {
-
- Poco::Net::HTTPRequest *pRequest;
+ try {
std::string uri;
if(section[0]=='/') { // Full URI?
uri = section;
@@ -124,24 +94,24 @@ std::shared_ptr<MediaContainer> Plexservice::GetSection(std::string section, boo
uri = Poco::format("%s/%s", m_vUriStack.top(), section);
}
- pRequest = CreateRequest(uri);
+ auto pRequest = CreateRequest(uri);
if(putOnStack) {
m_vUriStack.push(uri);
}
- m_pPlexSession->sendRequest(*pRequest);
+ Poco::Net::HTTPClientSession session(pServer->GetIpAdress(), pServer->GetPort());
+ session.sendRequest(*pRequest);
Poco::Net::HTTPResponse response;
- std::istream &rs = m_pPlexSession->receiveResponse(response);
+ std::istream &rs = session.receiveResponse(response);
- dsyslog("[plex] URI: http://%s:32400%s", m_pPlexSession->getHost().c_str(), pRequest->getURI().c_str());
+ dsyslog("[plex] URI: http://%s:%d%s", pServer->GetIpAdress().c_str(), pServer->GetPort(), uri.c_str());
std::shared_ptr<MediaContainer> pAllsections(new MediaContainer(&rs, *pServer));
-
- delete pRequest;
+
+ session.abort();
return pAllsections;
- } else {
- esyslog("[plex] %s HttpSession is NULL", __func__);
+ } catch (Poco::Exception &exc) {
return 0;
}
}
@@ -159,10 +129,10 @@ std::shared_ptr<MediaContainer> Plexservice::GetLastSection(bool current)
return NULL;
}
-Poco::Net::HTTPRequest* Plexservice::CreateRequest(std::string path)
+std::unique_ptr<Poco::Net::HTTPRequest> Plexservice::CreateRequest(std::string path)
{
- Poco::Net::HTTPRequest *pRequest = new Poco::Net::HTTPRequest(Poco::Net::HTTPRequest::HTTP_GET,
- path, Poco::Net::HTTPMessage::HTTP_1_1);
+ std::unique_ptr<Poco::Net::HTTPRequest> pRequest = std::unique_ptr<Poco::Net::HTTPRequest>(new Poco::Net::HTTPRequest(Poco::Net::HTTPRequest::HTTP_GET,
+ path, Poco::Net::HTTPMessage::HTTP_1_1));
PlexHelper::AddHttpHeader(*pRequest);
@@ -189,7 +159,8 @@ MediaContainer Plexservice::GetMediaContainer(std::string fullUrl)
std::istream &rs = session.receiveResponse(response);
MediaContainer allsections(&rs, PlexServer(fileuri.getHost(), fileuri.getPort()));
-
+
+ session.abort();
return allsections;
}