1 /*
2  * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "stdinc.h"
20 
21 #include "Transfer.h"
22 
23 #include "UserConnection.h"
24 #include "ClientManager.h"
25 
26 namespace dcpp {
27 
28 const string Transfer::names[] = {
29     "file", "file", "list", "tthl"
30 };
31 
32 const string Transfer::USER_LIST_NAME = "files.xml";
33 const string Transfer::USER_LIST_NAME_BZ = "files.xml.bz2";
34 
Transfer(UserConnection & conn,const string & path_,const TTHValue & tth_)35 Transfer::Transfer(UserConnection& conn, const string& path_, const TTHValue& tth_) : segment(0, -1), type(TYPE_FILE), start(0),
36     path(path_), tth(tth_), actual(0), pos(0), userConnection(conn) { }
37 
tick()38 void Transfer::tick() {
39     Lock l(cs);
40 
41     uint64_t t = GET_TICK();
42 
43     if(samples.size() >= 1) {
44         int64_t tdiff = samples.back().first - samples.front().first;
45         if((tdiff / 1000) > MIN_SECS) {
46             while(samples.size() >= MIN_SAMPLES) {
47                 samples.pop_front();
48             }
49         }
50 
51     }
52 
53     if(samples.size() > 1) {
54         if(samples.back().second == pos) {
55             // Position hasn't changed, just update the time
56             samples.back().first = t;
57             return;
58         }
59     }
60 
61     samples.push_back(std::make_pair(t, pos));
62 }
63 
getAverageSpeed() const64 double Transfer::getAverageSpeed() const {
65     Lock l(cs);
66     if(samples.size() < 2) {
67         return 0;
68     }
69     uint64_t ticks = samples.back().first - samples.front().first;
70     int64_t bytes = samples.back().second - samples.front().second;
71 
72     return ticks > 0 ? (static_cast<double>(bytes) / ticks) * 1000.0 : 0;
73 }
74 
getParams(const UserConnection & aSource,StringMap & params)75 void Transfer::getParams(const UserConnection& aSource, StringMap& params) {
76     params["userCID"] = aSource.getUser()->getCID().toBase32();
77     params["userNI"] = Util::toString(ClientManager::getInstance()->getNicks(aSource.getUser()->getCID(), aSource.getHubUrl()));
78     params["userI4"] = aSource.getRemoteIp();
79     StringList hubNames = ClientManager::getInstance()->getHubNames(aSource.getUser()->getCID(), aSource.getHubUrl());
80     if(hubNames.empty())
81         hubNames.push_back(_("Offline"));
82     params["hub"] = Util::toString(hubNames);
83     StringList hubs = ClientManager::getInstance()->getHubs(aSource.getUser()->getCID(), aSource.getHubUrl());
84     if(hubs.empty())
85         hubs.push_back(_("Offline"));
86     params["hubURL"] = Util::toString(hubs);
87     params["fileSI"] = Util::toString(getSize());
88     params["fileSIshort"] = Util::formatBytes(getSize());
89     params["fileSIactual"] = Util::toString(getActual());
90     params["fileSIactualshort"] = Util::formatBytes(getActual());
91     params["speed"] = str(F_("%1%/s") % Util::formatBytes(getAverageSpeed()));
92     params["time"] = Util::formatSeconds((GET_TICK() - getStart()) / 1000);
93     params["fileTR"] = getTTH().toBase32();
94 }
95 
getUser()96 UserPtr Transfer::getUser() {
97     return getUserConnection().getUser();
98 }
99 
getUser() const100 const UserPtr Transfer::getUser() const {
101     return getUserConnection().getUser();
102 }
103 
getHintedUser() const104 const HintedUser Transfer::getHintedUser() const {
105     return getUserConnection().getHintedUser();
106 }
107 
108 } // namespace dcpp
109