00001 // 00002 // Connection.h 00003 // DistributedCalendar 00004 // 00005 // Created by Jagdish on 8/5/10. 00006 // Copyright 2010 __MyCompanyName__. All rights reserved. 00007 // 00008 00032 #import <Foundation/Foundation.h> 00033 #import <CFNetwork/CFSocketStream.h> 00034 #import "ConnectionDelegate.h" 00035 00036 @class Request; 00037 00041 @interface Connection : NSObject { 00042 00043 id<ConnectionDelegate> delegate; 00044 00045 // Connection info: host address and port 00046 NSString* host; 00047 int port; 00048 00049 // Connection info: native socket handle 00050 CFSocketNativeHandle connectedSocketHandle; 00051 00052 // Connection info: NSNetService 00053 NSNetService* netService; 00054 00055 // Read stream 00056 CFReadStreamRef readStream; 00057 bool readStreamOpen; 00058 NSMutableData* incomingDataBuffer; 00059 int packetBodySize; 00060 00061 // Write stream 00062 CFWriteStreamRef writeStream; 00063 bool writeStreamOpen; 00064 NSMutableData* outgoingDataBuffer; 00065 00066 // Booleans 00067 BOOL resolved; 00068 } 00069 00070 @property (nonatomic, retain) id<ConnectionDelegate> delegate; 00071 00072 @property (nonatomic) BOOL resolved; 00073 00074 // Initialize and store connection information until 'connect' is called 00075 - (id)initWithHostAddress:(NSString*)host andPort:(int)port; 00076 00077 // Initialize using a native socket handle, assuming connection is open 00078 - (id)initWithNativeSocketHandle:(CFSocketNativeHandle)nativeSocketHandle; 00079 00080 // Initialize using an instance of NSNetService 00081 - (id)initWithNetService:(NSNetService*)netService; 00082 00083 // Connect using whatever connection info that was passed during initialization 00084 - (BOOL)connect; 00085 00086 // Close connection 00087 - (void)close; 00088 00089 // Send network message 00090 - (void)sendNetworkPacket:(NSDictionary*)packet; 00091 00092 00093 // gethost and getport for testing purpose only 00094 - (NSString*)getHost; 00095 - (NSInteger)getPort; 00096 00097 @end