2006-01-20 15:34:02 +00:00
|
|
|
//
|
2006-04-02 15:44:08 +00:00
|
|
|
// Node.m
|
2006-01-20 15:34:02 +00:00
|
|
|
// CogNew
|
|
|
|
//
|
2006-09-04 18:46:18 +00:00
|
|
|
// Created by Vincent Spader on 1/4/06.
|
|
|
|
// Copyright 2006 Vincent Spader. All rights reserved.
|
2006-01-20 15:34:02 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "Node.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
2021-01-27 02:12:07 +00:00
|
|
|
#import "BufferChain.h"
|
2013-10-11 12:03:55 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
@implementation Node
|
|
|
|
|
|
|
|
- (id)initWithController:(id)c previous:(id)p
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
buffer = [[VirtualRingBuffer alloc] initWithLength:BUFFER_SIZE];
|
|
|
|
semaphore = [[Semaphore alloc] init];
|
|
|
|
|
2007-03-19 00:19:47 +00:00
|
|
|
initialBufferFilled = NO;
|
|
|
|
|
2008-02-16 02:52:49 +00:00
|
|
|
controller = c;
|
2006-04-02 15:44:08 +00:00
|
|
|
endOfStream = NO;
|
|
|
|
shouldContinue = YES;
|
2008-02-16 02:46:19 +00:00
|
|
|
|
|
|
|
[self setPreviousNode:p];
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)writeData:(void *)ptr amount:(int)amount
|
|
|
|
{
|
|
|
|
void *writePtr;
|
|
|
|
int amountToCopy, availOutput;
|
|
|
|
int amountLeft = amount;
|
2006-04-02 15:44:08 +00:00
|
|
|
|
|
|
|
while (shouldContinue == YES && amountLeft > 0)
|
2006-01-20 15:34:02 +00:00
|
|
|
{
|
|
|
|
availOutput = [buffer lengthAvailableToWriteReturningPointer:&writePtr];
|
2007-10-03 20:23:14 +00:00
|
|
|
if (availOutput == 0) {
|
2007-03-19 00:19:47 +00:00
|
|
|
if (initialBufferFilled == NO) {
|
2007-10-03 20:23:14 +00:00
|
|
|
initialBufferFilled = YES;
|
2007-10-12 02:55:59 +00:00
|
|
|
if ([controller respondsToSelector:@selector(initialBufferFilled:)])
|
|
|
|
[controller performSelector:@selector(initialBufferFilled:) withObject:self];
|
2007-03-19 00:19:47 +00:00
|
|
|
}
|
2007-10-03 20:23:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (availOutput == 0 || shouldReset)
|
|
|
|
{
|
2022-01-24 03:36:33 +00:00
|
|
|
if (availOutput)
|
|
|
|
{
|
2022-01-24 03:53:50 +00:00
|
|
|
// Unlock the buffer
|
|
|
|
[buffer didWriteLength:0];
|
2022-01-24 03:36:33 +00:00
|
|
|
}
|
2006-01-20 15:34:02 +00:00
|
|
|
[semaphore wait];
|
2006-04-02 15:44:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
amountToCopy = availOutput;
|
|
|
|
if (amountToCopy > amountLeft)
|
|
|
|
amountToCopy = amountLeft;
|
2006-01-20 15:34:02 +00:00
|
|
|
|
2006-04-02 15:44:08 +00:00
|
|
|
memcpy(writePtr, &((char *)ptr)[amount - amountLeft], amountToCopy);
|
2022-01-24 03:36:33 +00:00
|
|
|
[buffer didWriteLength:amountToCopy];
|
2006-01-20 15:34:02 +00:00
|
|
|
|
2006-04-02 15:44:08 +00:00
|
|
|
amountLeft -= amountToCopy;
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
2006-04-02 15:44:08 +00:00
|
|
|
}
|
2006-01-20 15:34:02 +00:00
|
|
|
|
|
|
|
return (amount - amountLeft);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Should be overwriten by subclass.
|
|
|
|
- (void)process
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)threadEntry:(id)arg
|
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
[self process];
|
|
|
|
}
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int)readData:(void *)ptr amount:(int)amount
|
|
|
|
{
|
|
|
|
void *readPtr;
|
|
|
|
int amountToCopy;
|
|
|
|
int availInput;
|
2022-01-13 07:15:23 +00:00
|
|
|
|
|
|
|
if ([[previousNode buffer] isEmpty] && [previousNode endOfStream] == YES)
|
|
|
|
{
|
|
|
|
endOfStream = YES;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-20 15:34:02 +00:00
|
|
|
|
|
|
|
availInput = [[previousNode buffer] lengthAvailableToReadReturningPointer:&readPtr];
|
|
|
|
|
2007-07-11 01:20:32 +00:00
|
|
|
/* if (availInput <= 0) {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"BUFFER RAN DRY!");
|
2007-03-19 00:19:47 +00:00
|
|
|
}
|
|
|
|
else if (availInput < amount) {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"BUFFER IN DANGER");
|
2007-03-19 00:19:47 +00:00
|
|
|
}
|
2007-07-11 01:20:32 +00:00
|
|
|
*/
|
2007-10-03 20:23:14 +00:00
|
|
|
|
2022-01-24 03:36:33 +00:00
|
|
|
if ([previousNode shouldReset] == YES) {
|
2007-10-03 20:23:14 +00:00
|
|
|
[buffer empty];
|
|
|
|
|
|
|
|
shouldReset = YES;
|
|
|
|
[previousNode setShouldReset: NO];
|
|
|
|
|
|
|
|
[[previousNode semaphore] signal];
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
amountToCopy = availInput;
|
2006-04-02 15:44:08 +00:00
|
|
|
if (amountToCopy > amount)
|
2006-01-20 15:34:02 +00:00
|
|
|
{
|
|
|
|
amountToCopy = amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ptr, readPtr, amountToCopy);
|
|
|
|
|
2022-01-24 03:36:33 +00:00
|
|
|
[[previousNode buffer] didReadLength:amountToCopy];
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
if (amountToCopy > 0)
|
|
|
|
{
|
|
|
|
[[previousNode semaphore] signal];
|
|
|
|
}
|
|
|
|
|
|
|
|
return amountToCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)launchThread
|
|
|
|
{
|
|
|
|
[NSThread detachNewThreadSelector:@selector(threadEntry:) toTarget:self withObject:nil];
|
|
|
|
}
|
|
|
|
|
2008-02-16 02:46:19 +00:00
|
|
|
- (void)setPreviousNode:(id)p
|
|
|
|
{
|
|
|
|
previousNode = p;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
- (id)previousNode
|
|
|
|
{
|
|
|
|
return previousNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)shouldContinue
|
|
|
|
{
|
|
|
|
return shouldContinue;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setShouldContinue:(BOOL)s
|
|
|
|
{
|
|
|
|
shouldContinue = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (VirtualRingBuffer *)buffer
|
|
|
|
{
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2006-04-02 20:03:12 +00:00
|
|
|
- (void)resetBuffer
|
|
|
|
{
|
2007-10-03 20:23:14 +00:00
|
|
|
shouldReset = YES; //Will reset on next write.
|
|
|
|
if (previousNode == nil) {
|
|
|
|
[buffer empty];
|
|
|
|
}
|
2006-04-02 20:03:12 +00:00
|
|
|
}
|
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
- (Semaphore *)semaphore
|
|
|
|
{
|
|
|
|
return semaphore;
|
|
|
|
}
|
|
|
|
|
2006-04-02 15:44:08 +00:00
|
|
|
- (BOOL)endOfStream
|
2006-01-20 15:34:02 +00:00
|
|
|
{
|
2006-04-02 15:44:08 +00:00
|
|
|
return endOfStream;
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2006-04-02 15:44:08 +00:00
|
|
|
- (void)setEndOfStream:(BOOL)e
|
2006-01-20 15:34:02 +00:00
|
|
|
{
|
2006-04-02 15:44:08 +00:00
|
|
|
endOfStream = e;
|
2006-01-20 15:34:02 +00:00
|
|
|
}
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
- (void)setShouldReset:(BOOL)s
|
|
|
|
{
|
|
|
|
shouldReset = s;
|
|
|
|
}
|
|
|
|
- (BOOL)shouldReset
|
|
|
|
{
|
|
|
|
return shouldReset;
|
|
|
|
}
|
|
|
|
|
2022-01-14 07:05:32 +00:00
|
|
|
// Buffering nodes should implement this
|
|
|
|
- (double)secondsBuffered
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2007-10-03 20:23:14 +00:00
|
|
|
|
2006-01-20 15:34:02 +00:00
|
|
|
@end
|