source: trunk/src/halTorrentIntStates.cpp @ 747

Revision 747, 5.8 KB checked in by Eoin, 11 years ago (diff)
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
94active::active(base_type::my_context ctx) :
95        base_type::my_base(ctx)
96{
97        TORRENT_STATE_LOG(L"Entering active()");
98
99        torrent_internal& t_i = context<torrent_internal>();
100        t_i.state(torrent_details::torrent_active);
101}
102
103active::~active()
104{
105        context<torrent_internal>().handle_.pause();
106
107        TORRENT_STATE_LOG(L"Exiting ~active()");
108}
109
110sc::result active::react(const ev_force_recheck& evt)
111{
112        TORRENT_STATE_LOG(L"React active::react(const ev_force_recheck& evt)");
113
114        context<torrent_internal>().handle_.force_recheck();
115
116        return discard_event();
117}
118
119pausing::pausing(base_type::my_context ctx) :
120        base_type::my_base(ctx)
121{
122        TORRENT_STATE_LOG(L"Entering pausing()");
123
124        torrent_internal& t_i = context<torrent_internal>();
125        t_i.state(torrent_details::torrent_pausing);
126}
127
128pausing::~pausing()
129{
130        TORRENT_STATE_LOG(L"Exiting ~pausing()");
131}
132
133paused::paused(base_type::my_context ctx) :
134        base_type::my_base(ctx)
135{
136        TORRENT_STATE_LOG(L"Entering paused()");
137
138        torrent_internal& t_i = context<torrent_internal>();
139        t_i.state(torrent_details::torrent_paused);
140
141        post_event(ev_write_resume_data());
142}
143
144paused::~paused()
145{
146        TORRENT_STATE_LOG(L"Exiting ~paused()");
147}
148
149sc::result paused::react(const ev_stop& evt)
150{
151        if (state_downcast<const resume_data_waiting*>() != 0 )
152                return transit< stopping >();
153        else
154                return transit< stopped >();
155}
156
157sc::result paused::react(const ev_resume& evt)
158{
159        context<torrent_internal>().handle_.resume();
160
161        return transit< active >();
162}
163
164sc::result paused::react(const ev_force_recheck& evt)
165{
166        TORRENT_STATE_LOG(L"React paused::react(const ev_force_recheck& evt)");
167
168        context<torrent_internal>().handle_.force_recheck();
169
170        return discard_event();
171}
172
173in_error::in_error(base_type::my_context ctx) :
174        base_type::my_base(ctx)
175{
176        torrent_internal& t_i = context<torrent_internal>();
177
178        TORRENT_STATE_LOG(hal::wform(L"Entering in_error()() - %1%") % t_i.check_error());
179
180        t_i.state(torrent_details::torrent_in_error);
181}
182
183in_error::~in_error()
184{
185        TORRENT_STATE_LOG(L"Exiting ~in_error()");
186}
187
188stopping::stopping(base_type::my_context ctx) :
189        base_type::my_base(ctx)
190{
191        TORRENT_STATE_LOG(L"Entering stopping()");
192
193        torrent_internal& t_i = context<torrent_internal>();
194        t_i.state(torrent_details::torrent_stopping);
195}
196
197stopping::~stopping()
198{
199        TORRENT_STATE_LOG(L"Exiting ~stopping()");
200}
201
202sc::result stopping::react(const ev_paused_alert& evt)
203{
204        TORRENT_STATE_LOG(L"React stopping::react(const ev_paused_alert& evt)");
205
206        post_event(ev_write_resume_data());
207
208        return discard_event();
209}
210
211stopped::stopped(base_type::my_context ctx) :
212        base_type::my_base(ctx)
213{
214        TORRENT_STATE_LOG(L"Entering stopped()");
215
216        torrent_internal& t_i = context<torrent_internal>();
217        t_i.state(torrent_details::torrent_stopped);
218}
219
220stopped::~stopped()
221{
222        TORRENT_STATE_LOG(L"Exiting ~stopped()");
223}
224
225sc::result stopped::react(const ev_resume& evt)
226{
227        post_event(ev_add_to_session(false));
228
229        return discard_event();
230}
231
232resume_data_waiting::resume_data_waiting(base_type::my_context ctx) :
233        base_type::my_base(ctx)
234{
235        TORRENT_STATE_LOG(L"Entering resume_data_waiting()");
236
237        context<torrent_internal>().handle_.save_resume_data();
238}
239
240resume_data_waiting::~resume_data_waiting()
241{
242        TORRENT_STATE_LOG(L"Exiting ~resume_data_waiting()");
243}
244
245resume_data_idling::resume_data_idling()
246{
247        TORRENT_STATE_LOG(L"Entering resume_data_idling()");
248}
249
250resume_data_idling::~resume_data_idling()
251{
252        TORRENT_STATE_LOG(L"Exiting ~resume_data_idling()");
253}
254
255} // namespace hal
Note: See TracBrowser for help on using the repository browser.