now parse server info into host object all at once

This commit is contained in:
Diego Waxemberg
2015-01-08 23:58:04 -05:00
parent b7fcce08d6
commit 60cba0399d
5 changed files with 61 additions and 20 deletions

View File

@@ -46,7 +46,7 @@
DataManager* dataMan = [[DataManager alloc] init];
host = [dataMan createHost];
host.address = hostAddress;
[DiscoveryWorker updateHost:host withServerInfo:serverInfoData];
[HttpManager populateHostFromXML:serverInfoData host:host];
if (![self addHostToDiscovery:host]) {
[dataMan removeHost:host];
}

View File

@@ -15,6 +15,4 @@
- (void) discoverHost;
- (Host*) getHost;
+ (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData;
@end

View File

@@ -45,7 +45,7 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.localAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert];
NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]];
if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) {
[DiscoveryWorker updateHost:_host withServerInfo:serverInfoData];
[HttpManager populateHostFromXML:serverInfoData host:_host];
_host.address = _host.localAddress;
receivedResponse = YES;
}
@@ -54,7 +54,7 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.externalAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert];
NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]];
if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) {
[DiscoveryWorker updateHost:_host withServerInfo:serverInfoData];
[HttpManager populateHostFromXML:serverInfoData host:_host];
_host.address = _host.externalAddress;
receivedResponse = YES;
}
@@ -64,7 +64,7 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert];
NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]];
if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) {
[DiscoveryWorker updateHost:_host withServerInfo:serverInfoData];
[HttpManager populateHostFromXML:serverInfoData host:_host];
receivedResponse = YES;
}
}
@@ -77,18 +77,4 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
}
}
+ (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData {
host.name = [HttpManager getStringFromXML:serverInfoData tag:@"hostname"];
host.externalAddress = [HttpManager getStringFromXML:serverInfoData tag:@"ExternalIP"];
host.localAddress = [HttpManager getStringFromXML:serverInfoData tag:@"LocalIP"];
host.uuid = [HttpManager getStringFromXML:serverInfoData tag:@"uniqueid"];
host.mac = [HttpManager getStringFromXML:serverInfoData tag:@"mac"];
NSString* pairState = [HttpManager getStringFromXML:serverInfoData tag:@"PairStatus"];
if ([pairState isEqualToString:@"1"]) {
host.pairState = PairStatePaired;
} else {
host.pairState = PairStateUnpaired;
}
}
@end

View File

@@ -7,10 +7,12 @@
//
#import <Foundation/Foundation.h>
#import "Host.h"
@interface HttpManager : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>
+ (NSArray*) getAppListFromXML:(NSData*)xml;
+ (void) populateHostFromXML:(NSData*)xml host:(Host*)host;
+ (NSString*) getStringFromXML:(NSData*)xml tag:(NSString*)tag;
+ (NSString*) getStatusStringFromXML:(NSData*)xml;

View File

@@ -87,6 +87,61 @@ static const NSString* PORT = @"47984";
return appList;
}
+ (void) populateHostFromXML:(NSData*)xml host:(Host*)host {
xmlDocPtr docPtr = xmlParseMemory([xml bytes], (int)[xml length]);
if (docPtr == NULL) {
NSLog(@"ERROR: An error occured trying to parse xml.");
return;
}
xmlNodePtr node;
xmlNodePtr rootNode = node = xmlDocGetRootElement(docPtr);
// Check root status_code
if (![HttpManager verifyStatus: rootNode]) {
NSLog(@"ERROR: Request returned with failure status");
xmlFreeDoc(docPtr);
return;
}
// Skip the root node
node = node->children;
while (node != NULL) {
//NSLog(@"node: %s", node->name);
if (!xmlStrcmp(node->name, (const xmlChar*)"hostname")) {
xmlChar* nodeVal = xmlNodeListGetString(docPtr, node->xmlChildrenNode, 1);
host.name = [[NSString alloc] initWithCString:(const char*)nodeVal encoding:NSUTF8StringEncoding];
xmlFree(nodeVal);
} else if (!xmlStrcmp(node->name, (const xmlChar*)"ExternalIP")) {
xmlChar* nodeVal = xmlNodeListGetString(docPtr, node->xmlChildrenNode, 1);
host.externalAddress = [[NSString alloc] initWithCString:(const char*)nodeVal encoding:NSUTF8StringEncoding];
xmlFree(nodeVal);
} else if (!xmlStrcmp(node->name, (const xmlChar*)"LocalIP")) {
xmlChar* nodeVal = xmlNodeListGetString(docPtr, node->xmlChildrenNode, 1);
host.localAddress = [[NSString alloc] initWithCString:(const char*)nodeVal encoding:NSUTF8StringEncoding];
xmlFree(nodeVal);
} else if (!xmlStrcmp(node->name, (const xmlChar*)"uniqueid")) {
xmlChar* nodeVal = xmlNodeListGetString(docPtr, node->xmlChildrenNode, 1);
host.uuid = [[NSString alloc] initWithCString:(const char*)nodeVal encoding:NSUTF8StringEncoding];
xmlFree(nodeVal);
} else if (!xmlStrcmp(node->name, (const xmlChar*)"mac")) {
xmlChar* nodeVal = xmlNodeListGetString(docPtr, node->xmlChildrenNode, 1);
host.mac = [[NSString alloc] initWithCString:(const char*)nodeVal encoding:NSUTF8StringEncoding];
xmlFree(nodeVal);
} else if (!xmlStrcmp(node->name, (const xmlChar*)"PairStatus")) {
xmlChar* nodeVal = xmlNodeListGetString(docPtr, node->xmlChildrenNode, 1);
host.pairState = [[[NSString alloc] initWithCString:(const char*)nodeVal encoding:NSUTF8StringEncoding] isEqualToString:@"1"] ? PairStatePaired : PairStateUnpaired;
xmlFree(nodeVal);
}
node = node->next;
}
xmlFreeDoc(docPtr);
}
+ (NSString*) getStatusStringFromXML:(NSData*)xml {
xmlDocPtr docPtr = xmlParseMemory([xml bytes], (int)[xml length]);