http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/protocol/TProtocolUtil.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/protocol/TProtocolUtil.m b/depends/thirdparty/thrift/lib/cocoa/src/protocol/TProtocolUtil.m new file mode 100644 index 0000000..13d7095 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/protocol/TProtocolUtil.m @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TProtocolUtil.h" + +@implementation TProtocolUtil + ++ (void) skipType: (int) type onProtocol: (id <TProtocol>) protocol +{ + switch (type) { + case TType_BOOL: + [protocol readBool]; + break; + case TType_BYTE: + [protocol readByte]; + break; + case TType_I16: + [protocol readI16]; + break; + case TType_I32: + [protocol readI32]; + break; + case TType_I64: + [protocol readI64]; + break; + case TType_DOUBLE: + [protocol readDouble]; + break; + case TType_STRING: + [protocol readString]; + break; + case TType_STRUCT: + [protocol readStructBeginReturningName: NULL]; + while (true) { + int fieldType; + [protocol readFieldBeginReturningName: nil type: &fieldType fieldID: nil]; + if (fieldType == TType_STOP) { + break; + } + [TProtocolUtil skipType: fieldType onProtocol: protocol]; + [protocol readFieldEnd]; + } + [protocol readStructEnd]; + break; + case TType_MAP: + { + int keyType; + int valueType; + int size; + [protocol readMapBeginReturningKeyType: &keyType valueType: &valueType size: &size]; + int i; + for (i = 0; i < size; i++) { + [TProtocolUtil skipType: keyType onProtocol: protocol]; + [TProtocolUtil skipType: valueType onProtocol: protocol]; + } + [protocol readMapEnd]; + } + break; + case TType_SET: + { + int elemType; + int size; + [protocol readSetBeginReturningElementType: &elemType size: &size]; + int i; + for (i = 0; i < size; i++) { + [TProtocolUtil skipType: elemType onProtocol: protocol]; + } + [protocol readSetEnd]; + } + break; + case TType_LIST: + { + int elemType; + int size; + [protocol readListBeginReturningElementType: &elemType size: &size]; + int i; + for (i = 0; i < size; i++) { + [TProtocolUtil skipType: elemType onProtocol: protocol]; + } + [protocol readListEnd]; + } + break; + default: + return; + } +} + +@end
http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.h b/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.h new file mode 100644 index 0000000..c8ff9f0 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.h @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TProtocolFactory.h" +#import "TProcessorFactory.h" + +#if !TARGET_OS_IPHONE +#import <CoreServices/CoreServices.h> +#else +#import <CFNetwork/CFNetwork.h> +#endif + +extern NSString * const kTSocketServer_ClientConnectionFinishedForProcessorNotification; +extern NSString * const kTSocketServer_ProcessorKey; +extern NSString * const kTSockerServer_TransportKey; + + +@interface TSocketServer : NSObject { + NSFileHandle * mSocketFileHandle; + id <TProtocolFactory> mInputProtocolFactory; + id <TProtocolFactory> mOutputProtocolFactory; + id <TProcessorFactory> mProcessorFactory; +} + +- (id) initWithPort: (int) port + protocolFactory: (id <TProtocolFactory>) protocolFactory + processorFactory: (id <TProcessorFactory>) processorFactory; + +@end + + + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.m b/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.m new file mode 100644 index 0000000..07bc829 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/server/TSocketServer.m @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TSocketServer.h" +#import "TNSFileHandleTransport.h" +#import "TProtocol.h" +#import "TTransportException.h" +#import "TObjective-C.h" +#import <sys/socket.h> +#include <netinet/in.h> + + + +NSString * const kTSocketServer_ClientConnectionFinishedForProcessorNotification = @"TSocketServer_ClientConnectionFinishedForProcessorNotification"; +NSString * const kTSocketServer_ProcessorKey = @"TSocketServer_Processor"; +NSString * const kTSockerServer_TransportKey = @"TSockerServer_Transport"; + + +@implementation TSocketServer + +- (id) initWithPort: (int) port + protocolFactory: (id <TProtocolFactory>) protocolFactory + processorFactory: (id <TProcessorFactory>) processorFactory +{ + self = [super init]; + + mInputProtocolFactory = [protocolFactory retain_stub]; + mOutputProtocolFactory = [protocolFactory retain_stub]; + mProcessorFactory = [processorFactory retain_stub]; + + // create a socket. + int fd = -1; + CFSocketRef socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL); + if (socket) { + CFOptionFlags flagsToClear = kCFSocketCloseOnInvalidate; + CFSocketSetSocketFlags(socket, CFSocketGetSocketFlags(socket) & ~flagsToClear); + + fd = CFSocketGetNative(socket); + int yes = 1; + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes)); + + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_len = sizeof(addr); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = htonl(INADDR_ANY); + NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)]; + if (CFSocketSetAddress(socket, (bridge_stub CFDataRef)address) != kCFSocketSuccess) { + CFSocketInvalidate(socket); + CFRelease(socket); + NSLog(@"*** Could not bind to address"); + return nil; + } + } else { + NSLog(@"*** No server socket"); + return nil; + } + + // wrap it in a file handle so we can get messages from it + mSocketFileHandle = [[NSFileHandle alloc] initWithFileDescriptor: fd + closeOnDealloc: YES]; + + // throw away our socket + CFSocketInvalidate(socket); + CFRelease(socket); + + // register for notifications of accepted incoming connections + [[NSNotificationCenter defaultCenter] addObserver: self + selector: @selector(connectionAccepted:) + name: NSFileHandleConnectionAcceptedNotification + object: mSocketFileHandle]; + + // tell socket to listen + [mSocketFileHandle acceptConnectionInBackgroundAndNotify]; + + NSLog(@"Listening on TCP port %d", port); + + return self; +} + + +- (void) dealloc { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [mInputProtocolFactory release_stub]; + [mOutputProtocolFactory release_stub]; + [mProcessorFactory release_stub]; + [mSocketFileHandle release_stub]; + [super dealloc_stub]; +} + + +- (void) connectionAccepted: (NSNotification *) aNotification +{ + NSFileHandle * socket = [[aNotification userInfo] objectForKey: NSFileHandleNotificationFileHandleItem]; + + // now that we have a client connected, spin off a thread to handle activity + [NSThread detachNewThreadSelector: @selector(handleClientConnection:) + toTarget: self + withObject: socket]; + + [[aNotification object] acceptConnectionInBackgroundAndNotify]; +} + + +- (void) handleClientConnection: (NSFileHandle *) clientSocket +{ +#if __has_feature(objc_arc) + @autoreleasepool { + TNSFileHandleTransport * transport = [[TNSFileHandleTransport alloc] initWithFileHandle: clientSocket]; + id<TProcessor> processor = [mProcessorFactory processorForTransport: transport]; + + id <TProtocol> inProtocol = [mInputProtocolFactory newProtocolOnTransport: transport]; + id <TProtocol> outProtocol = [mOutputProtocolFactory newProtocolOnTransport: transport]; + + @try { + BOOL result = NO; + do { + @autoreleasepool { + result = [processor processOnInputProtocol: inProtocol outputProtocol: outProtocol]; + } + } while (result); + } + @catch (TTransportException * te) { + (void)te; + //NSLog(@"Caught transport exception, abandoning client connection: %@", te); + } + + NSNotification * n = [NSNotification notificationWithName: kTSocketServer_ClientConnectionFinishedForProcessorNotification + object: self + userInfo: [NSDictionary dictionaryWithObjectsAndKeys: + processor, + kTSocketServer_ProcessorKey, + transport, + kTSockerServer_TransportKey, + nil]]; + [[NSNotificationCenter defaultCenter] performSelectorOnMainThread: @selector(postNotification:) withObject: n waitUntilDone: YES]; + + } +#else + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + TNSFileHandleTransport * transport = [[TNSFileHandleTransport alloc] initWithFileHandle: clientSocket]; + id<TProcessor> processor = [mProcessorFactory processorForTransport: transport]; + + id <TProtocol> inProtocol = [[mInputProtocolFactory newProtocolOnTransport: transport] autorelease]; + id <TProtocol> outProtocol = [[mOutputProtocolFactory newProtocolOnTransport: transport] autorelease]; + + @try { + BOOL result = NO; + do { + NSAutoreleasePool * myPool = [[NSAutoreleasePool alloc] init]; + result = [processor processOnInputProtocol: inProtocol outputProtocol: outProtocol]; + [myPool release]; + } while (result); + } + @catch (TTransportException * te) { + //NSLog(@"Caught transport exception, abandoning client connection: %@", te); + } + + NSNotification * n = [NSNotification notificationWithName: kTSocketServer_ClientConnectionFinishedForProcessorNotification + object: self + userInfo: [NSDictionary dictionaryWithObjectsAndKeys: + processor, + kTSocketServer_ProcessorKey, + transport, + kTSockerServer_TransportKey, + nil]]; + [[NSNotificationCenter defaultCenter] performSelectorOnMainThread: @selector(postNotification:) withObject: n waitUntilDone: YES]; + + [pool release]; +#endif +} + + + +@end + + + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TAsyncTransport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TAsyncTransport.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TAsyncTransport.h new file mode 100644 index 0000000..f75b701 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TAsyncTransport.h @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TTransport.h" +#import "TException.h" + +typedef void(^TAsyncFailureBlock)(TException *); + +@protocol TAsyncTransport <TTransport> + +- (void) flush:(dispatch_block_t)flushed failure:(TAsyncFailureBlock)failure; + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.h new file mode 100644 index 0000000..fc38877 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.h @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TTransport.h" + +@interface TFramedTransport : NSObject <TTransport> { + id <TTransport> mTransport; +} + +- (id) initWithTransport: (id <TTransport>) transport; + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.m new file mode 100644 index 0000000..2148806 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TFramedTransport.m @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TFramedTransport.h" +#import "TTransportException.h" +#import "TObjective-C.h" + +#define HEADER_SIZE 4 +#define INIT_FRAME_SIZE 1024 + +@implementation TFramedTransport { + NSMutableData* writeBuffer; + NSMutableData* readBuffer; + NSUInteger readOffset; + uint8_t dummy_header[HEADER_SIZE]; +} + +- (id) initWithTransport:(id <TTransport>)transport +{ + mTransport = [transport retain_stub]; + readBuffer = nil; + readOffset = 0; + writeBuffer = [[NSMutableData alloc] initWithCapacity:INIT_FRAME_SIZE]; + [writeBuffer appendBytes:dummy_header length:HEADER_SIZE]; + return self; +} + +- (void) dealloc +{ + [mTransport release_stub]; + [writeBuffer release_stub]; + if (readBuffer != nil) + [readBuffer release_stub]; + [super dealloc_stub]; +} + +- (void)flush +{ + size_t headerAndDataLength = [writeBuffer length]; + if (headerAndDataLength < HEADER_SIZE) { + @throw [TTransportException exceptionWithReason:@"Framed transport buffer has no header"]; + } + + size_t dataLength = headerAndDataLength - HEADER_SIZE; + uint8_t i32rd[HEADER_SIZE]; + i32rd[0] = (uint8_t)(0xff & (dataLength >> 24)); + i32rd[1] = (uint8_t)(0xff & (dataLength >> 16)); + i32rd[2] = (uint8_t)(0xff & (dataLength >> 8)); + i32rd[3] = (uint8_t)(0xff & (dataLength)); + + // should we make a copy of the writeBuffer instead? Better for threaded operations! + [writeBuffer replaceBytesInRange:NSMakeRange(0, HEADER_SIZE) withBytes:i32rd length:HEADER_SIZE]; + [mTransport write:[writeBuffer mutableBytes] offset:0 length:headerAndDataLength]; + [mTransport flush]; + + // reuse old memory buffer + [writeBuffer setLength:0]; + [writeBuffer appendBytes:dummy_header length:HEADER_SIZE]; +} + +- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length +{ + [writeBuffer appendBytes:data+offset length:length]; +} + +- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length +{ + if (readBuffer == nil) { + [self readFrame]; + } + + if (readBuffer != nil) { + size_t bufferLength = [readBuffer length]; + if (bufferLength - readOffset >= length) { + [readBuffer getBytes:buf range:NSMakeRange(readOffset,length)]; // copy data + readOffset += length; + } else { + // void the previous readBuffer data and request a new frame + [self readFrame]; + [readBuffer getBytes:buf range:NSMakeRange(0,length)]; // copy data + readOffset = length; + } + } + return length; +} + +- (void)readFrame +{ + uint8_t i32rd[HEADER_SIZE]; + [mTransport readAll: i32rd offset: 0 length: HEADER_SIZE]; + int32_t headerValue = + ((i32rd[0] & 0xff) << 24) | + ((i32rd[1] & 0xff) << 16) | + ((i32rd[2] & 0xff) << 8) | + ((i32rd[3] & 0xff)); + if (headerValue < 0) { + NSString *reason = [NSString stringWithFormat: + @"Frame header reports negative frame size: %"PRId32, + headerValue]; + @throw [TTransportException exceptionWithReason:reason]; + } + + /* Cast should be safe: + * Have verified headerValue non-negative and of lesser or equal bitwidth to size_t. */ + size_t frameSize = (size_t)headerValue; + [self ensureReadBufferHasLength:frameSize]; + + [mTransport readAll:[readBuffer mutableBytes] offset:0 length:frameSize]; +} + +- (void)ensureReadBufferHasLength:(size_t)length +{ + if (readBuffer == nil) { + readBuffer = [[NSMutableData alloc] initWithLength:length]; + } else { + size_t currentLength = [readBuffer length]; + BOOL isTooLong = (currentLength >= length); + if (isTooLong) { + [readBuffer setLength:length]; + } else { + size_t lengthToAdd = length - currentLength; + [readBuffer increaseLengthBy:lengthToAdd]; + } + } +} + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.h new file mode 100644 index 0000000..78935fb --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.h @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TTransport.h" + +@interface THTTPClient : NSObject <TTransport> { + NSURL * mURL; + NSMutableURLRequest * mRequest; + NSMutableData * mRequestData; + NSData * mResponseData; + size_t mResponseDataOffset; + NSString * mUserAgent; + int mTimeout; +} + +- (id) initWithURL: (NSURL *) aURL; + +- (id) initWithURL: (NSURL *) aURL + userAgent: (NSString *) userAgent + timeout: (int) timeout; + +- (void) setURL: (NSURL *) aURL; + +@end + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.m new file mode 100644 index 0000000..169927c --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/THTTPClient.m @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "THTTPClient.h" +#import "TTransportException.h" +#import "TObjective-C.h" + +@implementation THTTPClient + + +- (void) setupRequest +{ + if (mRequest != nil) { + [mRequest release_stub]; + } + + // 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"]; + + NSString * userAgent = mUserAgent; + if (!userAgent) { + userAgent = @"Cocoa/THTTPClient"; + } + [mRequest setValue: userAgent forHTTPHeaderField: @"User-Agent"]; + + [mRequest setCachePolicy: NSURLRequestReloadIgnoringCacheData]; + if (mTimeout) { + [mRequest setTimeoutInterval: mTimeout]; + } +} + + +- (id) initWithURL: (NSURL *) aURL +{ + return [self initWithURL: aURL + userAgent: nil + timeout: 0]; +} + + +- (id) initWithURL: (NSURL *) aURL + userAgent: (NSString *) userAgent + timeout: (int) timeout +{ + self = [super init]; + if (!self) { + return nil; + } + + mTimeout = timeout; + if (userAgent) { + mUserAgent = [userAgent retain_stub]; + } + mURL = [aURL retain_stub]; + + [self setupRequest]; + + // create our request data buffer + mRequestData = [[NSMutableData alloc] initWithCapacity: 1024]; + + return self; +} + + +- (void) setURL: (NSURL *) aURL +{ + [aURL retain_stub]; + [mURL release_stub]; + mURL = aURL; + + [self setupRequest]; +} + + +- (void) dealloc +{ + [mURL release_stub]; + [mUserAgent release_stub]; + [mRequest release_stub]; + [mRequestData release_stub]; + [mResponseData release_stub]; + [super dealloc_stub]; +} + + +- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length +{ + NSRange r; + r.location = mResponseDataOffset; + r.length = length; + + [mResponseData getBytes: buf+offset range: r]; + mResponseDataOffset += length; + + return length; +} + + +- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) 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: @"TTransportException" + reason: @"Could not make HTTP request" + error: error]; + } + if (![response isKindOfClass: [NSHTTPURLResponse class]]) { + @throw [TTransportException exceptionWithName: @"TTransportException" + reason: [NSString stringWithFormat: @"Unexpected NSURLResponse type: %@", + NSStringFromClass([response class])]]; + } + + NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response; + if ([httpResponse statusCode] != 200) { + @throw [TTransportException exceptionWithName: @"TTransportException" + reason: [NSString stringWithFormat: @"Bad response from HTTP server: %ld", + (long)[httpResponse statusCode]]]; + } + + // phew! + [mResponseData release_stub]; + mResponseData = [responseData retain_stub]; + mResponseDataOffset = 0; +} + + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.h new file mode 100644 index 0000000..fa4d371 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.h @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TTransport.h" + +@interface TMemoryBuffer : NSObject <TTransport> { + NSMutableData *mBuffer; + NSUInteger mOffset; +} +- (id)initWithData:(NSData *)data; +- (NSData *)getBuffer; +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.m new file mode 100644 index 0000000..4513ab8 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TMemoryBuffer.m @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TMemoryBuffer.h" +#import "TTransportException.h" +#import "TObjective-C.h" + +#define GARBAGE_BUFFER_SIZE 4096 // 4KiB + +@implementation TMemoryBuffer +- (id)init { + if ((self = [super init])) { + mBuffer = [[NSMutableData alloc] init]; + mOffset = 0; + } + return self; +} + +- (id)initWithData:(NSData *)data { + if ((self = [super init])) { + mBuffer = [data mutableCopy]; + mOffset = 0; + } + return self; +} + +- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length +{ + if ([mBuffer length] - mOffset < length) { + @throw [TTransportException exceptionWithReason:@"Not enough bytes remain in buffer"]; + } + [mBuffer getBytes:buf range:NSMakeRange(mOffset, length)]; + mOffset += length; + if (mOffset >= GARBAGE_BUFFER_SIZE) { + [mBuffer replaceBytesInRange:NSMakeRange(0, mOffset) withBytes:NULL length:0]; + mOffset = 0; + } + return length; +} + +- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length +{ + [mBuffer appendBytes:data+offset length:length]; +} + +- (void)flush { + // noop +} + +- (NSData *)getBuffer { + return [[mBuffer copy] autorelease_stub]; +} + +- (void)dealloc { + [mBuffer release_stub]; + [super dealloc_stub]; +} +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.h new file mode 100644 index 0000000..ba2a209 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.h @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +#import <Foundation/Foundation.h> +#import "TTransport.h" + +@interface TNSFileHandleTransport : NSObject <TTransport> { + NSFileHandle * mInputFileHandle; + NSFileHandle * mOutputFileHandle; +} + +- (id) initWithFileHandle: (NSFileHandle *) fileHandle; + +- (id) initWithInputFileHandle: (NSFileHandle *) inputFileHandle + outputFileHandle: (NSFileHandle *) outputFileHandle; + + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.m new file mode 100644 index 0000000..c2b18ca --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSFileHandleTransport.m @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +#import "TNSFileHandleTransport.h" +#import "TTransportException.h" +#import "TObjective-C.h" + + +@implementation TNSFileHandleTransport + +- (id) initWithFileHandle: (NSFileHandle *) fileHandle +{ + return [self initWithInputFileHandle: fileHandle + outputFileHandle: fileHandle]; +} + + +- (id) initWithInputFileHandle: (NSFileHandle *) inputFileHandle + outputFileHandle: (NSFileHandle *) outputFileHandle +{ + self = [super init]; + + mInputFileHandle = [inputFileHandle retain_stub]; + mOutputFileHandle = [outputFileHandle retain_stub]; + + return self; +} + + +- (void) dealloc { + [mInputFileHandle release_stub]; + [mOutputFileHandle release_stub]; + [super dealloc_stub]; +} + + +- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length +{ + size_t totalBytesRead = 0; + while (totalBytesRead < length) { + NSData * data = [mInputFileHandle readDataOfLength: length-totalBytesRead]; + if ([data length] == 0) { + @throw [TTransportException exceptionWithName: @"TTransportException" + reason: @"Cannot read. No more data."]; + } + [data getBytes: buf+totalBytesRead]; + totalBytesRead += [data length]; + } + return totalBytesRead; +} + + +- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length +{ + const void *pos = data + offset; + NSData * dataObject = [[NSData alloc] initWithBytesNoCopy: (void *)pos + length: length + freeWhenDone: NO]; + + @try { + [mOutputFileHandle writeData: dataObject]; + } @catch (NSException * e) { + @throw [TTransportException exceptionWithName: @"TTransportException" + reason: [NSString stringWithFormat: @"%s: Unable to write data: %@", __PRETTY_FUNCTION__, e]]; + } + + [dataObject release_stub]; +} + + +- (void) flush +{ + +} + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.h new file mode 100644 index 0000000..8011fb9 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.h @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TTransport.h" + +@interface TNSStreamTransport : NSObject <TTransport> { + +} + +@property (nonatomic, strong) NSInputStream * mInput; +@property (nonatomic, strong) NSOutputStream * mOutput; + +- (id) initWithInputStream: (NSInputStream *) input + outputStream: (NSOutputStream *) output; + +- (id) initWithInputStream: (NSInputStream *) input; + +- (id) initWithOutputStream: (NSOutputStream *) output; + +@end + + + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.m new file mode 100644 index 0000000..7ac1cdc --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TNSStreamTransport.m @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TNSStreamTransport.h" +#import "TTransportException.h" +#import "TObjective-C.h" + + +@implementation TNSStreamTransport + +- (id) initWithInputStream: (NSInputStream *) input + outputStream: (NSOutputStream *) output +{ + self = [super init]; + self.mInput = [input retain_stub]; + self.mOutput = [output retain_stub]; + return self; +} + +- (id) initWithInputStream: (NSInputStream *) input +{ + return [self initWithInputStream: input outputStream: nil]; +} + +- (id) initWithOutputStream: (NSOutputStream *) output +{ + return [self initWithInputStream: nil outputStream: output]; +} + +- (void) dealloc +{ + [self.mInput release_stub]; + [self.mOutput release_stub]; + [super dealloc_stub]; +} + + +- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length +{ + size_t totalBytesRead = 0; + ssize_t bytesRead = 0; + while (totalBytesRead < length) { + bytesRead = [self.mInput read: buf+offset+totalBytesRead maxLength: length-totalBytesRead]; + + BOOL encounteredErrorOrEOF = (bytesRead <= 0); + if (encounteredErrorOrEOF) { + @throw [TTransportException exceptionWithReason: @"Cannot read. Remote side has closed."]; + } else { + /* bytesRead is guaranteed to be positive and within the range representable by size_t. */ + totalBytesRead += (size_t)bytesRead; + } + } + return totalBytesRead; +} + + +- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length +{ + size_t totalBytesWritten = 0; + ssize_t bytesWritten = 0; + while (totalBytesWritten < length) { + bytesWritten = [self.mOutput write: data+offset+totalBytesWritten maxLength: length-totalBytesWritten]; + if (bytesWritten < 0) { + @throw [TTransportException exceptionWithReason: @"Error writing to transport output stream." + error: [self.mOutput streamError]]; + } else if (bytesWritten == 0) { + @throw [TTransportException exceptionWithReason: @"End of output stream."]; + } else { + /* bytesWritten is guaranteed to be positive and within the range representable by size_t. */ + totalBytesWritten += (size_t)bytesWritten; + } + } +} + +- (void) flush +{ + // no flush for you! +} + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.h new file mode 100644 index 0000000..44de124 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.h @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TNSStreamTransport.h" + +@interface TSSLSocketClient : TNSStreamTransport +#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) +<NSStreamDelegate> +#endif +{ + NSInputStream *inputStream; + NSOutputStream *outputStream; +@private + NSString *sslHostname; + int sd; +} + +- (id) initWithHostname: (NSString *) hostname + port: (int) port; + +- (BOOL) isOpen; + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.m new file mode 100644 index 0000000..d8c55d6 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketClient.m @@ -0,0 +1,261 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#import <Foundation/Foundation.h> +#import <CoreFoundation/CoreFoundation.h> +#import "TSSLSocketClient.h" +#import "TSSLSocketException.h" +#import "TObjective-C.h" +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> + +#if !TARGET_OS_IPHONE +#import <CoreServices/CoreServices.h> +#else +#import <CFNetwork/CFNetwork.h> +#endif + +@implementation TSSLSocketClient + +- (id) initWithHostname: (NSString *) hostname + port: (int) port +{ + sslHostname = hostname; + CFReadStreamRef readStream = NULL; + CFWriteStreamRef writeStream = NULL; + + + /* create a socket structure */ + struct sockaddr_in pin; + struct hostent *hp = NULL; + for(int i = 0; i < 10; i++) { + + + + if ((hp = gethostbyname([hostname UTF8String])) == NULL) { + NSLog(@"failed to resolve hostname %@", hostname); + herror("resolv"); + if(i == 9) { + @throw [TSSLSocketException exceptionWithReason: @"failed to resolve hostname"]; + } + [NSThread sleepForTimeInterval:0.2]; + } else { + break; + } + } + + memset (&pin, 0, sizeof(pin)); + pin.sin_family = AF_INET; + memcpy(&pin.sin_addr, hp->h_addr, sizeof(struct in_addr)); + pin.sin_port = htons (port); + + /* create the socket */ + if ((sd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) + { + NSLog(@"failed to create socket for host %@:%d", hostname, port); + @throw [TSSLSocketException exceptionWithReason: @"failed to create socket"]; + } + + /* open a connection */ + if (connect (sd, (struct sockaddr *) &pin, sizeof(pin)) == -1) + { + NSLog(@"failed to create conenct to host %@:%d", hostname, port); + @throw [TSSLSocketException exceptionWithReason: @"failed to connect"]; + } + CFStreamCreatePairWithSocket(kCFAllocatorDefault, sd, &readStream, &writeStream); + + CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); + CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); + + if (readStream && writeStream) { + CFReadStreamSetProperty(readStream, + kCFStreamPropertySocketSecurityLevel, + kCFStreamSocketSecurityLevelTLSv1); + + NSDictionary *settings = + [NSDictionary dictionaryWithObjectsAndKeys: + (id)kCFBooleanTrue, (id)kCFStreamSSLValidatesCertificateChain, + nil]; + + CFReadStreamSetProperty((CFReadStreamRef)readStream, + kCFStreamPropertySSLSettings, + (CFTypeRef)settings); + CFWriteStreamSetProperty((CFWriteStreamRef)writeStream, + kCFStreamPropertySSLSettings, + (CFTypeRef)settings); + + inputStream = (bridge_stub NSInputStream *)readStream; + [inputStream retain_stub]; + [inputStream setDelegate:self]; + [inputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; + [inputStream open]; + + outputStream = (bridge_stub NSOutputStream *)writeStream; + [outputStream retain_stub]; + [outputStream setDelegate:self]; + [outputStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; + [outputStream open]; + + + CFRelease(readStream); + CFRelease(writeStream); + } + + + + self = [super initWithInputStream: inputStream outputStream: outputStream]; + + return self; +} + +#pragma mark - +#pragma mark NSStreamDelegate +- (void)stream:(NSStream *)aStream + handleEvent:(NSStreamEvent)eventCode { + switch (eventCode) { + case NSStreamEventNone: + break; + case NSStreamEventHasBytesAvailable: + break; + case NSStreamEventOpenCompleted: + break; + case NSStreamEventHasSpaceAvailable: + { + SecPolicyRef policy = SecPolicyCreateSSL(NO, (__bridge CFStringRef)(sslHostname)); + SecTrustRef trust = NULL; + CFArrayRef streamCertificatesRef = + CFBridgingRetain((__bridge id)((__bridge CFArrayRef)([aStream propertyForKey:(NSString *) kCFStreamPropertySSLPeerCertificates]))); + SecTrustCreateWithCertificates(CFBridgingRetain((__bridge id)(streamCertificatesRef)), + policy, + &trust); + + SecTrustResultType trustResultType = kSecTrustResultInvalid; + SecTrustEvaluate(trust, &trustResultType); + + BOOL proceed = NO; + switch (trustResultType) { + case kSecTrustResultProceed: + proceed = YES; + break; + case kSecTrustResultUnspecified: + NSLog(@"Trusted by OS"); + proceed = YES; + break; + case kSecTrustResultRecoverableTrustFailure: + proceed = recoverFromTrustFailure(trust); + break; + case kSecTrustResultDeny: + NSLog(@"Deny"); + break; + case kSecTrustResultFatalTrustFailure: + NSLog(@"FatalTrustFailure"); + break; + case kSecTrustResultOtherError: + NSLog(@"OtherError"); + break; + case kSecTrustResultInvalid: + NSLog(@"Invalid"); + break; + default: + NSLog(@"Default"); + break; + } + + if (trust) { + CFRelease(trust); + } + if (policy) { + CFRelease(policy); + } + if (!proceed) { + NSLog(@"Cannot trust certificate. TrustResultType: %u", trustResultType); + [aStream close]; + @throw [TSSLSocketException exceptionWithReason: @"Cannot trust certificate"]; + } + } + break; + case NSStreamEventErrorOccurred: + { + NSError *theError = [aStream streamError]; + NSLog(@"Error occurred opening stream: %@", theError); +// @throw [TSSLSocketException exceptionWithReason: @"Error occurred opening stream" error: theError]; + break; + } + case NSStreamEventEndEncountered: + break; + } +} + +bool recoverFromTrustFailure(SecTrustRef myTrust) +{ + + SecTrustResultType trustResult; + OSStatus status = SecTrustEvaluate(myTrust, &trustResult); + + CFAbsoluteTime trustTime,currentTime,timeIncrement,newTime; + CFDateRef newDate; + if (trustResult == kSecTrustResultRecoverableTrustFailure) { + trustTime = SecTrustGetVerifyTime(myTrust); + timeIncrement = 31536000; + currentTime = CFAbsoluteTimeGetCurrent(); + newTime = currentTime - timeIncrement; + if (trustTime - newTime){ + newDate = CFDateCreate(NULL, newTime); + SecTrustSetVerifyDate(myTrust, newDate); + status = SecTrustEvaluate(myTrust, &trustResult); + } + } + if (trustResult != kSecTrustResultProceed) { + NSLog(@"Certificate trust failure"); + return false; + } + return true; +} + +- (void)close +{ + if(self.mInput) { + //Close and reset inputstream + CFReadStreamSetProperty((__bridge CFReadStreamRef)(self.mInput), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); + [self.mInput setDelegate:nil]; + [self.mInput close]; + [self.mInput removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + self.mInput = nil; + } + + if(self.mOutput) { + //Close and reset outputstream + CFReadStreamSetProperty((__bridge CFReadStreamRef)(self.mOutput), kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); + [self.mOutput setDelegate:nil]; + [self.mOutput close]; + [self.mOutput removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + self.mOutput = nil; + } +} + +- (BOOL) isOpen +{ + if(sd > 0) + return TRUE; + else + return FALSE; +} + +@end + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.h new file mode 100644 index 0000000..c290b05 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.h @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TTransportException.h" + +@interface TSSLSocketException : TTransportException + ++ (id) exceptionWithReason: (NSString *) reason + error: (NSError *) error; + ++ (id) exceptionWithReason: (NSString *) reason; + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.m new file mode 100644 index 0000000..99eadf5 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSSLSocketException.m @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TSSLSocketException.h" + +@implementation TSSLSocketException + ++ (id) exceptionWithReason: (NSString *) reason + error: (NSError *) error +{ + NSDictionary * userInfo = nil; + if (error != nil) { + userInfo = [NSDictionary dictionaryWithObject: error forKey: @"error"]; + } + + return [super exceptionWithName: @"TSSLSocketException" + reason: reason + userInfo: userInfo]; +} + ++ (id) exceptionWithReason: (NSString *) reason +{ + return [self exceptionWithReason: reason error: nil]; +} + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.h new file mode 100644 index 0000000..81a0247 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.h @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import <Foundation/Foundation.h> +#import "TNSStreamTransport.h" + +@interface TSocketClient : TNSStreamTransport +#if TARGET_OS_IPHONE || (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) +<NSStreamDelegate> +#endif +{ +} + +- (id) initWithHostname: (NSString *) hostname + port: (UInt32) port; + +@end + + + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.m new file mode 100644 index 0000000..b0bac74 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TSocketClient.m @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#import "TSocketClient.h" +#import "TObjective-C.h" + +#if !TARGET_OS_IPHONE +#import <CoreServices/CoreServices.h> +#else +#import <CFNetwork/CFNetwork.h> +#endif + +@interface TSocketClient () +{ + NSInputStream * inputStream; + NSOutputStream * outputStream; +} +@end + +@implementation TSocketClient + +- (id) initWithHostname: (NSString *) hostname + port: (UInt32) port +{ + inputStream = NULL; + outputStream = NULL; + CFReadStreamRef readStream = NULL; + CFWriteStreamRef writeStream = NULL; + CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (bridge_stub CFStringRef)hostname, port, &readStream, &writeStream); + if (readStream && writeStream) { + CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); + CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); + + inputStream = (bridge_stub NSInputStream *)readStream; + [inputStream retain_stub]; + [inputStream setDelegate:self]; + [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + [inputStream open]; + + outputStream = (bridge_stub NSOutputStream *)writeStream; + [outputStream retain_stub]; + [outputStream setDelegate:self]; + [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + [outputStream open]; + CFRelease(readStream); + CFRelease(writeStream); + } + + self = [super initWithInputStream: inputStream outputStream: outputStream]; + + return self; +} + +-(void)dealloc +{ + [inputStream close]; + [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + [inputStream setDelegate:nil]; + [inputStream release_stub]; + + [outputStream close]; + [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + [outputStream setDelegate:nil]; + [outputStream release_stub]; + [super dealloc_stub]; +} + + +@end + + + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransport.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransport.h new file mode 100644 index 0000000..83aad9e --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransport.h @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +@protocol TTransport <NSObject> + + /** + * Guarantees that all of len bytes are read + * + * @param buf Buffer to read into + * @param offset Index in buffer to start storing bytes at + * @param length Maximum number of bytes to read + * @return The number of bytes actually read, which must be equal to len + * @throws TTransportException if there was an error reading data + */ +- (size_t) readAll: (uint8_t *) buf offset: (size_t) offset length: (size_t) length; + +- (void) write: (const uint8_t *) data offset: (size_t) offset length: (size_t) length; + +- (void) flush; +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.h b/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.h new file mode 100644 index 0000000..6749fe2 --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.h @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TException.h" + +@interface TTransportException : TException { +} + ++ (id) exceptionWithReason: (NSString *) reason + error: (NSError *) error; + ++ (id) exceptionWithReason: (NSString *) reason; + +@end http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.m ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.m b/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.m new file mode 100644 index 0000000..43cdfbd --- /dev/null +++ b/depends/thirdparty/thrift/lib/cocoa/src/transport/TTransportException.m @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#import "TTransportException.h" +#import "TObjective-C.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 http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cpp/3rdparty.props ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cpp/3rdparty.props b/depends/thirdparty/thrift/lib/cpp/3rdparty.props new file mode 100644 index 0000000..528d40a --- /dev/null +++ b/depends/thirdparty/thrift/lib/cpp/3rdparty.props @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ImportGroup Label="PropertySheets" /> + <PropertyGroup Label="UserMacros"> + <BOOST_ROOT>$(THIRD_PARTY)\boost\boost_1_47_0</BOOST_ROOT> + <OPENSSL_ROOT_DIR>$(THIRD_PARTY)\openssl\OpenSSL-Win32</OPENSSL_ROOT_DIR> + <LIBEVENT_ROOT>$(THIRD_PARTY)\libevent-2.0.21-stable</LIBEVENT_ROOT> + </PropertyGroup> + <PropertyGroup /> + <ItemDefinitionGroup /> + <ItemGroup> + <BuildMacro Include="BOOST_ROOT"> + <Value>$(BOOST_ROOT)</Value> + <EnvironmentVariable>true</EnvironmentVariable> + </BuildMacro> + <BuildMacro Include="OPENSSL_ROOT_DIR"> + <Value>$(OPENSSL_ROOT_DIR)</Value> + <EnvironmentVariable>true</EnvironmentVariable> + </BuildMacro> + <BuildMacro Include="LIBEVENT_ROOT"> + <Value>$(LIBEVENT_ROOT)</Value> + <EnvironmentVariable>true</EnvironmentVariable> + </BuildMacro> + </ItemGroup> +</Project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/cpp/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/cpp/CMakeLists.txt b/depends/thirdparty/thrift/lib/cpp/CMakeLists.txt new file mode 100755 index 0000000..4c7caeb --- /dev/null +++ b/depends/thirdparty/thrift/lib/cpp/CMakeLists.txt @@ -0,0 +1,209 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +# Find required packages +if(WITH_BOOSTTHREADS) + find_package(Boost 1.53.0 REQUIRED COMPONENTS system thread) +else() + find_package(Boost 1.53.0 REQUIRED) +endif() + +include_directories(SYSTEM "${Boost_INCLUDE_DIRS}") +include_directories(src) + +# SYSLIBS contains libraries that need to be linked to all lib targets +set(SYSLIBS "") + +# Create the thrift C++ library +set( thriftcpp_SOURCES + src/thrift/TApplicationException.cpp + src/thrift/TOutput.cpp + src/thrift/async/TAsyncChannel.cpp + src/thrift/async/TConcurrentClientSyncInfo.h + src/thrift/async/TConcurrentClientSyncInfo.cpp + src/thrift/concurrency/ThreadManager.cpp + src/thrift/concurrency/TimerManager.cpp + src/thrift/concurrency/Util.cpp + src/thrift/processor/PeekProcessor.cpp + src/thrift/protocol/TBase64Utils.cpp + src/thrift/protocol/TDebugProtocol.cpp + src/thrift/protocol/TJSONProtocol.cpp + src/thrift/protocol/TMultiplexedProtocol.cpp + src/thrift/protocol/TProtocol.cpp + src/thrift/transport/TTransportException.cpp + src/thrift/transport/TFDTransport.cpp + src/thrift/transport/TSimpleFileTransport.cpp + src/thrift/transport/THttpTransport.cpp + src/thrift/transport/THttpClient.cpp + src/thrift/transport/THttpServer.cpp + src/thrift/transport/TSocket.cpp + src/thrift/transport/TSocketPool.cpp + src/thrift/transport/TServerSocket.cpp + src/thrift/transport/TTransportUtils.cpp + src/thrift/transport/TBufferTransports.cpp + src/thrift/server/TConnectedClient.cpp + src/thrift/server/TServerFramework.cpp + src/thrift/server/TSimpleServer.cpp + src/thrift/server/TThreadPoolServer.cpp + src/thrift/server/TThreadedServer.cpp +) + +# This files don't work on Windows CE as there is no pipe support +# TODO: These files won't work with UNICODE support on windows. If fixed this can be re-added. +if (NOT WINCE) + list(APPEND thriftcpp_SOURCES + src/thrift/transport/TPipe.cpp + src/thrift/transport/TPipeServer.cpp + src/thrift/transport/TFileTransport.cpp + ) +endif() + + +if (WIN32) + list(APPEND thriftcpp_SOURCES + src/thrift/windows/TWinsockSingleton.cpp + src/thrift/windows/SocketPair.cpp + src/thrift/windows/GetTimeOfDay.cpp + src/thrift/windows/WinFcntl.cpp + ) + if(NOT WINCE) + # This file uses pipes so it currently won't work on Windows CE + list(APPEND thriftcpp_SOURCES + src/thrift/windows/OverlappedSubmissionThread.cpp + ) + endif() +else() + # These files evaluate to nothing on Windows, so omit them from the + # Windows build + list(APPEND thriftcpp_SOURCES + src/thrift/VirtualProfiling.cpp + src/thrift/server/TServer.cpp + ) +endif() + +# If OpenSSL is not found just ignore the OpenSSL stuff +find_package(OpenSSL) +if(OPENSSL_FOUND AND WITH_OPENSSL) + list( APPEND thriftcpp_SOURCES + src/thrift/transport/TSSLSocket.cpp + src/thrift/transport/TSSLServerSocket.cpp + ) + include_directories(SYSTEM "${OPENSSL_INCLUDE_DIR}") + list(APPEND SYSLIBS "${OPENSSL_LIBRARIES}") +endif() + +# WITH_*THREADS selects which threading library to use +if(WITH_BOOSTTHREADS) + set( thriftcpp_threads_SOURCES + src/thrift/concurrency/BoostThreadFactory.cpp + src/thrift/concurrency/BoostMonitor.cpp + src/thrift/concurrency/BoostMutex.cpp + ) + list(APPEND SYSLIBS "${Boost_LIBRARIES}") +elseif(UNIX AND NOT WITH_STDTHREADS) + list(APPEND SYSLIBS pthread) + set( thriftcpp_threads_SOURCES + src/thrift/concurrency/PosixThreadFactory.cpp + src/thrift/concurrency/Mutex.cpp + src/thrift/concurrency/Monitor.cpp + ) +else() + if(UNIX) + # need pthread for multi-thread support + list(APPEND SYSLIBS pthread) + endif() + set( thriftcpp_threads_SOURCES + src/thrift/concurrency/StdThreadFactory.cpp + src/thrift/concurrency/StdMutex.cpp + src/thrift/concurrency/StdMonitor.cpp + ) +endif() + +# Thrift non blocking server +set( thriftcppnb_SOURCES + src/thrift/server/TNonblockingServer.cpp + src/thrift/async/TAsyncProtocolProcessor.cpp + src/thrift/async/TEvhttpServer.cpp + src/thrift/async/TEvhttpClientChannel.cpp +) + +# Thrift zlib server +set( thriftcppz_SOURCES + src/thrift/transport/TZlibTransport.cpp +) + +# Thrift Qt4 server +set( thriftcppqt_SOURCES + src/thrift/qt/TQIODeviceTransport.cpp + src/thrift/qt/TQTcpServer.cpp +) + +# Contains the thrift specific ADD_LIBRARY_THRIFT and TARGET_LINK_LIBRARIES_THRIFT +include(ThriftMacros) + +ADD_LIBRARY_THRIFT(thrift ${thriftcpp_SOURCES} ${thriftcpp_threads_SOURCES}) +TARGET_LINK_LIBRARIES_THRIFT(thrift ${SYSLIBS}) + +if(WITH_LIBEVENT) + find_package(Libevent REQUIRED) # Libevent comes with CMake support form upstream + include_directories(SYSTEM ${LIBEVENT_INCLUDE_DIRS}) + + ADD_LIBRARY_THRIFT(thriftnb ${thriftcppnb_SOURCES}) + TARGET_LINK_LIBRARIES_THRIFT(thriftnb ${SYSLIBS} ${LIBEVENT_LIBRARIES}) + TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY(thriftnb thrift) +endif() + +if(WITH_ZLIB) + find_package(ZLIB REQUIRED) + include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS}) + + ADD_LIBRARY_THRIFT(thriftz ${thriftcppz_SOURCES}) + TARGET_LINK_LIBRARIES_THRIFT(thriftz ${SYSLIBS} ${ZLIB_LIBRARIES}) + TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY(thriftz thrift) +endif() + +if(WITH_QT4) + set(CMAKE_AUTOMOC ON) + find_package(Qt4 REQUIRED COMPONENTS QtCore QtNetwork) + ADD_LIBRARY_THRIFT(thriftqt ${thriftcppqt_SOURCES}) + TARGET_LINK_LIBRARIES_THRIFT(thriftqt ${SYSLIBS} Qt4::QtCore Qt4::QtNetwork) + TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY(thriftqt thrift) +endif() + +if(WITH_QT5) + # Qt5 has its own directory to avoid conflict with Qt4 caused by CMAKE_AUTOMOC + add_subdirectory(src/thrift/qt) +endif() + +if(MSVC) + add_definitions("-DUNICODE -D_UNICODE") +endif() + +add_definitions("-D__STDC_LIMIT_MACROS") + +# Install the headers +install(DIRECTORY "src/thrift" DESTINATION "${INCLUDE_INSTALL_DIR}" + FILES_MATCHING PATTERN "*.h" PATTERN "*.tcc") +# Copy config.h file +install(DIRECTORY "${CMAKE_BINARY_DIR}/thrift" DESTINATION "${INCLUDE_INSTALL_DIR}" + FILES_MATCHING PATTERN "*.h") + +if(BUILD_TESTING) + add_subdirectory(test) +endif()
