Inter-Process Communication on Mac – Know How part4(Distributed Notifications)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in previous blog  Distributed notifications work as follows :
1. A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria.
2. The notification information is encapsulated in NSNotification objects. Client objects register themselves with the notification center as observers of specific notifications posted by other objects.
3. When an event occurs, an object posts an appropriate notification to the notification center.

Posting a distributed notification is an expensive operation. The notification gets sent to a system wide server that then distributes it to all the processes that have objects registered for distributed notifications. The latency between posting the notification and the notification’s arrival in another process is unbounded. In fact, if too many notifications are being posted and the server’s queue fills up, notifications can be dropped.

Let’s look at a simple code to send and receive distributed notification.

Common to Server and Client

#define NotificationName    @”Notification from ServerApp”
#define NotificationObject  @”com.testnotif.com”

Server

Create a action to send a dictionary in a distributed notification.

– (IBAction)sendNotification:(id)sender
{
NSDictionary* itemTobeSent = [NSDictionary dictionaryWithObjectsAndKeys:@”Notification sent”,@”key”, nil];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:NotificationName object:NotificationObject userInfo:itemTobeSent options:NSNotificationDeliverImmediately];
}

Client

First register for the distributed notification as follows :

– (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification: ) name:NotificationName object:NotificationObject];
}

Write implementation of receivedNotification function

– (void)receivedNotification:(NSNotification*)notification
{
NSLog(@”recieved notification : %@”,[notification.userInfo objectForKey:@”key”]);

//Do something with received dictionary.
}

For more info and detailed documentation see Notification Programming.

 

Written By: HEM DUTT, Sr. Engineer/Tech Lead (Mac OSX development), Mindfire Solutions

Advertisements

About HEM DUTT

Seasoned Mac OS X developer. Expertise in Mac OSX application development. knowledge of MFC and IOS

Posted on November 11, 2014, in Cocoa Application, Cooca, Inter-Process Communication (IPC), Objective-C and tagged , , , , , , , , , . Bookmark the permalink. 2 Comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: