1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine the statistic arch.

This commit is contained in:
winlin 2015-01-05 12:40:38 +08:00
parent 40ed2249e8
commit cc796a433a
4 changed files with 90 additions and 105 deletions

View file

@ -26,7 +26,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifdef SRS_AUTO_HTTP_API #ifdef SRS_AUTO_HTTP_API
#include <sstream> #include <sstream>
#include <set>
using namespace std; using namespace std;
#include <srs_kernel_log.hpp> #include <srs_kernel_log.hpp>
@ -522,31 +521,15 @@ bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchil
int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
{ {
int ret = ERROR_SUCCESS;
std::stringstream ss; std::stringstream ss;
std::set<std::string> vhost_set; SrsStatistic* stat = SrsStatistic::instance();
std::map<void*, SrsStreamInfo*>* pool = SrsStatistic::instance()->get_pool(); if ((ret = stat->dumps_vhosts(ss)) != ERROR_SUCCESS) {
std::map<void*, SrsStreamInfo*>::iterator it; return ret;
for (it = pool->begin(); it != pool->end(); it++) {
if (it->second->_req == NULL)
continue;
vhost_set.insert(it->second->_req->vhost);
} }
ss << __SRS_JARRAY_START;
bool first = true;
std::set<std::string>::iterator it_set;
for (it_set = vhost_set.begin(); it_set != vhost_set.end(); it_set++) {
if (first) {
first = false;
} else {
ss << __SRS_JFIELD_CONT;
}
ss << "\"" << (*it_set) << "\"";
}
ss << __SRS_JARRAY_END;
return res_json(skt, req, ss.str()); return res_json(skt, req, ss.str());
} }
@ -565,40 +548,13 @@ bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchi
int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
{ {
int ret = ERROR_SUCCESS;
std::stringstream ss; std::stringstream ss;
std::string query_name = req->query_get("name"); SrsStatistic* stat = SrsStatistic::instance();
std::string query_vhost = req->query_get("vhost"); if ((ret = stat->dumps_streams(ss)) != ERROR_SUCCESS) {
if (query_name.size() > 0 || query_vhost.size() > 0) { return ret;
ss << __SRS_JARRAY_START;
bool first = true;
std::map<void*, SrsStreamInfo*>* pool = SrsStatistic::instance()->get_pool();
std::map<void*, SrsStreamInfo*>::iterator it;
for (it = pool->begin(); it != pool->end(); it++) {
SrsRequest* reqinfo = it->second->_req;
if (reqinfo == NULL)
continue;
if (reqinfo->stream == query_name || reqinfo->vhost == query_vhost) {
if (first) {
first = false;
} else {
ss << __SRS_JFIELD_CONT;
}
ss << __SRS_JOBJECT_START
<< __SRS_JFIELD_STR("name", reqinfo->stream) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_STR("url", reqinfo->tcUrl) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_ORG("clients", 0) << __SRS_JFIELD_CONT
<< __SRS_JFIELD_STR("status", "idle") << __SRS_JFIELD_CONT
<< __SRS_JFIELD_STR("type", "") << __SRS_JFIELD_CONT
<< __SRS_JFIELD_STR("codec", "")
<< __SRS_JOBJECT_END;
}
}
ss << __SRS_JARRAY_END;
} else {
return res_error(skt, req, 400, "Bad Request", "unknown query");
} }
return res_json(skt, req, ss.str()); return res_json(skt, req, ss.str());

View file

@ -396,7 +396,12 @@ int SrsRtmpConn::stream_service_cycle()
} }
srs_assert(source != NULL); srs_assert(source != NULL);
SrsStatistic::instance()->add_request_info(source, req); // update the statistic when source disconveried.
SrsStatistic* stat = SrsStatistic::instance();
if ((ret = stat->on_client(_srs_context->get_id(), req)) != ERROR_SUCCESS) {
srs_error("stat client failed. ret=%d", ret);
return ret;
}
// check ASAP, to fail it faster if invalid. // check ASAP, to fail it faster if invalid.
if (type != SrsRtmpConnPlay && !vhost_is_edge) { if (type != SrsRtmpConnPlay && !vhost_is_edge) {

View file

@ -25,17 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_protocol_rtmp.hpp> #include <srs_protocol_rtmp.hpp>
SrsStreamInfo::SrsStreamInfo() SrsStatistic* SrsStatistic::_instance = new SrsStatistic();
{
_req = NULL;
}
SrsStreamInfo::~SrsStreamInfo()
{
srs_freep(_req);
}
SrsStatistic* SrsStatistic::_instance = NULL;
SrsStatistic::SrsStatistic() SrsStatistic::SrsStatistic()
{ {
@ -43,43 +33,48 @@ SrsStatistic::SrsStatistic()
SrsStatistic::~SrsStatistic() SrsStatistic::~SrsStatistic()
{ {
std::map<void*, SrsStreamInfo*>::iterator it; if (true) {
for (it = pool.begin(); it != pool.end(); it++) { std::map<std::string, SrsStatisticVhost*>::iterator it;
SrsStreamInfo* si = it->second; for (it = vhosts.begin(); it != vhosts.end(); it++) {
srs_freep(si); SrsStatisticVhost* vhost = it->second;
srs_freep(vhost);
}
}
if (true) {
std::map<std::string, SrsStatisticStream*>::iterator it;
for (it = streams.begin(); it != streams.end(); it++) {
SrsStatisticStream* stream = it->second;
srs_freep(stream);
}
}
if (true) {
std::map<int, SrsStatisticClient*>::iterator it;
for (it = clients.begin(); it != clients.end(); it++) {
SrsStatisticClient* client = it->second;
srs_freep(client);
}
} }
} }
SrsStatistic* SrsStatistic::instance() SrsStatistic* SrsStatistic::instance()
{ {
if (_instance == NULL) {
_instance = new SrsStatistic();
}
return _instance; return _instance;
} }
std::map<void*, SrsStreamInfo*>* SrsStatistic::get_pool() int SrsStatistic::on_client(int id, SrsRequest *req)
{ {
return &pool; int ret = ERROR_SUCCESS;
return ret;
} }
SrsStreamInfo* SrsStatistic::get(void *p) int SrsStatistic::dumps_vhosts(std::stringstream& ss)
{ {
std::map<void*, SrsStreamInfo*>::iterator it = pool.find(p); int ret = ERROR_SUCCESS;
if (it == pool.end()) { return ret;
SrsStreamInfo* si = new SrsStreamInfo();
pool[p] = si;
return si;
} else {
SrsStreamInfo* si = it->second;
return si;
}
} }
void SrsStatistic::add_request_info(void *p, SrsRequest *req) int SrsStatistic::dumps_streams(std::stringstream& ss)
{ {
SrsStreamInfo* info = get(p); int ret = ERROR_SUCCESS;
if (info->_req == NULL) { return ret;
info->_req = req->copy();
}
} }

View file

@ -31,33 +31,62 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_core.hpp> #include <srs_core.hpp>
#include <map> #include <map>
#include <string>
class SrsRequest; class SrsRequest;
class SrsStreamInfo struct SrsStatisticVhost
{ {
public: public:
SrsStreamInfo(); std::string vhost;
virtual ~SrsStreamInfo(); };
struct SrsStatisticStream
{
public: public:
SrsRequest *_req; SrsStatisticVhost* vhost;
std::string app;
std::string stream;
};
struct SrsStatisticClient
{
public:
SrsStatisticStream* stream;
int id;
}; };
class SrsStatistic class SrsStatistic
{ {
public: private:
static SrsStatistic* instance(); static SrsStatistic *_instance;
public: // key: vhost name, value: vhost object.
virtual std::map<void*, SrsStreamInfo*>* get_pool(); std::map<std::string, SrsStatisticVhost*> vhosts;
virtual void add_request_info(void *p, SrsRequest *req); // key: stream name, value: stream object.
std::map<std::string, SrsStatisticStream*> streams;
// key: client id, value: stream object.
std::map<int, SrsStatisticClient*> clients;
private: private:
SrsStatistic(); SrsStatistic();
virtual ~SrsStatistic(); virtual ~SrsStatistic();
private: public:
static SrsStatistic *_instance; static SrsStatistic* instance();
std::map<void*, SrsStreamInfo*> pool; public:
private: /**
virtual SrsStreamInfo *get(void *p); * when got a client to publish/play stream,
* @param id, the client srs id.
* @param req, the client request object.
*/
virtual int on_client(int id, SrsRequest *req);
public:
/**
* dumps the vhosts to sstream in json.
*/
virtual int dumps_vhosts(std::stringstream& ss);
/**
* dumps the streams to sstream in json.
*/
virtual int dumps_streams(std::stringstream& ss);
}; };
#endif #endif