Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/michaelnisi/ola
Check reachability of host
https://github.com/michaelnisi/ola
Last synced: about 1 month ago
JSON representation
Check reachability of host
- Host: GitHub
- URL: https://github.com/michaelnisi/ola
- Owner: michaelnisi
- License: mit
- Created: 2014-12-28T19:03:25.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2020-11-22T09:55:46.000Z (about 4 years ago)
- Last Synced: 2024-05-02T05:35:42.191Z (8 months ago)
- Language: Swift
- Homepage:
- Size: 125 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Ola
The Ola Swift package lets you check network reachability of a named host. You can set a callback to run when the reachability of the host has changed. Ola is a simple Swift wrapper around some of Apple’s [System Configuration](https://developer.apple.com/reference/SystemConfiguration) APIs, making them easier to use.
## Example
```swift
import Foundation
import os.loglet host = "1.1.1.1"
var probe = Ola(host: host, log: .default)probe?.activate { status in
print("host status: (\(host), \(String(describing: status)))")
}sleep(10)
probe?.invalidate()
probe = nilprint("OK")
```## Types
```swift
enum OlaStatus: Int
````OlaStatus` eumerates three boiled down host states, derived from [SCNetworkReachabilityFlags](https://developer.apple.com/documentation/systemconfiguration/scnetworkreachabilityflags).
- `unknown`
- `reachable`
- `cellular````swift
class Ola: Reaching
````Ola` is the main object of this module, it implements the tiny `Reaching` API:
```swift
protocol Reaching {
func reach() -> OlaStatus
func reach(statusBlock: @escaping (OlaStatus) -> Void)
func activate(installing callback: @escaping (OlaStatus) -> Void) -> Bool
func invalidate()
}
```## Exports
### Creating a Probe
Each `Ola` object is dedicated to monitoring a specific host.
```swift
init?(host: String, log: OSLog?)
```- `host` The name of the host to monitor.
### Checking Host Reachability
The common use case is to synchronously—not on the main thread though [QA1693](https://developer.apple.com/library/content/qa/qa1693/_index.html)—check if a given host is reachable.
```swift
func reach() -> OlaStatus
```Returns the reachability of the host: unknown, reachable, or cellular.
```swift
func reach(statusBlock: @escaping (OlaStatus) -> Void)
```Same as `reach()`, but non-blocking, executing on a system-provided global concurrent dispatch queues.
### Monitoring Host
A less common use case is getting notified, when the state of a given host has changed. For example, to reason if it’s appropiate to issue a request.
```swift
func activate(installing callback: @escaping (OlaStatus) -> Void) -> Bool
```Returns `true` if installing the `callback` has been successful.
```swift
func invalidate()
```Invalidates the probe removing the callback.
## Install
📦 Add `https://github.com/michaelnisi/ola` to your package manifest.
## License
[MIT](https://raw.github.com/michaelnisi/ola/master/LICENSE)