开发者

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

开发者 https://www.devze.com 2023-03-12 16:22 出处:网络
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 cli

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 {
    NSConnect开发者_StackOverflowion* 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

0

精彩评论

暂无评论...
验证码 换一张
取 消