1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2006 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #ifndef D_UT_PEX_EXTENSION_MESSAGE_H
36 #define D_UT_PEX_EXTENSION_MESSAGE_H
37 
38 #include "ExtensionMessage.h"
39 
40 #include <utility>
41 #include <vector>
42 #include <memory>
43 
44 #include "a2time.h"
45 #include "a2functional.h"
46 
47 namespace aria2 {
48 
49 class PeerStorage;
50 class Peer;
51 
52 class UTPexExtensionMessage : public ExtensionMessage {
53 private:
54   uint8_t extensionMessageID_;
55 
56   std::vector<std::shared_ptr<Peer>> freshPeers_;
57 
58   std::vector<std::shared_ptr<Peer>> droppedPeers_;
59 
60   PeerStorage* peerStorage_;
61 
62   std::chrono::seconds interval_;
63 
64   size_t maxFreshPeer_;
65 
66   size_t maxDroppedPeer_;
67 
68   std::pair<std::pair<std::string, std::string>,
69             std::pair<std::string, std::string>>
70   createCompactPeerListAndFlag(const std::vector<std::shared_ptr<Peer>>& peers);
71 
72 public:
73   UTPexExtensionMessage(uint8_t extensionMessageID);
74 
75   virtual std::string getPayload() CXX11_OVERRIDE;
76 
getExtensionMessageID()77   virtual uint8_t getExtensionMessageID() const CXX11_OVERRIDE
78   {
79     return extensionMessageID_;
80   }
81 
getExtensionName()82   virtual const char* getExtensionName() const CXX11_OVERRIDE
83   {
84     return EXTENSION_NAME;
85   }
86 
87   static const char EXTENSION_NAME[];
88 
89   virtual std::string toString() const CXX11_OVERRIDE;
90 
91   virtual void doReceivedAction() CXX11_OVERRIDE;
92 
93   bool addFreshPeer(const std::shared_ptr<Peer>& peer);
94 
95   const std::vector<std::shared_ptr<Peer>>& getFreshPeers() const;
96 
97   bool freshPeersAreFull() const;
98 
99   bool addDroppedPeer(const std::shared_ptr<Peer>& peer);
100 
101   const std::vector<std::shared_ptr<Peer>>& getDroppedPeers() const;
102 
103   bool droppedPeersAreFull() const;
104 
105   void setPeerStorage(PeerStorage* peerStorage);
106 
107   static std::unique_ptr<UTPexExtensionMessage>
108   create(const unsigned char* data, size_t len);
109 
110   void setMaxFreshPeer(size_t maxFreshPeer);
111 
getMaxFreshPeer()112   size_t getMaxFreshPeer() const { return maxFreshPeer_; }
113 
114   void setMaxDroppedPeer(size_t maxDroppedPeer);
115 
getMaxDroppedPeer()116   size_t getMaxDroppedPeer() const { return maxDroppedPeer_; }
117 
118   constexpr static auto DEFAULT_INTERVAL = 1_min;
119 };
120 
121 } // namespace aria2
122 
123 #endif // D_UT_PEX_EXTENSION_MESSAGE_H
124