トップ «前の日記(2014-05-05) 最新 次の日記(2014-05-09)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2014-05-06 [OSX]プロセス間通信(Distributed Objects)

今度は、Distributed Objects。まずは、プロトコルを定義。

@protocol RemoteObjectProtocol
- (oneway void)receiveString:(NSString *)string;
@end

サーバ側を実装。クライアント側からのメソッド呼び出しに対応。

@interface AppDelegate () <RemoteObjectProtocol>
- (void)_registerForDistributedObjects;
@end
 
@implementation AppDelegate
 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self _registerForDistributedObjects];
}
 
- (void)_registerForDistributedObjects
{
    NSConnection    *conn = [NSConnection defaultConnection];
    [conn setRootObject:self];
    if ([conn registerName:@"DistributedServer"] == NO) {
        NSLog(@"%s error", __func__);
    }
}
 
- (oneway void)receiveString:(NSString *)string
{
    [self.label setStringValue:string];
}
 
@end

次は、クライアント側。

@interface AppDelegate ()
- (void)_postForDistributedObjects;
@end
 
@implementation AppDelegate
 
- (IBAction)postForDistributedObjects:(id)sender
{
    [self _postForDistributedObjects];
}
 
- (void)_postForDistributedObjects
{
    id  remoteObject;
    remoteObject = [NSConnection rootProxyForConnectionWithRegisteredName:@"DistributedServer"
                                                                     host:@""];
    [remoteObject setProtocolForProxy:@protocol(RemoteObjectProtocol)];
    [remoteObject receiveString:[NSString stringWithFormat:@"%@", [[NSDate date] description]]];
}
 
@end

名前DistributedServerでサーバを探して、プロトコルRemoteObjectProtocolのメソッドを呼ぶと、サーバ側のメソッドが実行される。

_ 関連情報

Cocoa in a Nutshell

_ 【Cocoa練習帳】

http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)

トップ «前の日記(2014-05-05) 最新 次の日記(2014-05-09)» 編集