简体   繁体   中英

How to find the connection for a particular distributed objects method invocation?

I have a Cocoa client and server application that communicate using distributed objects implemented via NSSocketPorts and NSConnections in the standard way. The server vends a single object to the client applications, of which there may be several. Each client application can gain access to the same distributed object getting its own proxy.

The vended object supports a particular protocol, which includes a method similar to the following:

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

When a client application invokes this method, the server is supposed to allocate the requested resource to that client application. But there could be several clients that request the same resource, and the server needs to track which clients have requested it.

What I'd like to be able to do on the server-side is determine the NSConnection used by the client's method invocation. How can I do that?

One way I have thought about is this (server-side):

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

However, I don't really want the client to have to pass through a dummy object for no real purpose (from the client's perspective). I could make the ServerResourceID be a class so that it gets passed through as a proxy, but I don't want to really do that either.

It seems to me that if I were doing the communications with raw sockets, I would be able to figure out which socket the message came in on and therefore be able to work out which client sent it without the client needing to send anything special as part of the message. I am needing a way to do this with a distributed objects method invocation.

Can anyone suggest a mechanism for doing this?

What you are looking for are NSConnection 's delegate methods. For example:

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

You could design an object for each individual connection (like VendedObjectProtocol) and get the connection with self.connection.

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

Or

You can make use of conversation tokens using +createConversationForConnection : and +currentConversation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM