mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
refine code, use global virtual id to generate the id of vhost and stream.
This commit is contained in:
parent
ae63af6a4a
commit
f1efdcd000
3 changed files with 57 additions and 2 deletions
|
@ -529,6 +529,7 @@ int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
|
|||
|
||||
ss << __SRS_JOBJECT_START
|
||||
<< __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT
|
||||
<< __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT
|
||||
<< __SRS_JFIELD_ORG("vhosts", __SRS_JARRAY_START)
|
||||
<< data.str()
|
||||
<< __SRS_JARRAY_END
|
||||
|
@ -560,6 +561,7 @@ int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
|
|||
|
||||
ss << __SRS_JOBJECT_START
|
||||
<< __SRS_JFIELD_ERROR(ret) << __SRS_JFIELD_CONT
|
||||
<< __SRS_JFIELD_ORG("server", stat->server_id()) << __SRS_JFIELD_CONT
|
||||
<< __SRS_JFIELD_ORG("streams", __SRS_JARRAY_START)
|
||||
<< data.str()
|
||||
<< __SRS_JARRAY_END
|
||||
|
|
|
@ -23,16 +23,44 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
|
||||
#include <srs_app_statistic.hpp>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
using namespace std;
|
||||
|
||||
#include <srs_protocol_rtmp.hpp>
|
||||
#include <srs_app_json.hpp>
|
||||
|
||||
int64_t __srs_gvid = getpid();
|
||||
|
||||
int64_t __srs_generate_id()
|
||||
{
|
||||
return __srs_gvid++;
|
||||
}
|
||||
|
||||
SrsStatisticVhost::SrsStatisticVhost()
|
||||
{
|
||||
id = __srs_generate_id();
|
||||
}
|
||||
|
||||
SrsStatisticVhost::~SrsStatisticVhost()
|
||||
{
|
||||
}
|
||||
|
||||
SrsStatisticStream::SrsStatisticStream()
|
||||
{
|
||||
id = __srs_generate_id();
|
||||
vhost = NULL;
|
||||
}
|
||||
|
||||
SrsStatisticStream::~SrsStatisticStream()
|
||||
{
|
||||
}
|
||||
|
||||
SrsStatistic* SrsStatistic::_instance = new SrsStatistic();
|
||||
|
||||
SrsStatistic::SrsStatistic()
|
||||
{
|
||||
_server_id = __srs_generate_id();
|
||||
}
|
||||
|
||||
SrsStatistic::~SrsStatistic()
|
||||
|
@ -87,7 +115,8 @@ int SrsStatistic::on_client(int id, SrsRequest* req)
|
|||
if (streams.find(url) == streams.end()) {
|
||||
stream = new SrsStatisticStream();
|
||||
stream->vhost = vhost;
|
||||
stream->stream = url;
|
||||
stream->stream = req->stream;
|
||||
stream->url = url;
|
||||
streams[url] = stream;
|
||||
} else {
|
||||
stream = streams[url];
|
||||
|
@ -96,6 +125,11 @@ int SrsStatistic::on_client(int id, SrsRequest* req)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int64_t SrsStatistic::server_id()
|
||||
{
|
||||
return _server_id;
|
||||
}
|
||||
|
||||
int SrsStatistic::dumps_vhosts(stringstream& ss)
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
@ -104,6 +138,7 @@ int SrsStatistic::dumps_vhosts(stringstream& ss)
|
|||
for (it = vhosts.begin(); it != vhosts.end(); it++) {
|
||||
SrsStatisticVhost* vhost = it->second;
|
||||
ss << __SRS_JOBJECT_START
|
||||
<< __SRS_JFIELD_ORG("id", vhost->id) << __SRS_JFIELD_CONT
|
||||
<< __SRS_JFIELD_STR("name", vhost->vhost)
|
||||
<< __SRS_JOBJECT_END;
|
||||
}
|
||||
|
@ -119,7 +154,9 @@ int SrsStatistic::dumps_streams(stringstream& ss)
|
|||
for (it = streams.begin(); it != streams.end(); it++) {
|
||||
SrsStatisticStream* stream = it->second;
|
||||
ss << __SRS_JOBJECT_START
|
||||
<< __SRS_JFIELD_STR("url", stream->stream)
|
||||
<< __SRS_JFIELD_ORG("id", stream->id) << __SRS_JFIELD_CONT
|
||||
<< __SRS_JFIELD_STR("name", stream->stream) << __SRS_JFIELD_CONT
|
||||
<< __SRS_JFIELD_ORG("vhost", stream->vhost->id)
|
||||
<< __SRS_JOBJECT_END;
|
||||
}
|
||||
|
||||
|
|
|
@ -38,15 +38,24 @@ class SrsRequest;
|
|||
struct SrsStatisticVhost
|
||||
{
|
||||
public:
|
||||
int64_t id;
|
||||
std::string vhost;
|
||||
public:
|
||||
SrsStatisticVhost();
|
||||
virtual ~SrsStatisticVhost();
|
||||
};
|
||||
|
||||
struct SrsStatisticStream
|
||||
{
|
||||
public:
|
||||
int64_t id;
|
||||
SrsStatisticVhost* vhost;
|
||||
std::string app;
|
||||
std::string stream;
|
||||
std::string url;
|
||||
public:
|
||||
SrsStatisticStream();
|
||||
virtual ~SrsStatisticStream();
|
||||
};
|
||||
|
||||
struct SrsStatisticClient
|
||||
|
@ -60,6 +69,8 @@ class SrsStatistic
|
|||
{
|
||||
private:
|
||||
static SrsStatistic *_instance;
|
||||
// the id to identify the sever.
|
||||
int64_t _server_id;
|
||||
// key: vhost name, value: vhost object.
|
||||
std::map<std::string, SrsStatisticVhost*> vhosts;
|
||||
// key: stream name, value: stream object.
|
||||
|
@ -79,6 +90,11 @@ public:
|
|||
*/
|
||||
virtual int on_client(int id, SrsRequest* req);
|
||||
public:
|
||||
/**
|
||||
* get the server id, used to identify the server.
|
||||
* for example, when restart, the server id must changed.
|
||||
*/
|
||||
virtual int64_t server_id();
|
||||
/**
|
||||
* dumps the vhosts to sstream in json.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue