Adding client_server NSNotification example

This commit is contained in:
Karmaz95
2024-12-16 15:21:42 +01:00
parent 043c2714f1
commit 3f3d5355b3
3 changed files with 65 additions and 0 deletions

View File

@@ -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

View File

@@ -0,0 +1,19 @@
// client.m
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
if (argc != 2) {
NSLog(@"Usage: %s <message>", 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;
}

View File

@@ -0,0 +1,31 @@
// server.m
#import <Foundation/Foundation.h>
@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;
}