OSDN Git Service

いろいろ追加
[kcd/KCD.git] / KCD / HMJSONReciever.m
1 //
2 //  HMJSONReciever.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/02/09.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMJSONReciever.h"
10
11 #import "CustomHTTPProtocol.h"
12 #import "HMAppDelegate.h"
13
14
15 @interface HMJSONReciever ()
16 @property (retain) NSMutableDictionary *recievers;
17 @end
18
19
20 @implementation HMJSONReciever
21
22 - (id)init
23 {
24         self = [super init];
25         if(self) {
26                 _recievers = [NSMutableDictionary new];
27                 
28                 [CustomHTTPProtocol setDelegate:self];
29         }
30         return self;
31 }
32
33 - (void)setProtocol:(NSURLProtocol *)protocol
34 {
35         [self.recievers setObject:[NSMutableData data]
36                                            forKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
37 }
38 - (NSMutableData *)dataForProtocol:(NSURLProtocol *)protocol
39 {
40         return [self.recievers objectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
41 }
42 - (void)removeDataForProtocol:(NSURLProtocol *)protocol
43 {
44         [self.recievers removeObjectForKey:[NSValue valueWithPointer:(__bridge const void *)(protocol)]];
45 }
46
47 - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveResponse:(NSHTTPURLResponse *)response
48 {
49         NSURL *url = protocol.request.URL;
50         NSString *path = url.path;
51         while(![path isEqualToString:@"/"]) {
52                 if([path hasSuffix:@"kcsapi"]) {
53                         [self setProtocol:protocol];
54                 }
55                 path = [path stringByDeletingLastPathComponent];
56         }
57 }
58
59 - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didRecieveData:(NSData *)data
60 {
61         NSMutableData *loadedData = [self dataForProtocol:protocol];
62         if(!loadedData) return;
63         
64         [loadedData appendData:data];
65 }
66
67 - (void)customHTTPProtocolDidFinishLoading:(CustomHTTPProtocol *)protocol
68 {
69         NSData  *data = [self dataForProtocol:protocol];
70         if(!data) return;
71         
72 #define JSON_LOG_STRING 0
73 #if JSON_LOG_STRING
74         NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
75         [self logLineReturn:@"body -> \n%@", string];
76 #else
77         NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
78         NSArray *elements = [string componentsSeparatedByString:@"="];
79         if([elements count] != 2) {
80                 [[NSApp delegate] logLineReturn:@"\e[1mwe could not compose data. api -> %@. Number of elements:\e[22m %ld", protocol.request.URL.path, [elements count]];
81                 [[NSApp delegate] logLineReturn:@"Original strings -> %@", string];
82                 [self removeDataForProtocol:protocol];
83                 return;
84         }
85         
86         
87         NSData *JSONData = [elements[1] dataUsingEncoding:NSUTF8StringEncoding];
88         NSError *error = nil;
89         id json = [NSJSONSerialization JSONObjectWithData:JSONData
90                                                                                           options:NSJSONReadingAllowFragments
91                                                                                                 error:&error];
92         if(error) {
93                 [[NSApp delegate] logLineReturn:@"\e[1m\e[31mFail decode JSON data\e[39m\e[22m %@", error];
94         } else {
95                 NSData *requestBodyData = [protocol.request HTTPBody];
96                 NSString *requestBodyString = [[NSString alloc] initWithData:requestBodyData encoding:NSUTF8StringEncoding];
97                 [self.queueu enqueue:@{@"api" : protocol.request.URL.path, @"argument": requestBodyString, @"json" : json}];
98         }
99 #endif
100         
101         
102         [self removeDataForProtocol:protocol];
103 }
104 - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didFailWithError:(NSError *)error
105 {
106         [self removeDataForProtocol:protocol];
107 }
108
109
110 //- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol logWithFormat:(NSString *)format arguments:(va_list)argList
111 //{
112 //      [self logLineReturn:[[NSString alloc] initWithFormat:format arguments:argList]];
113 //}
114
115
116 @end