diff options
Diffstat (limited to 'overrides/endless_private/connection_test.js')
-rw-r--r-- | overrides/endless_private/connection_test.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/overrides/endless_private/connection_test.js b/overrides/endless_private/connection_test.js new file mode 100644 index 0000000..24a2a3c --- /dev/null +++ b/overrides/endless_private/connection_test.js @@ -0,0 +1,42 @@ +const Gio = imports.gi.Gio; + +// Performs a connection test by first looking for an available connection, and +// performing a ping test with address parameters specified by hostname, scheme, +// and port. The test is performed asynchronously. +// successCallback, failureCallback, and errorCallback are optional parameters +// and are called in their relevent outcome cases. If the ping test completed, +// the userData will be passed to the callbacks. Additionally, the errorCallback +// will recieve the error as the first argument. +function doConnectionTestAsync(hostname, scheme, port, connectionSuccessCallback, + connectionFailureCallback, + errorCallback) { + let network_monitor = Gio.NetworkMonitor.get_default(); + let network_address = new Gio.NetworkAddress({ + 'hostname': hostname, + 'scheme': scheme, + 'port': port + }); + + if (network_monitor.get_network_available()) { + network_monitor.can_reach_async(network_address, null, + function(userData, asyncResult) { + try { + let ping_success = network_monitor. + can_reach_finish(asyncResult, null); + if (ping_success && connectionSuccessCallback) { + connectionSuccessCallback(userData); + } else if (!ping_success && connectionFailureCallback) { + connectionFailureCallback(userData); + } + } catch (err) { + if (err instanceof Gio.ResolverError) { + connectionFailureCallback(userData); + } else if (errorCallback) { + errorCallback(err, userData); + } + } + }); + } else if (connectionFailureCallback) { + connectionFailureCallback(); + } +} |