summaryrefslogtreecommitdiff
path: root/SubscriptionManager.cpp
blob: c4589ef22bddd61ca3314851c8d779d7c453bc2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "SubscriptionManager.h"
#include "Config.h"

#include <memory>

#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/MessageHeader.h>

namespace plexclient
{

SubscriptionManager::SubscriptionManager()
{
	
}

void SubscriptionManager::Notify() {
	//mutex.lock();
	
	//TODO: Implement
	for(std::map<std::string, Subscriber>::iterator it = m_mSubcribers.begin() ; it != m_mSubcribers.end(); ++it) {
		Subscriber subs = it->second;
		subs.SendUpdate(GetMsg(std::string(itoa(subs.m_iCommandId))), false);
	}
	
	//mutex.unlock();
}

void SubscriptionManager::AddSubscriber(Subscriber subs) {
	//mutex.lock();
	m_mSubcribers[subs.GetUuid()] = subs;
	std::cout << "AddSubscriber: " << subs.to_string() << std::endl;
	//mutex.unlock();
}

void SubscriptionManager::RemoveSubscriber(std::string uuid) {
	//mutex.lock();
	if(m_mSubcribers.find(uuid) != m_mSubcribers.end()) {
		m_mSubcribers.erase(uuid);
	}
	//mutex.unlock();
}

std::string SubscriptionManager::GetMsg(std::string commandId) {
	int time = 0;//PlayerGetCurrentPosition();
	
	//bool paused = PlayerPaused;
	
	std::stringstream msg;
	msg << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
		   "<MediaContainer commandID=\"" << commandId << "\""
		   " location=\"navigation\">";
	
	msg << "<Timeline location=\"navigation\" state=\"";
	//if(paused) 		msg << "paused";
	//else if (paused) 	msg	<< "paused";
	 				msg << "stopped";
	msg << "\" time=\"" << time << "\" type=\"video\" />";
	msg << "<Timeline location=\"navigation\" state=\"stopped\" time=\"0\" type=\"photo\" />";
	msg << "<Timeline location=\"navigation\" state=\"stopped\" time=\"0\" type=\"music\" />";
	
	msg << "</MediaContainer>";
	return msg.str();
}

Subscriber::Subscriber(std::string protocol, std::string host, int port, std::string uuid, int commandId) {
	m_sProtocol = protocol;
	m_sHost = host;
	m_iPort = port;
	m_sUuid = uuid;
	m_iCommandId = commandId;
}

void Subscriber::SendUpdate(std::string msg, bool isNav) {
	++m_iAge;
	
	Poco::Net::HTTPRequest *pRequest = new Poco::Net::HTTPRequest(Poco::Net::HTTPRequest::HTTP_POST,
	        "/:/timeline", Poco::Net::HTTPMessage::HTTP_1_1);

	pRequest->add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17");

	pRequest->add("X-Plex-Client-Capabilities", "protocols=shoutcast,http-video;videoDecoders=h264{profile:high&resolution:1080&level:51};audioDecoders=mp3,aac");
	pRequest->add("X-Plex-Client-Identifier", Config::GetInstance().GetUUID());
	pRequest->add("X-Plex-Device", "PC");
	pRequest->add("X-Plex-Device-Name", Config::GetInstance().GetHostname());
	pRequest->add("X-Plex-Language", Config::GetInstance().GetLanguage());
	pRequest->add("X-Plex-Model", "Linux");
	pRequest->add("X-Plex-Platform", "VDR");
	pRequest->add("X-Plex-Product", "plex-vdr");
	pRequest->add("X-Plex-Provides", "player");
	pRequest->add("X-Plex-Version", "0.0.1a");
	
	Poco::Net::HTTPClientSession* session = new Poco::Net::HTTPClientSession(m_sHost, m_iPort);
	std::ostream& oustr = session->sendRequest(*pRequest);
	oustr << msg;
}

ActionManager::ActionManager() {}

void ActionManager::AddAction(std::string file) {
	m_sAction = file;
	m_isAction = true;
}

std::string ActionManager::GetAction() {
	m_isAction = false;
	return m_sAction;
}

bool ActionManager::IsAction() {
	return m_isAction;
}

}