now send the device name when pairing. also fixed null-terminating buffers that caused all kinds of problems

This commit is contained in:
Diego Waxemberg 2014-02-01 13:30:53 -05:00
parent 4c6a3b2e43
commit 26492c5783

View File

@ -72,6 +72,16 @@ static MainFrameViewController* viewCont;
return [idString substringFromIndex:24]; return [idString substringFromIndex:24];
} }
+ (NSString*) getName
{
NSString* name = [UIDevice currentDevice].name;
NSLog(@"Device Name: %@", name);
//sanitize name
name = [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return name;
}
- (id) initForStream:(NSString*)host - (id) initForStream:(NSString*)host
{ {
self = [super init]; self = [super init];
@ -129,8 +139,6 @@ static MainFrameViewController* viewCont;
NSURLResponse* response = nil; NSURLResponse* response = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL]; NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
NSLog(@"url response: %@", response1);
[viewCont performSelector:@selector(segueIntoStream) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO]; [viewCont performSelector:@selector(segueIntoStream) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
break; break;
@ -151,26 +159,26 @@ static MainFrameViewController* viewCont;
if(pairState) if(pairState)
{ {
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Pairing" UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Pairing"
message:[NSString stringWithFormat:@"Already paired to:\n %@", self.hostName] message:[NSString stringWithFormat:@"Already paired to:\n %@", self.hostName]
delegate:nil delegate:nil
cancelButtonTitle:@"Okay" cancelButtonTitle:@"Okay"
otherButtonTitles:nil, nil]; otherButtonTitles:nil, nil];
[self showAlert:alert]; [self showAlert:alert];
NSLog(@"Showing alertview"); NSLog(@"Showing alertview");
break; break;
} }
// this will hang until it pairs successfully or unsuccessfully // this will hang until it pairs successfully or unsuccessfully
NSData* pairResponse = [self httpRequest:[NSString stringWithFormat:@"pair?uniqueid=%@", [ConnectionHandler getId]]]; NSData* pairResponse = [self httpRequest:[NSString stringWithFormat:@"pair?uniqueid=%@&devicename=%@", [ConnectionHandler getId], [ConnectionHandler getName]]];
void* buffer = [self getXMLBufferFromData:pairResponse]; void* buffer = [self getXMLBufferFromData:pairResponse];
if (buffer == NULL) { if (buffer == NULL) {
NSLog(@"ERROR: Unable to allocate pair buffer."); NSLog(@"ERROR: Unable to allocate pair buffer.");
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing" UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing"
message:[NSString stringWithFormat:@"Failed to pair with host:\n %@", self.hostName] message:[NSString stringWithFormat:@"Failed to pair with host:\n %@", self.hostName]
delegate:nil delegate:nil
cancelButtonTitle:@"Okay" cancelButtonTitle:@"Okay"
otherButtonTitles:nil, nil]; otherButtonTitles:nil, nil];
[self showAlert:alert]; [self showAlert:alert];
break; break;
} }
@ -191,18 +199,18 @@ static MainFrameViewController* viewCont;
if (xmlStrcmp(sessionid, (xmlChar*)"0")) { if (xmlStrcmp(sessionid, (xmlChar*)"0")) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing" UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing"
message:[NSString stringWithFormat:@"Successfully paired to host:\n %@", self.hostName] message:[NSString stringWithFormat:@"Successfully paired to host:\n %@", self.hostName]
delegate:nil delegate:nil
cancelButtonTitle:@"Okay" cancelButtonTitle:@"Okay"
otherButtonTitles:nil, nil]; otherButtonTitles:nil, nil];
[self showAlert:alert]; [self showAlert:alert];
} else { } else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing" UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing"
message:[NSString stringWithFormat:@"Pairing was declined by host:\n %@", self.hostName] message:[NSString stringWithFormat:@"Pairing was declined by host:\n %@", self.hostName]
delegate:nil delegate:nil
cancelButtonTitle:@"Okay" cancelButtonTitle:@"Okay"
otherButtonTitles:nil, nil]; otherButtonTitles:nil, nil];
[self showAlert:alert]; [self showAlert:alert];
} }
@ -235,10 +243,10 @@ static MainFrameViewController* viewCont;
if (buffer == NULL) { if (buffer == NULL) {
NSLog(@"ERROR: Unable to allocate pair state buffer."); NSLog(@"ERROR: Unable to allocate pair state buffer.");
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing" UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Pairing"
message:[NSString stringWithFormat:@"Failed to get pair state with host:\n %@", self.hostName] message:[NSString stringWithFormat:@"Failed to get pair state with host:\n %@", self.hostName]
delegate:nil delegate:nil
cancelButtonTitle:@"Okay" cancelButtonTitle:@"Okay"
otherButtonTitles:nil, nil]; otherButtonTitles:nil, nil];
[self showAlert:alert]; [self showAlert:alert];
return -1; return -1;
} }
@ -283,6 +291,7 @@ cleanup:
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"]; [request setHTTPMethod:@"GET"];
[request setTimeoutInterval:7];
NSURLResponse* response = nil; NSURLResponse* response = nil;
NSError* error = nil; NSError* error = nil;
@ -291,7 +300,6 @@ cleanup:
if (error != nil) { if (error != nil) {
NSLog(@"An error occured making HTTP request: %@\n Caused by: %@", [error localizedDescription], [error localizedFailureReason]); NSLog(@"An error occured making HTTP request: %@\n Caused by: %@", [error localizedDescription], [error localizedFailureReason]);
} }
return responseData; return responseData;
} }
@ -302,17 +310,19 @@ cleanup:
NSLog(@"ERROR: No data to get XML from!"); NSLog(@"ERROR: No data to get XML from!");
return NULL; return NULL;
} }
void* buffer = malloc([data length]); char* buffer = malloc([data length] + 1);
if (buffer == NULL) { if (buffer == NULL) {
NSLog(@"ERROR: Unable to allocate XML buffer."); NSLog(@"ERROR: Unable to allocate XML buffer.");
return NULL; return NULL;
} }
[data getBytes:buffer length:[data length]]; [data getBytes:buffer length:[data length]];
buffer[[data length]] = 0;
NSLog(@"Buffer: %s", buffer);
//#allthejank //#allthejank
void* newBuffer = (void*)[[[NSString stringWithUTF8String:buffer]stringByReplacingOccurrencesOfString:@"utf-16" withString:@"UTF-8" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [data length])] UTF8String]; void* newBuffer = (void*)[[[NSString stringWithUTF8String:buffer]stringByReplacingOccurrencesOfString:@"UTF-16" withString:@"UTF-8" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [data length])] UTF8String];
NSLog(@"New Buffer: %s", newBuffer);
free(buffer); free(buffer);
return newBuffer; return newBuffer;
} }