/** \brief convert the object to a string
 */
std::string	bt_tracker_reply_t::to_string()				const throw()
{
	std::ostringstream	oss;
	// handle the null case
	if( is_null() )	return "null";
	// build the string to return
	if( !failure_reason().empty() ){
		oss        << "failure_reason="<< failure_reason();
	}else{
		oss        << "request_period="	<< request_period();
		oss << " " << "nb_seeder="	<< nb_seeder();
		oss << " " << "nb_leecher="	<< nb_leecher();
		oss << " " << "peer_arr="	<< peer_arr();
	}
	// return the just built string
	return oss.str();
}
/** \brief return a bencoded std::string of the bt_tracker_reply_t
 * 
 * - the bt_tracker_request_t is used to determine the options to build the request
 *   - compact or nopeerid
 */
datum_t	bt_tracker_reply_t::to_bencode(const bt_tracker_request_t &request)	const throw()
{
	std::ostringstream	oss;
	dvar_t			dvar	= dvar_map_t();
	// sanity check - this bt_tracker_reply_t MUST not be null
	DBG_ASSERT( this->is_null() == false );
	// if this bt_tracker_reply_t failed
	if( !failure_reason().empty() ){
		dvar.map().insert("failure reason", dvar_str_t(failure_reason()));
	}else{
		dvar.map().insert("interval"	, dvar_int_t(request_period().to_sec_32bit()));
		dvar.map().insert("complete"	, dvar_int_t(nb_seeder()));
		dvar.map().insert("incomplete"	, dvar_int_t(nb_leecher()));
		dvar.map().insert("peers"	, peer_arr_to_dvar(request));
	}
	
	// bencode the just built dvar_t and return it
	return datum_t( bencode_t::from_dvar(dvar) );
}
Exemplo n.º 3
0
void
explain_failure(RECDB *recdb, int code)
{
    static char msg[1024];
    snprintf(msg, sizeof(msg), "%s (got '%c') at %s line %d column %d.",
             failure_reason(code), code & 255,
             recdb->source, recdb->ctx.line, recdb->ctx.col);
    if (MAIN_LOG == NULL) {
        fputs(msg, stderr);
        fputc('\n', stderr);
        fflush(stderr);
    } else
        log_module(MAIN_LOG, LOG_ERROR, "%s", msg);
}
Exemplo n.º 4
0
const char *
parse_record(const char *text, char **pname, struct record_data **prd)
{
    RECDB recdb;
    int res;
    *pname = NULL;
    *prd = NULL;
    recdb.source = "<user-supplied text>";
    recdb.f = NULL;
    recdb.s = strdup(text);
    recdb.length = strlen(text);
    recdb.pos = 0;
    recdb.type = RECDB_STRING;
    recdb.ctx.line = recdb.ctx.col = 1;
    if ((res = setjmp(recdb.env)) == 0) {
        parse_record_int(&recdb, pname, prd);
        return 0;
    } else {
        free(*pname);
        free(*prd);
        return failure_reason(res);
    }
}
/** \brief Return true if the object is to be considered null, false otherwise
 */
bool	bt_tracker_reply_t::is_null()	const throw()
{
	if( request_period().is_null() && failure_reason().empty() )	return true;
	// if this point is reached, the object is NOT null
	return false;
}