-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSiteManager.swift
64 lines (43 loc) · 2.1 KB
/
SiteManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// SiteManager.swift
// NoMAD
//
// Created by Joel Rennich on 9/11/17.
// Copyright © 2018 Orchard & Grove Inc. All rights reserved.
//
import Foundation
import SystemConfiguration
//import NoMADPRIVATE
// singleton for the class
let siteManager = SiteManager()
var updatePending = false
var updateTimer: Timer? = nil
// simple class to use as a global site manager
class SiteManager {
// variables
var sites = [String:[NoMADLDAPServer]]()
// this seems silly to set a notification to notify internally to clearSites... but here goes
let changed: SCDynamicStoreCallBack = { dynamicStore, _, _ in
// TODO: throttle too many lookups too quickly
print("Network change")
let updateNotification = Notification(name: Notification.Name(rawValue: "menu.nomad.NoMAD-ADAuth.updateNow"))
NotificationQueue.default.enqueue(updateNotification, postingStyle: .now)
}
func checkNetwork() {
var dynamicContext = SCDynamicStoreContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
let dcAddress = withUnsafeMutablePointer(to: &dynamicContext, {UnsafeMutablePointer<SCDynamicStoreContext>($0)})
if let dynamicStore = SCDynamicStoreCreate(kCFAllocatorDefault, "menu.nomad.NoMAD.networknotification" as CFString, changed, dcAddress) {
let keysArray = ["State:/Network/Global/IPv4" as CFString, "State:/Network/Global/IPv6"] as CFArray
SCDynamicStoreSetNotificationKeys(dynamicStore, nil, keysArray)
let loop = SCDynamicStoreCreateRunLoopSource(kCFAllocatorDefault, dynamicStore, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, .defaultMode)
}
// register for notifications
NotificationCenter.default.addObserver(self, selector: #selector(clearSites), name: NSNotification.Name(rawValue: "menu.nomad.NoMAD-ADAuth.updateNow"), object: nil)
}
@objc func clearSites() {
// removes all sites
sites.removeAll()
}
// listen for network changes
}