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@31
|
16 -(id)init{
|
rt300@31
|
17 self = [super init];
|
rt300@31
|
18 if(self != nil){
|
rt300@31
|
19 self.requestInProgress = NO;
|
rt300@31
|
20 }
|
rt300@31
|
21 return self;
|
rt300@31
|
22 }
|
rt300@30
|
23
|
rt300@30
|
24 // asynchronous one
|
rt300@30
|
25 -(BOOL)doPostRequest:(NSString *)type withData:(NSString *)data{
|
rt300@30
|
26
|
rt300@30
|
27 if(self.requestInProgress){
|
rt300@30
|
28 return NO;
|
rt300@30
|
29 }
|
rt300@30
|
30 self.currentRequestType = type;
|
rt300@30
|
31 NSString *localServerURL = @"http://127.0.0.1:8080/testservice/";
|
rt300@30
|
32 NSString *webServerURL = @"http://www.isophonics.net/datacollector/";
|
rt300@30
|
33
|
rt300@32
|
34 NSString *urls = [webServerURL stringByAppendingString:type];
|
rt300@30
|
35 NSURL *url = [NSURL URLWithString:urls];
|
rt300@30
|
36
|
rt300@30
|
37
|
rt300@30
|
38 // request object
|
rt300@30
|
39 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
rt300@30
|
40
|
rt300@30
|
41 // the data
|
rt300@30
|
42 NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]];
|
rt300@30
|
43 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
|
rt300@30
|
44 // length of data
|
rt300@30
|
45 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
|
rt300@30
|
46
|
rt300@30
|
47 // request properties/header fields
|
rt300@30
|
48 [request setHTTPMethod:@"POST"];
|
rt300@30
|
49 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
|
rt300@30
|
50 [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
|
rt300@30
|
51 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
|
rt300@30
|
52 [request setHTTPBody:postData ];
|
rt300@30
|
53
|
rt300@30
|
54 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
|
rt300@30
|
55 self.requestInProgress = YES;
|
rt300@30
|
56 return YES;
|
rt300@30
|
57 // asynchronous one??
|
rt300@29
|
58 }
|
rt300@30
|
59 //--------------------------------------------------------------------
|
rt300@30
|
60
|
rt300@30
|
61 // schncronous
|
rt300@30
|
62 -(BOOL)doSyncPostRequest:(NSString *)type withData:(NSString *)data{
|
rt300@30
|
63 BOOL success;
|
rt300@29
|
64
|
rt300@30
|
65 NSString *localServerURL = @"http://127.0.0.1:8080/testservice/";
|
rt300@30
|
66 NSString *webServerURL = @"http://www.isophonics.net/datacollector/";
|
rt300@30
|
67
|
rt300@32
|
68 NSString *urls = [webServerURL stringByAppendingString:type];
|
rt300@30
|
69 NSURL *url = [NSURL URLWithString:urls];
|
rt300@30
|
70
|
rt300@30
|
71
|
rt300@30
|
72 // request object
|
rt300@30
|
73 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
|
rt300@30
|
74
|
rt300@30
|
75 // the data
|
rt300@30
|
76 NSString *post = [@"jsontext=" stringByAppendingString:[NSString stringWithFormat:data]];
|
rt300@30
|
77 NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
|
rt300@30
|
78 // length of data
|
rt300@30
|
79 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
|
rt300@30
|
80
|
rt300@30
|
81 // request properties/header fields
|
rt300@29
|
82 [request setHTTPMethod:@"POST"];
|
rt300@30
|
83 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
|
rt300@30
|
84 [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
|
rt300@30
|
85 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
|
rt300@30
|
86 [request setHTTPBody:postData ];
|
rt300@29
|
87
|
rt300@30
|
88 NSURLResponse* response;
|
rt300@30
|
89 NSError* error = nil;
|
rt300@30
|
90 NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
|
rt300@30
|
91
|
rt300@30
|
92 NSString *responseDataString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
|
rt300@29
|
93
|
rt300@30
|
94 //NSString* responseDataString = [NSString stringWithUTF8String: ];
|
rt300@32
|
95 NSLog(@"sync req. data %@", responseDataString);
|
rt300@30
|
96 [self.data appendData:result];
|
rt300@29
|
97
|
rt300@30
|
98 if([responseDataString isEqualToString:@"testConnection:OK"]){
|
rt300@30
|
99
|
rt300@30
|
100 eventLogger.testConnectionOK();
|
rt300@36
|
101 [responseDataString release];
|
rt300@30
|
102 success = true;
|
rt300@30
|
103 }else if([responseDataString isEqualToString:@"questionnaire:OK"]){
|
rt300@30
|
104 eventLogger.questionnaireOK();
|
rt300@30
|
105 success = true;
|
rt300@30
|
106 }else if([responseDataString isEqualToString:@"eventlog:OK"]){
|
rt300@30
|
107 // call eventLogger eventlogUploadOK
|
rt300@30
|
108 eventLogger.eventlogOK();
|
rt300@30
|
109 success = true;
|
rt300@30
|
110 }else{
|
rt300@30
|
111 success = false;
|
rt300@31
|
112 if([type isEqualToString:@"testConnection"]) eventLogger.testConnectionNotOK();
|
rt300@31
|
113 if([type isEqualToString:@"eventlog"]) eventLogger.eventlogNotOK();
|
rt300@31
|
114 if([type isEqualToString:@"questionnaire"]) eventLogger.questionnaireNotOK();
|
rt300@30
|
115 }
|
rt300@29
|
116
|
rt300@30
|
117 // else check error??
|
rt300@30
|
118
|
rt300@30
|
119 // check result
|
rt300@30
|
120 self.requestInProgress = NO;
|
rt300@30
|
121 return success;
|
rt300@30
|
122 }
|
rt300@30
|
123 //-------------------------------------------------------------------------------------------------
|
rt300@30
|
124
|
rt300@30
|
125 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
rt300@30
|
126 [self.data setLength:0];
|
rt300@30
|
127 NSLog(@"didReceiveResponse : %@",response);
|
rt300@29
|
128 }
|
rt300@29
|
129
|
rt300@30
|
130 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
|
rt300@30
|
131 NSString *responseDataString = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
|
rt300@30
|
132
|
rt300@30
|
133 //NSString* responseDataString = [NSString stringWithUTF8String: ];
|
rt300@31
|
134 NSLog(@" %@ didRecieveData %@",self.currentRequestType, responseDataString);
|
rt300@30
|
135 [self.data appendData:d];
|
rt300@30
|
136
|
rt300@30
|
137 if([responseDataString isEqualToString:@"testConnection:OK"]){
|
rt300@29
|
138
|
rt300@30
|
139 eventLogger.testConnectionOK();
|
rt300@36
|
140 [responseDataString release];
|
rt300@30
|
141 }else if([responseDataString isEqualToString:@"questionnaire:OK"]){
|
rt300@30
|
142 eventLogger.questionnaireOK();
|
rt300@30
|
143 }else if([responseDataString isEqualToString:@"eventlog:OK"]){
|
rt300@30
|
144 // call eventLogger eventlogUploadOK
|
rt300@30
|
145 eventLogger.eventlogOK();
|
rt300@31
|
146 }else{
|
rt300@31
|
147 if([self.currentRequestType isEqualToString:@"testConnection"]) eventLogger.testConnectionNotOK();
|
rt300@31
|
148 if([self.currentRequestType isEqualToString:@"eventlog"]) eventLogger.eventlogNotOK();
|
rt300@31
|
149 if([self.currentRequestType isEqualToString:@"questionnaire"]) eventLogger.questionnaireNotOK();
|
rt300@30
|
150 }
|
rt300@29
|
151 }
|
rt300@29
|
152
|
rt300@30
|
153 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
|
rt300@31
|
154
|
rt300@30
|
155 NSLog(@"fail with error");
|
rt300@31
|
156 if([self.currentRequestType isEqualToString:@"testConnection"]) {
|
rt300@31
|
157 UIAlertView * av = [ [UIAlertView alloc] initWithTitle:NSLocalizedString(@"Sorry", @"")
|
rt300@31
|
158 message:[error localizedDescription]
|
rt300@31
|
159 delegate:nil
|
rt300@31
|
160 cancelButtonTitle:NSLocalizedString(@"OK", @"")
|
rt300@31
|
161 otherButtonTitles:nil ];
|
rt300@31
|
162 [av show];
|
rt300@31
|
163 eventLogger.testConnectionNotOK();
|
rt300@31
|
164
|
rt300@31
|
165 }
|
rt300@31
|
166 if([self.currentRequestType isEqualToString:@"eventlog"]) eventLogger.eventlogNotOK();
|
rt300@31
|
167 if([self.currentRequestType isEqualToString:@"questionnaire"]) eventLogger.questionnaireNotOK();
|
rt300@30
|
168 self.requestInProgress = NO;
|
rt300@30
|
169 // we won't know what kind of request method this was...
|
rt300@29
|
170 }
|
rt300@29
|
171
|
rt300@30
|
172 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
|
rt300@30
|
173 NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
|
rt300@30
|
174
|
rt300@30
|
175 // Do anything you want with it
|
rt300@30
|
176 NSLog(@"response text: %@",responseText);
|
rt300@36
|
177 [responseText release];
|
rt300@36
|
178
|
rt300@30
|
179 self.requestInProgress = NO;
|
rt300@30
|
180
|
rt300@29
|
181 }
|
rt300@29
|
182
|
rt300@30
|
183 // Handle basic authentication challenge if needed
|
rt300@30
|
184 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
|
rt300@30
|
185 NSString *username = @"username";
|
rt300@30
|
186 NSString *password = @"password";
|
rt300@30
|
187
|
rt300@30
|
188 NSURLCredential *credential = [NSURLCredential credentialWithUser:username
|
rt300@30
|
189 password:password
|
rt300@30
|
190 persistence:NSURLCredentialPersistenceForSession];
|
rt300@30
|
191 [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
|
rt300@30
|
192 }
|
rt300@30
|
193 /*
|
rt300@30
|
194 - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse{
|
rt300@30
|
195
|
rt300@30
|
196 }
|
rt300@29
|
197 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{
|
rt300@29
|
198
|
rt300@29
|
199 }
|
rt300@30
|
200 */
|
rt300@29
|
201
|
rt300@29
|
202 @end
|