|
@@ -10,6 +10,8 @@
|
|
|
#include "app/config/config.h"
|
|
|
#include "feature/hibernate/hibernate.h"
|
|
|
|
|
|
+#include "app/config/or_state_st.h"
|
|
|
+
|
|
|
/** Return true iff our network is in some sense disabled or shutting down:
|
|
|
* either we're hibernating, entering hibernation, or the network is turned
|
|
|
* off with DisableNetwork. */
|
|
@@ -31,6 +33,10 @@ net_is_completely_disabled(void)
|
|
|
/**
|
|
|
* The time at which we've last seen "user activity" -- that is, any activity
|
|
|
* that should keep us as a participant on the network.
|
|
|
+ *
|
|
|
+ * This is not actually the true time. We will adjust this forward if
|
|
|
+ * our clock jumps, or if Tor is shut down for a while, so that the time
|
|
|
+ * since our last activity remains as it was before the jump or shutdown.
|
|
|
*/
|
|
|
static time_t last_user_activity_seen = 0;
|
|
|
|
|
@@ -99,3 +105,38 @@ is_participating_on_network(void)
|
|
|
{
|
|
|
return participating_on_network;
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Update 'state' with the last time at which we were active on the network.
|
|
|
+ **/
|
|
|
+void
|
|
|
+netstatus_flush_to_state(or_state_t *state, time_t now)
|
|
|
+{
|
|
|
+ state->Dormant = ! participating_on_network;
|
|
|
+ if (participating_on_network) {
|
|
|
+ time_t sec_since_activity = MAX(0, now - last_user_activity_seen);
|
|
|
+ state->MinutesSinceUserActivity = (int)(sec_since_activity / 60);
|
|
|
+ } else {
|
|
|
+ state->MinutesSinceUserActivity = 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Update our current view of network participation from an or_state_t object.
|
|
|
+ **/
|
|
|
+void
|
|
|
+netstatus_load_from_state(const or_state_t *state, time_t now)
|
|
|
+{
|
|
|
+ time_t last_activity;
|
|
|
+ if (state->Dormant == -1) { // Initial setup.
|
|
|
+ last_activity = now;
|
|
|
+ participating_on_network = true;
|
|
|
+ } else if (state->Dormant) {
|
|
|
+ last_activity = 0;
|
|
|
+ participating_on_network = false;
|
|
|
+ } else {
|
|
|
+ last_activity = now - 60 * state->MinutesSinceUserActivity;
|
|
|
+ participating_on_network = true;
|
|
|
+ }
|
|
|
+ reset_user_activity(last_activity);
|
|
|
+}
|