2014-05-19 07:45:04 +00:00
|
|
|
/*
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2015-04-29 09:38:23 +00:00
|
|
|
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
|
2014-05-19 07:45:04 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <srs_app_heartbeat.hpp>
|
|
|
|
|
2015-05-27 02:23:40 +00:00
|
|
|
#ifdef SRS_AUTO_HTTP_CORE
|
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>
|
2014-05-19 09:39:01 +00:00
|
|
|
|
|
|
|
SrsHttpHeartbeat::SrsHttpHeartbeat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpHeartbeat::~SrsHttpHeartbeat()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsHttpHeartbeat::heartbeat()
|
|
|
|
{
|
|
|
|
int ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
std::string url = _srs_config->get_heartbeat_url();
|
|
|
|
|
|
|
|
SrsHttpUri uri;
|
|
|
|
if ((ret = uri.initialize(url)) != ERROR_SUCCESS) {
|
|
|
|
srs_error("http uri parse hartbeart url failed. url=%s, ret=%d", url.c_str(), ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ip = "";
|
|
|
|
std::string device_id = _srs_config->get_heartbeat_device_id();
|
|
|
|
|
|
|
|
vector<string>& ips = srs_get_local_ipv4_ips();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream ss;
|
2015-03-21 02:25:03 +00:00
|
|
|
ss << SRS_JOBJECT_START
|
|
|
|
<< SRS_JFIELD_STR("device_id", device_id) << SRS_JFIELD_CONT
|
|
|
|
<< SRS_JFIELD_STR("ip", ip);
|
2014-06-23 07:44:49 +00:00
|
|
|
if (_srs_config->get_heartbeat_summaries()) {
|
2015-03-21 02:25:03 +00:00
|
|
|
ss << SRS_JFIELD_CONT << SRS_JFIELD_ORG("summaries", "");
|
2014-06-23 07:44:49 +00:00
|
|
|
srs_api_dump_summaries(ss);
|
|
|
|
}
|
2015-03-21 02:25:03 +00:00
|
|
|
ss << SRS_JOBJECT_END;
|
2014-05-19 09:39:01 +00:00
|
|
|
|
2015-03-06 04:07:12 +00:00
|
|
|
std::string req = ss.str();
|
|
|
|
|
2014-05-19 09:39:01 +00:00
|
|
|
SrsHttpClient http;
|
2015-03-06 04:07:12 +00:00
|
|
|
if ((ret = http.initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:24:05 +00:00
|
|
|
ISrsHttpMessage* msg = NULL;
|
2015-03-06 04:07:12 +00:00
|
|
|
if ((ret = http.post(uri.get_path(), req, &msg)) != ERROR_SUCCESS) {
|
2015-07-10 03:50:35 +00:00
|
|
|
srs_info("http post hartbeart uri failed. url=%s, request=%s, ret=%d",
|
|
|
|
url.c_str(), req.c_str(), ret);
|
2014-05-19 09:39:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-22 14:24:05 +00:00
|
|
|
SrsAutoFree(ISrsHttpMessage, msg);
|
2015-03-06 03:51:20 +00:00
|
|
|
|
|
|
|
std::string res;
|
|
|
|
if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-19 09:39:01 +00:00
|
|
|
|
2015-07-10 03:50:35 +00:00
|
|
|
srs_info("http hook hartbeart success. url=%s, request=%s, response=%s, ret=%d",
|
|
|
|
url.c_str(), req.c_str(), res.c_str(), ret);
|
2014-05-19 09:39:01 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2014-05-19 07:45:04 +00:00
|
|
|
|
2014-05-19 09:39:01 +00:00
|
|
|
#endif
|
2014-08-02 14:18:39 +00:00
|
|
|
|