An Objective-C native implementation of dnode asynchronous RPC protocol. It uses the following library
- CocoaAsyncSocket for TCP networking
- SBJson for JSON stream handling
Start by subclassing DNode and define your local dnode interface in a header file.
#import "DNode.h"
@interface TestClient : DNode
-(void)foo; //the method your would like to call on remote
@end
To expose your local dnode interface to the remote, use C++ macro DEFINE_CLIENT_METHOD(local_name)
. For example
//the direct call from remote dnode
DEFINE_CLIENT_METHOD(hello) {
NSLog(@"hello from server: %@ ", args);
}
To explicitly declare a method on remote and receive callback, use DEFINE_SERVER_METHOD_WITH_CALLBACK(remote_name)
//the callback from a previous local-to-remote call
DEFINE_SERVER_METHOD_WITH_CALLBACK(echo) {
NSLog(@"callback from server %@", args);
}
To invoke a method on remote, use CALL_SERVER_METHOD(remote_name, arg)
whose remote_name must match the name defined in DEFINE_SERVER_METHOD_WITH_CALLBACK
. For example
-(void)foo
{
id arg = @[@"hello from client"];
CALL_SERVER_METHOD(echo, arg);
}
To create a dnode instance and connect to a remote
TestClient* client = [[TestClient alloc] init];
[client connectToHost:@"192.168.1.100" Port:8000 Callback:^(BOOL success, NSNumber* err){
NSLog(@"connected %d with err %@", success, err);
}];
Set up a delegate object to get notified on connection status
[client addListener:self CallbackQueue:dispatch_get_main_queue()];
A number of delegate callbacks are
-(void)connectSuccess; //connected to remote
-(void)connectFailed:(NSNumber*)code;
-(void)disconnected;
-(void)remoteReady; //remote interface ready
For details please look into the test directory for OSX and iOS demo projects.
Denny C. Dai [email protected] or visit http://dennycd.me
- local listening server that receives remote connection
- callback to remote from a previous remote-to-local invocation
- more unit testing and examples
The MIT License