From 68089b190f6a03c185722c8532197dacabf52f8d Mon Sep 17 00:00:00 2001 From: Alexander Strange Date: Thu, 24 Jul 2003 01:29:54 +0000 Subject: [PATCH] This whole thing could use a rewrite but whatever --- ArrayQueue.h | 59 +++++++++++++++++ ArrayQueue.m | 144 ++++++++++++++++++++++++++++++++++++++++++ ITByteStream.h | 13 ++-- ITByteStream.m | 4 +- ITChunkedByteStream.h | 17 +++-- ITChunkedByteStream.m | 49 ++++++++++++++ ITInetSocket.h | 4 +- 7 files changed, 274 insertions(+), 16 deletions(-) create mode 100755 ArrayQueue.h create mode 100755 ArrayQueue.m diff --git a/ArrayQueue.h b/ArrayQueue.h new file mode 100755 index 0000000..34a3e80 --- /dev/null +++ b/ArrayQueue.h @@ -0,0 +1,59 @@ +/************************ +A Cocoa DataStructuresFramework +Copyright (C) 2002 Phillip Morelock in the United States +http://www.phillipmorelock.com +Other copyrights for this specific file as acknowledged herein. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*******************************/ +/* + * ArrayQueue.h + * Data Structures Framework +/////SEE LICENSE FILE FOR LICENSE INFORMATION/////// + * + */ + +////////// +//A fairly basic queue implementation that puts its data in an NSMutableArray +//See the protocol definition for Queue to understand the contract. +///////// + +#import +#import "Queue.h" + +@interface ArrayQueue : NSObject +{ + NSMutableArray *theQ; + + int backIndex; //where to place the next element + int frontIndex; //the current front of the queue + unsigned int qSize; //the current size + unsigned int arrsz; + + id niller; //the marker for dead spots in the queue +} + +- init; +- initWithCapacity:(unsigned)capacity; + +//returns the size of the queue currently +-(unsigned int) count; + +/** + * see protocol declaration for Queue + */ ++(ArrayQueue *)queueWithArray:(NSArray *)array + ofOrder:(BOOL)direction; +@end \ No newline at end of file diff --git a/ArrayQueue.m b/ArrayQueue.m new file mode 100755 index 0000000..400ef4f --- /dev/null +++ b/ArrayQueue.m @@ -0,0 +1,144 @@ +/************************ +A Cocoa DataStructuresFramework +Copyright (C) 2002 Phillip Morelock in the United States +http://www.phillipmorelock.com +Other copyrights for this specific file as acknowledged herein. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*******************************/ + +/////SEE LICENSE FILE FOR LICENSE INFORMATION/////// + +#import "ArrayQueue.h" + +@implementation ArrayQueue + +- init +{ + return [self initWithCapacity:10]; +} + +-initWithCapacity:(unsigned)capacity +{ + self = [super init]; + + theQ = [[NSMutableArray alloc] initWithCapacity:capacity]; + + backIndex = -1; + frontIndex = 0; + qSize = 0; + + niller = [[NSString stringWithFormat:@"nothing"] retain]; + + return self; +} + +-(void) dealloc +{ + [theQ release]; + [niller release]; + [super dealloc]; +} + +-(BOOL) enqueue: (id)enqueuedObj +{ + if ( enqueuedObj == nil ) + return NO; + + ++backIndex; + [theQ insertObject:enqueuedObj atIndex:backIndex]; + ++qSize; + return YES; +} + +- dequeue +{ + id theObj; + + if ( qSize < 1 ) + return nil; + + //decrement the size of the Q + --qSize; + + //get it and retain it + theObj = [[theQ objectAtIndex:frontIndex] retain]; + [theQ replaceObjectAtIndex:frontIndex withObject:niller]; + + //now increment front -- if we have large array and we've "caught up" with + //the back, then let's dealloc and start over. + ++frontIndex; + if (frontIndex > 25 && qSize < 0) + { + [self removeAllObjects]; + } + + return [theObj autorelease]; +} + +-(unsigned) count +{ + return qSize; +} + +//simple BOOL for whether the queue is empty or not. +-(BOOL) isEmpty +{ + if ( qSize < 1) + return YES; + else + return NO; +} + +-(void) removeAllObjects +{ + if (theQ) + [theQ release]; + + theQ = [[NSMutableArray alloc] initWithCapacity:10]; + backIndex = -1; + frontIndex = 0; + qSize = 0; + +} + ++(ArrayQueue *)queueWithArray:(NSArray *)array + ofOrder:(BOOL)direction +{ + ArrayQueue *q; + int i,s; + + q = [[ArrayQueue alloc] init]; + s = [array count]; + i = 0; + + if (!array || !s) + {}//nada + else if (direction)//so the order to dequeue will be from 0...n + { + while (i < s) + [q enqueue: [array objectAtIndex: i++]]; + } + else //order to dequeue will be n...0 + { + while (s > i) + [q enqueue: [array objectAtIndex: --s]]; + } + + return [q autorelease]; +} + + +@end diff --git a/ITByteStream.h b/ITByteStream.h index 78ab7d9..3dce5c6 100755 --- a/ITByteStream.h +++ b/ITByteStream.h @@ -7,23 +7,18 @@ // #import +#import "ITConveniences.h" @class ITByteStream; -@protocol ITByteStreamDelegate --(oneway void)newDataAdded:(ITByteStream *)sender; -@end - -@interface ITByteStream : NSObject { +@interface ITByteStream : NSObject { @public NSMutableData *data; @private NSLock *lock; - id delegate; + id delegate; } --(id) initWithStream:(ITByteStream*)stream delegate:(id )d; --(id) initWithDelegate:(id )d; --(void) setDelegate:(id )d; +-(id) initWithStream:(ITByteStream*)stream delegate:(id )d; -(int) availableDataLength; -(NSData*) readDataOfLength:(int)length; -(NSData*) readAllData; diff --git a/ITByteStream.m b/ITByteStream.m index 3a878f3..e4607b4 100755 --- a/ITByteStream.m +++ b/ITByteStream.m @@ -53,10 +53,12 @@ [super dealloc]; } --(void) setDelegate:(id )d +-setDelegate:(id )d { + id old = delegate; [delegate release]; delegate = [d retain]; + return old; } -(int) availableDataLength diff --git a/ITChunkedByteStream.h b/ITChunkedByteStream.h index 47d90a3..a8f758c 100755 --- a/ITChunkedByteStream.h +++ b/ITChunkedByteStream.h @@ -7,10 +7,19 @@ // #import +#import "ITByteStream.h" +#import "ArrayQueue.h" - -@interface ITChunkedByteStream : NSObject { - +@interface ITChunkedByteStream : NSObject { + @public + ArrayQueue *q; + @private + NSLock *lock; + id delegate; } - +-(BOOL)empty; +-(NSData*) readData; +-(oneway void) writeData:(in NSData*)data; +-(oneway void) writeBytesNoCopy:(in char *)b len:(unsigned long)length; +-(oneway void) writeBytes:(in char *)b len:(unsigned long)length; @end diff --git a/ITChunkedByteStream.m b/ITChunkedByteStream.m index 1299503..cb31795 100755 --- a/ITChunkedByteStream.m +++ b/ITChunkedByteStream.m @@ -10,5 +10,54 @@ @implementation ITChunkedByteStream +-initWithDelegate:(id)d +{ + if (self = [super init]) { + q = [[ArrayQueue alloc] init]; + lock = [[NSLock alloc] init]; + delegate = [d retain]; + } + return self; +} +-(BOOL)empty +{ + BOOL a; + [lock lock]; + a = [q isEmpty]; + [lock unlock]; + return a; +} + +-(NSData*) readData +{ + NSData *d; + [lock lock]; + d = (NSData*)[q dequeue]; + [lock unlock]; + return d; +} + +-(oneway void) writeData:(in NSData*)d +{ + [lock lock]; + [q enqueue:d]; + [lock unlock]; +} + +-(oneway void) writeBytesNoCopy:(in char *)b len:(unsigned long)length +{ + [lock lock]; + [q enqueue:[NSData dataWithBytesNoCopy:b length:length]]; + [lock unlock]; +} + +-(oneway void) writeBytes:(in char *)b len:(unsigned long)length +{ + [lock lock]; + [q enqueue:[NSData dataWithBytes:b length:length]]; + [lock unlock]; +} +-delegate {return delegate;} +-setDelegate:(id)d {id old = delegate; [delegate release]; delegate = [d retain]; return old;} @end diff --git a/ITInetSocket.h b/ITInetSocket.h index 1613c69..592cc5d 100755 --- a/ITInetSocket.h +++ b/ITInetSocket.h @@ -54,7 +54,7 @@ typedef enum { * @abstract Delegate methods for ITInetSocket * @discussion ITInetSockets use these methods to communicate with their delegates */ -@protocol ITInetSocketDelegate +@protocol ITInetSocketDelegate /*! * @method errorOccured:during:onSocket: * @abstract Alerts the delegate of an error condition. @@ -78,7 +78,7 @@ typedef enum { * @abstract An Internet socket class. * @discussion ITInetSocket is an Internet socket class supporting IPv6 and Rendezvous. */ -@interface ITInetSocket : NSObject { +@interface ITInetSocket : NSObject { int sockfd; int port; int nc; -- 2.20.1