Handle piece message.
[tairent.git] / src / main / storage.h
blob26e8d629b519beb9c20858473edaa86c58db66ba
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _main_storage_h
18 #define _main_storage_h
20 #include <list>
21 #include <map>
22 #include <set>
24 #include <tairon/core/signals.h>
26 #include "core/bencode.h"
28 namespace Tairent
31 namespace Core
34 class BitField;
36 }; // namespace Core
38 namespace Main
41 class Connection;
43 struct FileStruct
45 int fd;
46 unsigned int refCount;
49 struct FilePositionStruct
51 String name;
52 uint64_t length;
53 uint64_t start;
56 struct PieceStruct
58 struct FilePieceStruct {
59 char *mem;
60 void *priv;
61 int fd;
62 uint32_t start;
63 uint32_t length;
64 uint32_t privLength;
67 unsigned int refCount;
68 std::list<FilePieceStruct> pieces;
71 struct RequestStruct
73 std::set<uint32_t> queued;
74 std::set<uint32_t> requested;
77 /** \brief Storage is an abstraction layer over filesystem.
79 class Storage
81 public:
82 /** Constructs a new Storage object for a torrent.
84 * \param i Info part of the torrent metainfo.
86 Storage(const Tairent::Core::BEncode &i);
88 /** Destroys the object.
90 ~Storage();
92 /** Adds a bitfield to the availability list.
94 void addBitField(Tairent::Core::BitField *b);
96 /** Starts downloading of the incoming piece.
98 void gotPiece(uint32_t index, uint32_t start, Connection *c);
100 /** Picks a piece to download. If there is a piece to download then
101 * this method returns true; otherwise returns false.
103 * \param b Peer's bitfield.
104 * \param index Index of the block.
105 * \param start Start of the piece within the block.
106 * \param legnth Length of the piece.
108 bool pickPiece(Tairent::Core::BitField *b, uint32_t &index, uint32_t &start, uint32_t &length);
110 /** Informs that a requested piece was downloaded.
112 void pieceDownloaded(uint32_t index, uint32_t start, Connection *c);
114 /** Removes a bitfield from the availability list.
116 void removeBitField(Tairent::Core::BitField *b);
118 /** Returns true if the client should be interested; otherwise returns
119 * false. (See BitTorrent protocol for details.)
121 bool shouldBeInterested(Tairent::Core::BitField *b);
123 public:
124 Tairon::Core::Signal2<void, Storage *, Connection *> invalidDataSignal;
126 private:
127 /** Fills files list and computes total length.
129 void buildFilesList();
131 /** Closes opened file if it isn't used.
133 void closeFile(const String &name);
135 /** Creates a file and truncates it to the specified length.
137 void createFile(const String &name, uint64_t length);
139 /** Creates files for this torrent.
141 void createFiles();
143 /** Returns length of a chunk.
145 * \param index Piece number.
146 * \param start Offset of the chunk within the piece.
148 inline uint32_t getChunkLength(uint32_t index, uint32_t start);
150 /** Returns length of a piece.
152 * \param index Piece number.
154 inline uint32_t getPieceLength(uint32_t index);
156 /** Converts a path list to a string.
158 String listToString(const Tairent::Core::BEncode::List &l);
160 /** Maps a piece to the memory.
162 void mapPiece(uint32_t index);
164 /** Opens a file.
166 * \param filename Path with the file including its name.
168 int openFile(const String &filename);
170 /** Creates a permutation of pieces numbers.
172 void scramble();
174 private:
175 /** How many peers have specific piece.
177 unsigned short *availability;
179 /** Bitfield of this storage.
181 Tairent::Core::BitField *bitfield;
183 /** List of opened files.
185 std::map<String, FileStruct> files;
187 /** List of positions of files.
189 std::list<FilePositionStruct> filesPositions;
191 /** Info part of the torrent metainfo.
193 const Tairent::Core::BEncode &info;
195 /** Number of pieces.
197 size_t pieceCount;
199 /** Length of one piece.
201 uint32_t pieceLength;
203 /** Mapping from piece index to its struct.
205 std::map<uint32_t, PieceStruct *> pieces;
207 /** List of active requests. The key is a piece index.
209 std::map<uint32_t, RequestStruct *> requests;
211 /** A permutation of length pieceCount.
213 std::list<uint32_t> scrambled;
215 /** Sum of lengths of all files.
217 uint64_t totalLength;
220 }; // namespace Main
222 }; // namespace Tairent
224 #endif
226 // vim: ai sw=4 ts=4 noet fdm=marker