cocoa 分布式对象中的双向通信
Bidirectional communication in Distributed Objects in cocoa
我是 Cocoa 编程的新手...我正在学习使用分布式对象的 IPC。
我做了一个简单的例子,我从服务器出售对象并在 client.I 中调用它们成功地将消息从客户端对象传递到服务器但我想将消息从服务器传递到客户端[双向].. .我该怎么做?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
MYMessageServer *server = [[MYMessageServer alloc] init];
NSConnection *defaultConnection=[NSConnection defaultConnection];
[defaultConnection setRootObject:server];
if ([defaultConnection registerName:@"server"] == NO)
{
NSLog(@"Error registering server");
}
else
NSLog(@"Connected");
[[NSRunLoop currentRunLoop] configureAsServer];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//Getting an Vended Object
server = [NSConnection rootProxyForConnectionWithRegisteredName:@"server" host:nil];
if(nil == server)
{
NSLog(@"Error: Failed to connect to server.");
}
else
{
//setProtocolForProxy is a method of NSDistantObject
[server setProtocolForProxy:@protocol(MYMessageServerProtocol)];
[server addMessageClient:self];
[server broadcastMessageString:[NSString stringWithFormat:@"Connected: %@ %d\n",
[[NSProcessInfo processInfo] processName],
[[NSProcessInfo processInfo] processIdentifier]]];
}
}
- (void)appendMessageString:(NSString *)aString
{
NSRange appendRange = NSMakeRange([[_messageView string] length], 0);
// Append text and scroll if neccessary
[_messageView replaceCharactersInRange:appendRange withString:aString];
[_messageView scrollRangeToVisible:appendRange];
}
- (void)addMessageClient:(id)aClient
{
if(nil == _myListOfClients)
{
_myListOfClients = [[NSMutableArray alloc] init];
}
[_myListOfClients addObject:aClient];
NSLog(@"Added client");
}
- (BOOL)removeMessageClient:(id)aClient
{
[_myListOfClients removeObject:aClient];
NSLog(@"Removed client");
return YES;
}
- (void)broadcastMessageString:(NSString *)aString
{
NSLog(@"Msg is %@",aString);
self.logStatement = aString;
[_myListOfClients makeObjectsPerformSelector:@selector(appendMessageString:)
withObject:aString];
}
@protocol MYMessageServerProtocol
- (void)addMessageClient:(id)aClient;
- (BOOL)removeMessageClient:(id)aClient;
- (void)broadcastMessageString:(NSString *)aString;
您没有显示 MYMessageServer
或 MYMessageServerProtocol
的代码,特别是 -addMessageClient:
方法。但是,一旦您向服务器传递了对客户端中对象的引用,服务器就可以像往常一样向该对象发送消息,并且该消息将通过 D.O 发送。给客户。
因此,客户端正在通过 -addMessageClient:
向服务器发送 self
(其应用程序委托)。服务器可以简单地调用它在 -addMessageClient:
的实现中接收到的对象的方法,这将调用客户端的应用程序委托对象的方法。服务器可以将该引用保存在某个地方,例如实例变量中的一组客户端,并且如果需要,也可以在以后继续向客户端发送消息。服务器将希望在连接关闭时清除该引用,它可以从 NSConnection
发布的通知中检测到这一点。
我是 Cocoa 编程的新手...我正在学习使用分布式对象的 IPC。 我做了一个简单的例子,我从服务器出售对象并在 client.I 中调用它们成功地将消息从客户端对象传递到服务器但我想将消息从服务器传递到客户端[双向].. .我该怎么做?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
MYMessageServer *server = [[MYMessageServer alloc] init];
NSConnection *defaultConnection=[NSConnection defaultConnection];
[defaultConnection setRootObject:server];
if ([defaultConnection registerName:@"server"] == NO)
{
NSLog(@"Error registering server");
}
else
NSLog(@"Connected");
[[NSRunLoop currentRunLoop] configureAsServer];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
//Getting an Vended Object
server = [NSConnection rootProxyForConnectionWithRegisteredName:@"server" host:nil];
if(nil == server)
{
NSLog(@"Error: Failed to connect to server.");
}
else
{
//setProtocolForProxy is a method of NSDistantObject
[server setProtocolForProxy:@protocol(MYMessageServerProtocol)];
[server addMessageClient:self];
[server broadcastMessageString:[NSString stringWithFormat:@"Connected: %@ %d\n",
[[NSProcessInfo processInfo] processName],
[[NSProcessInfo processInfo] processIdentifier]]];
}
}
- (void)appendMessageString:(NSString *)aString
{
NSRange appendRange = NSMakeRange([[_messageView string] length], 0);
// Append text and scroll if neccessary
[_messageView replaceCharactersInRange:appendRange withString:aString];
[_messageView scrollRangeToVisible:appendRange];
}
- (void)addMessageClient:(id)aClient
{
if(nil == _myListOfClients)
{
_myListOfClients = [[NSMutableArray alloc] init];
}
[_myListOfClients addObject:aClient];
NSLog(@"Added client");
}
- (BOOL)removeMessageClient:(id)aClient
{
[_myListOfClients removeObject:aClient];
NSLog(@"Removed client");
return YES;
}
- (void)broadcastMessageString:(NSString *)aString
{
NSLog(@"Msg is %@",aString);
self.logStatement = aString;
[_myListOfClients makeObjectsPerformSelector:@selector(appendMessageString:)
withObject:aString];
}
@protocol MYMessageServerProtocol
- (void)addMessageClient:(id)aClient;
- (BOOL)removeMessageClient:(id)aClient;
- (void)broadcastMessageString:(NSString *)aString;
您没有显示 MYMessageServer
或 MYMessageServerProtocol
的代码,特别是 -addMessageClient:
方法。但是,一旦您向服务器传递了对客户端中对象的引用,服务器就可以像往常一样向该对象发送消息,并且该消息将通过 D.O 发送。给客户。
因此,客户端正在通过 -addMessageClient:
向服务器发送 self
(其应用程序委托)。服务器可以简单地调用它在 -addMessageClient:
的实现中接收到的对象的方法,这将调用客户端的应用程序委托对象的方法。服务器可以将该引用保存在某个地方,例如实例变量中的一组客户端,并且如果需要,也可以在以后继续向客户端发送消息。服务器将希望在连接关闭时清除该引用,它可以从 NSConnection
发布的通知中检测到这一点。