NSConnection not working between processes
NSConnection not working between processes
- Subject: NSConnection not working between processes
- From: James Bucanek <email@hidden>
- Date: Wed, 22 Jun 2005 15:27:51 -0700
Hello again,
I posted this problem last week and didn't get any takers. I spend the better part of another day trying to get it to work, and have lost a lot of hair in the process.
Just to make sure it wasn't something else about my code or project that was the problem, I stripped down the code to just the essentials and created a new project to demonstrate the problem. Here's what I can't get to work:
-- Cocoa command-line application (aka "the helper")
1. Creates an NSConnection
2. Registers it with a name
3. Sets the rootObject
4. Runs the connection in a new thread
5. waits...
-- Cocoa application (aka "client")
1. Uses NSTask to start the helper
2. Creates an NSConnection connected to the named connection created by the helper
3. Gets the proxy for the rootObject in the helper
The last step always fails. The client gets back 'nil' when it calls [helperConnection rootProxy].
Just in case you want to play this yourself, you can download the complete, ready to complile, Xcode 2.1 project here: <ftp://ftp.twilightandbarking.com/Public/Temporary/61/DOHelperTest.zip>
Can someone give me a clue as to what I'm doing wrong?
As I noted in my last post, the basic principle of my code has been working flawlessly for months when both the client and the helper are seperate threads in the same process (which also makes debugging easier). But for the final product, the helper code has to be running in seperate process.
Here's the bulk of the code in the test project. The first program (DOHelper) is the command-line program that is launched by the second application (DOHelperClient).
James
-- DOHelper.h/.m ----------------------------
#import <Cocoa/Cocoa.h>
@interface DOHelper : NSObject
{
NSConnection* clientConnection;
NSLock* running;
}
- (id)init;
- (void)run;
- (void)doSomething:(NSString*)message;
- (void)stop;
@end
@implementation DOHelper
- (id)init
{
if ( self = [super init] )
{
running = [[NSLock alloc] init];
[running lock];
}
return (self);
}
- (void)run
{
NSLog(@"DOHelper started");
// Create a connection using two arbitrary ports
clientConnection = [NSConnection connectionWithReceivePort:
[NSPort port] sendPort:[NSPort port]];
// Register the connection
if ([clientConnection registerName:kDOPortName]==NO)
[[NSException exceptionWithName:@"Comm" reason:@"cannot
register NSConnection" userInfo:nil] raise];
// Vend ourselves to the client
[clientConnection setRootObject:self];
// Start a run loop to dispatch messages to this object
[clientConnection runInNewThread];
NSLog(@"DOHelper created connection, running...");
[running lock]; // we'll aquire this lock when the
client tells us to stop
[running unlock]; // release it in case anyone else is
using it for the same purpose
NSLog(@"DOHelper terminating");
}
- (void)doSomething:(NSString*)message
{
NSLog(@"The helper received this message: %@",message);
}
- (void)stop
{
[running unlock];
}
@end
-- DOHelperClient.h/.m ----------------------------
#import <Cocoa/Cocoa.h>
#define kDOPortName @"HelperClient.test"
@interface DOHelperClient : NSObject
{
}
- (IBAction)testHelper:(id)sender;
@end
@implementation DOHelperClient
- (IBAction)testHelper:(id)sender
{
NSLog(@"testHelper:");
// Fire up the helper
NSTask* helperTask = [[NSTask alloc] init];
[helperTask setLaunchPath:@"DOHelper"];
// Start the helper
[helperTask launch];
// Wait for the helper tool to start running and create its DO
connection
// (in real life we wait for the helper to start running in an
intellegent manner,
// but this is just to demonstrate this problem)
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:
3.0]];
// Open the connection that was just created by the helper tool
NSConnection* helperConnection = [[NSConnection
connectionWithRegisteredName:kDOPortName host:nil] retain];
if (helperConnection==nil)
[[NSException exceptionWithName:@"Comm" reason:@"cannot
connect with helper" userInfo:nil] raise];
// Get the helper's proxy object
id proxy = [helperConnection rootProxy];
NSAssert(proxy!=nil,@"nil proxy!");
// Send the proxy a message just to prove that it works.
[proxy doSomething:@"message from helper client"];
[proxy stop];
}
@end
--
James Bucanek <mailto:email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden