[launchd-dev] Distributed Objects communication with a launchd "on-demand" daemon

Frank Rizzo jerky.frank.rizzo at gmail.com
Thu Dec 17 10:47:22 PST 2009


I believe that I now have solved my problem using a launchd "on-demand"
daemon with Distributed Objects (DO).

I was only using TCP ports here for initial development because some of the
launchd "on-demand" sample code does and because I had been unable to create
an NSMachPort with the mach_port_t value I received from launchd.  However,
while writing a response to a replay to my message (which I had originally
posted on the Cocoa-dev list), I caught a trivial programming error that
fixed my NSMachPort creation problem (they were coming back nil) and also
allowed DO to work with the launchd daemon "on-demand."  I have not yet
tried the UNIX domain socket approach to client<->daemon communication DO
yet, and since I believe that Mach ports are preferred for DO I do not
intend to at this point.


Here is the mach services version of my code (which works with the launchd
daemon "on-demand"):

// TestDaemonLaunchd.plist

<dict>
<key>ServiceIPC</key>
<true/>
<key>MachServices</key>
<dict>
<key>com.example.TestDaemon</key>
</true>
</dict>
<key>OnDemand</key>
<true/>
<key>Label</key>
<string>com.example.TestDaemon</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/TestDaemon</string>
</array>
</dict>



//  TestDaemon.m

int main (int argc, const char * argv[]) {

    NSAutoreleasePool *pool = [NSAutoreleasePool new];

// NOTE: Getting the Mach port descriptor (mp) from launchd.
     launch_data_t checkinRequest =
launch_data_new_string(LAUNCH_KEY_CHECKIN);
launch_data_t checkinResponse = launch_msg(checkinRequest);
launch_data_t machServicesDict = launch_data_dict_lookup(checkinResponse,
LAUNCH_JOBKEY_MACHSERVICES);
launch_data_t machPort = launch_data_dict_lookup(machServicesDict,
"com.example.TestDaemon");

mach_port_t mp = launch_data_get_machport(machPort);

        launch_data_free(checkinResponse);
        launch_data_free(checkinRequest);

NSMachPort *receivePort = [[NSMachPort alloc] initWithMachPort:mp];
NSConnection *connection = [NSConnection
connectionWithReceivePort:receivePort sendPort:nil];
[receivePort release];

id testObject = [NSObject new];
[connection setRootObject:testObject];

[[NSRunLoop currentRunLoop] run];

[testObject release];
     [pool release];
    return 0;
}


//  TestClient.m
int main (int argc, const char * argv[]) {

    NSAutoreleasePool *pool = [NSAutoreleasePool new];

NSConnection *connection = [NSConnection connectionWithRegisteredName:@"com.
example.TestDaemon" host:nil];

[connection setRequestTimeout:10.0];
[connection setReplyTimeout:10.0];

// NOTE: No NSPortTimeoutException is raised when used with an "on-demand"
launchd daemon after switching to Mach ports for DO communication.
id proxy = [connection rootProxy];
[pool release];
    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/launchd-dev/attachments/20091217/35d6bbfe/attachment.html>


More information about the launchd-dev mailing list