forked from superpea/xbmc-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManualServerScanner.h
170 lines (136 loc) · 4.61 KB
/
ManualServerScanner.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#pragma once
//
// Copyright (c) 2011 Plex, Inc. All rights reserved.
//
#include <string>
#include <set>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition.hpp>
#include "PlexDirectory.h"
#include "PlexServerManager.h"
using namespace std;
using namespace XFILE;
class ManualServer
{
public:
ManualServer(const string& address)
: deleted(false)
, address(address)
, updatedAt(0) {}
string url()
{
return "http://" + address + ":32400";
}
bool deleted;
string uuid;
string name;
string address;
time_t updatedAt;
};
typedef boost::shared_ptr<ManualServer> ManualServerPtr;
typedef pair<string, ManualServerPtr> address_server_pair;
class ManualServerScanner
{
public:
/// Singleton.
static ManualServerScanner& Get()
{
static ManualServerScanner* g_instance;
if (g_instance == 0)
{
g_instance = new ManualServerScanner();
boost::thread t(boost::bind(&ManualServerScanner::run, g_instance));
}
return *g_instance;
}
/// Add a new manual server.
void addServer(const string& address, bool removeOthers=false)
{
boost::mutex::scoped_lock lk(m_mutex);
if (m_serverMap.find(address) == m_serverMap.end())
m_serverMap[address] = ManualServerPtr(new ManualServer(address));
if (removeOthers)
{
// Mark the others as deleted.
BOOST_FOREACH(address_server_pair pair, m_serverMap)
if (pair.first != address && pair.first != "127.0.0.1")
pair.second->deleted = true;
}
m_condition.notify_one();
}
/// Remove all servers except local.
void removeAllServersButLocal()
{
boost::mutex::scoped_lock lk(m_mutex);
// Mark non-local as deleted.
BOOST_FOREACH(address_server_pair pair, m_serverMap)
if (pair.first != "127.0.0.1")
pair.second->deleted = true;
}
private:
void run()
{
boost::mutex::scoped_lock lk(m_condMutex);
boost::posix_time::seconds delay(10);
while (true)
{
// Wait to be signaled or for the timeout to occur.
m_condition.timed_wait(lk, delay);
m_mutex.lock();
// Get a copy of the servers.
map<string, ManualServerPtr> servers(m_serverMap);
// Whack deleted servers.
BOOST_FOREACH(address_server_pair pair, servers)
if (pair.second->deleted)
m_serverMap.erase(pair.first);
m_mutex.unlock();
// See if they're alive.
BOOST_FOREACH(address_server_pair pair, servers)
{
// Make sure we don't replace localhost when we ask for listing. Only wait
// five seconds as well, otherwise if we hit some dead server we end up taking
// 5 whole MINUTES to timeout.
//
CPlexDirectory dir(true, false, false, 5);
CFileItemList list;
dprintf("Manual Server Scanner: About to manually test server %s (deleted: %d)", pair.second->address.c_str(), pair.second->deleted);
if (pair.second->deleted == false && dir.GetDirectory(pair.second->url(), list) && list.GetProperty("updatedAt").empty() == false)
{
// Update name and UUID.
pair.second->name = list.GetProperty("friendlyName");
pair.second->uuid = list.GetProperty("machineIdentifier");
time_t updatedAt = list.GetPropertyInt("updatedAt");
if (pair.second->updatedAt == 0)
{
dprintf("Manual Server Scanner: NEW SERVER: %s", pair.second->address.c_str());
PlexServerManager::Get().addServer(pair.second->uuid, pair.second->name, pair.second->address, 32400);
}
else if (pair.second->updatedAt != updatedAt)
{
dprintf("Manual Server Scanner: UPDATED SERVER: %s", pair.second->address.c_str());
PlexServerManager::Get().updateServer(pair.second->uuid, pair.second->name, pair.second->address, 32400, updatedAt);
}
pair.second->updatedAt = updatedAt;
}
else
{
if (pair.second->updatedAt != 0)
{
dprintf("Manual Server Scanner: DEAD SERVER: %s", pair.second->address.c_str());
PlexServerManager::Get().removeServer(pair.second->uuid, pair.second->name, pair.second->address, 32400);
}
pair.second->updatedAt = 0;
}
}
}
}
ManualServerScanner() {}
~ManualServerScanner() {}
map<string, ManualServerPtr> m_serverMap;
boost::condition m_condition;
boost::mutex m_condMutex;
boost::mutex m_mutex;
};