• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • transfer-plugins
  • bittorrent
btdatasource.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2008 Lukas Appelhans <l.appelhans@gmx.de>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 */
10 
11 #include "btdatasource.h"
12 #include "btcache.h"
13 #include "btchunkselector.h"
14 #include "bittorrentsettings.h"
15 #include "core/download.h"
16 #include <torrent/torrentcontrol.h>
17 #include <util/error.h>
18 #include <torrent/globals.h>
19 #include <torrent/server.h>
20 #include <btversion.h>
21 #include <util/log.h>
22 #include <util/bitset.h>
23 #include <peer/authenticationmonitor.h>
24 #include <util/functions.h>
25 
26 #include <kdebug.h>
27 #include <kstandarddirs.h>
28 
29 using namespace bt;
30 
31 
32 BTDataSource::BTDataSource(const KUrl &srcUrl, QObject *parent)
33  : TransferDataSource(srcUrl, parent),
34  m_offset(0),
35  m_bytes(0),
36  m_torrentSource(KUrl())
37 {
38  bt::InitLog(KStandardDirs::locateLocal("appdata", "torrentlog.log"));//initialize the torrent-log
39 
40  bt::SetClientInfo("KGet",2,1,0,bt::NORMAL,"KG");//Set client info to KGet, WARNING: Pls change this for every release
41 
42  bt::Uint16 i = 0;
43  do
44  {
45  kDebug(5001) << "Trying to set port to" << BittorrentSettings::port() + i;
46  bt::Globals::instance().initServer(BittorrentSettings::port() + i);
47  i++;
48  }while (!bt::Globals::instance().getServer().isOK() && i < 10);
49 
50  if (!bt::Globals::instance().getServer().isOK())
51  return;
52  tc = new TorrentControl();
53  csf = new BTChunkSelectorFactory();
54  cf = new BTCacheFactory();
55  connect(cf, SIGNAL(cacheAdded(BTCache*)), SLOT(cacheAdded(BTCache*)));
56  connect(csf, SIGNAL(selectorAdded(BTChunkSelector*)), SLOT(selectorAdded(BTChunkSelector*)));
57  tc->setChunkSelectorFactory(csf);
58  tc->setCacheFactory(cf);
59  connect(&timer, SIGNAL(timeout()), SLOT(update()));
60 }
61 
62 BTDataSource::~BTDataSource()
63 {
64  delete tc;
65  delete cs;
66  delete cf;
67 }
68 
69 void BTDataSource::cacheAdded(BTCache *cache)
70 {
71  connect(cache, SIGNAL(dataArrived(KIO::fileoffset_t,QByteArray)), SLOT(getData(KIO::fileoffset_t,QByteArray)));
72 }
73 
74 void BTDataSource::selectorAdded(BTChunkSelector* selector)
75 {
76  cs = selector;
77 }
78 
79 void BTDataSource::start()
80 {
81  if (m_torrentSource.isEmpty())
82  {
83  Download *download = new Download(m_source, KStandardDirs::locateLocal("appdata", "tmp/") + m_source.fileName());
84  connect(download, SIGNAL(finishedSuccessfully(KUrl,QByteArray)), SLOT(init(KUrl,QByteArray)));
85  }
86  else
87  {
88  cs->excludeAll();
89  const BitSet & bits = tc->availableChunksBitSet();
90  bool av = true;
91  Uint32 firstChunk = m_offset / tc->getStats().chunk_size;
92  Uint32 lastChunk = ((m_offset + m_bytes) / tc->getStats().chunk_size) + 1;//The +1 is only a workaround for rounding up, but I dunno how to do it ;)
93  for (int i = firstChunk * tc->getStats().chunk_size * 8; i <= lastChunk * tc->getStats().chunk_size * 8; i++)
94  {
95  if (!bits.get(i))
96  {
97  emit broken();
98  av = false;
99  continue;
100  }
101  }
102  if (av)
103  {
104  cs->reincluded(firstChunk, lastChunk);
105  tc->start();
106  timer.start(250);
107  }
108  }
109 }
110 
111 void BTDataSource::stop()
112 {
113  tc->stop(true);
114  timer.stop();
115 }
116 
117 void BTDataSource::update()
118 {
119  bt::UpdateCurrentTime();
120  bt::AuthenticationMonitor::instance().update();
121  tc->update();
122 }
123 
124 void BTDataSource::init(const KUrl &torrentSource, const QByteArray &data)
125 {
126  Q_UNUSED(data)
127  m_torrentSource = torrentSource;
128  try
129  {
130  tc->init(0, m_torrentSource.url(), QString(), QString(), 0);
131  }
132  catch (bt::Error &err)
133  {
134  kDebug(5001) << err.toString();
135  //m_ready = false;
136  }
137  start();
138 }
139 
140 void BTDataSource::addSegment(const KIO::fileoffset_t offset, const KIO::fileoffset_t bytes, int segmentNum)
141 {
142  kDebug(5001);
143 
144  if (offset < m_offset)
145  {
146  m_offset = offset;
147  if (m_bytes < bytes + m_offset - offset)
148  {
149  m_bytes = bytes + m_offset - offset;
150  }
151  }
152  if (offset > m_offset && m_bytes < bytes + m_offset - offset)
153  {
154  m_bytes = bytes + m_offset - offset;
155  }
156  if (offset == m_offset && m_bytes < bytes)
157  {
158  m_bytes = bytes;
159  }
160 }
161 
162 void BTDataSource::getData(const KIO::fileoffset_t &off, const QByteArray &dataArray)
163 {
164  QByteArray splittedData;
165  if (off < m_offset)
166  splittedData = dataArray.right(dataArray.size() - (m_offset - off));
167  else if (m_offset + m_bytes < off + dataArray.size())
168  splittedData = dataArray.left((off + dataArray.size()) - (m_offset + m_bytes));
169  else
170  splittedData = dataArray;
171 
172  emit data(off, splittedData);
173 
174  if (m_offset + m_bytes == off + dataArray.size())
175  emit finished();
176 }
177 
178 #include "btdatasource.moc"
BTDataSource::start
void start()
Definition: btdatasource.cpp:79
Download
Definition: download.h:23
TransferDataSource::finished
void finished()
emitted when there is no more data
TransferDataSource::broken
void broken(TransferDataSource *source, TransferDataSource::Error error)
Alert that datasource is no able to send any data.
download.h
btchunkselector.h
BTChunkSelector
Definition: btchunkselector.h:29
BTDataSource::BTDataSource
BTDataSource(const KUrl &srcUrl, QObject *parent)
Definition: btdatasource.cpp:32
TransferDataSource
This Class is an interface for inter-plugins data change.
Definition: transferdatasource.h:26
QObject
BittorrentSettings::port
static int port()
Get Port.
Definition: bittorrentsettings.h:68
btcache.h
BTChunkSelector::reincluded
virtual void reincluded(bt::Uint32 from, bt::Uint32 to)
Definition: btchunkselector.cpp:247
bittorrentsettings.h
BTCacheFactory
Definition: btcache.h:156
TransferDataSource::data
void data(KIO::fileoffset_t offset, const QByteArray &data, bool &worked)
Returns data in the forms of chucks.
BTDataSource::~BTDataSource
~BTDataSource()
Definition: btdatasource.cpp:62
BTChunkSelector::excludeAll
virtual void excludeAll()
Definition: btchunkselector.cpp:274
btdatasource.h
BTDataSource::getData
void getData(const KIO::fileoffset_t &off, const QByteArray &dataArray)
Definition: btdatasource.cpp:162
BTDataSource::stop
void stop()
Definition: btdatasource.cpp:111
BTChunkSelectorFactory
Definition: btchunkselector.h:49
BTCache
Definition: btcache.h:33
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal