source: trunk/src/halTorrentIntStates.cpp @ 748

Revision 748, 6.7 KB checked in by Eoin, 10 years ago (diff)

Restoring torrents to their proper states fixed.

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(L"Entering in_the_session()");
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
110active::~active()
111{
112        context<torrent_internal>().handle_.pause();
113
114        TORRENT_STATE_LOG(L"Exiting ~active()");
115}
116
117sc::result active::react(const ev_force_recheck& evt)
118{
119        TORRENT_STATE_LOG(L"React active::react(const ev_force_recheck& evt)");
120
121        context<torrent_internal>().handle_.force_recheck();
122
123        return discard_event();
124}
125
126pausing::pausing(base_type::my_context ctx) :
127        base_type::my_base(ctx)
128{
129        TORRENT_STATE_LOG(L"Entering pausing()");
130
131        torrent_internal& t_i = context<torrent_internal>();
132        t_i.state(torrent_details::torrent_pausing);
133}
134
135pausing::~pausing()
136{
137        TORRENT_STATE_LOG(L"Exiting ~pausing()");
138}
139
140paused::paused(base_type::my_context ctx) :
141        base_type::my_base(ctx)
142{
143        TORRENT_STATE_LOG(L"Entering paused()");
144
145        torrent_internal& t_i = context<torrent_internal>();
146        t_i.state(torrent_details::torrent_paused);
147
148        post_event(ev_write_resume_data());
149}
150
151paused::~paused()
152{
153        TORRENT_STATE_LOG(L"Exiting ~paused()");
154}
155
156sc::result paused::react(const ev_stop& evt)
157{
158        if (state_downcast<const resume_data_waiting*>() != 0 )
159                return transit< stopping >();
160        else
161                return transit< stopped >();
162}
163
164sc::result paused::react(const ev_resume& evt)
165{
166        context<torrent_internal>().handle_.resume();
167
168        return transit< active >();
169}
170
171sc::result paused::react(const ev_force_recheck& evt)
172{
173        TORRENT_STATE_LOG(L"React paused::react(const ev_force_recheck& evt)");
174
175        context<torrent_internal>().handle_.force_recheck();
176
177        return discard_event();
178}
179
180in_error::in_error(base_type::my_context ctx) :
181        base_type::my_base(ctx)
182{
183        torrent_internal& t_i = context<torrent_internal>();
184
185        TORRENT_STATE_LOG(hal::wform(L"Entering in_error()() - %1%") % t_i.check_error());
186
187        t_i.state(torrent_details::torrent_in_error);
188}
189
190in_error::~in_error()
191{
192        TORRENT_STATE_LOG(L"Exiting ~in_error()");
193}
194
195stopping::stopping(base_type::my_context ctx) :
196        base_type::my_base(ctx)
197{
198        TORRENT_STATE_LOG(L"Entering stopping()");
199
200        torrent_internal& t_i = context<torrent_internal>();
201        t_i.state(torrent_details::torrent_stopping);
202}
203
204stopping::~stopping()
205{
206        TORRENT_STATE_LOG(L"Exiting ~stopping()");
207}
208
209sc::result stopping::react(const ev_paused_alert& evt)
210{
211        TORRENT_STATE_LOG(L"React stopping::react(const ev_paused_alert& evt)");
212
213        post_event(ev_write_resume_data());
214
215        return discard_event();
216}
217
218stopped::stopped(base_type::my_context ctx) :
219        base_type::my_base(ctx)
220{
221        TORRENT_STATE_LOG(L"Entering stopped()");
222
223        torrent_internal& t_i = context<torrent_internal>();
224        t_i.state(torrent_details::torrent_stopped);
225}
226
227stopped::~stopped()
228{
229        TORRENT_STATE_LOG(L"Exiting ~stopped()");
230}
231
232not_started::not_started(base_type::my_context ctx) :
233        base_type::my_base(ctx)
234{
235        torrent_internal& t_i = context<torrent_internal>();
236        stored_state_ = t_i.state();
237
238        TORRENT_STATE_LOG(hal::wform(L"Entering not_started() - %1%") % stored_state_);
239}
240
241not_started::~not_started()
242{
243        TORRENT_STATE_LOG(L"Exiting ~not_started()");
244}
245
246sc::result not_started::react(const ev_start& evt)
247{
248        switch (stored_state_)
249        {
250        case torrent_details::torrent_active:
251                post_event(ev_add_to_session(false));
252                break;
253
254        case torrent_details::torrent_paused:
255        case torrent_details::torrent_pausing:
256                post_event(ev_add_to_session(true));
257                break;
258               
259        case torrent_details::torrent_stopped:
260        case torrent_details::torrent_stopping:         
261        case torrent_details::torrent_in_error:
262                return transit< stopped >();
263        };
264
265        return discard_event();
266}
267
268resume_data_waiting::resume_data_waiting(base_type::my_context ctx) :
269        base_type::my_base(ctx)
270{
271        TORRENT_STATE_LOG(L"Entering resume_data_waiting()");
272
273        context<torrent_internal>().handle_.save_resume_data();
274}
275
276resume_data_waiting::~resume_data_waiting()
277{
278        TORRENT_STATE_LOG(L"Exiting ~resume_data_waiting()");
279}
280
281resume_data_idling::resume_data_idling()
282{
283        TORRENT_STATE_LOG(L"Entering resume_data_idling()");
284}
285
286resume_data_idling::~resume_data_idling()
287{
288        TORRENT_STATE_LOG(L"Exiting ~resume_data_idling()");
289}
290
291} // namespace hal
Note: See TracBrowser for help on using the repository browser.