Can't create bi-directional DO connection with second process
Can't create bi-directional DO connection with second process
- Subject: Can't create bi-directional DO connection with second process
- From: James Bucanek <email@hidden>
- Date: Sun, 19 Jun 2005 10:51:35 -0700
Greetings,
This is driving me crazy. I can get my Distributed Objects to work
just fine between threads, but now that I'm trying to do it between
two processes it simply won't work.
This is a tad more complicated than most DO implementations or
example code, because I have asynchronous communications between the
client and the helper. Both client and helper vend an object (the
client vends a "listener" and the helper vends a "command" object).
The helper then forks a thread so the command receives messages from
the client in one thread and sends messages back to the client's
"listener" object in another.
As I mentioned, this has been working just great for weeks using two
threads in the same process. But now that my helper code has been
moved into a separate tool, it just won't fly.
Here's the code I have in my helper tool. This is a faceless UNIX
tool launched by the client GUI application.
main( ...)
(create command object)
// Create a connection using two arbitrary ports
NSConnection* clientConnection = [NSConnection connectionWithReceivePort:[NSPort port] sendPort:[NSPort port]];
// Vend the command object to the client
[clientConnection setRootObject:command];
// Link the command object to this connection, so the command can communicate with the client
[command setConnection:clientConnection];
// Register the connection using the name given to us
NSString* connectionName = [NSString stringWithCString:inParams[0]];
if ([clientConnection registerName:connectionName]==NO)
(throw exception)
// Start a run loop to dispatch messages to the command object
[clientConnection runInNewThread];
In my client application, I have
(launch helper, passing it a name to register its message port)
helperConnection = [[NSConnection
connectionWithRegisteredName:portName host:nil] retain];
[helperConnection setRootObject:listener];
The NSConnections get created and the client successfully connects
with then named port created by the helper. But when I call
[helperConnection rootProxy];
It returns nil.
Now, when I first wrote this, I mistakenly used [NSConnection
defaultConnection] in the helper. I could vend the object and
[helperConnection rootProxy] returned the proper "command" object
with no problem. But on the helper side, I couldn't get the
rootProxy of the listener object vended by the client. After a
little reading I discovered that [NSConnection defaultConnection]
uses the same NSPort for receiving and sending and "is only useful
for vending an object." But now that I've created separate NSPorts
for my NSConnection in the helper, I can no longer vend my object
at all! I should note that the code that works using two threads also uses
two abitrary NSPort objects.
I've see code that uses [[NSRunLoop currentRunLoop] run] instead of calling
[clientConnection runInNewThread]. I'm pretty sure I understand the
difference, but I'm wondering if my problem is with NSRunLoop and not
NSConnection, per se.
--
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