Created
November 23, 2011 18:04
-
-
Save macteo/1389384 to your computer and use it in GitHub Desktop.
Useful to communicate with SiriProxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (id) init { | |
if (self = [super init]) | |
{ | |
[self initNetworkCommunication]; | |
} | |
return self; | |
} | |
- (void) initNetworkCommunication { | |
CFReadStreamRef readStream; | |
CFWriteStreamRef writeStream; | |
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.2.9", 800, &readStream, &writeStream); | |
inputStream = (NSInputStream *)readStream; | |
outputStream = (NSOutputStream *)writeStream; | |
[inputStream setDelegate:self]; | |
[outputStream setDelegate:self]; | |
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[inputStream open]; | |
[outputStream open]; | |
[self joinChat]; | |
} | |
- (void) joinChat { | |
NSString *response = [NSString stringWithFormat:@"iam:iPad2"]; | |
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]]; | |
[outputStream write:[data bytes] maxLength:[data length]]; | |
} | |
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { | |
NSLog(@"stream event %i", streamEvent); | |
switch (streamEvent) { | |
case NSStreamEventOpenCompleted: | |
NSLog(@"Stream opened"); | |
break; | |
case NSStreamEventHasBytesAvailable: | |
if (theStream == inputStream) { | |
uint8_t buffer[1024]; | |
int len; | |
while ([inputStream hasBytesAvailable]) { | |
len = [inputStream read:buffer maxLength:sizeof(buffer)]; | |
if (len > 0) { | |
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; | |
if (nil != output) { | |
NSLog(@"server said: %@", output); | |
[self messageReceived:output]; | |
} | |
} | |
} | |
} | |
break; | |
case NSStreamEventErrorOccurred: | |
NSLog(@"Can not connect to the host!"); | |
break; | |
case NSStreamEventEndEncountered: | |
[theStream close]; | |
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[theStream release]; | |
theStream = nil; | |
break; | |
default: | |
NSLog(@"Unknown event"); | |
} | |
} | |
- (void) messageReceived:(NSString *)message { | |
[self.messages addObject:message]; | |
NSLog(@"Received Message"); | |
NSString *stripped = [message stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]]; | |
if([stripped isEqualToString:@"SIRINextPage"]) | |
[delegate moveToNextPage]; | |
else if([stripped isEqualToString:@"SIRIPreviousPage"]) | |
[delegate moveToPreviousPage]; | |
else if([stripped length] >= 10){ | |
NSLog(@"Length OK"); | |
NSLog(@"%@", [stripped substringToIndex:10]); | |
if([[stripped substringToIndex:15] isEqualToString:@"SIRISearchIndex"]){ | |
NSLog(@"%@", [stripped substringFromIndex:15]); | |
[delegate siriSearchSelect:[[stripped substringFromIndex:15] intValue]]; | |
} | |
else if([[stripped substringToIndex:10] isEqualToString:@"SIRISearch"]){ | |
NSLog(@"%@", [stripped substringFromIndex:10]); | |
[delegate siriSearchFor:[[stripped substringFromIndex:10] stringByReplacingOccurrencesOfString:@" " withString:@""]]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment