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