2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 Kry ( elkry@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
28 #include "config.h" // Needed for VERSION
31 #include <ec/cpp/ECMuleSocket.h> // Needed for CECSocket
33 #include <common/Format.h> // Needed for CFormat
35 #include <common/ClientVersion.h>
36 #include <common/MD5Sum.h>
38 #include "ExternalConn.h" // Interface declarations
39 #include "updownclient.h" // Needed for CUpDownClient
40 #include "Server.h" // Needed for CServer
41 #include "ServerList.h" // Needed for CServerList
42 #include "PartFile.h" // Needed for CPartFile
43 #include "ServerConnect.h" // Needed for CServerConnect
44 #include "UploadQueue.h" // Needed for CUploadQueue
45 #include "amule.h" // Needed for theApp
46 #include "SearchList.h" // Needed for GetSearchResults
47 #include "IPFilter.h" // Needed for CIPFilter
48 #include "ClientList.h"
49 #include "Preferences.h" // Needed for CPreferences
51 #include "GuiEvents.h" // Needed for Notify_* macros
52 #include "Statistics.h" // Needed for theStats
53 #include "KnownFileList.h" // Needed for CKnownFileList
54 #include "RandomFunctions.h"
55 #include "kademlia/kademlia/Kademlia.h"
56 #include "kademlia/kademlia/UDPFirewallTester.h"
59 //-------------------- File_Encoder --------------------
63 * Encode 'obtained parts' info to be sent to remote gui
65 class CKnownFile_Encoder
{
66 // number of sources for each part for progress bar colouring
69 const CKnownFile
*m_file
;
71 CKnownFile_Encoder(const CKnownFile
*file
= 0) { m_file
= file
; }
73 virtual ~CKnownFile_Encoder() {}
75 virtual void Encode(CECTag
*parent_tag
);
77 virtual void ResetEncoder()
79 m_enc_data
.ResetEncoder();
82 virtual void SetShared() { }
83 virtual bool IsShared() { return true; }
84 virtual bool IsDownload() { return m_file
->IsPartFile(); }
85 const CKnownFile
* GetFile() { return m_file
; }
89 * PartStatus strings and gap lists are quite long - RLE encoding will help.
91 * Instead of sending each time full part-status string, send
92 * RLE encoded difference from previous one.
94 * PartFileEncoderData class is used for decode only,
95 * while CPartFile_Encoder is used for encode only.
97 class CPartFile_Encoder
: public CKnownFile_Encoder
{
98 // blocks requested for download
99 RLE_Data m_req_status
;
101 RLE_Data m_gap_status
;
103 SourcenameItemMap m_sourcenameItemMap
;
104 // counter for unique source name ids
106 // not all part files are shared (only when at least one part is complete)
109 // cast inherited member to CPartFile
110 CPartFile
* m_PartFile() { wxASSERT(m_file
->IsCPartFile()); return (CPartFile
*)m_file
; }
113 CPartFile_Encoder(const CPartFile
*file
= 0) : CKnownFile_Encoder(file
)
119 virtual ~CPartFile_Encoder() {}
121 // encode - take data from m_file
122 virtual void Encode(CECTag
*parent_tag
);
124 // Encoder may reset history if full info requested
125 virtual void ResetEncoder();
127 virtual void SetShared() { m_shared
= true; }
128 virtual bool IsShared() { return m_shared
; }
129 virtual bool IsDownload() { return true; }
132 class CFileEncoderMap
: public std::map
<uint32
, CKnownFile_Encoder
*> {
133 typedef std::set
<uint32
> IDSet
;
136 void UpdateEncoders();
139 CFileEncoderMap::~CFileEncoderMap()
141 // DeleteContents() causes infinite recursion here!
142 for (iterator it
= begin(); it
!= end(); it
++) {
147 // Check if encoder contains files that are no longer used
148 // or if we have new files without encoder yet.
149 void CFileEncoderMap::UpdateEncoders()
151 IDSet curr_files
, dead_files
;
153 std::vector
<CPartFile
*> downloads
;
154 theApp
->downloadqueue
->CopyFileList(downloads
);
155 for (uint32 i
= downloads
.size(); i
--;) {
156 uint32 id
= downloads
[i
]->ECID();
157 curr_files
.insert(id
);
159 (*this)[id
] = new CPartFile_Encoder(downloads
[i
]);
163 std::vector
<CKnownFile
*> shares
;
164 theApp
->sharedfiles
->CopyFileList(shares
);
165 for (uint32 i
= shares
.size(); i
--;) {
166 uint32 id
= shares
[i
]->ECID();
167 if (shares
[i
]->IsPartFile()) { // we already have it
168 (*this)[id
]->SetShared();
171 curr_files
.insert(id
);
173 (*this)[id
] = new CKnownFile_Encoder(shares
[i
]);
176 // Check for removed files, and store them in a set for deletion.
177 // (std::map documentation is unclear if a construct like
178 // iterator to_del = it++; erase(to_del) ;
179 // works or invalidates it too.)
180 for (iterator it
= begin(); it
!= end(); it
++) {
181 if (!curr_files
.count(it
->first
)) {
182 dead_files
.insert(it
->first
);
186 for (IDSet::iterator it
= dead_files
.begin(); it
!= dead_files
.end(); it
++) {
187 iterator it2
= find(*it
);
194 //-------------------- CECServerSocket --------------------
196 class CECServerSocket
: public CECMuleSocket
199 CECServerSocket(ECNotifier
*notifier
);
200 virtual ~CECServerSocket();
202 virtual const CECPacket
*OnPacketReceived(const CECPacket
*packet
, uint32 trueSize
);
203 virtual void OnLost();
205 virtual void WriteDoneAndQueueEmpty();
207 ECNotifier
*m_ec_notifier
;
209 const CECPacket
*Authenticate(const CECPacket
*);
218 uint64_t m_passwd_salt
;
219 CLoggerAccess m_LoggerAccess
;
220 CFileEncoderMap m_FileEncoder
;
221 CObjTagMap m_obj_tagmap
;
222 CECPacket
*ProcessRequest2(const CECPacket
*request
);
224 virtual bool IsAuthorized() { return m_conn_state
== CONN_ESTABLISHED
; }
228 CECServerSocket::CECServerSocket(ECNotifier
*notifier
)
231 m_conn_state(CONN_INIT
),
232 m_passwd_salt(GetRandomUint64())
234 wxASSERT(theApp
->ECServerHandler
);
235 theApp
->ECServerHandler
->AddSocket(this);
236 m_ec_notifier
= notifier
;
240 CECServerSocket::~CECServerSocket()
242 wxASSERT(theApp
->ECServerHandler
);
243 theApp
->ECServerHandler
->RemoveSocket(this);
247 const CECPacket
*CECServerSocket::OnPacketReceived(const CECPacket
*packet
, uint32 trueSize
)
249 packet
->DebugPrint(true, trueSize
);
251 const CECPacket
*reply
= NULL
;
253 if (m_conn_state
== CONN_FAILED
) {
254 // Client didn't close the socket when authentication failed.
255 AddLogLineM(false, _("Client sent packet after authentication failed."));
259 if (m_conn_state
!= CONN_ESTABLISHED
) {
260 // This is called twice:
262 // 2) verify password
263 reply
= Authenticate(packet
);
265 reply
= ProcessRequest2(packet
);
271 void CECServerSocket::OnLost()
273 AddLogLineM(false,_("External connection closed."));
274 theApp
->ECServerHandler
->m_ec_notifier
->Remove_EC_Client(this);
278 void CECServerSocket::WriteDoneAndQueueEmpty()
280 if ( HaveNotificationSupport() && (m_conn_state
== CONN_ESTABLISHED
) ) {
281 CECPacket
*packet
= m_ec_notifier
->GetNextPacket(this);
286 //printf("[EC] %p: WriteDoneAndQueueEmpty but notification disabled\n", this);
290 //-------------------- ExternalConn --------------------
298 BEGIN_EVENT_TABLE(ExternalConn
, wxEvtHandler
)
299 EVT_SOCKET(SERVER_ID
, ExternalConn::OnServerEvent
)
303 ExternalConn::ExternalConn(amuleIPV4Address addr
, wxString
*msg
)
307 // Are we allowed to accept External Connections?
308 if ( thePrefs::AcceptExternalConnections() ) {
309 // We must have a valid password, otherwise we will not allow EC connections
310 if (thePrefs::ECPassword().IsEmpty()) {
311 *msg
+= wxT("External connections disabled due to empty password!\n");
312 AddLogLineM(true, _("External connections disabled due to empty password!"));
317 m_ECServer
= new wxSocketServer(addr
, wxSOCKET_REUSEADDR
);
318 m_ECServer
->SetEventHandler(*this, SERVER_ID
);
319 m_ECServer
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
320 m_ECServer
->Notify(true);
322 int port
= addr
.Service();
323 wxString ip
= addr
.IPAddress();
324 if (m_ECServer
->Ok()) {
325 msgLocal
= CFormat(wxT("*** TCP socket (ECServer) listening on %s:%d")) % ip
% port
;
326 *msg
+= msgLocal
+ wxT("\n");
327 AddLogLineM(false, msgLocal
);
329 msgLocal
= CFormat(wxT("Could not listen for external connections at %s:%d!")) % ip
% port
;
330 *msg
+= msgLocal
+ wxT("\n");
331 AddLogLineM(false, msgLocal
);
334 *msg
+= wxT("External connections disabled in config file\n");
335 AddLogLineM(false,_("External connections disabled in config file"));
337 m_ec_notifier
= new ECNotifier();
341 ExternalConn::~ExternalConn()
345 delete m_ec_notifier
;
349 void ExternalConn::AddSocket(CECServerSocket
*s
)
352 socket_list
.insert(s
);
356 void ExternalConn::RemoveSocket(CECServerSocket
*s
)
359 socket_list
.erase(s
);
363 void ExternalConn::KillAllSockets()
365 AddDebugLogLineN(logGeneral
,
366 CFormat(wxT("ExternalConn::KillAllSockets(): %d sockets to destroy.")) %
368 SocketSet::iterator it
= socket_list
.begin();
369 while (it
!= socket_list
.end()) {
370 CECServerSocket
*s
= *(it
++);
377 void ExternalConn::OnServerEvent(wxSocketEvent
& WXUNUSED(event
))
379 CECServerSocket
*sock
= new CECServerSocket(m_ec_notifier
);
380 // Accept new connection if there is one in the pending
381 // connections queue, else exit. We use Accept(FALSE) for
382 // non-blocking accept (although if we got here, there
383 // should ALWAYS be a pending connection).
384 if ( m_ECServer
->AcceptWith(*sock
, false) ) {
385 AddLogLineM(false, _("New external connection accepted"));
388 AddLogLineM(false, _("ERROR: couldn't accept a new external connection"));
396 const CECPacket
*CECServerSocket::Authenticate(const CECPacket
*request
)
400 if (request
== NULL
) {
401 return new CECPacket(EC_OP_AUTH_FAIL
);
404 // Password must be specified if we are to allow remote connections
405 if ( thePrefs::ECPassword().IsEmpty() ) {
406 AddLogLineM(true, _("External connection refused due to empty password in preferences!"));
408 return new CECPacket(EC_OP_AUTH_FAIL
);
411 if ((m_conn_state
== CONN_INIT
) && (request
->GetOpCode() == EC_OP_AUTH_REQ
) ) {
412 const CECTag
*clientName
= request
->GetTagByName(EC_TAG_CLIENT_NAME
);
413 const CECTag
*clientVersion
= request
->GetTagByName(EC_TAG_CLIENT_VERSION
);
415 AddLogLineM(false, CFormat( _("Connecting client: %s %s") )
416 % ( clientName
? clientName
->GetStringData() : wxString(_("Unknown")) )
417 % ( clientVersion
? clientVersion
->GetStringData() : wxString(_("Unknown version")) ) );
418 const CECTag
*protocol
= request
->GetTagByName(EC_TAG_PROTOCOL_VERSION
);
420 // For SVN versions, both client and server must use SVNDATE, and they must be the same
422 if (!vhash
.Decode(wxT(EC_VERSION_ID
))) {
423 response
= new CECPacket(EC_OP_AUTH_FAIL
);
424 response
->AddTag(CECTag(EC_TAG_STRING
, wxT("Fatal error, version hash is not a valid MD4-hash.")));
425 } else if (!request
->GetTagByName(EC_TAG_VERSION_ID
) || request
->GetTagByNameSafe(EC_TAG_VERSION_ID
)->GetMD4Data() != vhash
) {
426 response
= new CECPacket(EC_OP_AUTH_FAIL
);
427 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Incorrect EC version ID, there might be binary incompatibility. Use core and remote from same snapshot.")));
429 // For release versions, we don't want to allow connections from any arbitrary SVN client.
430 if (request
->GetTagByName(EC_TAG_VERSION_ID
)) {
431 response
= new CECPacket(EC_OP_AUTH_FAIL
);
432 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("You cannot connect to a release version from an arbitrary development snapshot! *sigh* possible crash prevented")));
434 } else if (protocol
!= NULL
) {
435 uint16 proto_version
= protocol
->GetInt();
436 if (proto_version
== EC_CURRENT_PROTOCOL_VERSION
) {
437 response
= new CECPacket(EC_OP_AUTH_SALT
);
438 response
->AddTag(CECTag(EC_TAG_PASSWD_SALT
, m_passwd_salt
));
439 m_conn_state
= CONN_SALT_SENT
;
441 // So far ok, check capabilities of client
443 bool canZLIB
= false, canUTF8numbers
= false;
444 if (request
->GetTagByName(EC_TAG_CAN_ZLIB
)) {
446 m_my_flags
|= EC_FLAG_ZLIB
;
448 if (request
->GetTagByName(EC_TAG_CAN_UTF8_NUMBERS
)) {
449 canUTF8numbers
= true;
450 m_my_flags
|= EC_FLAG_UTF8_NUMBERS
;
452 m_haveNotificationSupport
= request
->GetTagByName(EC_TAG_CAN_NOTIFY
) != NULL
;
453 AddDebugLogLineN(logEC
, CFormat(wxT("Client capabilities: ZLIB: %s UTF8 numbers: %s Push notification: %s") )
454 % (canZLIB
? wxT("yes") : wxT("no"))
455 % (canUTF8numbers
? wxT("yes") : wxT("no"))
456 % (m_haveNotificationSupport
? wxT("yes") : wxT("no")));
458 response
= new CECPacket(EC_OP_AUTH_FAIL
);
459 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid protocol version.")
460 + CFormat(wxT("( %i != %i )")) % proto_version
% EC_CURRENT_PROTOCOL_VERSION
));
463 response
= new CECPacket(EC_OP_AUTH_FAIL
);
464 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Missing protocol version tag.")));
466 } else if ((m_conn_state
== CONN_SALT_SENT
) && (request
->GetOpCode() == EC_OP_AUTH_PASSWD
)) {
467 const CECTag
*passwd
= request
->GetTagByName(EC_TAG_PASSWD_HASH
);
470 if (!passh
.Decode(thePrefs::ECPassword())) {
471 wxString err
= wxTRANSLATE("Authentication failed: invalid hash specified as EC password.");
472 AddLogLineN(wxString(wxGetTranslation(err
))
473 + wxT(" ") + thePrefs::ECPassword());
474 response
= new CECPacket(EC_OP_AUTH_FAIL
);
475 response
->AddTag(CECTag(EC_TAG_STRING
, err
));
477 wxString saltHash
= MD5Sum(CFormat(wxT("%lX")) % m_passwd_salt
).GetHash();
478 wxString saltStr
= CFormat(wxT("%lX")) % m_passwd_salt
;
480 passh
.Decode(MD5Sum(thePrefs::ECPassword().Lower() + saltHash
).GetHash());
482 if (passwd
&& passwd
->GetMD4Data() == passh
) {
483 response
= new CECPacket(EC_OP_AUTH_OK
);
484 response
->AddTag(CECTag(EC_TAG_SERVER_VERSION
, wxT(VERSION
)));
488 err
= wxTRANSLATE("Authentication failed: wrong password.");
490 err
= wxTRANSLATE("Authentication failed: missing password.");
493 response
= new CECPacket(EC_OP_AUTH_FAIL
);
494 response
->AddTag(CECTag(EC_TAG_STRING
, err
));
495 AddLogLineM(false, wxGetTranslation(err
));
499 response
= new CECPacket(EC_OP_AUTH_FAIL
);
500 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid request, please authenticate first.")));
503 if (response
->GetOpCode() == EC_OP_AUTH_OK
) {
504 m_conn_state
= CONN_ESTABLISHED
;
505 AddLogLineM(false, _("Access granted."));
506 // Establish notification handler if client supports it
507 if (HaveNotificationSupport()) {
508 theApp
->ECServerHandler
->m_ec_notifier
->Add_EC_Client(this);
510 } else if (response
->GetOpCode() == EC_OP_AUTH_FAIL
) {
511 // Log message sent to client
512 if (response
->GetFirstTagSafe()->IsString()) {
513 AddLogLineM(false, CFormat(_("Sent error message \"%s\" to client.")) % wxGetTranslation(response
->GetFirstTagSafe()->GetStringData()));
516 amuleIPV4Address address
;
518 AddLogLineM(false, CFormat(_("Unauthorized access attempt from %s. Connection closed.")) % address
.IPAddress() );
519 m_conn_state
= CONN_FAILED
;
525 // Make a Logger tag (if there are any logging messages) and add it to the response
526 static void AddLoggerTag(CECPacket
*response
, CLoggerAccess
&LoggerAccess
)
528 if (LoggerAccess
.HasString()) {
529 CECEmptyTag
tag(EC_TAG_STATS_LOGGER_MESSAGE
);
530 // Tag structure is fix: tag carries nothing, inside are the strings
531 // maximum of 200 log lines per message
534 while (entries
< 200 && LoggerAccess
.GetString(line
)) {
535 tag
.AddTag(CECTag(EC_TAG_STRING
, line
));
538 response
->AddTag(tag
);
539 //printf("send Log tag %d %d\n", FirstEntry, entries);
543 static CECPacket
*Get_EC_Response_StatRequest(const CECPacket
*request
, CLoggerAccess
&LoggerAccess
)
545 CECPacket
*response
= new CECPacket(EC_OP_STATS
);
547 switch (request
->GetDetailLevel()) {
549 response
->AddTag(CECTag(EC_TAG_STATS_UP_OVERHEAD
, (uint32
)theStats::GetUpOverheadRate()));
550 response
->AddTag(CECTag(EC_TAG_STATS_DOWN_OVERHEAD
, (uint32
)theStats::GetDownOverheadRate()));
551 response
->AddTag(CECTag(EC_TAG_STATS_BANNED_COUNT
, /*(uint32)*/theStats::GetBannedCount()));
552 AddLoggerTag(response
, LoggerAccess
);
555 response
->AddTag(CECTag(EC_TAG_STATS_UL_SPEED
, (uint32
)theStats::GetUploadRate()));
556 response
->AddTag(CECTag(EC_TAG_STATS_DL_SPEED
, (uint32
)(theStats::GetDownloadRate())));
557 response
->AddTag(CECTag(EC_TAG_STATS_UL_SPEED_LIMIT
, (uint32
)(thePrefs::GetMaxUpload()*1024.0)));
558 response
->AddTag(CECTag(EC_TAG_STATS_DL_SPEED_LIMIT
, (uint32
)(thePrefs::GetMaxDownload()*1024.0)));
559 response
->AddTag(CECTag(EC_TAG_STATS_UL_QUEUE_LEN
, /*(uint32)*/theStats::GetWaitingUserCount()));
560 response
->AddTag(CECTag(EC_TAG_STATS_TOTAL_SRC_COUNT
, /*(uint32)*/theStats::GetFoundSources()));
563 uint32 totaluser
= 0, totalfile
= 0;
564 theApp
->serverlist
->GetUserFileStatus( totaluser
, totalfile
);
565 response
->AddTag(CECTag(EC_TAG_STATS_ED2K_USERS
, totaluser
));
566 response
->AddTag(CECTag(EC_TAG_STATS_KAD_USERS
, Kademlia::CKademlia::GetKademliaUsers()));
567 response
->AddTag(CECTag(EC_TAG_STATS_ED2K_FILES
, totalfile
));
568 response
->AddTag(CECTag(EC_TAG_STATS_KAD_FILES
, Kademlia::CKademlia::GetKademliaFiles()));
571 if (Kademlia::CKademlia::IsConnected()) {
572 response
->AddTag(CECTag(EC_TAG_STATS_KAD_FIREWALLED_UDP
, Kademlia::CUDPFirewallTester::IsFirewalledUDP(true)));
573 response
->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_SOURCES
, Kademlia::CKademlia::GetIndexed()->m_totalIndexSource
));
574 response
->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_KEYWORDS
, Kademlia::CKademlia::GetIndexed()->m_totalIndexKeyword
));
575 response
->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_NOTES
, Kademlia::CKademlia::GetIndexed()->m_totalIndexNotes
));
576 response
->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_LOAD
, Kademlia::CKademlia::GetIndexed()->m_totalIndexLoad
));
577 response
->AddTag(CECTag(EC_TAG_STATS_KAD_IP_ADRESS
, wxUINT32_SWAP_ALWAYS(Kademlia::CKademlia::GetPrefs()->GetIPAddress())));
578 response
->AddTag(CECTag(EC_TAG_STATS_KAD_IN_LAN_MODE
, Kademlia::CKademlia::IsRunningInLANMode()));
579 response
->AddTag(CECTag(EC_TAG_STATS_BUDDY_STATUS
, theApp
->clientlist
->GetBuddyStatus()));
581 uint16 BuddyPort
= 0;
582 CUpDownClient
* Buddy
= theApp
->clientlist
->GetBuddy();
584 BuddyIP
= Buddy
->GetIP();
585 BuddyPort
= Buddy
->GetUDPPort();
587 response
->AddTag(CECTag(EC_TAG_STATS_BUDDY_IP
, BuddyIP
));
588 response
->AddTag(CECTag(EC_TAG_STATS_BUDDY_PORT
, BuddyPort
));
590 case EC_DETAIL_UPDATE
:
591 case EC_DETAIL_INC_UPDATE
:
598 static CECPacket
*Get_EC_Response_GetSharedFiles(const CECPacket
*request
, CFileEncoderMap
&encoders
)
600 wxASSERT(request
->GetOpCode() == EC_OP_GET_SHARED_FILES
);
602 CECPacket
*response
= new CECPacket(EC_OP_SHARED_FILES
);
604 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
606 // request can contain list of queried items
607 CTagSet
<uint32
, EC_TAG_KNOWNFILE
> queryitems(request
);
609 encoders
.UpdateEncoders();
611 for (uint32 i
= 0; i
< theApp
->sharedfiles
->GetFileCount(); ++i
) {
612 CKnownFile
*cur_file
= (CKnownFile
*)theApp
->sharedfiles
->GetFileByIndex(i
);
614 if ( !cur_file
|| (!queryitems
.empty() && !queryitems
.count(cur_file
->ECID())) ) {
618 CEC_SharedFile_Tag
filetag(cur_file
, detail_level
);
619 CKnownFile_Encoder
*enc
= encoders
[cur_file
->ECID()];
620 if ( detail_level
!= EC_DETAIL_UPDATE
) {
623 enc
->Encode(&filetag
);
624 response
->AddTag(filetag
);
629 static CECPacket
*Get_EC_Response_GetUpdate(CFileEncoderMap
&encoders
, CObjTagMap
&tagmap
)
631 CECPacket
*response
= new CECPacket(EC_OP_SHARED_FILES
);
633 encoders
.UpdateEncoders();
634 for (CFileEncoderMap::iterator it
= encoders
.begin(); it
!= encoders
.end(); ++it
) {
635 const CKnownFile
*cur_file
= it
->second
->GetFile();
636 CValueMap
&valuemap
= tagmap
.GetValueMap(cur_file
->ECID());
637 if (cur_file
->IsCPartFile()) {
638 CEC_PartFile_Tag
filetag((const CPartFile
*) cur_file
, EC_DETAIL_INC_UPDATE
, &valuemap
);
639 // Add information if partfile is shared
640 filetag
.AddTag(EC_TAG_PARTFILE_SHARED
, it
->second
->IsShared(), &valuemap
);
642 CPartFile_Encoder
* enc
= (CPartFile_Encoder
*) encoders
[cur_file
->ECID()];
643 enc
->Encode(&filetag
);
644 response
->AddTag(filetag
);
646 CEC_SharedFile_Tag
filetag(cur_file
, EC_DETAIL_INC_UPDATE
, &valuemap
);
647 CKnownFile_Encoder
* enc
= encoders
[cur_file
->ECID()];
648 enc
->Encode(&filetag
);
649 response
->AddTag(filetag
);
653 CECEmptyTag
clients(EC_TAG_CLIENT
);
654 const CClientList::IDMap
& clientList
= theApp
->clientlist
->GetClientList();
655 for (CClientList::IDMap::const_iterator it
= clientList
.begin(); it
!= clientList
.end(); it
++) {
656 const CUpDownClient
* cur_client
= it
->second
;
657 CValueMap
&valuemap
= tagmap
.GetValueMap(cur_client
->ECID());
658 clients
.AddTag(CEC_UpDownClient_Tag(cur_client
, EC_DETAIL_INC_UPDATE
, &valuemap
));
660 response
->AddTag(clients
);
665 static CECPacket
*Get_EC_Response_GetClientQueue(const CECPacket
*request
, CObjTagMap
&tagmap
, int op
)
667 CECPacket
*response
= new CECPacket(op
);
669 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
672 // request can contain list of queried items
673 // (not for incremental update of course)
674 CTagSet
<uint32
, EC_TAG_CLIENT
> queryitems(request
);
676 const CClientPtrList
& clients
= theApp
->uploadqueue
->GetUploadingList();
677 CClientPtrList::const_iterator it
= clients
.begin();
678 for (; it
!= clients
.end(); ++it
) {
679 CUpDownClient
* cur_client
= *it
;
681 if (!cur_client
) { // shouldn't happen
684 if (!queryitems
.empty() && !queryitems
.count(cur_client
->ECID())) {
687 CValueMap
*valuemap
= NULL
;
688 if (detail_level
== EC_DETAIL_INC_UPDATE
) {
689 valuemap
= &tagmap
.GetValueMap(cur_client
->ECID());
691 CEC_UpDownClient_Tag
cli_tag(cur_client
, detail_level
, valuemap
);
693 response
->AddTag(cli_tag
);
700 static CECPacket
*Get_EC_Response_GetDownloadQueue(const CECPacket
*request
, CFileEncoderMap
&encoders
)
702 CECPacket
*response
= new CECPacket(EC_OP_DLOAD_QUEUE
);
704 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
706 // request can contain list of queried items
707 CTagSet
<uint32
, EC_TAG_PARTFILE
> queryitems(request
);
709 encoders
.UpdateEncoders();
711 for (unsigned int i
= 0; i
< theApp
->downloadqueue
->GetFileCount(); i
++) {
712 CPartFile
*cur_file
= theApp
->downloadqueue
->GetFileByIndex(i
);
714 if ( !queryitems
.empty() && !queryitems
.count(cur_file
->ECID()) ) {
718 CEC_PartFile_Tag
filetag(cur_file
, detail_level
);
720 CPartFile_Encoder
* enc
= (CPartFile_Encoder
*) encoders
[cur_file
->ECID()];
721 if ( detail_level
!= EC_DETAIL_UPDATE
) {
724 enc
->Encode(&filetag
);
726 response
->AddTag(filetag
);
732 static CECPacket
*Get_EC_Response_PartFile_Cmd(const CECPacket
*request
)
734 CECPacket
*response
= NULL
;
736 // request can contain multiple files.
737 for (CECPacket::const_iterator it1
= request
->begin(); it1
!= request
->end(); it1
++) {
738 const CECTag
&hashtag
= *it1
;
740 wxASSERT(hashtag
.GetTagName() == EC_TAG_PARTFILE
);
742 CMD4Hash hash
= hashtag
.GetMD4Data();
743 CPartFile
*pfile
= theApp
->downloadqueue
->GetFileByID( hash
);
746 AddLogLineM(false,CFormat(_("Remote PartFile command failed: FileHash not found: %s")) % hash
.Encode());
747 response
= new CECPacket(EC_OP_FAILED
);
748 response
->AddTag(CECTag(EC_TAG_STRING
, CFormat(wxString(wxTRANSLATE("FileHash not found: %s"))) % hash
.Encode()));
752 switch (request
->GetOpCode()) {
753 case EC_OP_PARTFILE_SWAP_A4AF_THIS
:
754 if ((pfile
->GetStatus(false) == PS_READY
) ||
755 (pfile
->GetStatus(false) == PS_EMPTY
)) {
756 CKnownFile::SourceSet::const_iterator it
= pfile
->GetA4AFList().begin();
757 while ( it
!= pfile
->GetA4AFList().end() ) {
758 CUpDownClient
*cur_source
= *it
++;
760 cur_source
->SwapToAnotherFile(true, false, false, pfile
);
764 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO
:
765 pfile
->SetA4AFAuto(!pfile
->IsA4AFAuto());
767 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS
:
768 if ((pfile
->GetStatus(false) == PS_READY
) ||
769 (pfile
->GetStatus(false) == PS_EMPTY
)) {
770 CKnownFile::SourceSet::const_iterator it
= pfile
->GetSourceList().begin();
771 while ( it
!= pfile
->GetSourceList().end() ) {
772 CUpDownClient
* cur_source
= *it
++;
774 cur_source
->SwapToAnotherFile(false, false, false, NULL
);
778 case EC_OP_PARTFILE_PAUSE
:
781 case EC_OP_PARTFILE_RESUME
:
783 pfile
->SavePartFile();
785 case EC_OP_PARTFILE_STOP
:
788 case EC_OP_PARTFILE_PRIO_SET
: {
789 uint8 prio
= hashtag
.GetFirstTagSafe()->GetInt();
790 if ( prio
== PR_AUTO
) {
791 pfile
->SetAutoDownPriority(1);
793 pfile
->SetAutoDownPriority(0);
794 pfile
->SetDownPriority(prio
);
798 case EC_OP_PARTFILE_DELETE
:
799 if ( thePrefs::StartNextFile() && (pfile
->GetStatus() != PS_PAUSED
) ) {
800 theApp
->downloadqueue
->StartNextFile(pfile
);
805 case EC_OP_PARTFILE_SET_CAT
:
806 pfile
->SetCategory(hashtag
.GetFirstTagSafe()->GetInt());
810 response
= new CECPacket(EC_OP_FAILED
);
811 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("OOPS! OpCode processing error!")));
816 response
= new CECPacket(EC_OP_NOOP
);
821 static CECPacket
*Get_EC_Response_Server_Add(const CECPacket
*request
)
823 CECPacket
*response
= NULL
;
825 wxString full_addr
= request
->GetTagByNameSafe(EC_TAG_SERVER_ADDRESS
)->GetStringData();
826 wxString name
= request
->GetTagByNameSafe(EC_TAG_SERVER_NAME
)->GetStringData();
828 wxString s_ip
= full_addr
.Left(full_addr
.Find(':'));
829 wxString s_port
= full_addr
.Mid(full_addr
.Find(':') + 1);
831 long port
= StrToULong(s_port
);
832 CServer
* toadd
= new CServer(port
, s_ip
);
833 toadd
->SetListName(name
.IsEmpty() ? full_addr
: name
);
835 if ( theApp
->AddServer(toadd
, true) ) {
836 response
= new CECPacket(EC_OP_NOOP
);
838 response
= new CECPacket(EC_OP_FAILED
);
839 response
->AddTag(CECTag(EC_TAG_STRING
, _("Server not added")));
846 static CECPacket
*Get_EC_Response_Server(const CECPacket
*request
)
848 CECPacket
*response
= NULL
;
849 const CECTag
*srv_tag
= request
->GetFirstTagSafe();
852 srv
= theApp
->serverlist
->GetServerByIPTCP(srv_tag
->GetIPv4Data().IP(), srv_tag
->GetIPv4Data().m_port
);
853 // server tag passed, but server not found
855 response
= new CECPacket(EC_OP_FAILED
);
856 response
->AddTag(CECTag(EC_TAG_STRING
,
857 CFormat(wxString(wxTRANSLATE("server not found: %s"))) % srv_tag
->GetIPv4Data().StringIP()));
861 switch (request
->GetOpCode()) {
862 case EC_OP_SERVER_DISCONNECT
:
863 theApp
->serverconnect
->Disconnect();
864 response
= new CECPacket(EC_OP_NOOP
);
866 case EC_OP_SERVER_REMOVE
:
868 theApp
->serverlist
->RemoveServer(srv
);
869 response
= new CECPacket(EC_OP_NOOP
);
871 response
= new CECPacket(EC_OP_FAILED
);
872 response
->AddTag(CECTag(EC_TAG_STRING
,
873 wxTRANSLATE("need to define server to be removed")));
876 case EC_OP_SERVER_CONNECT
:
877 if (thePrefs::GetNetworkED2K()) {
879 theApp
->serverconnect
->ConnectToServer(srv
);
880 response
= new CECPacket(EC_OP_NOOP
);
882 theApp
->serverconnect
->ConnectToAnyServer();
883 response
= new CECPacket(EC_OP_NOOP
);
886 response
= new CECPacket(EC_OP_FAILED
);
887 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("eD2k is disabled in preferences.")));
892 response
= new CECPacket(EC_OP_FAILED
);
893 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("OOPS! OpCode processing error!")));
898 static CECPacket
*Get_EC_Response_Search_Results(const CECPacket
*request
)
900 CECPacket
*response
= new CECPacket(EC_OP_SEARCH_RESULTS
);
902 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
904 // request can contain list of queried items
905 CTagSet
<uint32
, EC_TAG_SEARCHFILE
> queryitems(request
);
907 const CSearchResultList
& list
= theApp
->searchlist
->GetSearchResults(0xffffffff);
908 CSearchResultList::const_iterator it
= list
.begin();
909 while (it
!= list
.end()) {
910 CSearchFile
* sf
= *it
++;
911 if ( !queryitems
.empty() && !queryitems
.count(sf
->ECID()) ) {
914 response
->AddTag(CEC_SearchFile_Tag(sf
, detail_level
));
919 static CECPacket
*Get_EC_Response_Search_Results(CObjTagMap
&tagmap
)
921 CECPacket
*response
= new CECPacket(EC_OP_SEARCH_RESULTS
);
923 const CSearchResultList
& list
= theApp
->searchlist
->GetSearchResults(0xffffffff);
924 CSearchResultList::const_iterator it
= list
.begin();
925 while (it
!= list
.end()) {
926 CSearchFile
* sf
= *it
++;
927 CValueMap
&valuemap
= tagmap
.GetValueMap(sf
->ECID());
928 response
->AddTag(CEC_SearchFile_Tag(sf
, EC_DETAIL_INC_UPDATE
, &valuemap
));
929 /* Here we could add the children, if amulegui were able to merge them.
930 if (sf->HasChildren()) {
931 const CSearchResultList& children = sf->GetChildren();
932 for (size_t i = 0; i < children.size(); ++i) {
933 CSearchFile* sfc = children.at(i);
934 CValueMap &valuemap1 = tagmap.GetValueMap(sfc->ECID());
935 response->AddTag(CEC_SearchFile_Tag(sfc, EC_DETAIL_INC_UPDATE, &valuemap1));
943 static CECPacket
*Get_EC_Response_Search_Results_Download(const CECPacket
*request
)
945 CECPacket
*response
= new CECPacket(EC_OP_STRINGS
);
946 for (CECPacket::const_iterator it
= request
->begin(); it
!= request
->end(); it
++) {
947 const CECTag
&tag
= *it
;
948 CMD4Hash hash
= tag
.GetMD4Data();
949 uint8 category
= tag
.GetFirstTagSafe()->GetInt();
950 theApp
->searchlist
->AddFileToDownloadByHash(hash
, category
);
955 static CECPacket
*Get_EC_Response_Search_Stop(const CECPacket
*WXUNUSED(request
))
957 CECPacket
*reply
= new CECPacket(EC_OP_MISC_DATA
);
958 theApp
->searchlist
->StopGlobalSearch();
962 static CECPacket
*Get_EC_Response_Search(const CECPacket
*request
)
966 CEC_Search_Tag
*search_request
= (CEC_Search_Tag
*)request
->GetFirstTagSafe();
967 theApp
->searchlist
->RemoveResults(0xffffffff);
969 CSearchList::CSearchParams params
;
970 params
.searchString
= search_request
->SearchText();
971 params
.typeText
= search_request
->SearchFileType();
972 params
.extension
= search_request
->SearchExt();
973 params
.minSize
= search_request
->MinSize();
974 params
.maxSize
= search_request
->MaxSize();
975 params
.availability
= search_request
->Avail();
978 EC_SEARCH_TYPE search_type
= search_request
->SearchType();
979 SearchType core_search_type
= LocalSearch
;
980 uint32 op
= EC_OP_FAILED
;
981 switch (search_type
) {
982 case EC_SEARCH_GLOBAL
:
983 core_search_type
= GlobalSearch
;
985 if (core_search_type
!= GlobalSearch
) { // Not a global search obviously
986 core_search_type
= KadSearch
;
988 case EC_SEARCH_LOCAL
: {
989 uint32 search_id
= 0xffffffff;
990 wxString error
= theApp
->searchlist
->StartNewSearch(&search_id
, core_search_type
, params
);
991 if (!error
.IsEmpty()) {
994 response
= wxTRANSLATE("Search in progress. Refetch results in a moment!");
1000 response
= wxTRANSLATE("WebSearch from remote interface makes no sense.");
1004 CECPacket
*reply
= new CECPacket(op
);
1005 // error or search in progress
1006 reply
->AddTag(CECTag(EC_TAG_STRING
, response
));
1011 static CECPacket
*Get_EC_Response_Set_SharedFile_Prio(const CECPacket
*request
)
1013 CECPacket
*response
= new CECPacket(EC_OP_NOOP
);
1014 for (CECPacket::const_iterator it
= request
->begin(); it
!= request
->end(); it
++) {
1015 const CECTag
&tag
= *it
;
1016 CMD4Hash hash
= tag
.GetMD4Data();
1017 uint8 prio
= tag
.GetFirstTagSafe()->GetInt();
1018 CKnownFile
* cur_file
= theApp
->sharedfiles
->GetFileByID(hash
);
1022 if (prio
== PR_AUTO
) {
1023 cur_file
->SetAutoUpPriority(1);
1024 cur_file
->UpdateAutoUpPriority();
1026 cur_file
->SetAutoUpPriority(0);
1027 cur_file
->SetUpPriority(prio
);
1029 Notify_SharedFilesUpdateItem(cur_file
);
1035 void CPartFile_Encoder::Encode(CECTag
*parent
)
1038 // Source part frequencies
1040 CKnownFile_Encoder::Encode(parent
);
1045 const CGapList
& gaplist
= m_PartFile()->GetGapList();
1046 const size_t gap_list_size
= gaplist
.size();
1047 ArrayOfUInts64 gaps
;
1048 gaps
.reserve(gap_list_size
* 2);
1050 for (CGapList::const_iterator curr_pos
= gaplist
.begin();
1051 curr_pos
!= gaplist
.end(); ++curr_pos
) {
1052 gaps
.push_back(curr_pos
.start());
1053 gaps
.push_back(curr_pos
.end());
1056 int gap_enc_size
= 0;
1058 const uint8
*gap_enc_data
= m_gap_status
.Encode(gaps
, gap_enc_size
, changed
);
1060 parent
->AddTag(CECTag(EC_TAG_PARTFILE_GAP_STATUS
, gap_enc_size
, (void *)gap_enc_data
));
1062 delete[] gap_enc_data
;
1067 ArrayOfUInts64 req_buffer
;
1068 const CPartFile::CReqBlockPtrList
& requestedblocks
= m_PartFile()->GetRequestedBlockList();
1069 CPartFile::CReqBlockPtrList::const_iterator curr_pos2
= requestedblocks
.begin();
1071 for ( ; curr_pos2
!= requestedblocks
.end(); ++curr_pos2
) {
1072 Requested_Block_Struct
* block
= *curr_pos2
;
1073 req_buffer
.push_back(block
->StartOffset
);
1074 req_buffer
.push_back(block
->EndOffset
);
1076 int req_enc_size
= 0;
1077 const uint8
*req_enc_data
= m_req_status
.Encode(req_buffer
, req_enc_size
, changed
);
1079 parent
->AddTag(CECTag(EC_TAG_PARTFILE_REQ_STATUS
, req_enc_size
, (void *)req_enc_data
));
1081 delete[] req_enc_data
;
1086 // First count occurrence of all source names
1088 CECEmptyTag
sourceNames(EC_TAG_PARTFILE_SOURCE_NAMES
);
1089 typedef std::map
<wxString
, int> strIntMap
;
1091 const CPartFile::SourceSet
&sources
= m_PartFile()->GetSourceList();
1092 for (CPartFile::SourceSet::const_iterator it
= sources
.begin(); it
!= sources
.end(); ++it
) {
1093 CUpDownClient
*cur_src
= *it
;
1094 if (cur_src
->GetRequestFile() != m_file
|| cur_src
->GetClientFilename().Length() == 0) {
1097 const wxString
&name
= cur_src
->GetClientFilename();
1098 strIntMap::iterator itm
= nameMap
.find(name
);
1099 if (itm
== nameMap
.end()) {
1106 // Go through our last list
1108 for (SourcenameItemMap::iterator it1
= m_sourcenameItemMap
.begin(); it1
!= m_sourcenameItemMap
.end();) {
1109 SourcenameItemMap::iterator it2
= it1
++;
1110 strIntMap::iterator itm
= nameMap
.find(it2
->second
.name
);
1111 if (itm
== nameMap
.end()) {
1112 // name doesn't exist anymore, tell client to forget it
1113 CECTag
tag(EC_TAG_PARTFILE_SOURCE_NAMES
, it2
->first
);
1114 tag
.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS
, 0));
1115 sourceNames
.AddTag(tag
);
1117 m_sourcenameItemMap
.erase(it2
);
1119 // update count if it changed
1120 if (it2
->second
.count
!= itm
->second
) {
1121 CECTag
tag(EC_TAG_PARTFILE_SOURCE_NAMES
, it2
->first
);
1122 tag
.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS
, itm
->second
));
1123 sourceNames
.AddTag(tag
);
1124 it2
->second
.count
= itm
->second
;
1126 // remove it from nameMap so that only new names are left there
1133 for (strIntMap::iterator it3
= nameMap
.begin(); it3
!= nameMap
.end(); it3
++) {
1134 int id
= ++m_sourcenameID
;
1135 CECIntTag
tag(EC_TAG_PARTFILE_SOURCE_NAMES
, id
);
1136 tag
.AddTag(CECTag(EC_TAG_PARTFILE_SOURCE_NAMES
, it3
->first
));
1137 tag
.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS
, it3
->second
));
1138 sourceNames
.AddTag(tag
);
1140 m_sourcenameItemMap
[id
] = SourcenameItem(it3
->first
, it3
->second
);
1142 if (sourceNames
.HasChildTags()) {
1143 parent
->AddTag(sourceNames
);
1148 void CPartFile_Encoder::ResetEncoder()
1150 CKnownFile_Encoder::ResetEncoder();
1151 m_gap_status
.ResetEncoder();
1152 m_req_status
.ResetEncoder();
1155 void CKnownFile_Encoder::Encode(CECTag
*parent
)
1158 // Source part frequencies
1160 // Reference to the availability list
1161 const ArrayOfUInts16
& list
= m_file
->IsPartFile() ?
1162 ((CPartFile
*)m_file
)->m_SrcpartFrequency
:
1163 m_file
->m_AvailPartFrequency
;
1164 // Don't add tag if available parts aren't populated yet.
1165 if (!list
.empty()) {
1168 const uint8
*part_enc_data
= m_enc_data
.Encode(list
, part_enc_size
, changed
);
1170 parent
->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS
, part_enc_size
, part_enc_data
));
1172 delete[] part_enc_data
;
1176 static CECPacket
*GetStatsGraphs(const CECPacket
*request
)
1178 CECPacket
*response
= NULL
;
1180 switch (request
->GetDetailLevel()) {
1182 case EC_DETAIL_FULL
: {
1183 double dTimestamp
= 0.0;
1184 if (request
->GetTagByName(EC_TAG_STATSGRAPH_LAST
) != NULL
) {
1185 dTimestamp
= request
->GetTagByName(EC_TAG_STATSGRAPH_LAST
)->GetDoubleData();
1187 uint16 nScale
= request
->GetTagByNameSafe(EC_TAG_STATSGRAPH_SCALE
)->GetInt();
1188 uint16 nMaxPoints
= request
->GetTagByNameSafe(EC_TAG_STATSGRAPH_WIDTH
)->GetInt();
1190 unsigned int numPoints
= theApp
->m_statistics
->GetHistoryForWeb(nMaxPoints
, (double)nScale
, &dTimestamp
, &graphData
);
1192 response
= new CECPacket(EC_OP_STATSGRAPHS
);
1193 response
->AddTag(CECTag(EC_TAG_STATSGRAPH_DATA
, 4 * numPoints
* sizeof(uint32
), graphData
));
1194 delete [] graphData
;
1195 response
->AddTag(CECTag(EC_TAG_STATSGRAPH_LAST
, dTimestamp
));
1197 response
= new CECPacket(EC_OP_FAILED
);
1198 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("No points for graph.")));
1202 case EC_DETAIL_INC_UPDATE
:
1203 case EC_DETAIL_UPDATE
:
1206 response
= new CECPacket(EC_OP_FAILED
);
1207 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Your client is not configured for this detail level.")));
1211 response
= new CECPacket(EC_OP_FAILED
);
1218 CECPacket
*CECServerSocket::ProcessRequest2(const CECPacket
*request
)
1225 CECPacket
*response
= NULL
;
1227 switch (request
->GetOpCode()) {
1231 case EC_OP_SHUTDOWN
:
1232 if (!theApp
->IsOnShutDown()) {
1233 response
= new CECPacket(EC_OP_NOOP
);
1234 AddLogLineM(true, _("External Connection: shutdown requested"));
1235 #ifndef AMULE_DAEMON
1238 evt
.SetCanVeto(false);
1239 theApp
->ShutDown(evt
);
1242 theApp
->ExitMainLoop();
1245 response
= new CECPacket(EC_OP_FAILED
);
1246 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Already shutting down.")));
1249 case EC_OP_ADD_LINK
:
1250 for (CECPacket::const_iterator it
= request
->begin(); it
!= request
->end(); it
++) {
1251 const CECTag
&tag
= *it
;
1252 wxString link
= tag
.GetStringData();
1254 const CECTag
*cattag
= tag
.GetTagByName(EC_TAG_PARTFILE_CAT
);
1256 category
= cattag
->GetInt();
1258 AddLogLineM(true, CFormat(_("ExternalConn: adding link '%s'.")) % link
);
1259 if ( theApp
->downloadqueue
->AddLink(link
, category
) ) {
1260 response
= new CECPacket(EC_OP_NOOP
);
1262 // Error messages are printed by the add function.
1263 response
= new CECPacket(EC_OP_FAILED
);
1264 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid link or already on list.")));
1271 case EC_OP_STAT_REQ
:
1272 response
= Get_EC_Response_StatRequest(request
, m_LoggerAccess
);
1273 response
->AddTag(CEC_ConnState_Tag(request
->GetDetailLevel()));
1275 case EC_OP_GET_CONNSTATE
:
1276 response
= new CECPacket(EC_OP_MISC_DATA
);
1277 response
->AddTag(CEC_ConnState_Tag(request
->GetDetailLevel()));
1282 case EC_OP_GET_SHARED_FILES
:
1283 if ( request
->GetDetailLevel() != EC_DETAIL_INC_UPDATE
) {
1284 response
= Get_EC_Response_GetSharedFiles(request
, m_FileEncoder
);
1287 case EC_OP_GET_DLOAD_QUEUE
:
1288 if ( request
->GetDetailLevel() != EC_DETAIL_INC_UPDATE
) {
1289 response
= Get_EC_Response_GetDownloadQueue(request
, m_FileEncoder
);
1293 // This will evolve into an update-all for inc tags
1295 case EC_OP_GET_UPDATE
:
1296 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1297 response
= Get_EC_Response_GetUpdate(m_FileEncoder
, m_obj_tagmap
);
1300 case EC_OP_GET_ULOAD_QUEUE
:
1301 response
= Get_EC_Response_GetClientQueue(request
, m_obj_tagmap
, EC_OP_ULOAD_QUEUE
);
1303 case EC_OP_PARTFILE_REMOVE_NO_NEEDED
:
1304 case EC_OP_PARTFILE_REMOVE_FULL_QUEUE
:
1305 case EC_OP_PARTFILE_REMOVE_HIGH_QUEUE
:
1306 case EC_OP_PARTFILE_CLEANUP_SOURCES
:
1307 case EC_OP_PARTFILE_SWAP_A4AF_THIS
:
1308 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO
:
1309 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS
:
1310 case EC_OP_PARTFILE_PAUSE
:
1311 case EC_OP_PARTFILE_RESUME
:
1312 case EC_OP_PARTFILE_STOP
:
1313 case EC_OP_PARTFILE_PRIO_SET
:
1314 case EC_OP_PARTFILE_DELETE
:
1315 case EC_OP_PARTFILE_SET_CAT
:
1316 response
= Get_EC_Response_PartFile_Cmd(request
);
1318 case EC_OP_SHAREDFILES_RELOAD
:
1319 theApp
->sharedfiles
->Reload();
1320 response
= new CECPacket(EC_OP_NOOP
);
1322 case EC_OP_SHARED_SET_PRIO
:
1323 response
= Get_EC_Response_Set_SharedFile_Prio(request
);
1325 case EC_OP_RENAME_FILE
: {
1326 CMD4Hash fileHash
= request
->GetTagByNameSafe(EC_TAG_KNOWNFILE
)->GetMD4Data();
1327 wxString newName
= request
->GetTagByNameSafe(EC_TAG_PARTFILE_NAME
)->GetStringData();
1328 // search first in downloadqueue - it might be in known files as well
1329 CKnownFile
* file
= theApp
->downloadqueue
->GetFileByID(fileHash
);
1331 file
= theApp
->knownfiles
->FindKnownFileByID(fileHash
);
1334 response
= new CECPacket(EC_OP_FAILED
);
1335 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("File not found.")));
1338 if (newName
.IsEmpty()) {
1339 response
= new CECPacket(EC_OP_FAILED
);
1340 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid file name.")));
1344 if (theApp
->sharedfiles
->RenameFile(file
, CPath(newName
))) {
1345 response
= new CECPacket(EC_OP_NOOP
);
1347 response
= new CECPacket(EC_OP_FAILED
);
1348 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Unable to rename file.")));
1358 case EC_OP_SERVER_ADD
:
1359 response
= Get_EC_Response_Server_Add(request
);
1361 case EC_OP_SERVER_DISCONNECT
:
1362 case EC_OP_SERVER_CONNECT
:
1363 case EC_OP_SERVER_REMOVE
:
1364 response
= Get_EC_Response_Server(request
);
1366 case EC_OP_GET_SERVER_LIST
: {
1367 response
= new CECPacket(EC_OP_SERVER_LIST
);
1368 if (!thePrefs::GetNetworkED2K()) {
1369 // Kad only: just send an empty list
1372 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
1373 std::vector
<const CServer
*> servers
= theApp
->serverlist
->CopySnapshot();
1375 std::vector
<const CServer
*>::const_iterator it
= servers
.begin();
1376 it
!= servers
.end();
1379 response
->AddTag(CEC_Server_Tag(*it
, detail_level
));
1383 case EC_OP_SERVER_UPDATE_FROM_URL
: {
1384 wxString url
= request
->GetFirstTagSafe()->GetStringData();
1386 // Save the new url, and update the UI (if not amuled).
1387 Notify_ServersURLChanged(url
);
1388 thePrefs::SetEd2kServersUrl(url
);
1390 theApp
->serverlist
->UpdateServerMetFromURL(url
);
1391 response
= new CECPacket(EC_OP_NOOP
);
1397 case EC_OP_IPFILTER_RELOAD
:
1398 theApp
->ipfilter
->Reload();
1399 response
= new CECPacket(EC_OP_NOOP
);
1402 case EC_OP_IPFILTER_UPDATE
: {
1403 wxString url
= request
->GetFirstTagSafe()->GetStringData();
1404 if (url
.IsEmpty()) {
1405 url
= thePrefs::IPFilterURL();
1407 theApp
->ipfilter
->Update(url
);
1408 response
= new CECPacket(EC_OP_NOOP
);
1414 case EC_OP_SEARCH_START
:
1415 response
= Get_EC_Response_Search(request
);
1418 case EC_OP_SEARCH_STOP
:
1419 response
= Get_EC_Response_Search_Stop(request
);
1422 case EC_OP_SEARCH_RESULTS
:
1423 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1424 response
= Get_EC_Response_Search_Results(m_obj_tagmap
);
1426 response
= Get_EC_Response_Search_Results(request
);
1430 case EC_OP_SEARCH_PROGRESS
:
1431 response
= new CECPacket(EC_OP_SEARCH_PROGRESS
);
1432 response
->AddTag(CECTag(EC_TAG_SEARCH_STATUS
,
1433 theApp
->searchlist
->GetSearchProgress()));
1436 case EC_OP_DOWNLOAD_SEARCH_RESULT
:
1437 response
= Get_EC_Response_Search_Results_Download(request
);
1442 case EC_OP_GET_PREFERENCES
:
1443 response
= new CEC_Prefs_Packet(request
->GetTagByNameSafe(EC_TAG_SELECT_PREFS
)->GetInt(), request
->GetDetailLevel());
1445 case EC_OP_SET_PREFERENCES
:
1446 ((CEC_Prefs_Packet
*)request
)->Apply();
1447 theApp
->glob_prefs
->Save();
1448 if (thePrefs::IsFilteringClients()) {
1449 theApp
->clientlist
->FilterQueues();
1451 if (thePrefs::IsFilteringServers()) {
1452 theApp
->serverlist
->FilterServers();
1454 if (!thePrefs::GetNetworkED2K() && theApp
->IsConnectedED2K()) {
1455 theApp
->DisconnectED2K();
1457 if (!thePrefs::GetNetworkKademlia() && theApp
->IsConnectedKad()) {
1460 response
= new CECPacket(EC_OP_NOOP
);
1463 case EC_OP_CREATE_CATEGORY
:
1464 if ( request
->GetTagCount() == 1 ) {
1465 CEC_Category_Tag
*tag
= (CEC_Category_Tag
*)request
->GetFirstTagSafe();
1466 if (tag
->Create()) {
1467 response
= new CECPacket(EC_OP_NOOP
);
1469 response
= new CECPacket(EC_OP_FAILED
);
1470 response
->AddTag(CECTag(EC_TAG_CATEGORY
, theApp
->glob_prefs
->GetCatCount() - 1));
1471 response
->AddTag(CECTag(EC_TAG_CATEGORY_PATH
, tag
->Path()));
1473 Notify_CategoryAdded();
1475 response
= new CECPacket(EC_OP_NOOP
);
1478 case EC_OP_UPDATE_CATEGORY
:
1479 if ( request
->GetTagCount() == 1 ) {
1480 CEC_Category_Tag
*tag
= (CEC_Category_Tag
*)request
->GetFirstTagSafe();
1482 response
= new CECPacket(EC_OP_NOOP
);
1484 response
= new CECPacket(EC_OP_FAILED
);
1485 response
->AddTag(CECTag(EC_TAG_CATEGORY
, tag
->GetInt()));
1486 response
->AddTag(CECTag(EC_TAG_CATEGORY_PATH
, tag
->Path()));
1488 Notify_CategoryUpdate(tag
->GetInt());
1490 response
= new CECPacket(EC_OP_NOOP
);
1493 case EC_OP_DELETE_CATEGORY
:
1494 if ( request
->GetTagCount() == 1 ) {
1495 uint32 cat
= request
->GetFirstTagSafe()->GetInt();
1496 // this noes not only update the gui, but actually deletes the cat
1497 Notify_CategoryDelete(cat
);
1499 response
= new CECPacket(EC_OP_NOOP
);
1505 case EC_OP_ADDLOGLINE
:
1506 AddLogLineM( (request
->GetTagByName(EC_TAG_LOG_TO_STATUS
) != NULL
), request
->GetTagByNameSafe(EC_TAG_STRING
)->GetStringData() );
1507 response
= new CECPacket(EC_OP_NOOP
);
1509 case EC_OP_ADDDEBUGLOGLINE
:
1510 if (request
->GetTagByName(EC_TAG_LOG_TO_STATUS
) != NULL
) {
1511 AddDebugLogLineC(logGeneral
, request
->GetTagByNameSafe(EC_TAG_STRING
)->GetStringData());
1513 AddDebugLogLineN(logGeneral
, request
->GetTagByNameSafe(EC_TAG_STRING
)->GetStringData());
1515 response
= new CECPacket(EC_OP_NOOP
);
1518 response
= new CECPacket(EC_OP_LOG
);
1519 response
->AddTag(CECTag(EC_TAG_STRING
, theApp
->GetLog(false)));
1521 case EC_OP_GET_DEBUGLOG
:
1522 response
= new CECPacket(EC_OP_DEBUGLOG
);
1523 response
->AddTag(CECTag(EC_TAG_STRING
, theApp
->GetDebugLog(false)));
1525 case EC_OP_RESET_LOG
:
1526 theApp
->GetLog(true);
1527 response
= new CECPacket(EC_OP_NOOP
);
1529 case EC_OP_RESET_DEBUGLOG
:
1530 theApp
->GetDebugLog(true);
1531 response
= new CECPacket(EC_OP_NOOP
);
1533 case EC_OP_GET_LAST_LOG_ENTRY
:
1535 wxString tmp
= theApp
->GetLog(false);
1536 if (tmp
.Last() == '\n') {
1539 response
= new CECPacket(EC_OP_LOG
);
1540 response
->AddTag(CECTag(EC_TAG_STRING
, tmp
.AfterLast('\n')));
1543 case EC_OP_GET_SERVERINFO
:
1544 response
= new CECPacket(EC_OP_SERVERINFO
);
1545 response
->AddTag(CECTag(EC_TAG_STRING
, theApp
->GetServerLog(false)));
1547 case EC_OP_CLEAR_SERVERINFO
:
1548 theApp
->GetServerLog(true);
1549 response
= new CECPacket(EC_OP_NOOP
);
1554 case EC_OP_GET_STATSGRAPHS
:
1555 response
= GetStatsGraphs(request
);
1557 case EC_OP_GET_STATSTREE
: {
1558 theApp
->m_statistics
->UpdateStatsTree();
1559 response
= new CECPacket(EC_OP_STATSTREE
);
1560 CECTag
* tree
= theStats::GetECStatTree(request
->GetTagByNameSafe(EC_TAG_STATTREE_CAPPING
)->GetInt());
1562 response
->AddTag(*tree
);
1565 if (request
->GetDetailLevel() == EC_DETAIL_WEB
) {
1566 response
->AddTag(CECTag(EC_TAG_SERVER_VERSION
, wxT(VERSION
)));
1567 response
->AddTag(CECTag(EC_TAG_USER_NICK
, thePrefs::GetUserNick()));
1575 case EC_OP_KAD_START
:
1576 if (thePrefs::GetNetworkKademlia()) {
1577 response
= new CECPacket(EC_OP_NOOP
);
1578 if ( !Kademlia::CKademlia::IsRunning() ) {
1579 Kademlia::CKademlia::Start();
1580 theApp
->ShowConnectionState();
1583 response
= new CECPacket(EC_OP_FAILED
);
1584 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Kad is disabled in preferences.")));
1587 case EC_OP_KAD_STOP
:
1589 theApp
->ShowConnectionState();
1590 response
= new CECPacket(EC_OP_NOOP
);
1592 case EC_OP_KAD_UPDATE_FROM_URL
: {
1593 wxString url
= request
->GetFirstTagSafe()->GetStringData();
1595 // Save the new url, and update the UI (if not amuled).
1596 Notify_NodesURLChanged(url
);
1597 thePrefs::SetKadNodesUrl(url
);
1599 theApp
->UpdateNotesDat(url
);
1600 response
= new CECPacket(EC_OP_NOOP
);
1603 case EC_OP_KAD_BOOTSTRAP_FROM_IP
:
1604 if (thePrefs::GetNetworkKademlia()) {
1605 theApp
->BootstrapKad(request
->GetTagByNameSafe(EC_TAG_BOOTSTRAP_IP
)->GetInt(),
1606 request
->GetTagByNameSafe(EC_TAG_BOOTSTRAP_PORT
)->GetInt());
1607 theApp
->ShowConnectionState();
1608 response
= new CECPacket(EC_OP_NOOP
);
1610 response
= new CECPacket(EC_OP_FAILED
);
1611 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Kad is disabled in preferences.")));
1617 // These requests are currently used only in the text client
1620 if (thePrefs::GetNetworkED2K()) {
1621 response
= new CECPacket(EC_OP_STRINGS
);
1622 if (theApp
->IsConnectedED2K()) {
1623 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Already connected to eD2k.")));
1625 theApp
->serverconnect
->ConnectToAnyServer();
1626 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Connecting to eD2k...")));
1629 if (thePrefs::GetNetworkKademlia()) {
1631 response
= new CECPacket(EC_OP_STRINGS
);
1633 if (theApp
->IsConnectedKad()) {
1634 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Already connected to Kad.")));
1637 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Connecting to Kad...")));
1641 theApp
->ShowConnectionState();
1643 response
= new CECPacket(EC_OP_FAILED
);
1644 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("All networks are disabled.")));
1647 case EC_OP_DISCONNECT
:
1648 if (theApp
->IsConnected()) {
1649 response
= new CECPacket(EC_OP_STRINGS
);
1650 if (theApp
->IsConnectedED2K()) {
1651 theApp
->serverconnect
->Disconnect();
1652 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Disconnected from eD2k.")));
1654 if (theApp
->IsConnectedKad()) {
1656 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Disconnected from Kad.")));
1658 theApp
->ShowConnectionState();
1660 response
= new CECPacket(EC_OP_NOOP
);
1665 AddLogLineN(CFormat(_("External Connection: invalid opcode received: %#x")) % request
->GetOpCode());
1667 response
= new CECPacket(EC_OP_FAILED
);
1668 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid opcode (wrong protocol version?)")));
1674 * Here notification-based EC. Notification will be sorted by priority for possible throttling.
1678 * Core general status
1680 ECStatusMsgSource::ECStatusMsgSource()
1682 m_last_ed2k_status_sent
= 0xffffffff;
1683 m_last_kad_status_sent
= 0xffffffff;
1684 m_server
= (void *)0xffffffff;
1687 uint32
ECStatusMsgSource::GetEd2kStatus()
1689 if ( theApp
->IsConnectedED2K() ) {
1690 return theApp
->GetED2KID();
1691 } else if ( theApp
->serverconnect
->IsConnecting() ) {
1698 uint32
ECStatusMsgSource::GetKadStatus()
1700 if ( theApp
->IsConnectedKad() ) {
1702 } else if ( Kademlia::CKademlia::IsFirewalled() ) {
1704 } else if ( Kademlia::CKademlia::IsRunning() ) {
1710 CECPacket
*ECStatusMsgSource::GetNextPacket()
1712 if ( (m_last_ed2k_status_sent
!= GetEd2kStatus()) ||
1713 (m_last_kad_status_sent
!= GetKadStatus()) ||
1714 (m_server
!= theApp
->serverconnect
->GetCurrentServer()) ) {
1716 m_last_ed2k_status_sent
= GetEd2kStatus();
1717 m_last_kad_status_sent
= GetKadStatus();
1718 m_server
= theApp
->serverconnect
->GetCurrentServer();
1720 CECPacket
*response
= new CECPacket(EC_OP_STATS
);
1721 response
->AddTag(CEC_ConnState_Tag(EC_DETAIL_UPDATE
));
1730 ECPartFileMsgSource::ECPartFileMsgSource()
1732 for (unsigned int i
= 0; i
< theApp
->downloadqueue
->GetFileCount(); i
++) {
1733 CPartFile
*cur_file
= theApp
->downloadqueue
->GetFileByIndex(i
);
1734 PARTFILE_STATUS status
= { true, false, false, false, true, cur_file
};
1735 m_dirty_status
[cur_file
->GetFileHash()] = status
;
1739 void ECPartFileMsgSource::SetDirty(CPartFile
*file
)
1741 CMD4Hash filehash
= file
->GetFileHash();
1742 if ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() ) {
1743 m_dirty_status
[filehash
].m_dirty
= true;;
1747 void ECPartFileMsgSource::SetNew(CPartFile
*file
)
1749 CMD4Hash filehash
= file
->GetFileHash();
1750 wxASSERT ( m_dirty_status
.find(filehash
) == m_dirty_status
.end() );
1751 PARTFILE_STATUS status
= { true, false, false, false, true, file
};
1752 m_dirty_status
[filehash
] = status
;
1755 void ECPartFileMsgSource::SetCompleted(CPartFile
*file
)
1757 CMD4Hash filehash
= file
->GetFileHash();
1758 wxASSERT ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() );
1760 m_dirty_status
[filehash
].m_finished
= true;
1763 void ECPartFileMsgSource::SetRemoved(CPartFile
*file
)
1765 CMD4Hash filehash
= file
->GetFileHash();
1766 wxASSERT ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() );
1768 m_dirty_status
[filehash
].m_removed
= true;
1771 CECPacket
*ECPartFileMsgSource::GetNextPacket()
1773 for(std::map
<CMD4Hash
, PARTFILE_STATUS
>::iterator it
= m_dirty_status
.begin();
1774 it
!= m_dirty_status
.end(); it
++) {
1775 if ( it
->second
.m_new
|| it
->second
.m_dirty
|| it
->second
.m_removed
) {
1776 CMD4Hash filehash
= it
->first
;
1778 CPartFile
*partfile
= it
->second
.m_file
;
1780 CECPacket
*packet
= new CECPacket(EC_OP_DLOAD_QUEUE
);
1781 if ( it
->second
.m_removed
) {
1782 CECTag
tag(EC_TAG_PARTFILE
, filehash
);
1783 packet
->AddTag(tag
);
1784 m_dirty_status
.erase(it
);
1786 CEC_PartFile_Tag
tag(partfile
, it
->second
.m_new
? EC_DETAIL_FULL
: EC_DETAIL_UPDATE
);
1787 packet
->AddTag(tag
);
1789 m_dirty_status
[filehash
].m_new
= false;
1790 m_dirty_status
[filehash
].m_dirty
= false;
1799 * Shared files - similar to downloading
1801 ECKnownFileMsgSource::ECKnownFileMsgSource()
1803 for (unsigned int i
= 0; i
< theApp
->sharedfiles
->GetFileCount(); i
++) {
1804 CKnownFile
*cur_file
= (CKnownFile
*)theApp
->sharedfiles
->GetFileByIndex(i
);
1805 KNOWNFILE_STATUS status
= { true, false, false, true, cur_file
};
1806 m_dirty_status
[cur_file
->GetFileHash()] = status
;
1810 void ECKnownFileMsgSource::SetDirty(CKnownFile
*file
)
1812 CMD4Hash filehash
= file
->GetFileHash();
1813 if ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() ) {
1814 m_dirty_status
[filehash
].m_dirty
= true;;
1818 void ECKnownFileMsgSource::SetNew(CKnownFile
*file
)
1820 CMD4Hash filehash
= file
->GetFileHash();
1821 wxASSERT ( m_dirty_status
.find(filehash
) == m_dirty_status
.end() );
1822 KNOWNFILE_STATUS status
= { true, false, false, true, file
};
1823 m_dirty_status
[filehash
] = status
;
1826 void ECKnownFileMsgSource::SetRemoved(CKnownFile
*file
)
1828 CMD4Hash filehash
= file
->GetFileHash();
1829 wxASSERT ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() );
1831 m_dirty_status
[filehash
].m_removed
= true;
1834 CECPacket
*ECKnownFileMsgSource::GetNextPacket()
1836 for(std::map
<CMD4Hash
, KNOWNFILE_STATUS
>::iterator it
= m_dirty_status
.begin();
1837 it
!= m_dirty_status
.end(); it
++) {
1838 if ( it
->second
.m_new
|| it
->second
.m_dirty
|| it
->second
.m_removed
) {
1839 CMD4Hash filehash
= it
->first
;
1841 CKnownFile
*partfile
= it
->second
.m_file
;
1843 CECPacket
*packet
= new CECPacket(EC_OP_SHARED_FILES
);
1844 if ( it
->second
.m_removed
) {
1845 CECTag
tag(EC_TAG_PARTFILE
, filehash
);
1846 packet
->AddTag(tag
);
1847 m_dirty_status
.erase(it
);
1849 CEC_SharedFile_Tag
tag(partfile
, it
->second
.m_new
? EC_DETAIL_FULL
: EC_DETAIL_UPDATE
);
1850 packet
->AddTag(tag
);
1852 m_dirty_status
[filehash
].m_new
= false;
1853 m_dirty_status
[filehash
].m_dirty
= false;
1862 * Notification about search status
1864 ECSearchMsgSource::ECSearchMsgSource()
1868 CECPacket
*ECSearchMsgSource::GetNextPacket()
1870 if ( m_dirty_status
.empty() ) {
1874 CECPacket
*response
= new CECPacket(EC_OP_SEARCH_RESULTS
);
1875 for(std::map
<CMD4Hash
, SEARCHFILE_STATUS
>::iterator it
= m_dirty_status
.begin();
1876 it
!= m_dirty_status
.end(); it
++) {
1878 if ( it
->second
.m_new
) {
1879 response
->AddTag(CEC_SearchFile_Tag(it
->second
.m_file
, EC_DETAIL_FULL
));
1880 it
->second
.m_new
= false;
1881 } else if ( it
->second
.m_dirty
) {
1882 response
->AddTag(CEC_SearchFile_Tag(it
->second
.m_file
, EC_DETAIL_UPDATE
));
1890 void ECSearchMsgSource::FlushStatus()
1892 m_dirty_status
.clear();
1895 void ECSearchMsgSource::SetDirty(CSearchFile
*file
)
1897 if ( m_dirty_status
.count(file
->GetFileHash()) ) {
1898 m_dirty_status
[file
->GetFileHash()].m_dirty
= true;
1900 m_dirty_status
[file
->GetFileHash()].m_new
= true;
1901 m_dirty_status
[file
->GetFileHash()].m_dirty
= true;
1902 m_dirty_status
[file
->GetFileHash()].m_child_dirty
= true;
1903 m_dirty_status
[file
->GetFileHash()].m_file
= file
;
1907 void ECSearchMsgSource::SetChildDirty(CSearchFile
*file
)
1909 m_dirty_status
[file
->GetFileHash()].m_child_dirty
= true;
1913 * Notification about uploading clients
1915 CECPacket
*ECClientMsgSource::GetNextPacket()
1921 // Notification iface per-client
1923 ECNotifier::ECNotifier()
1927 ECNotifier::~ECNotifier()
1929 while (m_msg_source
.begin() != m_msg_source
.end())
1930 Remove_EC_Client(m_msg_source
.begin()->first
);
1933 CECPacket
*ECNotifier::GetNextPacket(ECUpdateMsgSource
*msg_source_array
[])
1935 CECPacket
*packet
= 0;
1937 // priority 0 is highest
1939 for(int i
= 0; i
< EC_STATUS_LAST_PRIO
; i
++) {
1940 if ( (packet
= msg_source_array
[i
]->GetNextPacket()) != 0 ) {
1947 CECPacket
*ECNotifier::GetNextPacket(CECServerSocket
*sock
)
1950 // OnOutput is called for a first time before
1951 // socket is registered
1953 if ( m_msg_source
.count(sock
) ) {
1954 ECUpdateMsgSource
**notifier_array
= m_msg_source
[sock
];
1955 if ( !notifier_array
) {
1958 CECPacket
*packet
= GetNextPacket(notifier_array
);
1959 printf("[EC] next update packet; opcode=%x\n",packet
? packet
->GetOpCode() : 0xff);
1967 // Interface to notification macros
1969 void ECNotifier::DownloadFile_SetDirty(CPartFile
*file
)
1971 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
1972 i
!= m_msg_source
.end(); i
++) {
1973 CECServerSocket
*sock
= i
->first
;
1974 if ( sock
->HaveNotificationSupport() ) {
1975 ECUpdateMsgSource
**notifier_array
= i
->second
;
1976 ((ECPartFileMsgSource
*)notifier_array
[EC_PARTFILE
])->SetDirty(file
);
1979 NextPacketToSocket();
1982 void ECNotifier::DownloadFile_RemoveFile(CPartFile
*file
)
1984 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
1985 i
!= m_msg_source
.end(); i
++) {
1986 ECUpdateMsgSource
**notifier_array
= i
->second
;
1987 ((ECPartFileMsgSource
*)notifier_array
[EC_PARTFILE
])->SetRemoved(file
);
1989 NextPacketToSocket();
1992 void ECNotifier::DownloadFile_RemoveSource(CPartFile
*)
1994 // per-partfile source list is not supported (yet), and IMHO quite useless
1997 void ECNotifier::DownloadFile_AddFile(CPartFile
*file
)
1999 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
2000 i
!= m_msg_source
.end(); i
++) {
2001 ECUpdateMsgSource
**notifier_array
= i
->second
;
2002 ((ECPartFileMsgSource
*)notifier_array
[EC_PARTFILE
])->SetNew(file
);
2004 NextPacketToSocket();
2007 void ECNotifier::DownloadFile_AddSource(CPartFile
*)
2009 // per-partfile source list is not supported (yet), and IMHO quite useless
2012 void ECNotifier::SharedFile_AddFile(CKnownFile
*file
)
2014 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
2015 i
!= m_msg_source
.end(); i
++) {
2016 ECUpdateMsgSource
**notifier_array
= i
->second
;
2017 ((ECKnownFileMsgSource
*)notifier_array
[EC_KNOWN
])->SetNew(file
);
2019 NextPacketToSocket();
2022 void ECNotifier::SharedFile_RemoveFile(CKnownFile
*file
)
2024 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
2025 i
!= m_msg_source
.end(); i
++) {
2026 ECUpdateMsgSource
**notifier_array
= i
->second
;
2027 ((ECKnownFileMsgSource
*)notifier_array
[EC_KNOWN
])->SetRemoved(file
);
2029 NextPacketToSocket();
2032 void ECNotifier::SharedFile_RemoveAllFiles()
2034 // need to figure out what to do here
2037 void ECNotifier::Add_EC_Client(CECServerSocket
*sock
)
2039 ECUpdateMsgSource
**notifier_array
= new ECUpdateMsgSource
*[EC_STATUS_LAST_PRIO
];
2040 notifier_array
[EC_STATUS
] = new ECStatusMsgSource();
2041 notifier_array
[EC_SEARCH
] = new ECSearchMsgSource();
2042 notifier_array
[EC_PARTFILE
] = new ECPartFileMsgSource();
2043 notifier_array
[EC_CLIENT
] = new ECClientMsgSource();
2044 notifier_array
[EC_KNOWN
] = new ECKnownFileMsgSource();
2046 m_msg_source
[sock
] = notifier_array
;
2049 void ECNotifier::Remove_EC_Client(CECServerSocket
*sock
)
2051 if (m_msg_source
.count(sock
)) {
2052 ECUpdateMsgSource
**notifier_array
= m_msg_source
[sock
];
2054 m_msg_source
.erase(sock
);
2056 for(int i
= 0; i
< EC_STATUS_LAST_PRIO
; i
++) {
2057 delete notifier_array
[i
];
2059 delete [] notifier_array
;
2063 void ECNotifier::NextPacketToSocket()
2065 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
2066 i
!= m_msg_source
.end(); i
++) {
2067 CECServerSocket
*sock
= i
->first
;
2068 if ( sock
->HaveNotificationSupport() && !sock
->DataPending() ) {
2069 ECUpdateMsgSource
**notifier_array
= i
->second
;
2070 CECPacket
*packet
= GetNextPacket(notifier_array
);
2072 printf("[EC] sending update packet; opcode=%x\n",packet
->GetOpCode());
2073 sock
->SendPacket(packet
);
2079 // File_checked_for_headers