From ae63af6a4adb2a788aaff184b1617d8d82639eb7 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 5 Jan 2015 12:57:48 +0800 Subject: [PATCH] refine code, implements the stat.: --- trunk/src/app/srs_app_statistic.cpp | 53 +++++++++++++++++++++++++++-- trunk/src/app/srs_app_statistic.hpp | 2 +- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/trunk/src/app/srs_app_statistic.cpp b/trunk/src/app/srs_app_statistic.cpp index 7b5c60944..5c0f1aad3 100644 --- a/trunk/src/app/srs_app_statistic.cpp +++ b/trunk/src/app/srs_app_statistic.cpp @@ -23,7 +23,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include +using namespace std; + #include +#include SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); @@ -61,20 +65,63 @@ SrsStatistic* SrsStatistic::instance() return _instance; } -int SrsStatistic::on_client(int id, SrsRequest *req) +int SrsStatistic::on_client(int id, SrsRequest* req) { int ret = ERROR_SUCCESS; + + // create vhost if not exists. + SrsStatisticVhost* vhost = NULL; + if (vhosts.find(req->vhost) == vhosts.end()) { + vhost = new SrsStatisticVhost(); + vhost->vhost = req->vhost; + vhosts[req->vhost] = vhost; + } else { + vhost = vhosts[req->vhost]; + } + + // the url to identify the stream. + std::string url = req->get_stream_url(); + + // create stream if not exists. + SrsStatisticStream* stream = NULL; + if (streams.find(url) == streams.end()) { + stream = new SrsStatisticStream(); + stream->vhost = vhost; + stream->stream = url; + streams[url] = stream; + } else { + stream = streams[url]; + } + return ret; } -int SrsStatistic::dumps_vhosts(std::stringstream& ss) +int SrsStatistic::dumps_vhosts(stringstream& ss) { int ret = ERROR_SUCCESS; + + std::map::iterator it; + for (it = vhosts.begin(); it != vhosts.end(); it++) { + SrsStatisticVhost* vhost = it->second; + ss << __SRS_JOBJECT_START + << __SRS_JFIELD_STR("name", vhost->vhost) + << __SRS_JOBJECT_END; + } + return ret; } -int SrsStatistic::dumps_streams(std::stringstream& ss) +int SrsStatistic::dumps_streams(stringstream& ss) { int ret = ERROR_SUCCESS; + + std::map::iterator it; + for (it = streams.begin(); it != streams.end(); it++) { + SrsStatisticStream* stream = it->second; + ss << __SRS_JOBJECT_START + << __SRS_JFIELD_STR("url", stream->stream) + << __SRS_JOBJECT_END; + } + return ret; } diff --git a/trunk/src/app/srs_app_statistic.hpp b/trunk/src/app/srs_app_statistic.hpp index e7ec72ed6..fa14e4b41 100644 --- a/trunk/src/app/srs_app_statistic.hpp +++ b/trunk/src/app/srs_app_statistic.hpp @@ -77,7 +77,7 @@ public: * @param id, the client srs id. * @param req, the client request object. */ - virtual int on_client(int id, SrsRequest *req); + virtual int on_client(int id, SrsRequest* req); public: /** * dumps the vhosts to sstream in json.