1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_heartbeat.cpp

92 lines
2.4 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2021 Winlin
//
// SPDX-License-Identifier: MIT
//
2014-05-19 07:45:04 +00:00
#include <srs_app_heartbeat.hpp>
2014-05-19 09:39:01 +00:00
#include <sstream>
using namespace std;
2014-05-19 07:45:04 +00:00
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
2014-05-19 09:39:01 +00:00
#include <srs_app_config.hpp>
#include <srs_app_http_client.hpp>
2015-08-21 08:20:19 +00:00
#include <srs_protocol_json.hpp>
2014-05-19 09:39:01 +00:00
#include <srs_app_utility.hpp>
2015-03-06 03:51:20 +00:00
#include <srs_core_autofree.hpp>
2015-05-22 14:34:03 +00:00
#include <srs_app_http_conn.hpp>
2015-09-22 01:05:21 +00:00
#include <srs_protocol_amf0.hpp>
2014-05-19 09:39:01 +00:00
SrsHttpHeartbeat::SrsHttpHeartbeat()
{
}
SrsHttpHeartbeat::~SrsHttpHeartbeat()
{
}
void SrsHttpHeartbeat::heartbeat()
{
2018-01-01 11:39:57 +00:00
srs_error_t err = do_heartbeat();
if (err != srs_success) {
srs_warn("heartbeat err=%s", srs_error_desc(err).c_str());
}
srs_freep(err);
return;
}
srs_error_t SrsHttpHeartbeat::do_heartbeat()
{
srs_error_t err = srs_success;
2014-05-19 09:39:01 +00:00
std::string url = _srs_config->get_heartbeat_url();
SrsHttpUri uri;
2018-01-01 11:39:57 +00:00
if ((err = uri.initialize(url)) != srs_success) {
return srs_error_wrap(err, "http uri parse hartbeart url failed. url=%s", url.c_str());
2014-05-19 09:39:01 +00:00
}
2017-03-25 09:21:39 +00:00
SrsIPAddress* ip = NULL;
2014-05-19 09:39:01 +00:00
std::string device_id = _srs_config->get_heartbeat_device_id();
vector<SrsIPAddress*>& ips = srs_get_local_ips();
2014-05-19 09:39:01 +00:00
if (!ips.empty()) {
2014-07-27 10:39:20 +00:00
ip = ips[_srs_config->get_stats_network() % (int)ips.size()];
2014-05-19 09:39:01 +00:00
}
2015-09-19 04:27:31 +00:00
SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj);
2015-08-28 07:11:11 +00:00
2015-09-19 04:27:31 +00:00
obj->set("device_id", SrsJsonAny::str(device_id.c_str()));
obj->set("ip", SrsJsonAny::str(ip->ip.c_str()));
2015-08-28 07:11:11 +00:00
if (_srs_config->get_heartbeat_summaries()) {
2015-09-19 04:27:31 +00:00
SrsJsonObject* summaries = SrsJsonAny::object();
2015-08-28 07:11:11 +00:00
obj->set("summaries", summaries);
srs_api_dump_summaries(summaries);
}
2014-05-19 09:39:01 +00:00
SrsHttpClient http;
if ((err = http.initialize(uri.get_schema(), uri.get_host(), uri.get_port())) != srs_success) {
2018-01-01 11:39:57 +00:00
return srs_error_wrap(err, "init uri=%s", uri.get_url().c_str());
}
2015-09-19 05:37:56 +00:00
std::string req = obj->dumps();
ISrsHttpMessage* msg = NULL;
if ((err = http.post(uri.get_path(), req, &msg)) != srs_success) {
2018-01-01 11:39:57 +00:00
return srs_error_wrap(err, "http post hartbeart uri failed. url=%s, request=%s", url.c_str(), req.c_str());
2014-05-19 09:39:01 +00:00
}
SrsAutoFree(ISrsHttpMessage, msg);
2015-03-06 03:51:20 +00:00
std::string res;
2018-01-01 11:39:57 +00:00
if ((err = msg->body_read_all(res)) != srs_success) {
return srs_error_wrap(err, "read body");
2015-03-06 03:51:20 +00:00
}
2014-05-19 09:39:01 +00:00
2018-01-01 11:39:57 +00:00
return err;
2014-05-19 09:39:01 +00:00
}
2014-05-19 07:45:04 +00:00