-
Notifications
You must be signed in to change notification settings - Fork 2
/
SCWatchdog.m
125 lines (103 loc) · 3.99 KB
/
SCWatchdog.m
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//---------------------------------------------------------------------------------------
// SCWatchdog.m created by znek on Fri Jul 30 2004
// @(#)$Id: SCWatchdog.m,v 1.1 2007-08-01 08:20:02 znek Exp $
//
// Copyright (c) 2004 by Mulle Kybernetik. All rights reserved.
//
// Permission to use, copy, modify and distribute this software and its documentation
// is hereby granted, provided that both the copyright notice and this permission
// notice appear in all copies of the software, derivative works or modified versions,
// and any portions thereof, and that both notices appear in supporting documentation,
// and that credit is given to Mulle Kybernetik in all documents and publicity
// pertaining to direct or indirect use of this code or its derivatives.
//
// THIS IS EXPERIMENTAL SOFTWARE AND IT IS KNOWN TO HAVE BUGS, SOME OF WHICH MAY HAVE
// SERIOUS CONSEQUENCES. THE COPYRIGHT HOLDER ALLOWS FREE USE OF THIS SOFTWARE IN ITS
// "AS IS" CONDITION. THE COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY
// DAMAGES WHATSOEVER RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE
// OR OF ANY DERIVATIVE WORK.
//---------------------------------------------------------------------------------------
#include "SCWatchdog.h"
#import <SystemConfiguration/SystemConfiguration.h>
struct SCWatcherExtraInfoStructure {
id object;
SEL selector;
};
void SysConfCallBackFunction(SCDynamicStoreRef store,
CFArrayRef changedKeys, void *info)
{
struct SCWatcherExtraInfoStructure *extra;
extra = (struct SCWatcherExtraInfoStructure *)info;
[extra->object performSelector:extra->selector
withObject:(NSArray *)changedKeys];
}
@interface SCWatchdog (Private)
- (void)setup;
@end
@implementation SCWatchdog
NSString *SCWatchdogKeysDidChangeNotification = @"SCWatchdogKeysDidChange";
NSString *SCWatchdogChangedKeysKey = @"SCWatchdogChangedKeys";
- (id)init {
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
struct SCWatcherExtraInfoStructure *info;
SCDynamicStoreContext ctx = { 0, NULL, NULL, NULL, NULL };
SCDynamicStoreRef comRef;
CFRunLoopSourceRef scRunLoopSrc;
CFRunLoopRef ourCFRunLoop;
Boolean status;
NSMutableArray *keys;
info = malloc(sizeof(struct SCWatcherExtraInfoStructure));
if (info == NULL) {
NSLog(@"Setup ExtraInfo Failure");
return;
}
info->object = self;
info->selector = @selector(keysDidChange:);
ctx.info = (void *)info;
comRef = SCDynamicStoreCreate(NULL,
(CFStringRef)@"SCWatchdog",
SysConfCallBackFunction,
&ctx);
if (comRef == NULL) {
NSLog(@"Setup DSCM Failure");
return;
}
keys = [NSMutableArray arrayWithCapacity:1];
[keys addObject:(NSString *)SCDynamicStoreKeyCreate(NULL,
(CFStringRef)@"State:/Network/Global/IPv4")];
status = SCDynamicStoreSetNotificationKeys(comRef, (CFArrayRef)keys, NULL);
if (status == FALSE) {
CFRelease(comRef);
NSLog(@"!! Setup StoreNotification Failure");
return;
}
scRunLoopSrc = SCDynamicStoreCreateRunLoopSource(NULL, comRef, (CFIndex)0);
if (scRunLoopSrc == NULL) {
CFRelease(comRef);
NSLog(@"!! Setup RunLoop Failure");
return;
}
ourCFRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
CFRunLoopAddSource(ourCFRunLoop,
scRunLoopSrc,
kCFRunLoopDefaultMode);
}
/* SC notifications */
- (void)keysDidChange:(NSArray *)_changedKeys {
NSNotificationCenter *nc;
NSDictionary *ui;
ui = [NSDictionary dictionaryWithObjectsAndKeys:_changedKeys,
SCWatchdogChangedKeysKey,
nil];
nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:SCWatchdogKeysDidChangeNotification
object:self
userInfo:ui];
}
@end