2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 Kry ( elkry@users.sourceforge.net / http://www.amule.org )
5 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2008 Froenchenko Leonid (lfroen@gmail.com)
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #ifndef EXTERNALCONN_H
28 #define EXTERNALCONN_H
32 #include <ec/cpp/ECSpecialTags.h>
34 #include "amuleIPV4Address.h" // for amuleIPV4Address
35 #include "RLE.h" // for RLE
36 #include "DownloadQueue.h"
43 // E - type of encoder
44 // C - type of container in theApp
45 template <class T
, class E
, class C
>
46 class CFileEncoderMap
: public std::map
<T
*, E
> {
48 void UpdateEncoders(C
*container
)
50 // check if encoder contains files that no longer in container
51 // or, we have new files without encoder yet
52 std::set
<T
*> curr_files
, dead_files
;
53 for (unsigned int i
= 0; i
< container
->GetFileCount(); i
++) {
54 // cast for case of 'const'
55 T
*cur_file
= (T
*)container
->GetFileByIndex(i
);
56 curr_files
.insert(cur_file
);
57 if ( this->count(cur_file
) == 0 ) {
58 this->operator [](cur_file
) = E(cur_file
);
62 // curr_files set is created to minimize lookup time in download queue,
63 // since GetFileByID have loop inside leading to O(n), in this case
64 // it will mean O(n^2)
65 typename
std::map
<T
*, E
>::iterator i
;
66 for(i
= this->begin(); i
!= this->end(); i
++) {
67 if ( curr_files
.count(i
->first
) == 0 ) {
68 dead_files
.insert(i
->first
);
71 typename
std::set
<T
*>::iterator j
;
72 for(j
= dead_files
.begin(); j
!= dead_files
.end(); j
++) {
79 * PartStatus strings and gap lists are quite long - RLE encoding will help.
81 * Instead of sending each time full part-status string, send
82 * RLE encoded difference from previous one.
84 class CPartFile_Encoder
{
87 // PartFileEncoderData class is used for decode only,
88 // while CPartFile_Encoder is used for encode only.
89 PartFileEncoderData m_enc_data
;
94 CPartFile_Encoder(CPartFile
*file
= 0) { m_file
= file
; }
96 // encode - take data from m_file
97 void Encode(CECTag
*parent_tag
);
101 m_enc_data
.ResetEncoder();
105 typedef CFileEncoderMap
<CPartFile
, CPartFile_Encoder
, CDownloadQueue
> CPartFile_Encoder_Map
;
108 * Encode 'obtained parts' info to be sent to remote gui
110 class CKnownFile_Encoder
{
114 CKnownFile_Encoder(CKnownFile
*file
= 0) { m_file
= file
; }
116 // encode - take data from m_file
117 void Encode(CECTag
*parent_tag
);
121 m_enc_data
.ResetEncoder();
125 typedef CFileEncoderMap
<CKnownFile
, CKnownFile_Encoder
, CSharedFileList
> CKnownFile_Encoder_Map
;
127 template <class T
, ec_tagname_t OP
>
128 class CTagSet
: public std::set
<T
> {
129 void InSet(const CECTag
*tag
, uint32
)
131 this->insert(tag
->GetInt());
133 void InSet(const CECTag
*tag
, CMD4Hash
)
135 this->insert(tag
->GetMD4Data());
138 CTagSet(const CECPacket
*request
) : std::set
<T
>()
140 for (unsigned int i
= 0;i
< request
->GetTagCount();i
++) {
141 const CECTag
*tag
= request
->GetTagByIndex(i
);
142 if ( tag
->GetTagName() == OP
) {
143 this->InSet(tag
, T());
151 std::map
<void *, CValueMap
> m_obj_map
;
153 CValueMap
&GetValueMap(void *object
)
155 return m_obj_map
[object
];
160 return m_obj_map
.size();
163 void RemoveDeleted(std::set
<void *>& WXUNUSED(current_set
))
165 for(std::map<void *, CValueMap>::iterator i = m_obj_map.begin(); i != m_obj_map.end(); i++) {
166 if ( !current_set.count(i->first) ) {
167 m_obj_map.erase(i->first);
175 class CECServerSocket
;
178 class ExternalConn
: public wxEvtHandler
181 typedef std::set
<CECServerSocket
*> SocketSet
;
182 SocketSet socket_list
;
185 ExternalConn(amuleIPV4Address addr
, wxString
*msg
);
188 wxSocketServer
*m_ECServer
;
189 ECNotifier
*m_ec_notifier
;
191 void AddSocket(CECServerSocket
*s
);
192 void RemoveSocket(CECServerSocket
*s
);
193 void KillAllSockets();
196 // event handlers (these functions should _not_ be virtual)
197 void OnServerEvent(wxSocketEvent
& event
);
198 DECLARE_EVENT_TABLE()
201 class ECUpdateMsgSource
{
203 virtual ~ECUpdateMsgSource()
206 virtual CECPacket
*GetNextPacket() = 0;
209 class ECPartFileMsgSource
: public ECUpdateMsgSource
{
212 bool m_comment_changed
;
218 std::map
<CMD4Hash
, PARTFILE_STATUS
> m_dirty_status
;
220 ECPartFileMsgSource();
222 void SetDirty(CPartFile
*file
);
223 void SetNew(CPartFile
*file
);
224 void SetCompleted(CPartFile
*file
);
225 void SetRemoved(CPartFile
*file
);
227 virtual CECPacket
*GetNextPacket();
231 class ECKnownFileMsgSource
: public ECUpdateMsgSource
{
234 bool m_comment_changed
;
239 std::map
<CMD4Hash
, KNOWNFILE_STATUS
> m_dirty_status
;
241 ECKnownFileMsgSource();
243 void SetDirty(CKnownFile
*file
);
244 void SetNew(CKnownFile
*file
);
245 void SetRemoved(CKnownFile
*file
);
247 virtual CECPacket
*GetNextPacket();
250 class ECClientMsgSource
: public ECUpdateMsgSource
{
252 virtual CECPacket
*GetNextPacket();
255 class ECStatusMsgSource
: public ECUpdateMsgSource
{
256 uint32 m_last_ed2k_status_sent
;
257 uint32 m_last_kad_status_sent
;
260 uint32
GetEd2kStatus();
261 uint32
GetKadStatus();
265 virtual CECPacket
*GetNextPacket();
268 class ECSearchMsgSource
: public ECUpdateMsgSource
{
275 std::map
<CMD4Hash
, SEARCHFILE_STATUS
> m_dirty_status
;
279 void SetDirty(CSearchFile
*file
);
280 void SetChildDirty(CSearchFile
*file
);
284 virtual CECPacket
*GetNextPacket();
289 // designated priority for each type of update
291 enum EC_SOURCE_PRIO
{
301 //ECUpdateMsgSource *m_msg_source[EC_STATUS_LAST_PRIO];
302 std::map
<CECServerSocket
*, ECUpdateMsgSource
**> m_msg_source
;
304 void NextPacketToSocket();
306 CECPacket
*GetNextPacket(ECUpdateMsgSource
*msg_source_array
[]);
310 void Add_EC_Client(CECServerSocket
*sock
);
311 void Remove_EC_Client(CECServerSocket
*sock
);
313 CECPacket
*GetNextPacket(CECServerSocket
*sock
);
316 // Interface to notification macros
318 void DownloadFile_SetDirty(CPartFile
*file
);
319 void DownloadFile_RemoveFile(CPartFile
*file
);
320 void DownloadFile_RemoveSource(CPartFile
*file
);
321 void DownloadFile_AddFile(CPartFile
*file
);
322 void DownloadFile_AddSource(CPartFile
*file
);
324 void Status_ConnectionState();
325 void Status_QueueCount();
326 void Status_UserCount();
328 void SharedFile_AddFile(CKnownFile
*file
);
329 void SharedFile_RemoveFile(CKnownFile
*file
);
330 void SharedFile_RemoveAllFiles();
335 #endif // EXTERNALCONN_H
336 // File_checked_for_headers