summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plex.cpp20
-rw-r--r--plexgdm.cpp239
-rw-r--r--plexgdm.h12
3 files changed, 148 insertions, 123 deletions
diff --git a/plex.cpp b/plex.cpp
index 0c269dd..05a8b4e 100644
--- a/plex.cpp
+++ b/plex.cpp
@@ -52,7 +52,7 @@ void cPlexBrowser::CreateMenu()
// Clear Menu
Clear();
// Directory or Video?
- if(pCont->m_vDirectories.size() > 0) {
+ if(pCont && pCont->m_vDirectories.size() > 0) {
for(std::vector<plexclient::Directory>::iterator it = pCont->m_vDirectories.begin(); it != pCont->m_vDirectories.end(); ++it) {
plexclient::Directory *pDir = &(*it);
@@ -60,7 +60,7 @@ void cPlexBrowser::CreateMenu()
}
}
- if(pCont->m_vVideos.size() > 0) {
+ if(pCont && pCont->m_vVideos.size() > 0) {
for(std::vector<plexclient::Video>::iterator it = pCont->m_vVideos.begin(); it != pCont->m_vVideos.end(); ++it) {
plexclient::Video *vid = &(*it); // cast raw pointer
Add(new cPlexOsdItem( vid->m_sTitle.c_str(), vid) );
@@ -291,18 +291,10 @@ bool cMyPlugin::Initialize(void)
{
// First Startup? Save UUID
SetupStore("UUID", Config::GetInstance().GetUUID().c_str());
-
- plexclient::plexgdm::GetInstance().discover();
-
- if (plexclient::plexgdm::GetInstance().GetPlexservers().size() > 0) {
- plexclient::plexgdm::GetInstance().clientDetails(Config::GetInstance().GetUUID(), DESCRIPTION, "3200", "VDR", VERSION);
- plexclient::plexgdm::GetInstance().Start();
-
- plexclient::ControlServer::GetInstance().Start();
-
- } else {
- esyslog("[plex]No Plexmediaserver found");
- }
+
+ plexclient::plexgdm::GetInstance().clientDetails(Config::GetInstance().GetUUID(), DESCRIPTION, "3200", "VDR", VERSION);
+ plexclient::plexgdm::GetInstance().Start();
+ plexclient::ControlServer::GetInstance().Start();
return true;
}
diff --git a/plexgdm.cpp b/plexgdm.cpp
index 31e40b7..0ad2fe0 100644
--- a/plexgdm.cpp
+++ b/plexgdm.cpp
@@ -7,19 +7,22 @@ namespace plexclient
plexgdm::plexgdm()
{
- _discoverMessage = "M-SEARCH * HTTP/1.0";
- _clientHeader = "* HTTP/1.0";
- _multicastAddress = "239.0.0.250";
- _clientUpdatePort = 32412;
-
- m_discoverAdress = Poco::Net::SocketAddress(_multicastAddress, 32414);
- m_clientRegisterGroup = Poco::Net::SocketAddress(_multicastAddress, 32413);
-
- m_discoveryComplete = false;
- m_registrationIsRunning = false;
- m_clientRegistered = false;
- m_discoveryInterval = 120;
- m_discoveryIsRunning = false;
+ _discoverMessage = "M-SEARCH * HTTP/1.0";
+ _clientHeader = "* HTTP/1.0";
+ _multicastAddress = "239.0.0.250";
+ _clientUpdatePort = 32412;
+ _helloSent = false;
+ _discoverInterval = 10 * 1000;
+ _discoverTimer.Set(1);
+
+ m_discoverAdress = Poco::Net::SocketAddress(_multicastAddress, 32414);
+ m_clientRegisterGroup = Poco::Net::SocketAddress(_multicastAddress, 32413);
+
+ m_discoveryComplete = false;
+ m_registrationIsRunning = false;
+ m_clientRegistered = false;
+ m_discoveryInterval = 120;
+ m_discoveryIsRunning = false;
}
plexgdm::~plexgdm()
@@ -33,115 +36,141 @@ void plexgdm::clientDetails(std::string c_id,
std::string c_version)
{
_clientData = "Content-Type: plex/media-player\r\n"
- "Resource-Identifier: " + c_id + "\r\n"
- "Device-Class: PC\r\n"
- "Name: " + c_name + "\r\n"
- "Port: " + c_port + "\r\n"
- "Product: " + c_product + "\r\n"
- "Protocol: plex\r\n"
- "Protocol-Capabilities: navigation,playback,timeline\r\n"
- "Protocol-Version: 1\r\n"
- "Version: " + c_version + "\r\n";
- _clientId = c_id;
+ "Resource-Identifier: " + c_id + "\r\n"
+ "Device-Class: PC\r\n"
+ "Name: " + c_name + "\r\n"
+ "Port: " + c_port + "\r\n"
+ "Product: " + c_product + "\r\n"
+ "Protocol: plex\r\n"
+ "Protocol-Capabilities: navigation,playback,timeline\r\n"
+ "Protocol-Version: 1\r\n"
+ "Version: " + c_version + "\r\n";
+ _clientId = c_id;
}
std::string plexgdm::getClientDetails()
{
- if(_clientData.empty())
- throw "client_Data not initialized";
- return _clientData;
+ if(_clientData.empty())
+ throw "client_Data not initialized";
+ return _clientData;
}
void plexgdm::Action()
{
- char buffer[1024];
- m_registrationIsRunning = true;
- cMutexLock lock(&m_mutex);
-
- Poco::Net::MulticastSocket update_sock(Poco::Net::SocketAddress(Poco::Net::IPAddress(), _clientUpdatePort), true);
-
- update_sock.setLoopback(true);
- update_sock.setReuseAddress(true);
- update_sock.setTimeToLive(255);
- update_sock.setBlocking(false);
- // Send initial Client Registration
-
- std::string s = Poco::format("HELLO %s\n%s", _clientHeader, _clientData);
- update_sock.sendTo(s.c_str(), s.length(), m_clientRegisterGroup, 0);
-
- while(m_registrationIsRunning && Running()) {
- Poco::Net::SocketAddress sender;
- int n = 0;
- try {
- n = update_sock.receiveFrom(buffer, sizeof(buffer), sender);
- } catch(Poco::TimeoutException& e) {
- n = 0;
- }
- if(n > 0) {
- std::string buf(buffer, n);
- if(buf.find("M-SEARCH * HTTP/1.") != std::string::npos) {
- //dsyslog("[plex]: Detected client discovery request from %s", sender.host().toString().c_str());
- int t = std::time(0);
- s = Poco::format("HTTP/1.0 200 OK\r\n%sUpdated-At: %d", _clientData, t);
- update_sock.sendTo(s.c_str(), s.length(), sender, 0);
- m_clientRegistered = true;
- }
+ char buffer[1024];
+ m_registrationIsRunning = true;
+ m_discoveryIsRunning = true;
+ cMutexLock lock(&m_mutex);
+
+ Poco::Net::MulticastSocket update_sock(Poco::Net::SocketAddress(Poco::Net::IPAddress(), _clientUpdatePort), true);
+
+ update_sock.setLoopback(true);
+ update_sock.setReuseAddress(true);
+ update_sock.setTimeToLive(255);
+ update_sock.setBlocking(false);
+
+ while(Running()) {
+ try {
+ if(!_helloSent) {
+ // Send initial Client Registration
+ std::string s = Poco::format("HELLO %s\n%s", _clientHeader, _clientData);
+ update_sock.sendTo(s.c_str(), s.length(), m_clientRegisterGroup, 0);
+ _helloSent = true;
+ }
+ if(m_registrationIsRunning) {
+ Poco::Net::SocketAddress sender;
+ int n = 0;
+ try {
+ n = update_sock.receiveFrom(buffer, sizeof(buffer), sender);
+ } catch(Poco::TimeoutException& e) {
+ n = 0;
+ }
+ if(n > 0) {
+ std::string buf(buffer, n);
+ if(buf.find("M-SEARCH * HTTP/1.") != std::string::npos) {
+ //dsyslog("[plex]: Detected client discovery request from %s", sender.host().toString().c_str());
+ int t = std::time(0);
+ std::string s = Poco::format("HTTP/1.0 200 OK\r\n%sUpdated-At: %d", _clientData, t);
+ update_sock.sendTo(s.c_str(), s.length(), sender, 0);
+ m_clientRegistered = true;
+ }
+ }
+ }
+
+ } catch(Poco::IOException) {
+ // networkconnection is lost, reset
+ _helloSent = false;
+ }
+
+ if(m_discoveryIsRunning && _discoverTimer.TimedOut()) {
+ discover();
+ _discoverTimer.Set(_discoverInterval);
+ }
+ m_waitCondition.TimedWait(m_mutex, 500);
}
- m_waitCondition.TimedWait(m_mutex, 500);
- }
- // Client loop stopped
-
- // unregister from Server
- s = Poco::format("BYE %s\r\n%s", _clientHeader, _clientData);
- update_sock.sendTo(s.c_str(), s.length(), m_clientRegisterGroup, 0);
-
- m_clientRegistered = false;
+ // Client loop stopped
+ try {
+ // unregister from Server
+ std::string s = Poco::format("BYE %s\r\n%s", _clientHeader, _clientData);
+ update_sock.sendTo(s.c_str(), s.length(), m_clientRegisterGroup, 0);
+ } catch (Poco::IOException) {}
+
+ m_clientRegistered = false;
}
void plexgdm::discover()
{
- Poco::Net::MulticastSocket socket(Poco::Net::SocketAddress(Poco::Net::IPAddress(), m_discoverAdress.port()), true);
- // TODO: Discover multiple servers
- char buffer[1024];
- std::map<std::string, std::string> vBuffer;
-
- socket.setLoopback(true);
- socket.setTimeToLive(1);
- socket.setReceiveTimeout(Poco::Timespan(0, 600 * 1000)); // microseconds
- socket.joinGroup(m_discoverAdress.host());
- socket.sendTo(_discoverMessage.c_str(), _discoverMessage.length(), m_discoverAdress, 0);
-
- try {
- while(true) {
- Poco::Net::SocketAddress sender;
- int n = socket.receiveFrom(buffer, sizeof(buffer), sender);
- std::string buf(buffer, n);
- if(buf.find("200 OK") != std::string::npos) {
- vBuffer[sender.host().toString()] = buf;
- }
- }
- } catch(Poco::TimeoutException& exc) {
- dsyslog("[plex]TimeoutException in %s s%", __func__, exc.displayText().c_str());
- }
-
- socket.close();
- m_discoveryComplete = true;
-
- for(std::map<std::string, std::string>::iterator it = vBuffer.begin(); it != vBuffer.end(); ++it) {
- std::string host = it->first;
- std::string data = it->second;
-
- m_vServers.push_back(PlexServer(data, host));
- }
+ try {
+ Poco::Net::MulticastSocket socket(Poco::Net::SocketAddress(Poco::Net::IPAddress(), m_discoverAdress.port()), true);
+ // TODO: Discover multiple servers
+ char buffer[1024];
+ std::map<std::string, std::string> vBuffer;
+
+ socket.setLoopback(true);
+ socket.setTimeToLive(1);
+ socket.setReceiveTimeout(Poco::Timespan(0, 600 * 1000)); // microseconds
+ socket.joinGroup(m_discoverAdress.host());
+ socket.sendTo(_discoverMessage.c_str(), _discoverMessage.length(), m_discoverAdress, 0);
+
+ try {
+ while(true) {
+ Poco::Net::SocketAddress sender;
+ int n = socket.receiveFrom(buffer, sizeof(buffer), sender);
+ std::string buf(buffer, n);
+ if(buf.find("200 OK") != std::string::npos) {
+ vBuffer[sender.host().toString()] = buf;
+ }
+ }
+ } catch(Poco::TimeoutException& exc) {
+ dsyslog("[plex]TimeoutException in %s s%", __func__, exc.displayText().c_str());
+ }
+
+ socket.close();
+ m_discoveryComplete = true;
+
+ for(std::map<std::string, std::string>::iterator it = vBuffer.begin(); it != vBuffer.end(); ++it) {
+ std::string host = it->first;
+ std::string data = it->second;
+ bool flag = true;
+ // Check for duplicates
+ for(std::vector<PlexServer>::iterator s_it = m_vServers.begin(); s_it != m_vServers.end(); ++s_it) {
+ if(s_it->GetIpAdress() == host) flag = false;
+ }
+ if(flag) {
+ m_vServers.push_back(PlexServer(data, host));
+ isyslog("[plex] New Server Discovered: %s", host.c_str());
+ }
+ }
+ } catch (Poco::IOException) {} // No Networkconnection
}
void plexgdm::stopRegistration()
{
- if(m_registrationIsRunning) {
- m_registrationIsRunning = false;
- m_waitCondition.Broadcast();
- Cancel();
- }
+ if(m_registrationIsRunning) {
+ m_registrationIsRunning = false;
+ m_waitCondition.Broadcast();
+ Cancel();
+ }
}
}
diff --git a/plexgdm.h b/plexgdm.h
index 8cbedea..8aa8ad4 100644
--- a/plexgdm.h
+++ b/plexgdm.h
@@ -15,6 +15,7 @@
#include <Poco/Format.h>
#include <vdr/thread.h>
+#include <vdr/tools.h>
#include "PlexServer.h"
@@ -32,11 +33,11 @@ public:
void clientDetails(std::string c_id, std::string c_name, std::string c_port, std::string c_product, std::string c_version);
std::string getClientDetails();
PlexServer* getServerList();
- void discover();;
+ void discover();
void checkClientRegistration();
-
+
void Action(void);
-
+
//void startAll();
void startRegistration();
@@ -50,10 +51,13 @@ public:
protected:
- private:
+private:
plexgdm();
cMutex m_mutex;
cCondVar m_waitCondition;
+ int _discoverInterval;
+ cTimeMs _discoverTimer;
+ bool _helloSent;
Poco::Net::SocketAddress m_discoverAdress;
Poco::Net::SocketAddress m_clientRegisterGroup;