core: clean up connectivity code a bit
Remove some unnecessary comments and some unnecessary code. Fix indentation.
This commit is contained in:
@@ -36,25 +36,19 @@ G_DEFINE_TYPE (NMConnectivity, nm_connectivity, G_TYPE_OBJECT)
|
|||||||
#define DEFAULT_RESPONSE "NetworkManager is online" /* NOT LOCALIZED */
|
#define DEFAULT_RESPONSE "NetworkManager is online" /* NOT LOCALIZED */
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
/* used for http requests */
|
|
||||||
SoupSession *soup_session;
|
|
||||||
/* indicates if a connectivity check is currently running */
|
|
||||||
gboolean running;
|
|
||||||
/* the uri to check */
|
|
||||||
char *uri;
|
char *uri;
|
||||||
/* seconds when a check will be repeated */
|
|
||||||
guint interval;
|
|
||||||
/* the expected response for the connectivity check */
|
|
||||||
char *response;
|
char *response;
|
||||||
/* indicates if the last connection check was successful */
|
guint interval;
|
||||||
gboolean connected;
|
|
||||||
/* the source id for the periodic check */
|
SoupSession *soup_session;
|
||||||
|
gboolean running;
|
||||||
guint check_id;
|
guint check_id;
|
||||||
|
|
||||||
|
gboolean connected;
|
||||||
} NMConnectivityPrivate;
|
} NMConnectivityPrivate;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
PROP_0,
|
PROP_0,
|
||||||
PROP_RUNNING,
|
|
||||||
PROP_URI,
|
PROP_URI,
|
||||||
PROP_INTERVAL,
|
PROP_INTERVAL,
|
||||||
PROP_RESPONSE,
|
PROP_RESPONSE,
|
||||||
@@ -92,37 +86,30 @@ nm_connectivity_check_cb (SoupSession *session, SoupMessage *msg, gpointer user_
|
|||||||
{
|
{
|
||||||
NMConnectivity *self = NM_CONNECTIVITY (user_data);
|
NMConnectivity *self = NM_CONNECTIVITY (user_data);
|
||||||
NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
||||||
SoupURI *soup_uri;
|
|
||||||
gboolean connected_new = FALSE;
|
gboolean connected_new = FALSE;
|
||||||
const char *nm_header;
|
const char *nm_header;
|
||||||
char *uri_string;
|
|
||||||
|
|
||||||
soup_uri = soup_message_get_uri (msg);
|
|
||||||
uri_string = soup_uri_to_string (soup_uri, FALSE);
|
|
||||||
|
|
||||||
/* Check headers; if we find the NM-specific one we're done */
|
/* Check headers; if we find the NM-specific one we're done */
|
||||||
nm_header = soup_message_headers_get_one (msg->response_headers, "X-NetworkManager-Status");
|
nm_header = soup_message_headers_get_one (msg->response_headers, "X-NetworkManager-Status");
|
||||||
if (g_strcmp0 (nm_header, "online") == 0) {
|
if (g_strcmp0 (nm_header, "online") == 0) {
|
||||||
nm_log_dbg (LOGD_CORE, "Connectivity check for uri '%s' with Status header successful.", uri_string);
|
nm_log_dbg (LOGD_CORE, "Connectivity check for uri '%s' with Status header successful.", priv->uri);
|
||||||
connected_new = TRUE;
|
connected_new = TRUE;
|
||||||
} else {
|
} else {
|
||||||
/* check response */
|
/* check response */
|
||||||
if (msg->response_body->data && (g_str_has_prefix (msg->response_body->data, priv->response))) {
|
if (msg->response_body->data && (g_str_has_prefix (msg->response_body->data, priv->response))) {
|
||||||
nm_log_dbg (LOGD_CORE, "Connectivity check for uri '%s' with expected response '%s' successful.",
|
nm_log_dbg (LOGD_CORE, "Connectivity check for uri '%s' with expected response '%s' successful.",
|
||||||
uri_string, priv->response);
|
priv->uri, priv->response);
|
||||||
connected_new = TRUE;
|
connected_new = TRUE;
|
||||||
} else {
|
} else {
|
||||||
nm_log_dbg (LOGD_CORE, "Connectivity check for uri '%s' with expected response '%s' failed (status %d).",
|
nm_log_dbg (LOGD_CORE, "Connectivity check for uri '%s' with expected response '%s' failed (status %d).",
|
||||||
uri_string, priv->response, msg->status_code);
|
priv->uri, priv->response, msg->status_code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g_free (uri_string);
|
|
||||||
|
|
||||||
/* update connectivity and emit signal */
|
/* update connectivity and emit signal */
|
||||||
update_connected (self, connected_new);
|
update_connected (self, connected_new);
|
||||||
|
|
||||||
priv->running = FALSE;
|
priv->running = FALSE;
|
||||||
g_object_notify (G_OBJECT (self), NM_CONNECTIVITY_RUNNING);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
@@ -130,16 +117,14 @@ run_check (gpointer user_data)
|
|||||||
{
|
{
|
||||||
NMConnectivity *self = NM_CONNECTIVITY (user_data);
|
NMConnectivity *self = NM_CONNECTIVITY (user_data);
|
||||||
NMConnectivityPrivate *priv;
|
NMConnectivityPrivate *priv;
|
||||||
SoupURI *soup_uri;
|
|
||||||
SoupMessage *msg;
|
SoupMessage *msg;
|
||||||
|
|
||||||
g_return_val_if_fail (NM_IS_CONNECTIVITY (self), FALSE);
|
g_return_val_if_fail (NM_IS_CONNECTIVITY (self), FALSE);
|
||||||
priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
||||||
|
|
||||||
/* check given url async */
|
msg = soup_message_new ("GET", priv->uri);
|
||||||
soup_uri = soup_uri_new (priv->uri);
|
g_return_val_if_fail (msg != NULL, FALSE);
|
||||||
if (soup_uri && SOUP_URI_VALID_FOR_HTTP (soup_uri)) {
|
|
||||||
msg = soup_message_new_from_uri ("GET", soup_uri);
|
|
||||||
soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
|
soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
|
||||||
soup_session_queue_message (priv->soup_session,
|
soup_session_queue_message (priv->soup_session,
|
||||||
msg,
|
msg,
|
||||||
@@ -147,15 +132,8 @@ run_check (gpointer user_data)
|
|||||||
self);
|
self);
|
||||||
|
|
||||||
priv->running = TRUE;
|
priv->running = TRUE;
|
||||||
g_object_notify (G_OBJECT (self), NM_CONNECTIVITY_RUNNING);
|
|
||||||
nm_log_dbg (LOGD_CORE, "Connectivity check with uri '%s' started.", priv->uri);
|
nm_log_dbg (LOGD_CORE, "Connectivity check with uri '%s' started.", priv->uri);
|
||||||
} else
|
return TRUE;
|
||||||
nm_log_err (LOGD_CORE, "Invalid uri '%s' for connectivity check.", priv->uri);
|
|
||||||
|
|
||||||
if (soup_uri)
|
|
||||||
soup_uri_free (soup_uri);
|
|
||||||
|
|
||||||
return TRUE; /* keep firing */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -171,7 +149,6 @@ nm_connectivity_start_check (NMConnectivity *self)
|
|||||||
if (priv->check_id == 0)
|
if (priv->check_id == 0)
|
||||||
priv->check_id = g_timeout_add_seconds (priv->interval, run_check, self);
|
priv->check_id = g_timeout_add_seconds (priv->interval, run_check, self);
|
||||||
|
|
||||||
/* Start an immediate check */
|
|
||||||
if (priv->running == FALSE)
|
if (priv->running == FALSE)
|
||||||
run_check (self);
|
run_check (self);
|
||||||
}
|
}
|
||||||
@@ -211,18 +188,16 @@ nm_connectivity_new (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
sanitize_string_val (const GValue *val)
|
get_non_empty_string_value (const GValue *val)
|
||||||
{
|
{
|
||||||
char *s;
|
const char *s;
|
||||||
|
|
||||||
/* Return NULL if string is NULL or zero-length */
|
s = g_value_get_string (val);
|
||||||
s = g_value_dup_string (val);
|
if (s && s[0])
|
||||||
if (!s || !s[0]) {
|
return g_strdup (s);
|
||||||
g_free (s);
|
else
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
set_property (GObject *object, guint property_id,
|
set_property (GObject *object, guint property_id,
|
||||||
@@ -232,22 +207,26 @@ set_property (GObject *object, guint property_id,
|
|||||||
NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
||||||
|
|
||||||
switch (property_id) {
|
switch (property_id) {
|
||||||
case PROP_RUNNING:
|
|
||||||
priv->running = g_value_get_boolean (value);
|
|
||||||
break;
|
|
||||||
case PROP_URI:
|
case PROP_URI:
|
||||||
g_free (priv->uri);
|
g_free (priv->uri);
|
||||||
priv->uri = sanitize_string_val (value);
|
priv->uri = get_non_empty_string_value (value);
|
||||||
|
|
||||||
|
if (priv->uri) {
|
||||||
|
SoupURI *uri = soup_uri_new (priv->uri);
|
||||||
|
|
||||||
|
if (!uri || !SOUP_URI_VALID_FOR_HTTP (uri)) {
|
||||||
|
nm_log_err (LOGD_CORE, "Invalid uri '%s' for connectivity check.", priv->uri);
|
||||||
|
g_free (priv->uri);
|
||||||
|
priv->uri = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PROP_INTERVAL:
|
case PROP_INTERVAL:
|
||||||
priv->interval = g_value_get_uint (value);
|
priv->interval = g_value_get_uint (value);
|
||||||
break;
|
break;
|
||||||
case PROP_RESPONSE:
|
case PROP_RESPONSE:
|
||||||
g_free (priv->response);
|
g_free (priv->response);
|
||||||
priv->response = sanitize_string_val (value);
|
priv->response = get_non_empty_string_value (value);
|
||||||
break;
|
|
||||||
case PROP_CONNECTED:
|
|
||||||
priv->connected = g_value_get_boolean (value);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||||
@@ -263,9 +242,6 @@ get_property (GObject *object, guint property_id,
|
|||||||
NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self);
|
||||||
|
|
||||||
switch (property_id) {
|
switch (property_id) {
|
||||||
case PROP_RUNNING:
|
|
||||||
g_value_set_boolean (value, priv->running);
|
|
||||||
break;
|
|
||||||
case PROP_URI:
|
case PROP_URI:
|
||||||
g_value_set_string (value, priv->uri);
|
g_value_set_string (value, priv->uri);
|
||||||
break;
|
break;
|
||||||
@@ -302,8 +278,7 @@ dispose (GObject *object)
|
|||||||
|
|
||||||
if (priv->soup_session) {
|
if (priv->soup_session) {
|
||||||
soup_session_abort (priv->soup_session);
|
soup_session_abort (priv->soup_session);
|
||||||
g_object_unref (priv->soup_session);
|
g_clear_object (&priv->soup_session);
|
||||||
priv->soup_session = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (priv->uri);
|
g_free (priv->uri);
|
||||||
@@ -328,14 +303,6 @@ nm_connectivity_class_init (NMConnectivityClass *klass)
|
|||||||
object_class->dispose = dispose;
|
object_class->dispose = dispose;
|
||||||
|
|
||||||
/* properties */
|
/* properties */
|
||||||
g_object_class_install_property
|
|
||||||
(object_class, PROP_RUNNING,
|
|
||||||
g_param_spec_boolean (NM_CONNECTIVITY_RUNNING,
|
|
||||||
"Running",
|
|
||||||
"Connectivity check is running",
|
|
||||||
FALSE,
|
|
||||||
G_PARAM_READABLE));
|
|
||||||
|
|
||||||
g_object_class_install_property
|
g_object_class_install_property
|
||||||
(object_class, PROP_URI,
|
(object_class, PROP_URI,
|
||||||
g_param_spec_string (NM_CONNECTIVITY_URI,
|
g_param_spec_string (NM_CONNECTIVITY_URI,
|
||||||
|
@@ -35,7 +35,6 @@
|
|||||||
#define NM_CONNECTIVITY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_CONNECTIVITY, NMConnectivityClass))
|
#define NM_CONNECTIVITY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_CONNECTIVITY, NMConnectivityClass))
|
||||||
|
|
||||||
/* Properties */
|
/* Properties */
|
||||||
#define NM_CONNECTIVITY_RUNNING "running"
|
|
||||||
#define NM_CONNECTIVITY_URI "uri"
|
#define NM_CONNECTIVITY_URI "uri"
|
||||||
#define NM_CONNECTIVITY_INTERVAL "interval"
|
#define NM_CONNECTIVITY_INTERVAL "interval"
|
||||||
#define NM_CONNECTIVITY_RESPONSE "response"
|
#define NM_CONNECTIVITY_RESPONSE "response"
|
||||||
|
Reference in New Issue
Block a user