-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
STStatusMonitor.m
116 lines (95 loc) · 3.26 KB
/
STStatusMonitor.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// STStatusMonitor.m
// syncthing
//
// Created by Victor Babenko on 29.06.17.
// Copyright © 2017 Jerry Jacobs. All rights reserved.
//
#import "STStatusMonitor.h"
@interface STStatusMonitor ()
@property (nonatomic, assign) BOOL enabled;
@property (nonatomic, assign) long lastSeenId;
@property (nonatomic) NSMutableDictionary *folderStates;
@property (nonatomic, assign) SyncthingStatus currentStatus;
@property (nonatomic, strong, readwrite) NSTimer *updateTimer;
@end
@implementation STStatusMonitor
- (instancetype)init {
self = [super init];
self.folderStates = [[NSMutableDictionary alloc] init];
return self;
}
- (void) longPoll {
@autoreleasepool {
id result = [self.syncthing getEventsSince:self.lastSeenId];
if (result == nil) {
// Failed to get events
[NSThread sleepForTimeInterval:1.0];
} else {
// Got events, process them.
[self performSelectorOnMainThread:@selector(dataReceived:) withObject:result waitUntilDone:YES];
}
if (self.enabled)
[self performSelectorInBackground:@selector(longPoll) withObject: nil];
}
}
- (void) dataReceived: (id) data {
if ([data isKindOfClass:[NSArray class]]) {
for (NSDictionary *event in data) {
[self processEvent:event];
}
}
}
- (void) processEvent:(NSDictionary *)event {
NSNumber *eventId = [event objectForKey:@"id"];
NSString *eventType = [event objectForKey:@"type"];
NSDictionary *eventData = [event objectForKey:@"data"];
self.lastSeenId = [eventId longValue];
if ([eventType isEqualToString:@"StateChanged"]) {
NSString *folder = [eventData objectForKey:@"folder"];
NSString *newState = [eventData objectForKey:@"to"];
self.folderStates[folder] = newState;
[self updateCurrentStatus];
}
[self.delegate syncMonitorEventReceived:event];
}
- (void) updateStatusFromTimer {
if (![_syncthing ping]) {
self.currentStatus = SyncthingStatusOffline;
}
else if (self.currentStatus == SyncthingStatusOffline) {
self.currentStatus = SyncthingStatusIdle;
}
}
- (void) updateCurrentStatus {
SyncthingStatus newStatus = SyncthingStatusIdle;
for (NSString *key in self.folderStates) {
NSString *state = self.folderStates[key];
if (newStatus != SyncthingStatusError) {
if ([state isEqualToString:@"scanning"] || [state isEqualToString:@"syncing"])
newStatus = SyncthingStatusBusy;
else if ([state isEqualToString:@"error"])
newStatus = SyncthingStatusError;
}
}
self.currentStatus = newStatus;
}
- (void)setCurrentStatus:(SyncthingStatus)newStatus {
if (_currentStatus != newStatus) {
[self.delegate syncMonitorStatusChanged:newStatus];
}
_currentStatus = newStatus;
}
- (void) startMonitoring {
if (!self.enabled) {
self.enabled = YES;
[self performSelectorInBackground:@selector(longPoll) withObject: nil];
_updateTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(updateStatusFromTimer) userInfo:nil repeats:YES];
}
}
- (void) stopMonitoring {
self.enabled = NO;
[_updateTimer invalidate];
_updateTimer = nil;
}
@end