annotate ServerComms.mm @ 30:c0a6f7c66719

Josh M test "in house" version 0.1
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Wed, 27 Feb 2013 11:39:07 +0000
parents fabb3a5cdfc9
children 23ef179c3748
rev   line source
rt300@29 1 //
rt300@29 2 // ServerComms.m
rt300@30 3 // httpPost
rt300@29 4 //
rt300@30 5 // Created by Robert Tubb on 24/02/2013.
rt300@30 6 // Copyright (c) 2013 Robert Tubb. All rights reserved.
rt300@29 7 //
rt300@29 8
rt300@29 9 #import "ServerComms.h"
rt300@30 10 #import "eventLogger.h"
rt300@30 11
rt300@30 12 extern EventLogger eventLogger;
rt300@29 13
rt300@29 14 @implementation ServerComms
rt300@30 15 //
rt300@30 16
rt300@30 17
rt300@30 18 // asynchronous one
rt300@30 19 -(BOOL)doPostRequest:(NSString *)type withData:(NSString *)data{
rt300@30 20
rt300@30 21 if(self.requestInProgress){
rt300@30 22 return NO;
rt300@30 23 }
rt300@30 24 self.currentRequestType = type;
rt300@30 25 NSString *localServerURL = @"http://127.0.0.1:8080/testservice/";
rt300@30 26 NSString *webServerURL = @"http://www.isophonics.net/datacollector/";
rt300@30 27
rt300@30 28 NSString *urls = [webServerURL stringByAppendingString:type];
rt300@30 29 NSURL *url = [NSURL URLWithString:urls];
rt300@30 30
rt300@30 31
rt300@30 32 // request object
rt300@30 33 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
rt300@30 34
rt300@30 35 // the data
rt300@30 36 NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]];
rt300@30 37 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
rt300@30 38 // length of data
rt300@30 39 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
rt300@30 40
rt300@30 41 // request properties/header fields
rt300@30 42 [request setHTTPMethod:@"POST"];
rt300@30 43 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
rt300@30 44 [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
rt300@30 45 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
rt300@30 46 [request setHTTPBody:postData ];
rt300@30 47
rt300@30 48 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
rt300@30 49 self.requestInProgress = YES;
rt300@30 50 return YES;
rt300@30 51 // asynchronous one??
rt300@29 52 }
rt300@30 53 //--------------------------------------------------------------------
rt300@30 54
rt300@30 55 // schncronous
rt300@30 56 -(BOOL)doSyncPostRequest:(NSString *)type withData:(NSString *)data{
rt300@30 57 BOOL success;
rt300@29 58
rt300@30 59 NSString *localServerURL = @"http://127.0.0.1:8080/testservice/";
rt300@30 60 NSString *webServerURL = @"http://www.isophonics.net/datacollector/";
rt300@30 61
rt300@30 62 NSString *urls = [webServerURL stringByAppendingString:type];
rt300@30 63 NSURL *url = [NSURL URLWithString:urls];
rt300@30 64
rt300@30 65
rt300@30 66 // request object
rt300@30 67 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
rt300@30 68
rt300@30 69 // the data
rt300@30 70 NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]];
rt300@30 71 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
rt300@30 72 // length of data
rt300@30 73 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
rt300@30 74
rt300@30 75 // request properties/header fields
rt300@29 76 [request setHTTPMethod:@"POST"];
rt300@30 77 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
rt300@30 78 [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
rt300@30 79 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
rt300@30 80 [request setHTTPBody:postData ];
rt300@29 81
rt300@30 82 NSURLResponse* response;
rt300@30 83 NSError* error = nil;
rt300@30 84 NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
rt300@30 85
rt300@30 86 NSString *responseDataString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
rt300@29 87
rt300@30 88 //NSString* responseDataString = [NSString stringWithUTF8String: ];
rt300@30 89 NSLog(@"didRecieveDta %@", responseDataString);
rt300@30 90 [self.data appendData:result];
rt300@29 91
rt300@30 92 if([responseDataString isEqualToString:@"testConnection:OK"]){
rt300@30 93
rt300@30 94 eventLogger.testConnectionOK();
rt300@30 95 success = true;
rt300@30 96 }else if([responseDataString isEqualToString:@"questionnaire:OK"]){
rt300@30 97 eventLogger.questionnaireOK();
rt300@30 98 success = true;
rt300@30 99 }else if([responseDataString isEqualToString:@"eventlog:OK"]){
rt300@30 100 // call eventLogger eventlogUploadOK
rt300@30 101 eventLogger.eventlogOK();
rt300@30 102 success = true;
rt300@30 103 }else{
rt300@30 104 success = false;
rt300@30 105 }
rt300@29 106
rt300@30 107 // else check error??
rt300@30 108
rt300@30 109 // check result
rt300@30 110 self.requestInProgress = NO;
rt300@30 111 return success;
rt300@30 112 }
rt300@30 113 //-------------------------------------------------------------------------------------------------
rt300@30 114
rt300@30 115 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
rt300@30 116 [self.data setLength:0];
rt300@30 117 NSLog(@"didReceiveResponse : %@",response);
rt300@29 118 }
rt300@29 119
rt300@30 120 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
rt300@30 121 NSString *responseDataString = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
rt300@30 122
rt300@30 123 //NSString* responseDataString = [NSString stringWithUTF8String: ];
rt300@30 124 NSLog(@"didRecieveDta %@", responseDataString);
rt300@30 125 [self.data appendData:d];
rt300@30 126
rt300@30 127 if([responseDataString isEqualToString:@"testConnection:OK"]){
rt300@29 128
rt300@30 129 eventLogger.testConnectionOK();
rt300@30 130 }else if([responseDataString isEqualToString:@"questionnaire:OK"]){
rt300@30 131 eventLogger.questionnaireOK();
rt300@30 132 }else if([responseDataString isEqualToString:@"eventlog:OK"]){
rt300@30 133 // call eventLogger eventlogUploadOK
rt300@30 134 eventLogger.eventlogOK();
rt300@30 135 }
rt300@30 136 // or?
rt300@30 137 /*
rt300@30 138 if([self.currentRequestType isEqualToString:@"testConnection"){
rt300@30 139
rt300@30 140 }
rt300@30 141 */
rt300@29 142 }
rt300@29 143
rt300@30 144 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
rt300@30 145 UIAlertView * av = [ [UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
rt300@30 146 message:[error localizedDescription]
rt300@30 147 delegate:nil
rt300@30 148 cancelButtonTitle:NSLocalizedString(@"OK", @"")
rt300@30 149 otherButtonTitles:nil ];
rt300@30 150 [av show];
rt300@30 151 NSLog(@"fail with error");
rt300@30 152 self.requestInProgress = NO;
rt300@30 153 // we won't know what kind of request method this was...
rt300@29 154 }
rt300@29 155
rt300@30 156 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
rt300@30 157 NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
rt300@30 158
rt300@30 159 // Do anything you want with it
rt300@30 160 NSLog(@"response text: %@",responseText);
rt300@30 161 self.requestInProgress = NO;
rt300@30 162
rt300@29 163 }
rt300@29 164
rt300@30 165 // Handle basic authentication challenge if needed
rt300@30 166 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
rt300@30 167 NSString *username = @"username";
rt300@30 168 NSString *password = @"password";
rt300@30 169
rt300@30 170 NSURLCredential *credential = [NSURLCredential credentialWithUser:username
rt300@30 171 password:password
rt300@30 172 persistence:NSURLCredentialPersistenceForSession];
rt300@30 173 [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
rt300@30 174 }
rt300@30 175 /*
rt300@30 176 - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse{
rt300@30 177
rt300@30 178 }
rt300@29 179 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
rt300@29 180
rt300@29 181 }
rt300@30 182 */
rt300@29 183
rt300@29 184 @end