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