How to find a connection to call a method of a specific object?

I have a Cocoa client and server application that interacts standardly using distributed objects implemented through NSSocketPorts and NSConnections. The server supports one object for client applications, which may be several. Each client application can access the same distributed object that receives its own proxy.

The Vend object supports a specific protocol, which includes a method similar to the following:

@protocol VendedObjectProtocol - (void) acquireServerResource:(ServerResourceID)rsc; @end 

When the client application calls this method, the server must assign the requested resource to this client application. But there may be several clients that request the same resource, and the server must keep track of which clients requested it.

What I would like to do on the server side is the NSConnection definition used by calling the client method. How can i do this?

One of the ways I thought of is this (server side):

 - (void) acquireServerResource:(ServerResourceID)rsc withDummyObject:(id)dummy { NSConnection* conn = [dummy connectionForProxy]; // Map the connection to a particular client // ... } 

However, I do not want the client to go through the dummy object without any real purpose (from the client's point of view). I could make ServerResourceID a class so that it goes through the proxy server, but I don't want to do that either.

It seems to me that if I made a connection with raw sockets, I could find out in which socket the message appeared, and, therefore, be able to decide which client sent it without the client sending anything special as part of the message. I need a way to do this using a distributed object method call.

Can anyone suggest a mechanism for this?

+4
source share
1 answer

What you are looking for is an NSConnection delegate . For instance:

 - (BOOL)connection:(NSConnection *)parentConnection shouldMakeNewConnection:(NSConnection *)newConnnection { // setup and map newConnnection to a particular client id<VendedObjectProtocol> obj = //... obj.connection = newConnection; return YES; } 

You can create an object for each individual connection (for example, VendObjectProtocol) and get a connection with self.connection.

 - (void) acquireServerResource:(ServerResourceID)rsc { NSConnection* conn = self.connection; // Map the connection to a particular client // ... } 

or

You can use conversation tokens using + createConversationForConnection : + currentConversation

0
source

All Articles