-
Notifications
You must be signed in to change notification settings - Fork 0
/
TorrentModel.m
157 lines (121 loc) · 2.91 KB
/
TorrentModel.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Torrent.m
// jigsaw
//
// Created by Will Goring on 09/12/2009.
//
#import "TorrentModel.h"
#import "XMLRPC/XMLRPC.h"
@implementation TorrentModel
#pragma mark initialisation
+ (TorrentModel *)withHash:(NSString *)hash
{
TorrentModel *torrent = [[TorrentModel alloc] init];
[torrent setHash:hash];
return torrent;
}
#pragma mark properties
@synthesize name;
@synthesize hash;
@synthesize size;
@synthesize upRate;
@synthesize uploaded;
@synthesize downRate;
@synthesize downloaded;
@synthesize ratio;
@synthesize active;
@synthesize url;
@synthesize priority;
- (BOOL) active
{
return active;
}
- (void) setActive:(BOOL)newActive
{
if (newActive != active) {
active = newActive;
if (url != nil) {
if (active) {
[self start];
} else {
[self stop];
}
}
}
}
#pragma mark control of remote torrent
- (void) start
{
NSLog(@"start");
NSURL *URL = [NSURL URLWithString:url];
XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
[request setMethod: @"d.start" withParameter:hash];
[XMLRPCConnection sendSynchronousXMLRPCRequest:request];
}
- (void) stop
{
NSLog(@"Stop");
NSURL *URL = [NSURL URLWithString:url];
XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
[request setMethod: @"d.stop" withParameter:hash];
[XMLRPCConnection sendSynchronousXMLRPCRequest:request];
}
- (void) remove
{
NSLog(@"delete");
NSURL *URL = [NSURL URLWithString:url];
XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
[request setMethod: @"d.erase" withParameter:hash];
[XMLRPCConnection sendSynchronousXMLRPCRequest:request];
}
- (void) changePriority:(int)new_priority
{
NSLog(@"set priority");
NSURL *URL = [NSURL URLWithString:url];
XMLRPCRequest *request = [[XMLRPCRequest alloc] initWithURL: URL];
[request setMethod: @"d.set_priority" withParameters:[NSArray arrayWithObjects:hash, [NSString stringWithFormat:@"%d", new_priority], nil]];
[XMLRPCConnection sendSynchronousXMLRPCRequest:request];
}
#pragma mark calculated properties
- (double) proportionComplete
{
return (double)downloaded / (double)size;
}
- (unsigned long long) secondsRemaining
{
long long bytesRemaining = size - downloaded;
if ( bytesRemaining == 0 ) {
return 0;
}
long long rate = downRate;
if (rate == 0) {
return ULLONG_MAX;
} else {
long long secondsRemaining = bytesRemaining / rate;
return secondsRemaining;
}
}
- (double) normalisedRatio
{
return ratio / 1000;
}
#pragma mark system overrrides
- (id)copyWithZone:(NSZone *)zone
{
TorrentModel *torrent = [TorrentModel withHash:name];
[torrent setHash:hash];
[torrent setUpRate:upRate];
[torrent setUploaded:uploaded];
[torrent setDownRate:downRate];
[torrent setDownloaded:downloaded];
[torrent setRatio:ratio];
[torrent setActive:active];
[torrent setUrl:url];
[torrent setPriority:priority];
return torrent;
}
- (BOOL)isEqual:(TorrentModel*)other
{
return [hash isEqual:[other hash]];
}
@end