Permalink
Browse files

Add signal handling

Remove observer.
  • Loading branch information...
Marc Liyanage
Marc Liyanage committed May 2, 2011
1 parent 51946d6 commit f6fb28f4cde1103ff77fd521e3787e0b8013a792
View
@@ -0,0 +1,11 @@
+Usage:
+
+ notificationlistener
+
+to see all notifications or
+
+ notificationlistener -notificationName <notification name>
+
+to see only one type of notification.
+
+
@@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */
1BA6F5B4136B8A4D00B5DA51 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1BA6F5B3136B8A4D00B5DA51 /* Foundation.framework */; };
1BA6F5B7136B8A4D00B5DA51 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1BA6F5B6136B8A4D00B5DA51 /* main.m */; };
- 1BA6F5BB136B8A4D00B5DA51 /* notificationlistener.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 1BA6F5BA136B8A4D00B5DA51 /* notificationlistener.1 */; };
+ 1BA6F5C3136B8B9000B5DA51 /* DistributedNotificationListener.m in Sources */ = {isa = PBXBuildFile; fileRef = 1BA6F5C2136B8B9000B5DA51 /* DistributedNotificationListener.m */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
@@ -19,7 +19,6 @@
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
- 1BA6F5BB136B8A4D00B5DA51 /* notificationlistener.1 in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 1;
};
@@ -29,8 +28,9 @@
1BA6F5AF136B8A4D00B5DA51 /* notificationlistener */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = notificationlistener; sourceTree = BUILT_PRODUCTS_DIR; };
1BA6F5B3136B8A4D00B5DA51 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1BA6F5B6136B8A4D00B5DA51 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
- 1BA6F5B9136B8A4D00B5DA51 /* notificationlistener-Prefix.pch */ = {isa = PBXFileReference; path = "notificationlistener-Prefix.pch"; sourceTree = "<group>"; };
- 1BA6F5BA136B8A4D00B5DA51 /* notificationlistener.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = notificationlistener.1; sourceTree = "<group>"; };
+ 1BA6F5B9136B8A4D00B5DA51 /* notificationlistener-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "notificationlistener-Prefix.pch"; sourceTree = "<group>"; };
+ 1BA6F5C1136B8B9000B5DA51 /* DistributedNotificationListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DistributedNotificationListener.h; sourceTree = "<group>"; };
+ 1BA6F5C2136B8B9000B5DA51 /* DistributedNotificationListener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DistributedNotificationListener.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -74,7 +74,8 @@
isa = PBXGroup;
children = (
1BA6F5B6136B8A4D00B5DA51 /* main.m */,
- 1BA6F5BA136B8A4D00B5DA51 /* notificationlistener.1 */,
+ 1BA6F5C1136B8B9000B5DA51 /* DistributedNotificationListener.h */,
+ 1BA6F5C2136B8B9000B5DA51 /* DistributedNotificationListener.m */,
1BA6F5B8136B8A4D00B5DA51 /* Supporting Files */,
);
path = notificationlistener;
@@ -139,6 +140,7 @@
buildActionMask = 2147483647;
files = (
1BA6F5B7136B8A4D00B5DA51 /* main.m in Sources */,
+ 1BA6F5C3136B8B9000B5DA51 /* DistributedNotificationListener.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -224,6 +226,7 @@
1BA6F5C0136B8A4D00B5DA51 /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
@@ -0,0 +1,18 @@
+//
+// DistributedNotificationListener.h
+// notificationlistener
+//
+// Created by Marc Liyanage on 4/29/11.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface DistributedNotificationListener : NSObject {
+@private
+ BOOL shouldTerminate;
+}
+
+- (void)run;
+
+@end
@@ -0,0 +1,60 @@
+//
+// DistributedNotificationListener.m
+// notificationlistener
+//
+// Created by Marc Liyanage on 4/29/11.
+//
+
+#import "DistributedNotificationListener.h"
+
+
+@implementation DistributedNotificationListener
+
+- (id)init
+{
+ self = [super init];
+ if (self) {
+ }
+
+ return self;
+}
+
+
+- (void)run
+{
+ shouldTerminate = NO;
+
+ NSString *notificationName = [[NSUserDefaults standardUserDefaults] stringForKey:@"notificationName"];
+ [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(notify:) name:notificationName object:nil];
+
+ dispatch_source_t signalsource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, dispatch_get_main_queue());
+ dispatch_source_set_event_handler(signalsource, ^{shouldTerminate = YES;});
+ dispatch_resume(signalsource);
+ sigignore(SIGINT);
+
+ NSRunLoop *mainRunLoop = [NSRunLoop mainRunLoop];
+ while (!shouldTerminate) {
+ [mainRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
+ }
+
+ dispatch_source_cancel(signalsource);
+ dispatch_release(signalsource);
+
+ [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
+}
+
+
+- (void)notify:(NSNotification *)notification
+{
+ NSLog(@"Notification: %@", notification);
+}
+
+
+- (void)dealloc
+{
+ [super dealloc];
+}
+
+
+
+@end
@@ -3,18 +3,19 @@
// notificationlistener
//
// Created by Marc Liyanage on 4/29/11.
-// Copyright 2011 Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
+#import "DistributedNotificationListener.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- // insert code here...
- NSLog(@"Hello, World!");
+ DistributedNotificationListener *listener = [[DistributedNotificationListener alloc] init];
+ [listener run];
+ [listener release];
[pool drain];
return 0;
@@ -1,79 +0,0 @@
-.\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples.
-.\"See Also:
-.\"man mdoc.samples for a complete listing of options
-.\"man mdoc for the short list of editing options
-.\"/usr/share/misc/mdoc.template
-.Dd 4/29/11 \" DATE
-.Dt notificationlistener 1 \" Program name and manual section number
-.Os Darwin
-.Sh NAME \" Section Header - required - don't modify
-.Nm notificationlistener,
-.\" The following lines are read in generating the apropos(man -k) database. Use only key
-.\" words here as the database is built based on the words here and in the .ND line.
-.Nm Other_name_for_same_program(),
-.Nm Yet another name for the same program.
-.\" Use .Nm macro to designate other names for the documented program.
-.Nd This line parsed for whatis database.
-.Sh SYNOPSIS \" Section Header - required - don't modify
-.Nm
-.Op Fl abcd \" [-abcd]
-.Op Fl a Ar path \" [-a path]
-.Op Ar file \" [file]
-.Op Ar \" [file ...]
-.Ar arg0 \" Underlined argument - use .Ar anywhere to underline
-arg2 ... \" Arguments
-.Sh DESCRIPTION \" Section Header - required - don't modify
-Use the .Nm macro to refer to your program throughout the man page like such:
-.Nm
-Underlining is accomplished with the .Ar macro like this:
-.Ar underlined text .
-.Pp \" Inserts a space
-A list of items with descriptions:
-.Bl -tag -width -indent \" Begins a tagged list
-.It item a \" Each item preceded by .It macro
-Description of item a
-.It item b
-Description of item b
-.El \" Ends the list
-.Pp
-A list of flags and their descriptions:
-.Bl -tag -width -indent \" Differs from above in tag removed
-.It Fl a \"-a flag as a list item
-Description of -a flag
-.It Fl b
-Description of -b flag
-.El \" Ends the list
-.Pp
-.\" .Sh ENVIRONMENT \" May not be needed
-.\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1
-.\" .It Ev ENV_VAR_1
-.\" Description of ENV_VAR_1
-.\" .It Ev ENV_VAR_2
-.\" Description of ENV_VAR_2
-.\" .El
-.Sh FILES \" File used or created by the topic of the man page
-.Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact
-.It Pa /usr/share/file_name
-FILE_1 description
-.It Pa /Users/joeuser/Library/really_long_file_name
-FILE_2 description
-.El \" Ends the list
-.\" .Sh DIAGNOSTICS \" May not be needed
-.\" .Bl -diag
-.\" .It Diagnostic Tag
-.\" Diagnostic informtion here.
-.\" .It Diagnostic Tag
-.\" Diagnostic informtion here.
-.\" .El
-.Sh SEE ALSO
-.\" List links in ascending order by section, alphabetically within a section.
-.\" Please do not reference files that do not exist without filing a bug report
-.Xr a 1 ,
-.Xr b 1 ,
-.Xr c 1 ,
-.Xr a 2 ,
-.Xr b 2 ,
-.Xr a 3 ,
-.Xr b 3
-.\" .Sh BUGS \" Document known, unremedied bugs
-.\" .Sh HISTORY \" Document history if command behaves in a unique manner

0 comments on commit f6fb28f

Please sign in to comment.