Alexglvr
Alexglvr

Reputation: 437

Need help implementing interprocess communication in cocoa

I developed a cocoa application and I now would like to run several instance of it (locally). But all the running application instances have to share some resources.

Therefore, I thought making a "server" type third app that could deal a token and share the mandatory resources.

I then search a way to do that, and find the "Distributed Objects" solution.

I then started implementing it using several examples found on the internet, but always have warnings or error regarding deprecation or ATC…

Could somebody give me an up to date example on how to implement this please?

EDIT:

What I need to do exactly is:

Thank you for your help,

Upvotes: 3

Views: 332

Answers (1)

Joshua Nozzi
Joshua Nozzi

Reputation: 61228

You probably want to use XPC Services rather than Distributed Objects. XPC is newer and Sandbox friendly (critical if you want to distribute through the App Store). The Daemons and Services Programming Guide gives you a conceptual overview of XPC and then some.

You'll want to decide how best to design your application, however, since you know best about your requirements. A look at the Designing Daemons and Services section for an overview of possible routes and best practices for overall design. If your app runs entirely in user space (is not "for all users on the system"), you can go with a "login item" as your server application if you want to provide UI for starting and managing tasks.

The tasks themselves will be instances of an XPC Service. If my assumptions above are correct, you'll probably want to use the NSXPCConnection API (the highest level XPC API) for your remote procedure calls between the login item app and its XPC service instances. You'll create and hold onto an NSXPCInterface instance for your app, using the protocol you develop. For each task instance you run, you'll use the interface to establish an NSXPCConnection (each of which you'll also hold on to until it's done).

The service's listener instance will talk to the service's delegate object (of your design) to decide whether to accept a connection and how to respond to your interface protocol. Once you've established the connection (there are a few steps outlined in the links above), you can start sending it protocol-defined messages through your connection instance's -remoteObjectProxy (ex: [[myConnection remoteObjectProxy] makeARandomCatMemeWithImageAtURL:someURL];). The service will message your application back with some reply (by calling methods of the app's -exportedObject, provided via its connection).

The details and approach change somewhat (to "login item" or to "launch daemon/agent", etc.) if the main application is multi-user or you intend to communicate with a service running on another host, but I'm replying based on what I think you're trying to achieve.

I hope this helps.

Upvotes: 2

Related Questions