00001 /* 00002 Copyright (C) 2009 Olivier Halligon. All rights reserved. 00003 00004 Redistribution and use in source and binary forms, with or without 00005 modification, are permitted provided that the following conditions are met: 00006 00007 * Redistributions of source code must retain the above copyright notice, this 00008 list of conditions and the following disclaimer. 00009 00010 * Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions and the following disclaimer in the documentation 00012 and/or other materials provided with the distribution. 00013 00014 * Neither the name of the author nor the names of its contributors may be used 00015 to endorse or promote products derived from this software without specific 00016 prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #import <Foundation/Foundation.h> 00031 00034 00035 00036 NSString* const JSONRPCServerErrorDomain; 00037 NSString* const JSONRPCServerErrorNotification; 00038 NSString* const JSONRPCErrorJSONObjectKey; 00039 00040 NSString* const JSONRPCInternalErrorDomain; 00041 NSInteger const JSONRPCFormatErrorCode; 00042 NSString* const JSONRPCFormatErrorString; 00043 NSInteger const JSONRPCConversionErrorCode; 00044 NSString* const JSONRPCConversionErrorString; 00045 NSString* const JSONRPCErrorClassNameKey; 00046 00047 00048 @class JSONRPCMethodCall; 00049 @class JSONRPCResponseHandler; 00050 00051 00052 00054 // MARK: - 00055 // MARK: Error Handling 00057 00059 @protocol JSONRPCDelegate <NSObject> 00060 @optional 00067 -(void)methodCall:(JSONRPCMethodCall*)methodCall didReturn:(id)result error:(NSError*)error; 00073 -(BOOL)methodCall:(JSONRPCMethodCall*)methodCall shouldForwardConnectionError:(NSError*)error; 00074 00079 -(void)methodCall:(JSONRPCMethodCall*)methodCall willRetryAfterError:(NSError*)error; 00080 00084 -(void)methodCallIsRetrying:(JSONRPCMethodCall*)methodCall; 00085 @end 00086 00087 00088 00090 // MARK: - 00091 // MARK: JSON-RPC Service 00093 00095 typedef enum { 00096 JSONRPCVersion_1_0, 00097 JSONRPCVersion_1_1, 00098 JSONRPCVersion_2_0 00099 } JSONRPCVersion; 00100 00101 00103 @interface JSONRPCService : NSObject { 00105 NSURL* _serviceURL; 00106 JSONRPCVersion _version; 00107 NSObject<JSONRPCDelegate>* delegate; 00108 } 00109 @property(nonatomic, retain) NSURL* serviceURL; 00110 @property(nonatomic, assign) JSONRPCVersion version; 00111 @property(nonatomic, assign) NSObject<JSONRPCDelegate>* delegate; 00112 @property(nonatomic, readonly) id proxy; 00113 00114 00115 00117 // MARK: - 00118 // MARK: Constructors 00120 00125 +(id)serviceWithURL:(NSURL*)url version:(JSONRPCVersion)version; 00130 -(id)initWithURL:(NSURL*)url version:(JSONRPCVersion)version; 00131 00132 00133 00135 // MARK: - 00136 // MARK: Calling a Procedure 00138 00143 - (JSONRPCResponseHandler*)callMethod:(JSONRPCMethodCall*)methodCall; 00149 - (JSONRPCResponseHandler*)callMethod:(JSONRPCMethodCall*)methodCall reuseResponseHandler:(JSONRPCResponseHandler*)responseHandler; 00150 00156 // MARK: - 00157 - (JSONRPCResponseHandler*)callMethodWithName:(NSString *)methodName parameters:(NSArray*)params; 00163 - (JSONRPCResponseHandler*)callMethodWithNameAndParams:(NSString *)methodName, ... NS_REQUIRES_NIL_TERMINATION; 00164 00170 - (JSONRPCResponseHandler*)callMethodWithName:(NSString *)methodName namedParameters:(NSDictionary*)params; 00178 - (JSONRPCResponseHandler*)callMethodWithNameAndNamedParams:(NSString *)methodName, ... NS_REQUIRES_NIL_TERMINATION; 00179 @end 00180 00181 00182 00183 00185 // MARK: - 00186 // MARK: JSONRPC Service Proxy 00188 00203 @interface JSONRPCServiceProxy : NSProxy 00204 { 00206 JSONRPCService* _service; 00207 } 00208 @property(nonatomic, assign) JSONRPCService* service; 00209 -(id)initWithService:(JSONRPCService*)service; 00210 @end