OSDN Git Service

HMMasterMissionCommandクラスをSwiftで書き換え
[kcd/KCD.git] / KCD / HMJSONTracker.m
1 //
2 //  HMJSONTracker.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 "HMJSONTracker.h"
10
11 #import "HMQueue.h"
12 #import "HMJSONCommand.h"
13
14 static HMJSONTracker *sTracker = nil;
15
16 @interface HMJSONTracker ()
17 @property (strong) HMQueue *queue;
18
19 @property (strong) HMJSONReciever *reciever;
20
21 @end
22
23 @implementation HMJSONTracker
24 + (void)load
25 {
26         static dispatch_once_t onceToken;
27         dispatch_once(&onceToken, ^{
28                 sTracker = [HMJSONTracker new];
29         });
30 }
31 - (id)init
32 {
33         self = [super init];
34         if(self) {
35                 _queue = [HMQueue new];
36                 _reciever = [HMJSONReciever new];
37                 self.reciever.queueu = self.queue;
38                 [self start];
39         }
40         return self;
41 }
42
43 - (void)start
44 {
45         dispatch_queue_t queue = dispatch_queue_create("HMJSONTracker", DISPATCH_QUEUE_SERIAL);
46         dispatch_async(queue, ^{
47                 while(YES) {
48                         @autoreleasepool {
49                                 @try {
50                                         NSDictionary *item = [self.queue dequeue];
51                                         HMJSONCommand *command = [HMJSONCommand commandForAPI:[item objectForKey:@"api"]];
52                                         command.argumentsString = [item objectForKey:@"argument"];
53                                         command.jsonData = [item objectForKey:@"json"];
54                                         command.recieveDate = [item objectForKey:@"date"];
55                                         
56                                         [command execute];
57                                         [NSThread sleepForTimeInterval:0.1];
58                                 }
59                                 @catch (id e) {
60                                         NSLog(@"HMJSONTracker Cought Exception -> %@", e);
61                                 }
62                         }
63                 }
64         });
65 }
66
67 @end