From 3f3d5355b3e6527afbc9fd39c110df13f172e843 Mon Sep 17 00:00:00 2001 From: Karmaz95 Date: Mon, 16 Dec 2024 15:21:42 +0100 Subject: [PATCH] Adding client_server NSNotification example --- .../client_server_NSNotification/Makefile | 15 +++++++++ .../client_server_NSNotification/client.m | 19 ++++++++++++ .../client_server_NSNotification/server.m | 31 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 X. NU/custom/mach_ipc/client_server_NSNotification/Makefile create mode 100644 X. NU/custom/mach_ipc/client_server_NSNotification/client.m create mode 100644 X. NU/custom/mach_ipc/client_server_NSNotification/server.m diff --git a/X. NU/custom/mach_ipc/client_server_NSNotification/Makefile b/X. NU/custom/mach_ipc/client_server_NSNotification/Makefile new file mode 100644 index 0000000..72de9e1 --- /dev/null +++ b/X. NU/custom/mach_ipc/client_server_NSNotification/Makefile @@ -0,0 +1,15 @@ +CC = clang +CFLAGS = -framework Foundation + +all: client server + +client: client.m + $(CC) $(CFLAGS) client.m -o client + +server: server.m + $(CC) $(CFLAGS) server.m -o server + +clean: + rm -f client server *.o + +.PHONY: all clean \ No newline at end of file diff --git a/X. NU/custom/mach_ipc/client_server_NSNotification/client.m b/X. NU/custom/mach_ipc/client_server_NSNotification/client.m new file mode 100644 index 0000000..4410cb0 --- /dev/null +++ b/X. NU/custom/mach_ipc/client_server_NSNotification/client.m @@ -0,0 +1,19 @@ +// client.m +#import + +int main(int argc, char *argv[]) { + @autoreleasepool { + if (argc != 2) { + NSLog(@"Usage: %s ", argv[0]); + return 1; + } + + NSString *message = [NSString stringWithUTF8String:argv[1]]; + [[NSDistributedNotificationCenter defaultCenter] + postNotificationName:@"com.crimson.message_service" + object:nil + userInfo:@{@"message": message} + deliverImmediately:YES]; + } + return 0; +} \ No newline at end of file diff --git a/X. NU/custom/mach_ipc/client_server_NSNotification/server.m b/X. NU/custom/mach_ipc/client_server_NSNotification/server.m new file mode 100644 index 0000000..20ff082 --- /dev/null +++ b/X. NU/custom/mach_ipc/client_server_NSNotification/server.m @@ -0,0 +1,31 @@ +// server.m +#import + +@interface MessageServer : NSObject +- (void)handleMessage:(NSNotification *)notification; +@end + +@implementation MessageServer +- (id)init { + if (self = [super init]) { + [[NSDistributedNotificationCenter defaultCenter] + addObserver:self + selector:@selector(handleMessage:) + name:@"com.crimson.message_service" + object:nil]; + } + return self; +} + +- (void)handleMessage:(NSNotification *)notification { + NSLog(@"Received: %@", notification.userInfo[@"message"]); +} +@end + +int main() { + @autoreleasepool { + MessageServer *server = [[MessageServer alloc] init]; + [[NSRunLoop currentRunLoop] run]; + } + return 0; +} \ No newline at end of file