source: trunk/src/halTorrentIntStates.cpp @ 749

Revision 749, 6.8 KB checked in by Eoin, 11 years ago (diff)

Added override for torrent paused state in resume data file.

Line 
1
2//         Copyright Eóin O'Callaghan 2008 - 2009.
3// Distributed under the Boost Software License, Version 1.0.
4//    (See accompanying file LICENSE_1_0.txt or copy at
5//          http://www.boost.org/LICENSE_1_0.txt)
6
7#include "stdAfx.hpp"
8
9#include "halTorrentIntStates.hpp"
10
11namespace hal
12{
13
14// -------- in_the_session --------
15
16in_the_session::in_the_session(base_type::my_context ctx) :
17        base_type::my_base(ctx)
18{
19        TORRENT_STATE_LOG(L"Entering in_the_session()");
20
21        torrent_internal& t_i = context<torrent_internal>();
22//      preserve_me_ = t_i.own_weak_ptr_.lock();
23
24        assert(t_i.in_session());
25
26        t_i.apply_settings();
27}
28
29in_the_session::~in_the_session()
30{
31        torrent_internal& t_i = context<torrent_internal>();
32
33        HAL_DEV_MSG(L"Removing handle from session");
34        (*t_i.the_session_)->remove_torrent(t_i.handle_);
35
36        TORRENT_STATE_LOG(L"Exiting ~in_the_session()");
37}
38
39// -------- out_of_session --------
40
41out_of_session::out_of_session(base_type::my_context ctx) :
42        base_type::my_base(ctx)
43{
44        TORRENT_STATE_LOG(L"Entering out_of_session()");
45
46        context<torrent_internal>().in_session_ = false;
47}
48
49out_of_session::~out_of_session()
50{
51        TORRENT_STATE_LOG(L"Exiting ~out_of_session()");
52}
53
54sc::result out_of_session::react(const ev_add_to_session& evt)
55{
56        TORRENT_STATE_LOG(hal::wform(L"Adding to session, paused - %1%") % evt.pause());
57        torrent_internal& t_i = context<torrent_internal>();
58
59        assert(!t_i.in_session());
60
61        libt::add_torrent_params p;
62
63        string torrent_file = to_utf8((hal::app().get_working_directory()/L"torrents"/t_i.filename_).string());
64        t_i.info_memory_.reset(new libt::torrent_info(torrent_file.c_str()));
65
66        std::string resume_file = to_utf8((hal::app().get_working_directory()/L"resume" / (t_i.name_ + L".fastresume")).string());
67
68        std::vector<char> buf;
69        if (libt::load_file(resume_file.c_str(), buf) == 0)
70        {
71                HAL_DEV_MSG(L"Using resume data");
72                p.resume_data = &buf;
73        }
74
75        p.ti = t_i.info_memory_;
76        p.save_path = path_to_utf8(t_i.save_directory_);
77        p.storage_mode = hal_allocation_to_libt(t_i.allocation_);
78        p.paused = evt.pause();
79        p.duplicate_is_error = false;
80        p.auto_managed = t_i.managed_;
81
82        t_i.handle_ = (*t_i.the_session_)->add_torrent(p);
83
84        assert(t_i.handle_.is_valid());
85        t_i.in_session_ = true;
86
87        if (evt.pause())
88                return transit< paused >();
89        else
90                return transit< active >();
91
92}
93
94sc::result out_of_session::react(const ev_resume& evt)
95{
96        post_event(ev_add_to_session(false));
97
98        return discard_event();
99}
100
101active::active(base_type::my_context ctx) :
102        base_type::my_base(ctx)
103{
104        TORRENT_STATE_LOG(L"Entering active()");
105
106        torrent_internal& t_i = context<torrent_internal>();
107        t_i.state(torrent_details::torrent_active);
108
109        if (t_i.handle_.is_paused())
110                t_i.handle_.resume();
111}
112
113active::~active()
114{
115        context<torrent_internal>().handle_.pause();
116
117        TORRENT_STATE_LOG(L"Exiting ~active()");
118}
119
120sc::result active::react(const ev_force_recheck& evt)
121{
122        TORRENT_STATE_LOG(L"React active::react(const ev_force_recheck& evt)");
123
124        context<torrent_internal>().handle_.force_recheck();
125
126        return discard_event();
127}
128
129pausing::pausing(base_type::my_context ctx) :
130        base_type::my_base(ctx)
131{
132        TORRENT_STATE_LOG(L"Entering pausing()");
133
134        torrent_internal& t_i = context<torrent_internal>();
135        t_i.state(torrent_details::torrent_pausing);
136}
137
138pausing::~pausing()
139{
140        TORRENT_STATE_LOG(L"Exiting ~pausing()");
141}
142
143paused::paused(base_type::my_context ctx) :
144        base_type::my_base(ctx)
145{
146        TORRENT_STATE_LOG(L"Entering paused()");
147
148        torrent_internal& t_i = context<torrent_internal>();
149        t_i.state(torrent_details::torrent_paused);
150
151        post_event(ev_write_resume_data());
152}
153
154paused::~paused()
155{
156        TORRENT_STATE_LOG(L"Exiting ~paused()");
157}
158
159sc::result paused::react(const ev_stop& evt)
160{
161        if (state_downcast<const resume_data_waiting*>() != 0 )
162                return transit< stopping >();
163        else
164                return transit< stopped >();
165}
166
167sc::result paused::react(const ev_resume& evt)
168{
169        context<torrent_internal>().handle_.resume();
170
171        return transit< active >();
172}
173
174sc::result paused::react(const ev_force_recheck& evt)
175{
176        TORRENT_STATE_LOG(L"React paused::react(const ev_force_recheck& evt)");
177
178        context<torrent_internal>().handle_.force_recheck();
179
180        return discard_event();
181}
182
183in_error::in_error(base_type::my_context ctx) :
184        base_type::my_base(ctx)
185{
186        torrent_internal& t_i = context<torrent_internal>();
187
188        TORRENT_STATE_LOG(hal::wform(L"Entering in_error()() - %1%") % t_i.check_error());
189
190        t_i.state(torrent_details::torrent_in_error);
191}
192
193in_error::~in_error()
194{
195        TORRENT_STATE_LOG(L"Exiting ~in_error()");
196}
197
198stopping::stopping(base_type::my_context ctx) :
199        base_type::my_base(ctx)
200{
201        TORRENT_STATE_LOG(L"Entering stopping()");
202
203        torrent_internal& t_i = context<torrent_internal>();
204        t_i.state(torrent_details::torrent_stopping);
205}
206
207stopping::~stopping()
208{
209        TORRENT_STATE_LOG(L"Exiting ~stopping()");
210}
211
212sc::result stopping::react(const ev_paused_alert& evt)
213{
214        TORRENT_STATE_LOG(L"React stopping::react(const ev_paused_alert& evt)");
215
216        post_event(ev_write_resume_data());
217
218        return discard_event();
219}
220
221stopped::stopped(base_type::my_context ctx) :
222        base_type::my_base(ctx)
223{
224        TORRENT_STATE_LOG(L"Entering stopped()");
225
226        torrent_internal& t_i = context<torrent_internal>();
227        t_i.state(torrent_details::torrent_stopped);
228}
229
230stopped::~stopped()
231{
232        TORRENT_STATE_LOG(L"Exiting ~stopped()");
233}
234
235not_started::not_started(base_type::my_context ctx) :
236        base_type::my_base(ctx)
237{
238        torrent_internal& t_i = context<torrent_internal>();
239        stored_state_ = t_i.state();
240        t_i.state(torrent_details::torrent_not_started);
241
242        TORRENT_STATE_LOG(hal::wform(L"Entering not_started() - %1%") % stored_state_);
243}
244
245not_started::~not_started()
246{
247        TORRENT_STATE_LOG(L"Exiting ~not_started()");
248}
249
250sc::result not_started::react(const ev_start& evt)
251{
252        switch (stored_state_)
253        {
254        case torrent_details::torrent_active:
255                post_event(ev_add_to_session(false));
256                break;
257
258        case torrent_details::torrent_paused:
259        case torrent_details::torrent_pausing:
260                post_event(ev_add_to_session(true));
261                break;
262               
263        case torrent_details::torrent_stopped:
264        case torrent_details::torrent_stopping:         
265        case torrent_details::torrent_in_error:
266                return transit< stopped >();
267        };
268
269        return discard_event();
270}
271
272resume_data_waiting::resume_data_waiting(base_type::my_context ctx) :
273        base_type::my_base(ctx)
274{
275        TORRENT_STATE_LOG(L"Entering resume_data_waiting()");
276
277        context<torrent_internal>().handle_.save_resume_data();
278}
279
280resume_data_waiting::~resume_data_waiting()
281{
282        TORRENT_STATE_LOG(L"Exiting ~resume_data_waiting()");
283}
284
285resume_data_idling::resume_data_idling()
286{
287        TORRENT_STATE_LOG(L"Entering resume_data_idling()");
288}
289
290resume_data_idling::~resume_data_idling()
291{
292        TORRENT_STATE_LOG(L"Exiting ~resume_data_idling()");
293}
294
295} // namespace hal
Note: See TracBrowser for help on using the repository browser.