Stream data in iphone

Hi Friends,

I am hassanvfx, and i will share with the software developer community little bits of code that was part of the research process in our applications.

For this first post i want to share this partial implementation data stream over TCP / IP.

i really hope that someone there could find it useful.


//
// TheStream.h
// CoordinArt
//
// Created by hassanvfx on 21/03/09.
// Copyright 2009 hassanvfx. All rights reserved.
//

#import Foundation/Foundation.h


@interface TheStream : NSObject {

NSOutputStream *oStream;
NSInputStream *iStream;
NSMutableString *streambuffer;
BOOL sending;
NSUInteger _connections;
NSUInteger _oldconnections;
NSTimeInterval _dogPatient;
NSTimer *dogWatcher;

}

@property (retain,nonatomic) NSStream *iStream;
@property (retain,nonatomic) NSStream *oStream;
@property (retain,nonatomic) NSMutableString *streambuffer;
@property (assign) BOOL sending;
@property (assign) NSUInteger _connections;
@property (assign) NSUInteger _oldconnections;
@property (assign) NSTimeInterval _dogPatient;
@property (retain,nonatomic)NSTimer *dogWatcher;

- (void) startSTREAMING;
- (void) streamRealtime;
- (void) streamPackage;
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode;
- (void) stopstream;
- (void) startstream;
- (void) loopstream;
- (void) killstreams;
- (void) dogWatchStreams;




@end



//
// TheStream.m
// CoordinArt
//
// Created by hassanvfx on 21/03/09.
// Copyright 2009 hassanvfx. All rights reserved.
//

#import "TheStream.h"
#import "Globals.h"

@implementation TheStream

@synthesize iStream,oStream,sending,streambuffer,_connections,_oldconnections,dogWatcher,_dogPatient;

- (void)startSTREAMING {

//NSHost *host = [NSHost hostWithAddress:(NSString *)GLOBALRemoteIP];
NSHost *host = [NSHost hostWithAddress:(NSString *)GLOBALRemoteIP];
if (host != nil)
{
// iStream and oStream are instance variables
[NSStream getStreamsToHost:host port:22222 inputStream:&iStream outputStream:&oStream];

//iStream is instance var of NSSInputStream
[iStream retain];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream open];

//oStream is instance var of NSSOutputStream
[oStream retain];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self loopstream];

NSError *streamError;
streamError = [iStream streamError];
streamError = [oStream streamError];

NSStreamStatus streamStatus;
streamStatus = [iStream streamStatus];
streamStatus = [oStream streamStatus];
}

}

- (void) streamRealtime{

[self startstream];

}


- (void) streamPackage{

}

- (void) startstream{


if(sending ==FALSE){
[oStream open];
}
sending=TRUE;

}

- (void) stopstream{


if(sending ==TRUE){
[oStream close];
}
sending=FALSE;

}

- (void) loopstream{

if(sending ==TRUE){
[self stopstream];
}
[self startstream];


//resetear al perro
if ([dogWatcher isValid]) {
[dogWatcher setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}

}
-(void) killstreams {

[iStream close];
[iStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[iStream release];
iStream = nil;

[oStream close];
[oStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream release];
oStream = nil;
}

-(void) dogWatchStreams{
dogWatcher = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(dogReviewStreams) userInfo:nil repeats:YES];

}

-(void) dogReviewStreams{
if(_oldconnections==_connections && sending==TRUE){
_connections=0;
_oldconnections=1000;
[self killstreams];
[self startSTREAMING];
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Dog Wathcer" message:@"i trap a leak" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"ok",nil];
//[alert show];

}
_oldconnections=_connections;
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSString *io;
if (theStream == iStream) io = @">>";
else io = @"<<"; NSString *event; switch (streamEvent) { case NSStreamEventNone: event = @"None"; //resultText.font = [UIFont fontWithName:@"Helvetica" size:10.0]; //resultText.textColor = [UIColor whiteColor]; //resultText.text = [[NSString alloc] initWithFormat: @"Can not connect to the host!"]; break; case NSStreamEventOpenCompleted: event = @"Connected"; break; /* case NSStreamEventHasBytesAvailable: event = @"NSStreamEventHasBytesAvailable"; if (theStream == iStream) { //read data uint8_t buffer[1024]; int len; while ([iStream hasBytesAvailable]) { len = [iStream read:buffer maxLength:sizeof(buffer)]; if (len > 0)
{
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
//NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
if (nil != output)
{
//do something with data
}
}
}
}
break;
*/
#pragma mark WORKING HERE!!!!!!!!!!!!!!!!!!

case NSStreamEventHasSpaceAvailable:
event = @"Sending";
if (theStream == oStream) {

NSString *valor;

//valor=[NSString stringWithFormat:@"STAMP %.6f %.6f %.6f %.6f", currentAcceleration.timestamp,currentAcceleration.x,currentAcceleration.y,currentAcceleration.z];
valor=[[NSString alloc] initWithString:streambuffer];

//NSString *_accel=@"aca";
NSString *_accel=valor;

//NSString *_accel=@"HOLA_MUNDO";
int length=[_accel length];


// convert the NSString to raw bytes
NSData *_data = [_accel dataUsingEncoding:NSUTF8StringEncoding];
uint8_t *rawstring = (uint8_t *)[_data bytes];


[oStream write:rawstring maxLength:length];
streambuffer = [[NSMutableString alloc] initWithString:@""];

_connections=_connections+1;
[self loopstream];

}
break;
/*
case NSStreamEventErrorOccurred:
event = @"NSStreamEventErrorOccurred";
[self killstreams];
[self startSTREAMING];
break;
case NSStreamEventEndEncountered:
event = @"NSStreamEventEndEncountered";
[self killstreams];
[self startSTREAMING];

break;
*/
default:
event = @"Looping";
[self killstreams];
[self startSTREAMING];
}

//LABELcurrentAcceleration.text =[NSString stringWithFormat:@"%d# %@ : %@",_connections, io, event];

}


@end


0 comentarios:

Publicar un comentario