pplx::task<void> cloud_queue::add_message_async(cloud_queue_message& message, std::chrono::seconds time_to_live, std::chrono::seconds initial_visibility_timeout, queue_request_options& options, operation_context context)
    {
        if (time_to_live.count() <= 0LL)
        {
            throw std::invalid_argument(protocol::error_non_positive_time_to_live);
        }

        if (time_to_live.count() > 604800LL)
        {
            throw std::invalid_argument(protocol::error_large_time_to_live);
        }

        if (initial_visibility_timeout.count() < 0LL)
        {
            throw std::invalid_argument(protocol::error_negative_initial_visibility_timeout);
        }

        if (initial_visibility_timeout.count() > 604800LL)
        {
            throw std::invalid_argument(protocol::error_large_initial_visibility_timeout);
        }

        queue_request_options modified_options = get_modified_options(options);
        storage_uri uri = protocol::generate_queue_message_uri(service_client(), *this);

        std::shared_ptr<core::storage_command<void>> command = std::make_shared<core::storage_command<void>>(uri);
        command->set_build_request(std::bind(protocol::add_message, message, time_to_live, initial_visibility_timeout, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        command->set_authentication_handler(service_client().authentication_handler());
        command->set_preprocess_response(std::bind(protocol::preprocess_response_void, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        return core::executor<void>::execute_async(command, modified_options, context);
    }
Beispiel #2
0
bool SocketImpl::Connect(const SocketAddress& address, const std::chrono::seconds& timeout /*= std::chrono::seconds(0)*/)
{
    if (INVALID_SOCKET == m_sockfd)
        Init(AF_INET);
    if (timeout.count() > 0)
        SetBlocking(false);
    bool bResult = false;
    do 
    {
        if (SOCKET_ERROR == connect(m_sockfd, address.GetAddress(), address.GetLength()))
        {
            if (timeout.count() == 0)
                break;
            int err = WSAGetLastError();
            if (WSAEINPROGRESS != err
                && WSAEWOULDBLOCK != err
                && WSAEISCONN != err)
                break;
            if (!Poll(timeout, SELECT_READ | SELECT_WRITE | SELECT_ERROR))
                break;
            if (GetSocketError() != 0)
                break;
        }
        bResult = true;
    } while (0);
    if (timeout.count() > 0)
        SetBlocking(true);
    return bResult;
}
bool wpa_access_point::check_hysteresis(int new_rssi,int hyst_difference_rssi,int minrssi, std::chrono::seconds& transition_time)
{
    lock_guard<mutex> lock(m_locker);
    if(m_bHysteresisMode)
    {
        if(abs(m_rssi - new_rssi) >= hyst_difference_rssi && new_rssi  > minrssi && m_rssi > new_rssi) // patch
        {
            auto current_time = duration_cast<seconds>(system_clock::now().time_since_epoch());
            if(current_time.count() - m_HysteresisStartTime.count() >= transition_time.count())
            {
                cancel_hysteresis();
                cout << "[" << m_HysteresisStartTime.count() << "-" << current_time.count() << "]\n";
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            cancel_hysteresis();
            return false;
        }
    }
    else
    {
        return false;
    }
}
Beispiel #4
0
bool SocketImpl::SetOption(int level, int option, const std::chrono::seconds& value)
{
    struct timeval tv;
    tv.tv_sec = (long)value.count();
    tv.tv_usec = 0L;
    return SetRawOption(level, option, &tv, sizeof(tv));
}
    pplx::task<std::vector<cloud_queue_message>> cloud_queue::get_messages_async(size_t message_count, std::chrono::seconds visibility_timeout, queue_request_options& options, operation_context context)
    {
        if (message_count > 32U)
        {
            throw std::invalid_argument(protocol::error_large_message_count);
        }

        if (visibility_timeout.count() < 0LL)
        {
            throw std::invalid_argument(protocol::error_negative_visibility_timeout);
        }

        if (visibility_timeout.count() > 604800LL)
        {
            throw std::invalid_argument(protocol::error_large_visibility_timeout);
        }

        queue_request_options modified_options = get_modified_options(options);
        storage_uri uri = protocol::generate_queue_message_uri(service_client(), *this);

        std::shared_ptr<core::storage_command<std::vector<cloud_queue_message>>> command = std::make_shared<core::storage_command<std::vector<cloud_queue_message>>>(uri);
        command->set_build_request(std::bind(protocol::get_messages, message_count, visibility_timeout, /* is_peek */ false, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        command->set_authentication_handler(service_client().authentication_handler());
        command->set_preprocess_response(std::bind(protocol::preprocess_response<std::vector<cloud_queue_message>>, std::vector<cloud_queue_message>(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        command->set_postprocess_response([] (const web::http::http_response& response, const request_result&, const core::ostream_descriptor&, operation_context context) -> pplx::task<std::vector<cloud_queue_message>>
        {
            UNREFERENCED_PARAMETER(context);
            protocol::message_reader reader(response.body());
            std::vector<protocol::cloud_message_list_item> queue_items = reader.move_items();

            std::vector<cloud_queue_message> results;
            results.reserve(queue_items.size());

            for (std::vector<protocol::cloud_message_list_item>::iterator it = queue_items.begin(); it != queue_items.end(); ++it)
            {
                cloud_queue_message message(it->move_content(), it->move_id(), it->move_pop_receipt(), it->insertion_time(), it->expiration_time(), it->next_visible_time(), it->dequeue_count());
                results.push_back(message);
            }

            return pplx::task_from_result(results);
        });
        return core::executor<std::vector<cloud_queue_message>>::execute_async(command, modified_options, context);
    }
Beispiel #6
0
 Inactivity(ActivityChecker & checker, std::chrono::seconds timeout, time_t start, uint32_t verbose)
 : inactivity_timeout(std::max<time_t>(timeout.count(), 30))
 , last_activity_time(start)
 , checker(checker)
 , verbose(verbose)
 {
     if (this->verbose & 0x10) {
         LOG(LOG_INFO, "INACTIVITY CONSTRUCTOR");
     }
 }
Beispiel #7
0
    void Socket::SetLinger(bool on, const std::chrono::seconds& seconds)
    {
        struct linger l;

#pragma push_macro("max")
#undef max
        std::chrono::milliseconds::rep maxTimespan = std::numeric_limits<decltype(l.l_linger)>::max();
#pragma pop_macro("max")

        if (seconds.count() > maxTimespan)
        {
            /* throw invalid argument */
        }

        l.l_onoff = on ? 1 : 0;
        l.l_linger = static_cast<decltype(l.l_linger)>(seconds.count());

        SetOption(SOL_SOCKET, SO_LINGER, reinterpret_cast<const std::int8_t *>(&l), sizeof(l));
    }
    web::http::http_request add_message(const cloud_queue& queue, const cloud_queue_message& message, std::chrono::seconds time_to_live, std::chrono::seconds initial_visibility_timeout, web::http::uri_builder uri_builder, const std::chrono::seconds& timeout, operation_context context)
    {
        if (time_to_live.count() >= 0LL && time_to_live.count() != 604800LL)
        {
            uri_builder.append_query(U("messagettl"), time_to_live.count());
        }

        if (initial_visibility_timeout.count() > 0LL)
        {
            uri_builder.append_query(U("visibilitytimeout"), initial_visibility_timeout.count());
        }

        web::http::http_request request = queue_base_request(web::http::methods::POST, uri_builder, timeout, context);

        protocol::message_writer writer;
        std::string content = writer.write(message.content_as_string());
        request.set_body(content);

        return request;
    }
Beispiel #9
0
 KeepAlive(std::chrono::seconds _grace_delay, uint32_t verbose)
     : grace_delay(_grace_delay.count())
     , timeout(0)
     , renew_time(0)
     , wait_answer(false)
     , verbose(verbose)
     , connected(false)
 {
     if (this->verbose & 0x10) {
         LOG(LOG_INFO, "KEEP ALIVE CONSTRUCTOR");
     }
 }
    web::http::http_request base_request(web::http::method method, web::http::uri_builder uri_builder, const std::chrono::seconds& timeout, operation_context context)
    {
        if (timeout.count() > 0)
        {
            uri_builder.append_query(core::make_query_parameter(uri_query_timeout, timeout.count(), /* do_encoding */ false));
        }

        web::http::http_request request(method);
        request.set_request_uri(uri_builder.to_uri());

        web::http::http_headers& headers = request.headers();
        headers.add(web::http::header_names::user_agent, header_value_user_agent);
        headers.add(ms_header_version, header_value_storage_version);

        if (method == web::http::methods::PUT)
        {
            headers.set_content_length(0);
        }

        return request;
    }
    web::http::http_request get_messages(const cloud_queue& queue, size_t message_count, std::chrono::seconds visibility_timeout, bool is_peek, web::http::uri_builder uri_builder, const std::chrono::seconds& timeout, operation_context context)
    {
        if (is_peek)
        {
            uri_builder.append_query(U("peekonly=true"));
        }

        if (message_count > 1U)
        {
            // The service uses the default value 1
            uri_builder.append_query(U("numofmessages"), message_count);
        }

        if (!is_peek && visibility_timeout.count() > 0LL)
        {
            uri_builder.append_query(U("visibilitytimeout"), visibility_timeout.count());
        }

        web::http::http_request request = queue_base_request(web::http::methods::GET, uri_builder, timeout, context);
        return request;
    }
Beispiel #12
0
bool file::can_write(std::chrono::seconds s, std::chrono::nanoseconds n)
{
    timespec time = { static_cast<std::time_t>(s.count()), static_cast<long>(n.count()) };

    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(_M_fd, &fds);

    int count = pselect(_M_fd+1, 0, &fds, 0, &time, nullptr);
    if(count == -1) throw errno_error();

    return count;
}
Beispiel #13
0
 explicit PauseRecord(
     std::chrono::seconds timeout,
     Front & front, MMApi & mm, Inifile & ini)
 : stop_record_inactivity(false)
 , stop_record_time(std::max<time_t>(timeout.count(), 30))
 , last_record_activity_time(0)
 , last_total_received(0)
 , last_total_sent(0)
 , front(front)
 , mm(mm)
 , ini(ini)
 {
 }
    pplx::task<void> cloud_queue::update_message_async(cloud_queue_message& message, std::chrono::seconds visibility_timeout, bool update_content, queue_request_options& options, operation_context context)
    {
        if (message.id().empty())
        {
            throw std::invalid_argument(protocol::error_empty_message_id);
        }

        if (message.pop_receipt().empty())
        {
            throw std::invalid_argument(protocol::error_empty_message_pop_receipt);
        }

        if (visibility_timeout.count() < 0LL)
        {
            throw std::invalid_argument(protocol::error_negative_visibility_timeout);
        }

        if (visibility_timeout.count() > 604800LL)
        {
            throw std::invalid_argument(protocol::error_large_visibility_timeout);
        }

        queue_request_options modified_options = get_modified_options(options);
        storage_uri uri = protocol::generate_queue_message_uri(service_client(), *this, message);

        std::shared_ptr<core::storage_command<void>> command = std::make_shared<core::storage_command<void>>(uri);
        command->set_build_request(std::bind(protocol::update_message, message, visibility_timeout, update_content, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        command->set_authentication_handler(service_client().authentication_handler());
        command->set_preprocess_response([&message] (const web::http::http_response& response, const request_result& result, operation_context context) mutable
        {
            protocol::preprocess_response_void(response, result, context);
            message.set_pop_receipt(protocol::parse_pop_receipt(response));
            message.set_next_visible_time(protocol::parse_next_visible_time(response));
        });
        return core::executor<void>::execute_async(command, modified_options, context);
    }
    web::http::http_request update_message(const cloud_queue& queue, const cloud_queue_message& message, std::chrono::seconds visibility_timeout, bool update_contents, web::http::uri_builder uri_builder, const std::chrono::seconds& timeout, operation_context context)
    {
        uri_builder.append_query(U("popreceipt"), message.pop_receipt());
        uri_builder.append_query(U("visibilitytimeout"), visibility_timeout.count());

        web::http::http_request request = queue_base_request(web::http::methods::PUT, uri_builder, timeout, context);

        if (update_contents)
        {
            protocol::message_writer writer;
            std::string content = writer.write(message.content_as_string());
            request.set_body(content);
        }

        return request;
    }
Beispiel #16
0
bool
AmendmentTableImpl::shouldEnable (std::uint32_t closeTime,
    const AmendmentState& fs)
{
    if (fs.mVetoed || fs.mEnabled || !fs.mSupported || (fs.m_lastMajority != m_lastReport))
        return false;

    if (fs.m_firstMajority == m_firstReport)
    {
        // had a majority when we first started the server, relaxed check
        // WRITEME
    }

    // didn't have a majority when we first started the server, normal check
    return (fs.m_lastMajority - fs.m_firstMajority) > m_majorityTime.count();
}
Beispiel #17
0
void GMainLoopSource::scheduleAfterDelay(const char* name, std::function<bool ()> function, std::chrono::seconds delay, int priority, std::function<void ()> destroyFunction, GMainContext* context)
{
    cancel();

    ASSERT(!m_context.source);
    m_context = {
        adoptGRef(g_timeout_source_new_seconds(delay.count())),
        nullptr, // cancellable
        nullptr, // socketCancellable
        nullptr, // voidCallback
        WTF::move(function),
        nullptr, // socketCallback
        WTF::move(destroyFunction)
    };
    scheduleTimeoutSource(name, reinterpret_cast<GSourceFunc>(boolSourceCallback), priority, context);
}
    web::http::http_request get_messages(size_t message_count, std::chrono::seconds visibility_timeout, bool is_peek, web::http::uri_builder& uri_builder, const std::chrono::seconds& timeout, operation_context context)
    {
        if (is_peek)
        {
            uri_builder.append_query(_XPLATSTR("peekonly=true"));
        }

        if (message_count > 1U)
        {
            // The service uses the default value 1
            uri_builder.append_query(core::make_query_parameter(_XPLATSTR("numofmessages"), message_count, /* do_encoding */ false));
        }

        if (!is_peek && visibility_timeout.count() > 0LL)
        {
            uri_builder.append_query(core::make_query_parameter(_XPLATSTR("visibilitytimeout"), visibility_timeout.count(), /* do_encoding */ false));
        }

        web::http::http_request request = queue_base_request(web::http::methods::GET, uri_builder, timeout, context);
        return request;
    }
Beispiel #19
0
int TakeoverAgent::takeover(std::chrono::seconds timeoutSec) {
#ifdef _MSC_VER
  return -1;
#else
  int ret;

  Logger::Info("takeover: beginning listen socket acquisition");
  uint8_t fd_request[3] = P_VERSION C_FD_REQ;
  uint8_t fd_response[3] = {0,0,0};
  uint32_t response_len = sizeof(fd_response);
  afdt_error_t err = AFDT_ERROR_T_INIT;
  // TODO(dreiss): Make this timeout configurable.
  struct timeval timeout = { timeoutSec.count() , 0 };
  ret = afdt_sync_client(m_transfer_fname.c_str(),
                         fd_request,
                         sizeof(fd_request) - 1,
                         fd_response,
                         &response_len,
                         &m_sock,
                         &timeout,
                         &err);
  if (ret < 0) {
    fd_transfer_error_handler(&err, nullptr);
    errno = EADDRINUSE;
    return -1;
  } else if (m_sock < 0) {
    String resp((const char*)fd_response, response_len, CopyString);
    Logger::Error("AFDT did not receive a file descriptor: response = '%s'",
                  HHVM_FN(addcslashes)(resp, null_string).data());
    errno = EADDRINUSE;
    return -1;
  }

  Logger::Info("takeover: acquired listen socket");
  m_took_over = true;

  return m_sock;
#endif
}
Beispiel #20
0
void BcmPort::updatePktLenHist(
    std::chrono::seconds now,
    stats::ExportedHistogramMapImpl::LockableHistogram* hist,
    const std::vector<opennsl_stat_val_t>& stats) {
  // Get the counter values
  uint64_t counters[10];
  // opennsl_stat_multi_get() unfortunately doesn't correctly const qualify
  // it's stats arguments right now.
  opennsl_stat_val_t* statsArg =
      const_cast<opennsl_stat_val_t*>(&stats.front());
  auto ret = opennsl_stat_multi_get(unit_, port_,
                                stats.size(), statsArg, counters);
  if (OPENNSL_FAILURE(ret)) {
    LOG(ERROR) << "Failed to get packet length stats for port " << port_
               << " :" << opennsl_errmsg(ret);
    return;
  }

  // Update the histogram
  auto guard = hist->makeLockGuard();
  for (int idx = 0; idx < stats.size(); ++idx) {
    hist->addValueLocked(guard, now.count(), idx, counters[idx]);
  }
}
fcppt::string
fruitlib::human_readable_time_difference(
	std::chrono::seconds const &_seconds)
{
	typedef
	std::chrono::seconds::rep
	arithmetic_type;

	arithmetic_type const seconds =
		_seconds.count();

	arithmetic_type const minutes =
		seconds / 60;

	if(minutes < 2)
		return
			FCPPT_TEXT("Just now");

	if(minutes < 45)
		return
			fcppt::insert_to_fcppt_string(
				minutes)+
			FCPPT_TEXT(" minutes ago");

	arithmetic_type const hours =
		minutes / 60;

	if(hours < 2)
		return
			FCPPT_TEXT("An hour ago");

	if(hours < 24)
		return
			fcppt::insert_to_fcppt_string(
				hours)+
			FCPPT_TEXT(" hours ago");

	arithmetic_type const days =
		hours / 24;

	if(days < 2)
		return
			FCPPT_TEXT("A day ago");

	if(days < 7)
		return
			fcppt::insert_to_fcppt_string(
				days)+
			FCPPT_TEXT(" days ago");

	arithmetic_type const weeks =
		days / 7;

	if(weeks < 2)
		return
			FCPPT_TEXT("A week ago");

	if(weeks < 4)
		return
			fcppt::insert_to_fcppt_string(
				weeks)+
			FCPPT_TEXT(" weeks ago");

	arithmetic_type const months =
		weeks / 4;

	if(months < 2)
		return
			FCPPT_TEXT("A month ago");

	if(months < 12)
		return
			fcppt::insert_to_fcppt_string(
				months)+
			FCPPT_TEXT(" months ago");

	arithmetic_type const years =
		months / 12;

	if(years < 2)
		return
			FCPPT_TEXT("A year ago");

	return
		fcppt::insert_to_fcppt_string(
			years)+
		FCPPT_TEXT(" years ago");
}
    web::http::http_request update_message(const cloud_queue_message& message, std::chrono::seconds visibility_timeout, bool update_contents, web::http::uri_builder& uri_builder, const std::chrono::seconds& timeout, operation_context context)
    {
        uri_builder.append_query(core::make_query_parameter(_XPLATSTR("popreceipt"), message.pop_receipt()));
        uri_builder.append_query(core::make_query_parameter(_XPLATSTR("visibilitytimeout"), visibility_timeout.count(), /* do_encoding */ false));

        web::http::http_request request = queue_base_request(web::http::methods::PUT, uri_builder, timeout, context);

        if (update_contents)
        {
            protocol::message_writer writer;
            std::string content = writer.write(message);
            request.set_body(content);
        }

        return request;
    }
Beispiel #23
0
 /// Constructor from std::chrono::seconds
 explicit seconds_field(std::chrono::seconds const& s)
     : count_(s.count()) {
 }
void ViewGestureController::SnapshotRemovalTracker::startWatchdog(std::chrono::seconds duration)
{
    log(String::format("(re)started watchdog timer for %lld seconds", duration.count()));
    m_watchdogTimer.startOneShot(duration.count());
}
Beispiel #25
0
void SSHConnection::set_timeouts(const std::chrono::seconds &sec)
{
    auto count = sec.count();
    ssh_options_set(session.get(), SSH_OPTIONS_TIMEOUT, &count);
}
void HttpRequest::setTimeout(const std::chrono::seconds& timeout)
{
    if (timeout >= timeout.zero()) {
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout.count());
    }
}