Added list of viewers for all streams to balancer

This commit is contained in:
Thulinma 2016-07-21 15:19:55 +02:00
parent f9dcbc7f13
commit 09b398f36c

View file

@ -81,6 +81,14 @@ class hostDetails{
r["streams"] = (long long)streams.size(); r["streams"] = (long long)streams.size();
r["viewers"] = (long long)total; r["viewers"] = (long long)total;
} }
///Fills out a by reference given JSON::Value with current streams.
void fillStreams(JSON::Value & r){
if (!hostMutex){hostMutex = new tthread::mutex();}
tthread::lock_guard<tthread::mutex> guard(*hostMutex);
for (std::map<std::string, struct streamDetails>::iterator jt = streams.begin(); jt != streams.end(); ++jt){
r[jt->first] = r[jt->first].asInt() + jt->second.total;
}
}
///Scores a potential new connection to this server, on a scale from 0 to 3200. ///Scores a potential new connection to this server, on a scale from 0 to 3200.
///0 is horrible, 3200 is perfect. ///0 is horrible, 3200 is perfect.
unsigned int rate(std::string & s){ unsigned int rate(std::string & s){
@ -201,21 +209,25 @@ int handleRequest(Socket::Connection & conn){
if (H.url.size() == 1){ if (H.url.size() == 1){
std::string host = H.GetVar("host"); std::string host = H.GetVar("host");
std::string stream = H.GetVar("stream"); std::string stream = H.GetVar("stream");
std::string viewers = H.GetVar("viewers");
H.Clean(); H.Clean();
H.SetHeader("Content-Type", "text/plain"); H.SetHeader("Content-Type", "text/plain");
JSON::Value ret; JSON::Value ret;
if (viewers.size()){
for (std::map<std::string, hostDetails>::iterator it = hosts.begin(); it != hosts.end(); ++it){
it->second.fillStreams(ret);
}
H.SetBody(ret.toPrettyString());
H.SendResponse("200", "OK", conn);
H.Clean();
continue;
}else{
if (!host.size() && !stream.size()){ if (!host.size() && !stream.size()){
for (std::map<std::string, hostDetails>::iterator it = hosts.begin(); it != hosts.end(); ++it){ for (std::map<std::string, hostDetails>::iterator it = hosts.begin(); it != hosts.end(); ++it){
it->second.fillState(ret[it->first]); it->second.fillState(ret[it->first]);
} }
}else{ }else{
if (stream.size()){ if (hosts.count(host)){
unsigned long long strTot = 0;
for (std::map<std::string, hostDetails>::iterator it = hosts.begin(); it != hosts.end(); ++it){
strTot += it->second.count(stream);
}
ret = (long long)strTot;
}else if (hosts.count(host)){
hosts[host].fillState(ret); hosts[host].fillState(ret);
} }
} }
@ -224,6 +236,7 @@ int handleRequest(Socket::Connection & conn){
H.Clean(); H.Clean();
continue; continue;
} }
}
std::string stream = H.url.substr(1); std::string stream = H.url.substr(1);
INFO_MSG("Balancing stream %s", stream.c_str()); INFO_MSG("Balancing stream %s", stream.c_str());
H.Clean(); H.Clean();