refine code, use global virtual id to generate the id of vhost and stream.
正在显示
3 个修改的文件
包含
57 行增加
和
2 行删除
| @@ -529,6 +529,7 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) | @@ -529,6 +529,7 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) | ||
| 529 | 529 | ||
| 530 | ss << __SRS_JOBJECT_START | 530 | ss << __SRS_JOBJECT_START |
| 531 | << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT | 531 | << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT |
| 532 | + << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT | ||
| 532 | << __SRS_JFIELD_ORG("vhosts", __SRS_JARRAY_START) | 533 | << __SRS_JFIELD_ORG("vhosts", __SRS_JARRAY_START) |
| 533 | << data.str() | 534 | << data.str() |
| 534 | << __SRS_JARRAY_END | 535 | << __SRS_JARRAY_END |
| @@ -560,6 +561,7 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) | @@ -560,6 +561,7 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) | ||
| 560 | 561 | ||
| 561 | ss << __SRS_JOBJECT_START | 562 | ss << __SRS_JOBJECT_START |
| 562 | << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT | 563 | << __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT |
| 564 | + << __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT | ||
| 563 | << __SRS_JFIELD_ORG("streams", __SRS_JARRAY_START) | 565 | << __SRS_JFIELD_ORG("streams", __SRS_JARRAY_START) |
| 564 | << data.str() | 566 | << data.str() |
| 565 | << __SRS_JARRAY_END | 567 | << __SRS_JARRAY_END |
| @@ -23,16 +23,44 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -23,16 +23,44 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 23 | 23 | ||
| 24 | #include <srs_app_statistic.hpp> | 24 | #include <srs_app_statistic.hpp> |
| 25 | 25 | ||
| 26 | +#include <unistd.h> | ||
| 26 | #include <sstream> | 27 | #include <sstream> |
| 27 | using namespace std; | 28 | using namespace std; |
| 28 | 29 | ||
| 29 | #include <srs_protocol_rtmp.hpp> | 30 | #include <srs_protocol_rtmp.hpp> |
| 30 | #include <srs_app_json.hpp> | 31 | #include <srs_app_json.hpp> |
| 31 | 32 | ||
| 33 | +int64_t __srs_gvid = getpid(); | ||
| 34 | + | ||
| 35 | +int64_t __srs_generate_id() | ||
| 36 | +{ | ||
| 37 | + return __srs_gvid++; | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +SrsStatisticVhost::SrsStatisticVhost() | ||
| 41 | +{ | ||
| 42 | + id = __srs_generate_id(); | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +SrsStatisticVhost::~SrsStatisticVhost() | ||
| 46 | +{ | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +SrsStatisticStream::SrsStatisticStream() | ||
| 50 | +{ | ||
| 51 | + id = __srs_generate_id(); | ||
| 52 | + vhost = NULL; | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +SrsStatisticStream::~SrsStatisticStream() | ||
| 56 | +{ | ||
| 57 | +} | ||
| 58 | + | ||
| 32 | SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); | 59 | SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); |
| 33 | 60 | ||
| 34 | SrsStatistic::SrsStatistic() | 61 | SrsStatistic::SrsStatistic() |
| 35 | { | 62 | { |
| 63 | + _server_id = __srs_generate_id(); | ||
| 36 | } | 64 | } |
| 37 | 65 | ||
| 38 | SrsStatistic::~SrsStatistic() | 66 | SrsStatistic::~SrsStatistic() |
| @@ -87,7 +115,8 @@ int SrsStatistic::on_client(int id, SrsRequest* req) | @@ -87,7 +115,8 @@ int SrsStatistic::on_client(int id, SrsRequest* req) | ||
| 87 | if (streams.find(url) == streams.end()) { | 115 | if (streams.find(url) == streams.end()) { |
| 88 | stream = new SrsStatisticStream(); | 116 | stream = new SrsStatisticStream(); |
| 89 | stream->vhost = vhost; | 117 | stream->vhost = vhost; |
| 90 | - stream->stream = url; | 118 | + stream->stream = req->stream; |
| 119 | + stream->url = url; | ||
| 91 | streams[url] = stream; | 120 | streams[url] = stream; |
| 92 | } else { | 121 | } else { |
| 93 | stream = streams[url]; | 122 | stream = streams[url]; |
| @@ -96,6 +125,11 @@ int SrsStatistic::on_client(int id, SrsRequest* req) | @@ -96,6 +125,11 @@ int SrsStatistic::on_client(int id, SrsRequest* req) | ||
| 96 | return ret; | 125 | return ret; |
| 97 | } | 126 | } |
| 98 | 127 | ||
| 128 | +int64_t SrsStatistic::server_id() | ||
| 129 | +{ | ||
| 130 | + return _server_id; | ||
| 131 | +} | ||
| 132 | + | ||
| 99 | int SrsStatistic::dumps_vhosts(stringstream& ss) | 133 | int SrsStatistic::dumps_vhosts(stringstream& ss) |
| 100 | { | 134 | { |
| 101 | int ret = ERROR_SUCCESS; | 135 | int ret = ERROR_SUCCESS; |
| @@ -104,6 +138,7 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) | @@ -104,6 +138,7 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) | ||
| 104 | for (it = vhosts.begin(); it != vhosts.end(); it++) { | 138 | for (it = vhosts.begin(); it != vhosts.end(); it++) { |
| 105 | SrsStatisticVhost* vhost = it->second; | 139 | SrsStatisticVhost* vhost = it->second; |
| 106 | ss << __SRS_JOBJECT_START | 140 | ss << __SRS_JOBJECT_START |
| 141 | + << __SRS_JFIELD_ORG("id", vhost->id) << __SRS_JFIELD_CONT | ||
| 107 | << __SRS_JFIELD_STR("name", vhost->vhost) | 142 | << __SRS_JFIELD_STR("name", vhost->vhost) |
| 108 | << __SRS_JOBJECT_END; | 143 | << __SRS_JOBJECT_END; |
| 109 | } | 144 | } |
| @@ -119,7 +154,9 @@ int SrsStatistic::dumps_streams(stringstream& ss) | @@ -119,7 +154,9 @@ int SrsStatistic::dumps_streams(stringstream& ss) | ||
| 119 | for (it = streams.begin(); it != streams.end(); it++) { | 154 | for (it = streams.begin(); it != streams.end(); it++) { |
| 120 | SrsStatisticStream* stream = it->second; | 155 | SrsStatisticStream* stream = it->second; |
| 121 | ss << __SRS_JOBJECT_START | 156 | ss << __SRS_JOBJECT_START |
| 122 | - << __SRS_JFIELD_STR("url", stream->stream) | 157 | + << __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT |
| 158 | + << __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT | ||
| 159 | + << __SRS_JFIELD_ORG("vhost", stream->vhost->id) | ||
| 123 | << __SRS_JOBJECT_END; | 160 | << __SRS_JOBJECT_END; |
| 124 | } | 161 | } |
| 125 | 162 |
| @@ -38,15 +38,24 @@ class SrsRequest; | @@ -38,15 +38,24 @@ class SrsRequest; | ||
| 38 | struct SrsStatisticVhost | 38 | struct SrsStatisticVhost |
| 39 | { | 39 | { |
| 40 | public: | 40 | public: |
| 41 | + int64_t id; | ||
| 41 | std::string vhost; | 42 | std::string vhost; |
| 43 | +public: | ||
| 44 | + SrsStatisticVhost(); | ||
| 45 | + virtual ~SrsStatisticVhost(); | ||
| 42 | }; | 46 | }; |
| 43 | 47 | ||
| 44 | struct SrsStatisticStream | 48 | struct SrsStatisticStream |
| 45 | { | 49 | { |
| 46 | public: | 50 | public: |
| 51 | + int64_t id; | ||
| 47 | SrsStatisticVhost* vhost; | 52 | SrsStatisticVhost* vhost; |
| 48 | std::string app; | 53 | std::string app; |
| 49 | std::string stream; | 54 | std::string stream; |
| 55 | + std::string url; | ||
| 56 | +public: | ||
| 57 | + SrsStatisticStream(); | ||
| 58 | + virtual ~SrsStatisticStream(); | ||
| 50 | }; | 59 | }; |
| 51 | 60 | ||
| 52 | struct SrsStatisticClient | 61 | struct SrsStatisticClient |
| @@ -60,6 +69,8 @@ class SrsStatistic | @@ -60,6 +69,8 @@ class SrsStatistic | ||
| 60 | { | 69 | { |
| 61 | private: | 70 | private: |
| 62 | static SrsStatistic *_instance; | 71 | static SrsStatistic *_instance; |
| 72 | + // the id to identify the sever. | ||
| 73 | + int64_t _server_id; | ||
| 63 | // key: vhost name, value: vhost object. | 74 | // key: vhost name, value: vhost object. |
| 64 | std::map<std::string, SrsStatisticVhost*> vhosts; | 75 | std::map<std::string, SrsStatisticVhost*> vhosts; |
| 65 | // key: stream name, value: stream object. | 76 | // key: stream name, value: stream object. |
| @@ -80,6 +91,11 @@ public: | @@ -80,6 +91,11 @@ public: | ||
| 80 | virtual int on_client(int id, SrsRequest* req); | 91 | virtual int on_client(int id, SrsRequest* req); |
| 81 | public: | 92 | public: |
| 82 | /** | 93 | /** |
| 94 | + * get the server id, used to identify the server. | ||
| 95 | + * for example, when restart, the server id must changed. | ||
| 96 | + */ | ||
| 97 | + virtual int64_t server_id(); | ||
| 98 | + /** | ||
| 83 | * dumps the vhosts to sstream in json. | 99 | * dumps the vhosts to sstream in json. |
| 84 | */ | 100 | */ |
| 85 | virtual int dumps_vhosts(std::stringstream& ss); | 101 | virtual int dumps_vhosts(std::stringstream& ss); |
-
请 注册 或 登录 后发表评论