From: Mark Slee Date: Wed, 19 Sep 2007 21:10:18 +0000 (+0000) Subject: Whoops forgot to actually svn add Andrew's files X-Git-Tag: 0.2.0~1195 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=2ac60ed96d3075b367ef18aa96787c68330d0248;p=common%2Fthrift.git Whoops forgot to actually svn add Andrew's files Summary: Tricky svn add command will get you every time Reviewed By: dreiss Test Plan: test/ tutorial/ Cocoa code git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665277 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cocoa/TApplicationException.m b/lib/cocoa/TApplicationException.m new file mode 100644 index 00000000..1f658de6 --- /dev/null +++ b/lib/cocoa/TApplicationException.m @@ -0,0 +1,84 @@ +#import "TApplicationException.h" +#import "TProtocolUtil.h" + +@implementation TApplicationException + +- (id) initWithType: (int) type + reason: (NSString *) reason +{ + NSString * name; + switch (type) { + case TApplicationException_UNKNOWN_METHOD: + name = @"Unknown method"; + break; + case TApplicationException_INVALID_MESSAGE_TYPE: + name = @"Invalid message type"; + break; + case TApplicationException_WRONG_METHOD_NAME: + name = @"Wrong method name"; + break; + case TApplicationException_BAD_SEQUENCE_ID: + name = @"Bad sequence ID"; + break; + case TApplicationException_MISSING_RESULT: + name = @"Missing result"; + break; + default: + name = @"Unknown"; + break; + } + + self = [super initWithName: name reason: reason userInfo: nil]; + return self; +} + + ++ (TApplicationException *) read: (id ) protocol +{ + NSString * reason = nil; + int type = TApplicationException_UNKNOWN; + int fieldType; + int fieldID; + + while (true) { + [protocol readFieldBeginReturningName: NULL + type: &fieldType + fieldID: &fieldID]; + if (fieldType == TType_STOP) { + break; + } + switch (fieldID) { + case 1: + if (fieldType == TType_STRING) { + reason = [protocol readString]; + } else { + [TProtocolUtil skipType: fieldType onProtocol: protocol]; + } + break; + case 2: + if (fieldType == TType_I32) { + type = [protocol readI32]; + } else { + [TProtocolUtil skipType: fieldType onProtocol: protocol]; + } + break; + default: + [TProtocolUtil skipType: fieldType onProtocol: protocol]; + break; + } + [protocol readFieldEnd]; + } + [protocol readStructEnd]; + + return [TApplicationException exceptionWithType: type reason: reason]; +} + + ++ (TApplicationException *) exceptionWithType: (int) type + reason: (NSString *) reason +{ + return [[[TApplicationException alloc] initWithType: type + reason: reason] autorelease]; +} + +@end diff --git a/lib/cocoa/TException.m b/lib/cocoa/TException.m new file mode 100644 index 00000000..38d00ca2 --- /dev/null +++ b/lib/cocoa/TException.m @@ -0,0 +1,33 @@ +#import "TException.h" + +@implementation TException + ++ (id) exceptionWithName: (NSString *) name +{ + return [self exceptionWithName: name reason: @"unknown" error: nil]; +} + + ++ (id) exceptionWithName: (NSString *) name + reason: (NSString *) reason +{ + return [self exceptionWithName: name reason: reason error: nil]; +} + + ++ (id) exceptionWithName: (NSString *) name + reason: (NSString *) reason + error: (NSError *) error +{ + NSDictionary * userInfo = nil; + if (error != nil) { + userInfo = [NSDictionary dictionaryWithObject: error forKey: @"error"]; + } + + return [super exceptionWithName: name + reason: reason + userInfo: userInfo]; +} + + +@end diff --git a/lib/cocoa/THTTPClient.h b/lib/cocoa/THTTPClient.h new file mode 100644 index 00000000..98446cd4 --- /dev/null +++ b/lib/cocoa/THTTPClient.h @@ -0,0 +1,18 @@ +#import +#import "TTransport.h" + +@interface THTTPClient : NSObject { + NSURL * mURL; + NSMutableURLRequest * mRequest; + NSMutableData * mRequestData; + NSData * mResponseData; + int mResponseDataOffset; +} + +- (id) initWithURL: (NSURL *) aURL; + +- (id) initWithURL: (NSURL *) aURL + timeout: (int) timeout; + +@end + diff --git a/lib/cocoa/THTTPClient.m b/lib/cocoa/THTTPClient.m new file mode 100644 index 00000000..096770d7 --- /dev/null +++ b/lib/cocoa/THTTPClient.m @@ -0,0 +1,100 @@ +#import "THTTPClient.h" +#import "TTransportException.h" + +@implementation THTTPClient + +- (id) initWithURL: (NSURL *) aURL +{ + self = [super init]; + mURL = [aURL retain]; + + // set up our request object that we'll use for each request + mRequest = [[NSMutableURLRequest alloc] initWithURL: mURL]; + [mRequest setHTTPMethod: @"POST"]; + [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Content-Type"]; + [mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Accept"]; + [mRequest setValue: @"Cocoa/THTTPClient" forHTTPHeaderField: @"User-Agent"]; + [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData]; + + // create our request data buffer + mRequestData = [[NSMutableData alloc] initWithCapacity: 1024]; + + return self; +} + + +- (id) initWithURL: (NSURL *) aURL + timeout: (int) timeout +{ + self = [self initWithURL: aURL]; + + [mRequest setTimeoutInterval: timeout]; + + return self; +} + + +- (void) dealloc +{ + [mURL release]; + [mRequest release]; + [mRequestData release]; + [super dealloc]; +} + + +- (int) readAll: (uint8_t *) buf offset: (int) off length: (int) len +{ + NSRange r; + r.location = mResponseDataOffset; + r.length = len; + + [mResponseData getBytes: buf+off range: r]; + mResponseDataOffset += len; + + return len; +} + + +- (void) write: (const uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length +{ + [mRequestData appendBytes: data+offset length: length]; +} + + +- (void) flush +{ + [mRequest setHTTPBody: mRequestData]; // not sure if it copies the data + + // make the HTTP request + NSURLResponse * response; + NSError * error; + NSData * responseData = + [NSURLConnection sendSynchronousRequest: mRequest returningResponse: &response error: &error]; + + [mRequestData setLength: 0]; + + if (responseData == nil) { + @throw [TTransportException exceptionWithName: @"Could not make HTTP request" + reason: @"unknown" + error: error]; + } + if (![response isKindOfClass: [NSHTTPURLResponse class]]) { + @throw [TTransportException exceptionWithName: @"Unexpected NSURLResponse type"]; + } + + NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; + if ([httpResponse statusCode] != 200) { + @throw [TTransportException exceptionWithName: + [NSString stringWithFormat: @"Bad response from HTTP server: %d", + [httpResponse statusCode]]]; + } + + // phew! + [mResponseData release]; + mResponseData = [responseData retain]; + mResponseDataOffset = 0; +} + + +@end diff --git a/lib/cocoa/TProtocolException.h b/lib/cocoa/TProtocolException.h new file mode 100644 index 00000000..c49f170c --- /dev/null +++ b/lib/cocoa/TProtocolException.h @@ -0,0 +1,6 @@ +#import "TException.h" + +@interface TProtocolException : TException { +} + +@end diff --git a/lib/cocoa/TProtocolException.m b/lib/cocoa/TProtocolException.m new file mode 100644 index 00000000..ee56bc5d --- /dev/null +++ b/lib/cocoa/TProtocolException.m @@ -0,0 +1,4 @@ +#import "TProtocolException.h" + +@implementation TProtocolException +@end diff --git a/lib/cocoa/TTransportException.h b/lib/cocoa/TTransportException.h new file mode 100644 index 00000000..5d3802ba --- /dev/null +++ b/lib/cocoa/TTransportException.h @@ -0,0 +1,6 @@ +#import "TException.h" + +@interface TTransportException : TException { +} + +@end diff --git a/lib/cocoa/TTransportException.m b/lib/cocoa/TTransportException.m new file mode 100644 index 00000000..e1102e21 --- /dev/null +++ b/lib/cocoa/TTransportException.m @@ -0,0 +1,4 @@ +#import "TTransportException.h" + +@implementation TTransportException +@end