Cocoa Thrift binding patches from Andrew McGeachie
Summary: Latest updates to the Cocoa bindings
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665354 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cocoa/src/transport/THTTPClient.h b/lib/cocoa/src/transport/THTTPClient.h
index 98446cd..802ff51 100644
--- a/lib/cocoa/src/transport/THTTPClient.h
+++ b/lib/cocoa/src/transport/THTTPClient.h
@@ -14,5 +14,7 @@
- (id) initWithURL: (NSURL *) aURL
timeout: (int) timeout;
+- (void) setURL: (NSURL *) aURL;
+
@end
diff --git a/lib/cocoa/src/transport/THTTPClient.m b/lib/cocoa/src/transport/THTTPClient.m
index 45c0b80..2e31a98 100644
--- a/lib/cocoa/src/transport/THTTPClient.m
+++ b/lib/cocoa/src/transport/THTTPClient.m
@@ -3,11 +3,13 @@
@implementation THTTPClient
-- (id) initWithURL: (NSURL *) aURL
-{
- self = [super init];
- mURL = [aURL retain];
+- (void) setupRequest
+{
+ if (mRequest != nil) {
+ [mRequest release];
+ }
+
// set up our request object that we'll use for each request
mRequest = [[NSMutableURLRequest alloc] initWithURL: mURL];
[mRequest setHTTPMethod: @"POST"];
@@ -15,6 +17,15 @@
[mRequest setValue: @"application/x-thrift" forHTTPHeaderField: @"Accept"];
[mRequest setValue: @"Cocoa/THTTPClient" forHTTPHeaderField: @"User-Agent"];
[mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData];
+}
+
+
+- (id) initWithURL: (NSURL *) aURL
+{
+ self = [super init];
+ mURL = [aURL retain];
+
+ [self setupRequest];
// create our request data buffer
mRequestData = [[NSMutableData alloc] initWithCapacity: 1024];
@@ -34,6 +45,16 @@
}
+- (void) setURL: (NSURL *) aURL
+{
+ [aURL retain];
+ [mURL release];
+ mURL = aURL;
+
+ [self setupRequest];
+}
+
+
- (void) dealloc
{
[mURL release];
diff --git a/lib/cocoa/src/transport/TNSFileHandleTransport.m b/lib/cocoa/src/transport/TNSFileHandleTransport.m
index 7ad1ba7..79a9e8d 100644
--- a/lib/cocoa/src/transport/TNSFileHandleTransport.m
+++ b/lib/cocoa/src/transport/TNSFileHandleTransport.m
@@ -1,5 +1,6 @@
#import "TNSFileHandleTransport.h"
+#import "TTransportException.h"
@implementation TNSFileHandleTransport
@@ -36,9 +37,8 @@
while (got < len) {
NSData * d = [mInputFileHandle readDataOfLength: len-got];
if ([d length] == 0) {
- @throw [NSException exceptionWithName: @"TTransportException"
- reason: @"Cannot read. No more data."
- userInfo: nil];
+ @throw [TTransportException exceptionWithName: @"TTransportException"
+ reason: @"Cannot read. No more data."];
}
[d getBytes: buf+got];
got += [d length];
@@ -62,7 +62,7 @@
- (void) flush
{
- [mOutputFileHandle synchronizeFile]; // ?
+
}
@end
diff --git a/lib/cocoa/src/transport/TNSStreamTransport.m b/lib/cocoa/src/transport/TNSStreamTransport.m
index 8130a8b..42a197e 100644
--- a/lib/cocoa/src/transport/TNSStreamTransport.m
+++ b/lib/cocoa/src/transport/TNSStreamTransport.m
@@ -1,4 +1,6 @@
#import "TNSStreamTransport.h"
+#import "TTransportException.h"
+
@implementation TNSStreamTransport
@@ -29,9 +31,7 @@
while (got < len) {
ret = [mInput read: buf+off+got maxLength: len-got];
if (ret <= 0) {
- @throw [NSException exceptionWithName: @"TTransportException"
- reason: @"Cannot read. Remote side has closed."
- userInfo: nil];
+ @throw [TTransportException exceptionWithReason: @"Cannot read. Remote side has closed."];
}
got += ret;
}
@@ -39,23 +39,17 @@
}
+// FIXME:geech:20071019 - make this write all
- (void) write: (uint8_t *) data offset: (unsigned int) offset length: (unsigned int) length
{
int result = [mOutput write: data+offset maxLength: length];
if (result == -1) {
- NSDictionary * errorInfo = [NSDictionary dictionaryWithObject: [mOutput streamError]
- forKey: @"error"];
- @throw [NSException exceptionWithName: @"TTransportException"
- reason: [NSString stringWithFormat: @"Error writing to transport output stream (%@).", [mOutput streamError]]
- userInfo: errorInfo];
+ @throw [TTransportException exceptionWithReason: @"Error writing to transport output stream."
+ error: [mOutput streamError]];
} else if (result == 0) {
- @throw [NSException exceptionWithName: @"TTransportException"
- reason: @"End of output stream."
- userInfo: nil];
+ @throw [TTransportException exceptionWithReason: @"End of output stream."];
} else if (result != length) {
- @throw [NSException exceptionWithName: @"TTransportException"
- reason: @"Output stream did not write all of our data."
- userInfo: nil];
+ @throw [TTransportException exceptionWithReason: @"Output stream did not write all of our data."];
}
}
diff --git a/lib/cocoa/src/transport/TSocketClient.m b/lib/cocoa/src/transport/TSocketClient.m
index f3acc3b..e8ae6e9 100644
--- a/lib/cocoa/src/transport/TSocketClient.m
+++ b/lib/cocoa/src/transport/TSocketClient.m
@@ -14,7 +14,11 @@
inputStream: &input
outputStream: &output];
- return [super initWithInputStream: input outputStream: output];
+ self = [super initWithInputStream: input outputStream: output];
+ [input open];
+ [output open];
+
+ return self;
}
diff --git a/lib/cocoa/src/transport/TTransportException.h b/lib/cocoa/src/transport/TTransportException.h
index 5d3802b..7488ca5 100644
--- a/lib/cocoa/src/transport/TTransportException.h
+++ b/lib/cocoa/src/transport/TTransportException.h
@@ -3,4 +3,9 @@
@interface TTransportException : TException {
}
++ (id) exceptionWithReason: (NSString *) reason
+ error: (NSError *) error;
+
++ (id) exceptionWithReason: (NSString *) reason;
+
@end
diff --git a/lib/cocoa/src/transport/TTransportException.m b/lib/cocoa/src/transport/TTransportException.m
index e1102e2..3f82935 100644
--- a/lib/cocoa/src/transport/TTransportException.m
+++ b/lib/cocoa/src/transport/TTransportException.m
@@ -1,4 +1,24 @@
#import "TTransportException.h"
@implementation TTransportException
+
++ (id) exceptionWithReason: (NSString *) reason
+ error: (NSError *) error
+{
+ NSDictionary * userInfo = nil;
+ if (error != nil) {
+ userInfo = [NSDictionary dictionaryWithObject: error forKey: @"error"];
+ }
+
+ return [super exceptionWithName: @"TTransportException"
+ reason: reason
+ userInfo: userInfo];
+}
+
+
++ (id) exceptionWithReason: (NSString *) reason
+{
+ return [self exceptionWithReason: reason error: nil];
+}
+
@end