source: trunk/src/halTorrentIntStates.cpp @ 686

Revision 686, 4.6 KB checked in by Eoin, 11 years ago (diff)

More advanced state machine being implemented for torrent_internal.

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        assert(context<torrent_internal>().in_session());
22}
23
24sc::result in_the_session::react(const ev_remove_from_session& evt)
25{
26        torrent_internal& t_i = context<torrent_internal>();
27
28        if (evt.write_data())
29        {
30                HAL_DEV_MSG(L"requesting resume data");
31                t_i.save_resume_data(); 
32
33                return transit< leaving_session >();
34        }
35        else
36        {
37                HAL_DEV_MSG(L"removing handle from session");
38                t_i.remove_torrent();
39
40                assert(!t_i.in_session());     
41                HAL_DEV_MSG(L"Removed from session!");
42
43                return transit< out_of_session >();
44        }
45}
46
47in_the_session::~in_the_session()
48{
49        TORRENT_STATE_LOG(L"Exiting ~in_the_session()");
50}
51
52// -------- leaving_session --------
53
54leaving_session::leaving_session(base_type::my_context ctx) :
55        base_type::my_base(ctx)
56{
57        TORRENT_STATE_LOG(L"Entering leaving_session()");
58}
59
60leaving_session::~leaving_session()
61{
62        TORRENT_STATE_LOG(L"Exiting ~leaving_session()");
63}
64
65sc::result leaving_session::react(const ev_add_to_session& evt)
66{
67        torrent_internal& t_i = context<torrent_internal>();
68        assert(t_i.in_session());
69
70        return transit< in_the_session >();
71}
72
73sc::result leaving_session::react(const ev_resume_data_written& evt)
74{
75        torrent_internal& t_i = context<torrent_internal>();
76
77        HAL_DEV_MSG(L"Removing handle from session");
78        t_i.remove_torrent();
79        HAL_DEV_MSG(L"Removed from session!");
80
81        return transit< out_of_session >();
82}
83
84// -------- out_of_session --------
85
86out_of_session::out_of_session(base_type::my_context ctx) :
87        base_type::my_base(ctx)
88{
89        TORRENT_STATE_LOG(L"Entering out_of_session()");
90}
91
92out_of_session::~out_of_session()
93{
94        TORRENT_STATE_LOG(L"Exiting ~out_of_session()");
95}
96
97sc::result out_of_session::react(const ev_add_to_session& evt)
98{
99        TORRENT_STATE_LOG(L"Entering in_the_session()");
100        torrent_internal& t_i = context<torrent_internal>();
101
102        assert(!t_i.in_session());
103
104        libt::add_torrent_params p;
105
106        string torrent_file = to_utf8((hal::app().get_working_directory()/L"torrents"/t_i.filename_).string());
107        t_i.info_memory_.reset(new libt::torrent_info(torrent_file.c_str()));
108
109        std::string resume_file = to_utf8((hal::app().get_working_directory()/L"resume" / (t_i.name_ + L".fastresume")).string());
110
111        std::vector<char> buf;
112        if (libt::load_file(resume_file.c_str(), buf) == 0)
113        {
114                HAL_DEV_MSG(L"Using resume data");
115                p.resume_data = &buf;
116        }
117
118        p.ti = t_i.info_memory_;
119        p.save_path = path_to_utf8(t_i.save_directory_);
120        p.storage_mode = hal_allocation_to_libt(t_i.allocation_);
121        p.paused = evt.pause();
122        p.duplicate_is_error = false;
123        p.auto_managed = t_i.managed_;
124
125        t_i.handle_ = t_i.the_session_->add_torrent(p);         
126        assert(t_i.handle_.is_valid());
127        t_i.in_session_ = true;
128
129        return transit< in_the_session >();
130}
131
132active::active(base_type::my_context ctx) :
133        base_type::my_base(ctx)
134{
135        TORRENT_STATE_LOG(L"Entering active()");
136}
137
138active::~active()
139{
140        TORRENT_STATE_LOG(L"Exiting ~active()");
141}
142
143sc::result active::react(const ev_pause& evt)
144{
145        context<torrent_internal>().handle_.pause();
146
147        return transit< pausing >();
148}
149
150pausing::pausing()
151{
152        TORRENT_STATE_LOG(L"Entering pausing()");
153}
154
155pausing::~pausing()
156{
157        TORRENT_STATE_LOG(L"Exiting ~pausing()");
158}
159
160paused::paused(base_type::my_context ctx) :
161        base_type::my_base(ctx)
162{
163        TORRENT_STATE_LOG(L"Entering paused()");
164}
165
166paused::~paused()
167{
168        TORRENT_STATE_LOG(L"Exiting ~paused()");
169}
170
171stopping::stopping()
172{
173        TORRENT_STATE_LOG(L"Entering stopping()");
174}
175
176stopping::~stopping()
177{
178        TORRENT_STATE_LOG(L"Exiting ~stopping()");
179}
180
181stopped::stopped()
182{
183        TORRENT_STATE_LOG(L"Entering stopped()");
184}
185
186stopped::~stopped()
187{
188        TORRENT_STATE_LOG(L"Exiting ~stopped()");
189}
190
191resume_data_waiting::resume_data_waiting()
192{
193        TORRENT_STATE_LOG(L"Entering resume_data_waiting()");
194}
195
196resume_data_waiting::~resume_data_waiting()
197{
198        TORRENT_STATE_LOG(L"Exiting ~resume_data_waiting()");
199}
200
201resume_data_idling::resume_data_idling()
202{
203        TORRENT_STATE_LOG(L"Entering resume_data_idling()");
204}
205
206resume_data_idling::~resume_data_idling()
207{
208        TORRENT_STATE_LOG(L"Exiting ~resume_data_idling()");
209}
210
211} // namespace hal
Note: See TracBrowser for help on using the repository browser.