Upstream tarball 10068
[amule.git] / src / ExternalConn.cpp
blob4bfd8cc0ff3d1a000c83d488da07923f4c1bb866
1 //
2 // This file is part of the aMule Project.
3 //
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)
7 //
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.
21 //
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 #ifdef HAVE_CONFIG_H
28 #include "config.h" // Needed for VERSION
29 #endif
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
50 #include "Logger.h"
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
67 RLE_Data m_enc_data;
68 protected:
69 const CKnownFile *m_file;
70 public:
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; }
88 /*!
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;
100 // gap list
101 RLE_Data m_gap_status;
102 // source names
103 SourcenameItemMap m_sourcenameItemMap;
104 // counter for unique source name ids
105 int m_sourcenameID;
106 // not all part files are shared (only when at least one part is complete)
107 bool m_shared;
109 // cast inherited member to CPartFile
110 CPartFile * m_PartFile() { wxASSERT(m_file->IsCPartFile()); return (CPartFile *)m_file; }
111 public:
112 // encoder side
113 CPartFile_Encoder(const CPartFile *file = 0) : CKnownFile_Encoder(file)
115 m_sourcenameID = 0;
116 m_shared = false;
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;
134 public:
135 ~CFileEncoderMap();
136 void UpdateEncoders();
139 CFileEncoderMap::~CFileEncoderMap()
141 // DeleteContents() causes infinite recursion here!
142 for (iterator it = begin(); it != end(); it++) {
143 delete it->second;
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;
152 // Downloads
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);
158 if (!count(id)) {
159 (*this)[id] = new CPartFile_Encoder(downloads[i]);
162 // Shares
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();
169 continue;
171 curr_files.insert(id);
172 if (!count(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);
185 // then delete them
186 for (IDSet::iterator it = dead_files.begin(); it != dead_files.end(); it++) {
187 iterator it2 = find(*it);
188 delete it2->second;
189 erase(it2);
194 //-------------------- CECServerSocket --------------------
196 class CECServerSocket : public CECMuleSocket
198 public:
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();
206 private:
207 ECNotifier *m_ec_notifier;
209 const CECPacket *Authenticate(const CECPacket *);
211 enum {
212 CONN_INIT,
213 CONN_SALT_SENT,
214 CONN_ESTABLISHED,
215 CONN_FAILED
216 } m_conn_state;
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);
226 CECServerSocket::CECServerSocket(ECNotifier *notifier)
228 CECMuleSocket(true),
229 m_conn_state(CONN_INIT),
230 m_passwd_salt(GetRandomUint64())
232 wxASSERT(theApp->ECServerHandler);
233 theApp->ECServerHandler->AddSocket(this);
234 m_ec_notifier = notifier;
238 CECServerSocket::~CECServerSocket()
240 wxASSERT(theApp->ECServerHandler);
241 theApp->ECServerHandler->RemoveSocket(this);
245 const CECPacket *CECServerSocket::OnPacketReceived(const CECPacket *packet, uint32 trueSize)
247 packet->DebugPrint(true, trueSize);
249 const CECPacket *reply = NULL;
251 if (m_conn_state == CONN_FAILED) {
252 // Client didn't close the socket when authentication failed.
253 AddLogLineM(false, _("Client sent packet after authentication failed."));
254 CloseSocket();
257 if (m_conn_state != CONN_ESTABLISHED) {
258 // This is called twice:
259 // 1) send salt
260 // 2) verify password
261 reply = Authenticate(packet);
262 } else {
263 reply = ProcessRequest2(packet);
265 return reply;
269 void CECServerSocket::OnLost()
271 AddLogLineM(false,_("External connection closed."));
272 theApp->ECServerHandler->m_ec_notifier->Remove_EC_Client(this);
273 DestroySocket();
276 void CECServerSocket::WriteDoneAndQueueEmpty()
278 if ( HaveNotificationSupport() && (m_conn_state == CONN_ESTABLISHED) ) {
279 CECPacket *packet = m_ec_notifier->GetNextPacket(this);
280 if ( packet ) {
281 SendPacket(packet);
283 } else {
284 //printf("[EC] %p: WriteDoneAndQueueEmpty but notification disabled\n", this);
288 //-------------------- ExternalConn --------------------
290 enum
291 { // id for sockets
292 SERVER_ID = 1000
296 BEGIN_EVENT_TABLE(ExternalConn, wxEvtHandler)
297 EVT_SOCKET(SERVER_ID, ExternalConn::OnServerEvent)
298 END_EVENT_TABLE()
301 ExternalConn::ExternalConn(amuleIPV4Address addr, wxString *msg)
303 wxString msgLocal;
304 m_ECServer = NULL;
305 // Are we allowed to accept External Connections?
306 if ( thePrefs::AcceptExternalConnections() ) {
307 // We must have a valid password, otherwise we will not allow EC connections
308 if (thePrefs::ECPassword().IsEmpty()) {
309 *msg += wxT("External connections disabled due to empty password!\n");
310 AddLogLineM(true, _("External connections disabled due to empty password!"));
311 return;
314 // Create the socket
315 m_ECServer = new wxSocketServer(addr, wxSOCKET_REUSEADDR);
316 m_ECServer->SetEventHandler(*this, SERVER_ID);
317 m_ECServer->SetNotify(wxSOCKET_CONNECTION_FLAG);
318 m_ECServer->Notify(true);
320 int port = addr.Service();
321 wxString ip = addr.IPAddress();
322 if (m_ECServer->Ok()) {
323 msgLocal = wxT("*** TCP socket (ECServer) listening on ") + ip +
324 wxString::Format(wxT(":%d"), port);
325 *msg += msgLocal + wxT("\n");
326 AddLogLineM(false, msgLocal);
327 } else {
328 msgLocal = wxT("Could not listen for external connections at ") + ip +
329 wxString::Format(wxT(":%d!"), port);
330 *msg += msgLocal + wxT("\n");
331 AddLogLineM(false, msgLocal);
333 } else {
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()
343 KillAllSockets();
344 delete m_ECServer;
345 delete m_ec_notifier;
349 void ExternalConn::AddSocket(CECServerSocket *s)
351 wxASSERT(s);
352 socket_list.insert(s);
356 void ExternalConn::RemoveSocket(CECServerSocket *s)
358 wxASSERT(s);
359 socket_list.erase(s);
363 void ExternalConn::KillAllSockets()
365 AddDebugLogLineM(false, logGeneral,
366 CFormat(wxT("ExternalConn::KillAllSockets(): %d sockets to destroy.")) %
367 socket_list.size());
368 SocketSet::iterator it = socket_list.begin();
369 while (it != socket_list.end()) {
370 CECServerSocket *s = *(it++);
371 s->Close();
372 s->Destroy();
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"));
386 } else {
387 delete sock;
388 AddLogLineM(false, _("ERROR: couldn't accept a new external connection"));
394 // Authentication
396 const CECPacket *CECServerSocket::Authenticate(const CECPacket *request)
398 CECPacket *response;
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);
419 #ifdef EC_VERSION_ID
420 // For SVN versions, both client and server must use SVNDATE, and they must be the same
421 CMD4Hash vhash;
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.")));
428 #else
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 SVN version! *sigh* possible crash prevented")));
433 #endif
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;
440 } else {
441 response = new CECPacket(EC_OP_AUTH_FAIL);
442 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid protocol version.") + wxString::Format(wxT("( %i != %i )"),proto_version,EC_CURRENT_PROTOCOL_VERSION)));
444 } else {
445 response = new CECPacket(EC_OP_AUTH_FAIL);
446 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Missing protocol version tag.")));
448 } else if ((m_conn_state == CONN_SALT_SENT) && (request->GetOpCode() == EC_OP_AUTH_PASSWD)) {
449 const CECTag *passwd = request->GetTagByName(EC_TAG_PASSWD_HASH);
450 CMD4Hash passh;
452 if (!passh.Decode(thePrefs::ECPassword())) {
453 wxString err = wxTRANSLATE("Authentication failed: invalid hash specified as EC password.");
454 AddLogLineM(false, wxString(wxGetTranslation(err)) + wxT(" ") + thePrefs::ECPassword());
455 response = new CECPacket(EC_OP_AUTH_FAIL);
456 response->AddTag(CECTag(EC_TAG_STRING, err));
457 } else {
458 wxString saltHash = MD5Sum(CFormat(wxT("%lX")) % m_passwd_salt).GetHash();
459 wxString saltStr = CFormat(wxT("%lX")) % m_passwd_salt;
461 passh.Decode(MD5Sum(thePrefs::ECPassword().Lower() + saltHash).GetHash());
463 if (passwd && passwd->GetMD4Data() == passh) {
464 response = new CECPacket(EC_OP_AUTH_OK);
465 response->AddTag(CECTag(EC_TAG_SERVER_VERSION, wxT(VERSION)));
466 } else {
467 wxString err;
468 if (passwd) {
469 err = wxTRANSLATE("Authentication failed: wrong password.");
470 } else {
471 err = wxTRANSLATE("Authentication failed: missing password.");
474 response = new CECPacket(EC_OP_AUTH_FAIL);
475 response->AddTag(CECTag(EC_TAG_STRING, err));
476 AddLogLineM(false, wxGetTranslation(err));
479 } else {
480 response = new CECPacket(EC_OP_AUTH_FAIL);
481 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid request, please authenticate first.")));
484 if (response->GetOpCode() == EC_OP_AUTH_OK) {
485 m_conn_state = CONN_ESTABLISHED;
486 AddLogLineM(false, _("Access granted."));
487 // Establish notification handler if client supports it
488 if (HaveNotificationSupport()) {
489 theApp->ECServerHandler->m_ec_notifier->Add_EC_Client(this);
491 } else if (response->GetOpCode() == EC_OP_AUTH_FAIL) {
492 // Log message sent to client
493 if (response->GetTagByIndex(0)->IsString()) {
494 AddLogLineM(false, CFormat(_("Sent error message \"%s\" to client.")) % wxGetTranslation(response->GetTagByIndex(0)->GetStringData()));
496 // Access denied!
497 AddLogLineM(false, _("Unauthorized access attempt. Connection closed."));
498 m_conn_state = CONN_FAILED;
501 return response;
504 // Make a Logger tag (if there are any logging messages) and add it to the response
505 static void AddLoggerTag(CECPacket *response, CLoggerAccess &LoggerAccess)
507 if (LoggerAccess.HasString()) {
508 CECEmptyTag tag(EC_TAG_STATS_LOGGER_MESSAGE);
509 // Tag structure is fix: tag carries nothing, inside are the strings
510 // maximum of 200 log lines per message
511 int entries = 0;
512 wxString line;
513 while (entries < 200 && LoggerAccess.GetString(line)) {
514 tag.AddTag(CECTag(EC_TAG_STRING, line));
515 entries++;
517 response->AddTag(tag);
518 //printf("send Log tag %d %d\n", FirstEntry, entries);
522 static CECPacket *Get_EC_Response_StatRequest(const CECPacket *request, CLoggerAccess &LoggerAccess)
524 CECPacket *response = new CECPacket(EC_OP_STATS);
526 switch (request->GetDetailLevel()) {
527 case EC_DETAIL_FULL:
528 response->AddTag(CECTag(EC_TAG_STATS_UP_OVERHEAD, (uint32)theStats::GetUpOverheadRate()));
529 response->AddTag(CECTag(EC_TAG_STATS_DOWN_OVERHEAD, (uint32)theStats::GetDownOverheadRate()));
530 response->AddTag(CECTag(EC_TAG_STATS_BANNED_COUNT, /*(uint32)*/theStats::GetBannedCount()));
531 AddLoggerTag(response, LoggerAccess);
532 case EC_DETAIL_WEB:
533 case EC_DETAIL_CMD:
534 response->AddTag(CECTag(EC_TAG_STATS_UL_SPEED, (uint32)theStats::GetUploadRate()));
535 response->AddTag(CECTag(EC_TAG_STATS_DL_SPEED, (uint32)(theStats::GetDownloadRate())));
536 response->AddTag(CECTag(EC_TAG_STATS_UL_SPEED_LIMIT, (uint32)(thePrefs::GetMaxUpload()*1024.0)));
537 response->AddTag(CECTag(EC_TAG_STATS_DL_SPEED_LIMIT, (uint32)(thePrefs::GetMaxDownload()*1024.0)));
538 response->AddTag(CECTag(EC_TAG_STATS_UL_QUEUE_LEN, /*(uint32)*/theStats::GetWaitingUserCount()));
539 response->AddTag(CECTag(EC_TAG_STATS_TOTAL_SRC_COUNT, /*(uint32)*/theStats::GetFoundSources()));
540 // User/Filecounts
542 uint32 totaluser = 0, totalfile = 0;
543 theApp->serverlist->GetUserFileStatus( totaluser, totalfile );
544 response->AddTag(CECTag(EC_TAG_STATS_ED2K_USERS, totaluser));
545 response->AddTag(CECTag(EC_TAG_STATS_KAD_USERS, Kademlia::CKademlia::GetKademliaUsers()));
546 response->AddTag(CECTag(EC_TAG_STATS_ED2K_FILES, totalfile));
547 response->AddTag(CECTag(EC_TAG_STATS_KAD_FILES, Kademlia::CKademlia::GetKademliaFiles()));
549 // Kad stats
550 if (Kademlia::CKademlia::IsConnected()) {
551 response->AddTag(CECTag(EC_TAG_STATS_KAD_FIREWALLED_UDP, Kademlia::CUDPFirewallTester::IsFirewalledUDP(true)));
552 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_SOURCES, Kademlia::CKademlia::GetIndexed()->m_totalIndexSource));
553 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_KEYWORDS, Kademlia::CKademlia::GetIndexed()->m_totalIndexKeyword));
554 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_NOTES, Kademlia::CKademlia::GetIndexed()->m_totalIndexNotes));
555 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_LOAD, Kademlia::CKademlia::GetIndexed()->m_totalIndexLoad));
556 response->AddTag(CECTag(EC_TAG_STATS_KAD_IP_ADRESS, wxUINT32_SWAP_ALWAYS(Kademlia::CKademlia::GetPrefs()->GetIPAddress())));
557 response->AddTag(CECTag(EC_TAG_STATS_BUDDY_STATUS, theApp->clientlist->GetBuddyStatus()));
558 uint32 BuddyIP = 0;
559 uint16 BuddyPort = 0;
560 CUpDownClient * Buddy = theApp->clientlist->GetBuddy();
561 if (Buddy) {
562 BuddyIP = Buddy->GetIP();
563 BuddyPort = Buddy->GetUDPPort();
565 response->AddTag(CECTag(EC_TAG_STATS_BUDDY_IP, BuddyIP));
566 response->AddTag(CECTag(EC_TAG_STATS_BUDDY_PORT, BuddyPort));
568 case EC_DETAIL_UPDATE:
569 case EC_DETAIL_INC_UPDATE:
570 break;
573 return response;
576 static CECPacket *Get_EC_Response_GetSharedFiles(const CECPacket *request, CFileEncoderMap &encoders)
578 wxASSERT(request->GetOpCode() == EC_OP_GET_SHARED_FILES);
580 CECPacket *response = new CECPacket(EC_OP_SHARED_FILES);
582 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
584 // request can contain list of queried items
585 CTagSet<uint32, EC_TAG_KNOWNFILE> queryitems(request);
587 encoders.UpdateEncoders();
589 for (uint32 i = 0; i < theApp->sharedfiles->GetFileCount(); ++i) {
590 CKnownFile *cur_file = (CKnownFile *)theApp->sharedfiles->GetFileByIndex(i);
592 if ( !cur_file || (!queryitems.empty() && !queryitems.count(cur_file->ECID())) ) {
593 continue;
596 CEC_SharedFile_Tag filetag(cur_file, detail_level);
597 CKnownFile_Encoder *enc = encoders[cur_file->ECID()];
598 if ( detail_level != EC_DETAIL_UPDATE ) {
599 enc->ResetEncoder();
601 enc->Encode(&filetag);
602 response->AddTag(filetag);
604 return response;
607 static CECPacket *Get_EC_Response_GetUpdate(CFileEncoderMap &encoders, CObjTagMap &tagmap)
609 CECPacket *response = new CECPacket(EC_OP_SHARED_FILES);
611 encoders.UpdateEncoders();
612 for (CFileEncoderMap::iterator it = encoders.begin(); it != encoders.end(); ++it) {
613 const CKnownFile *cur_file = it->second->GetFile();
614 CValueMap &valuemap = tagmap.GetValueMap(cur_file);
615 if (cur_file->IsCPartFile()) {
616 CEC_PartFile_Tag filetag((const CPartFile*) cur_file, EC_DETAIL_INC_UPDATE, &valuemap);
617 // Add information if partfile is shared
618 filetag.AddTag(EC_TAG_PARTFILE_SHARED, it->second->IsShared(), &valuemap);
620 CPartFile_Encoder * enc = (CPartFile_Encoder *) encoders[cur_file->ECID()];
621 enc->Encode(&filetag);
622 response->AddTag(filetag);
623 } else {
624 CEC_SharedFile_Tag filetag(cur_file, EC_DETAIL_INC_UPDATE, &valuemap);
625 CKnownFile_Encoder * enc = encoders[cur_file->ECID()];
626 enc->Encode(&filetag);
627 response->AddTag(filetag);
630 return response;
633 static CECPacket *Get_EC_Response_GetClientQueue(const CECPacket *request, CObjTagMap &tagmap, int op)
635 CECPacket *response = new CECPacket(op);
637 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
640 // request can contain list of queried items
641 // (not for incremental update of course)
642 CTagSet<uint32, EC_TAG_CLIENT> queryitems(request);
644 const CClientPtrList& clients = (op == EC_OP_WAIT_QUEUE) ? theApp->uploadqueue->GetWaitingList()
645 : theApp->uploadqueue->GetUploadingList();
646 CClientPtrList::const_iterator it = clients.begin();
647 for (; it != clients.end(); ++it) {
648 CUpDownClient* cur_client = *it;
650 if (!cur_client) { // shouldn't happen
651 continue;
653 if (!queryitems.empty() && !queryitems.count(cur_client->ECID())) {
654 continue;
656 CValueMap *valuemap = NULL;
657 if (detail_level == EC_DETAIL_INC_UPDATE) {
658 valuemap = &tagmap.GetValueMap(cur_client);
660 CEC_UpDownClient_Tag cli_tag(cur_client, detail_level, valuemap);
662 response->AddTag(cli_tag);
665 return response;
669 static CECPacket *Get_EC_Response_GetDownloadQueue(const CECPacket *request, CFileEncoderMap &encoders)
671 CECPacket *response = new CECPacket(EC_OP_DLOAD_QUEUE);
673 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
675 // request can contain list of queried items
676 CTagSet<uint32, EC_TAG_PARTFILE> queryitems(request);
678 encoders.UpdateEncoders();
680 for (unsigned int i = 0; i < theApp->downloadqueue->GetFileCount(); i++) {
681 CPartFile *cur_file = theApp->downloadqueue->GetFileByIndex(i);
683 if ( !queryitems.empty() && !queryitems.count(cur_file->ECID()) ) {
684 continue;
687 CEC_PartFile_Tag filetag(cur_file, detail_level);
689 CPartFile_Encoder * enc = (CPartFile_Encoder *) encoders[cur_file->ECID()];
690 if ( detail_level != EC_DETAIL_UPDATE ) {
691 enc->ResetEncoder();
693 enc->Encode(&filetag);
695 response->AddTag(filetag);
697 return response;
701 static CECPacket *Get_EC_Response_PartFile_Cmd(const CECPacket *request)
703 CECPacket *response = NULL;
705 // request can contain multiple files.
706 for (unsigned int i = 0; i < request->GetTagCount(); ++i) {
707 const CECTag *hashtag = request->GetTagByIndex(i);
709 wxASSERT(hashtag->GetTagName() == EC_TAG_PARTFILE);
711 CMD4Hash hash = hashtag->GetMD4Data();
712 CPartFile *pfile = theApp->downloadqueue->GetFileByID( hash );
714 if ( !pfile ) {
715 AddLogLineM(false,CFormat(_("Remote PartFile command failed: FileHash not found: %s")) % hash.Encode());
716 response = new CECPacket(EC_OP_FAILED);
717 response->AddTag(CECTag(EC_TAG_STRING, CFormat(wxString(wxTRANSLATE("FileHash not found: %s"))) % hash.Encode()));
718 //return response;
719 break;
721 switch (request->GetOpCode()) {
722 case EC_OP_PARTFILE_SWAP_A4AF_THIS:
723 if ((pfile->GetStatus(false) == PS_READY) ||
724 (pfile->GetStatus(false) == PS_EMPTY)) {
725 CPartFile::SourceSet::const_iterator it = pfile->GetA4AFList().begin();
726 while ( it != pfile->GetA4AFList().end() ) {
727 CUpDownClient *cur_source = *it++;
729 cur_source->SwapToAnotherFile(true, false, false, pfile);
732 break;
733 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO:
734 pfile->SetA4AFAuto(!pfile->IsA4AFAuto());
735 break;
736 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS:
737 if ((pfile->GetStatus(false) == PS_READY) ||
738 (pfile->GetStatus(false) == PS_EMPTY)) {
739 CPartFile::SourceSet::const_iterator it = pfile->GetSourceList().begin();
740 while ( it != pfile->GetSourceList().end() ) {
741 CUpDownClient* cur_source = *it++;
743 cur_source->SwapToAnotherFile(false, false, false, NULL);
746 break;
747 case EC_OP_PARTFILE_PAUSE:
748 pfile->PauseFile();
749 break;
750 case EC_OP_PARTFILE_RESUME:
751 pfile->ResumeFile();
752 pfile->SavePartFile();
753 break;
754 case EC_OP_PARTFILE_STOP:
755 pfile->StopFile();
756 break;
757 case EC_OP_PARTFILE_PRIO_SET: {
758 uint8 prio = hashtag->GetTagByIndexSafe(0)->GetInt();
759 if ( prio == PR_AUTO ) {
760 pfile->SetAutoDownPriority(1);
761 } else {
762 pfile->SetAutoDownPriority(0);
763 pfile->SetDownPriority(prio);
766 break;
767 case EC_OP_PARTFILE_DELETE:
768 if ( thePrefs::StartNextFile() && (pfile->GetStatus() != PS_PAUSED) ) {
769 theApp->downloadqueue->StartNextFile(pfile);
771 pfile->Delete();
772 break;
774 case EC_OP_PARTFILE_SET_CAT:
775 pfile->SetCategory(hashtag->GetTagByIndexSafe(0)->GetInt());
776 break;
778 default:
779 response = new CECPacket(EC_OP_FAILED);
780 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("OOPS! OpCode processing error!")));
781 break;
784 if (!response) {
785 response = new CECPacket(EC_OP_NOOP);
787 return response;
790 static CECPacket *Get_EC_Response_Server_Add(const CECPacket *request)
792 CECPacket *response = NULL;
794 const CECTag *srv_tag = request->GetTagByIndex(0);
796 wxString full_addr = srv_tag->GetTagByName(EC_TAG_SERVER_ADDRESS)->GetStringData();
797 wxString name = srv_tag->GetTagByName(EC_TAG_SERVER_NAME)->GetStringData();
799 wxString s_ip = full_addr.Left(full_addr.Find(':'));
800 wxString s_port = full_addr.Mid(full_addr.Find(':') + 1);
802 long port = StrToULong(s_port);
803 CServer* toadd = new CServer(port, s_ip);
804 toadd->SetListName(name.IsEmpty() ? full_addr : name);
806 if ( theApp->AddServer(toadd, true) ) {
807 response = new CECPacket(EC_OP_NOOP);
808 } else {
809 response = new CECPacket(EC_OP_FAILED);
810 response->AddTag(CECTag(EC_TAG_STRING, _("Server not added")));
811 delete toadd;
814 return response;
817 static CECPacket *Get_EC_Response_Server(const CECPacket *request)
819 CECPacket *response = NULL;
820 const CECTag *srv_tag = request->GetTagByIndex(0);
821 CServer *srv = 0;
822 if ( srv_tag ) {
823 srv = theApp->serverlist->GetServerByIPTCP(srv_tag->GetIPv4Data().IP(), srv_tag->GetIPv4Data().m_port);
824 // server tag passed, but server not found
825 if ( !srv ) {
826 response = new CECPacket(EC_OP_FAILED);
827 response->AddTag(CECTag(EC_TAG_STRING,
828 CFormat(wxString(wxTRANSLATE("server not found: %s"))) % srv_tag->GetIPv4Data().StringIP()));
829 return response;
832 switch (request->GetOpCode()) {
833 case EC_OP_SERVER_DISCONNECT:
834 theApp->serverconnect->Disconnect();
835 response = new CECPacket(EC_OP_NOOP);
836 break;
837 case EC_OP_SERVER_REMOVE:
838 if ( srv ) {
839 theApp->serverlist->RemoveServer(srv);
840 response = new CECPacket(EC_OP_NOOP);
841 } else {
842 response = new CECPacket(EC_OP_FAILED);
843 response->AddTag(CECTag(EC_TAG_STRING,
844 wxTRANSLATE("need to define server to be removed")));
846 break;
847 case EC_OP_SERVER_CONNECT:
848 if (thePrefs::GetNetworkED2K()) {
849 if ( srv ) {
850 theApp->serverconnect->ConnectToServer(srv);
851 response = new CECPacket(EC_OP_NOOP);
852 } else {
853 theApp->serverconnect->ConnectToAnyServer();
854 response = new CECPacket(EC_OP_NOOP);
856 } else {
857 response = new CECPacket(EC_OP_FAILED);
858 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("eD2k is disabled in preferences.")));
860 break;
862 if (!response) {
863 response = new CECPacket(EC_OP_FAILED);
864 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("OOPS! OpCode processing error!")));
866 return response;
869 static CECPacket *Get_EC_Response_Search_Results(const CECPacket *request)
871 CECPacket *response = new CECPacket(EC_OP_SEARCH_RESULTS);
873 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
875 // request can contain list of queried items
876 CTagSet<CMD4Hash, EC_TAG_SEARCHFILE> queryitems(request);
878 const CSearchResultList& list = theApp->searchlist->GetSearchResults(0xffffffff);
879 CSearchResultList::const_iterator it = list.begin();
880 while (it != list.end()) {
881 CSearchFile* sf = *it++;
882 if ( !queryitems.empty() && !queryitems.count(sf->GetFileHash()) ) {
883 continue;
885 response->AddTag(CEC_SearchFile_Tag(sf, detail_level));
887 return response;
890 static CECPacket *Get_EC_Response_Search_Results(CObjTagMap &tagmap)
892 CECPacket *response = new CECPacket(EC_OP_SEARCH_RESULTS);
894 const CSearchResultList& list = theApp->searchlist->GetSearchResults(0xffffffff);
895 CSearchResultList::const_iterator it = list.begin();
896 while (it != list.end()) {
897 CSearchFile* sf = *it++;
898 CValueMap &valuemap = tagmap.GetValueMap(sf);
899 response->AddTag(CEC_SearchFile_Tag(sf, EC_DETAIL_INC_UPDATE, &valuemap));
901 return response;
904 static CECPacket *Get_EC_Response_Search_Results_Download(const CECPacket *request)
906 CECPacket *response = new CECPacket(EC_OP_STRINGS);
907 for (unsigned int i = 0;i < request->GetTagCount();i++) {
908 const CECTag *tag = request->GetTagByIndex(i);
909 CMD4Hash hash = tag->GetMD4Data();
910 uint8 category = tag->GetTagByIndexSafe(0)->GetInt();
911 theApp->searchlist->AddFileToDownloadByHash(hash, category);
913 return response;
916 static CECPacket *Get_EC_Response_Search_Stop(const CECPacket *WXUNUSED(request))
918 CECPacket *reply = new CECPacket(EC_OP_MISC_DATA);
919 theApp->searchlist->StopGlobalSearch();
920 return reply;
923 static CECPacket *Get_EC_Response_Search(const CECPacket *request)
925 wxString response;
927 CEC_Search_Tag *search_request = (CEC_Search_Tag *)request->GetTagByIndex(0);
928 theApp->searchlist->RemoveResults(0xffffffff);
930 CSearchList::CSearchParams params;
931 params.searchString = search_request->SearchText();
932 params.typeText = search_request->SearchFileType();
933 params.extension = search_request->SearchExt();
934 params.minSize = search_request->MinSize();
935 params.maxSize = search_request->MaxSize();
936 params.availability = search_request->Avail();
939 EC_SEARCH_TYPE search_type = search_request->SearchType();
940 SearchType core_search_type = LocalSearch;
941 switch (search_type) {
942 case EC_SEARCH_GLOBAL:
943 core_search_type = GlobalSearch;
944 case EC_SEARCH_KAD:
945 if (core_search_type != GlobalSearch) { // Not a global search obviously
946 core_search_type = KadSearch;
948 case EC_SEARCH_LOCAL: {
949 uint32 search_id = 0xffffffff;
950 wxString error = theApp->searchlist->StartNewSearch(&search_id, core_search_type, params);
951 if (!error.IsEmpty()) {
952 response = error;
953 } else {
954 response = wxTRANSLATE("Search in progress. Refetch results in a moment!");
956 break;
958 case EC_SEARCH_WEB:
959 response = wxTRANSLATE("WebSearch from remote interface makes no sense.");
960 break;
963 CECPacket *reply = new CECPacket(EC_OP_FAILED);
964 // error or search in progress
965 reply->AddTag(CECTag(EC_TAG_STRING, response));
967 return reply;
970 static CECPacket *Get_EC_Response_Set_SharedFile_Prio(const CECPacket *request)
972 CECPacket *response = new CECPacket(EC_OP_NOOP);
973 for (unsigned int i = 0;i < request->GetTagCount();i++) {
974 const CECTag *tag = request->GetTagByIndex(i);
975 CMD4Hash hash = tag->GetMD4Data();
976 uint8 prio = tag->GetTagByIndexSafe(0)->GetInt();
977 CKnownFile* cur_file = theApp->sharedfiles->GetFileByID(hash);
978 if ( !cur_file ) {
979 continue;
981 if (prio == PR_AUTO) {
982 cur_file->SetAutoUpPriority(1);
983 cur_file->UpdateAutoUpPriority();
984 } else {
985 cur_file->SetAutoUpPriority(0);
986 cur_file->SetUpPriority(prio);
988 Notify_SharedFilesUpdateItem(cur_file);
991 return response;
994 static CECPacket *Get_EC_Response_Kad_Connect(const CECPacket *request)
996 CECPacket *response;
997 if (thePrefs::GetNetworkKademlia()) {
998 response = new CECPacket(EC_OP_NOOP);
999 if ( !Kademlia::CKademlia::IsRunning() ) {
1000 Kademlia::CKademlia::Start();
1001 theApp->ShowConnectionState();
1003 const CECTag *addrtag = request->GetTagByIndex(0);
1004 if ( addrtag ) {
1005 uint32 ip = addrtag->GetIPv4Data().IP();
1006 uint16 port = addrtag->GetIPv4Data().m_port;
1007 Kademlia::CKademlia::Bootstrap(ip, port, true);
1009 } else {
1010 response = new CECPacket(EC_OP_FAILED);
1011 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Kad is disabled in preferences.")));
1014 return response;
1017 void CPartFile_Encoder::Encode(CECTag *parent)
1020 // Source part frequencies
1022 CKnownFile_Encoder::Encode(parent);
1025 // Gaps
1027 const CGapList& gaplist = m_PartFile()->GetNewGapList();
1028 const size_t gap_list_size = gaplist.size();
1029 ArrayOfUInts64 gaps;
1030 gaps.reserve(gap_list_size * 2);
1032 for (CGapList::const_iterator curr_pos = gaplist.begin();
1033 curr_pos != gaplist.end(); ++curr_pos) {
1034 gaps.push_back(curr_pos.start());
1035 gaps.push_back(curr_pos.end());
1038 int gap_enc_size = 0;
1039 bool changed;
1040 const uint8 *gap_enc_data = m_gap_status.Encode(gaps, gap_enc_size, changed);
1041 if (changed) {
1042 parent->AddTag(CECTag(EC_TAG_PARTFILE_GAP_STATUS, gap_enc_size, (void *)gap_enc_data));
1044 delete[] gap_enc_data;
1047 // Requested blocks
1049 ArrayOfUInts64 req_buffer;
1050 const CPartFile::CReqBlockPtrList& requestedblocks = m_PartFile()->GetRequestedBlockList();
1051 CPartFile::CReqBlockPtrList::const_iterator curr_pos2 = requestedblocks.begin();
1053 for ( ; curr_pos2 != requestedblocks.end(); ++curr_pos2 ) {
1054 Requested_Block_Struct* block = *curr_pos2;
1055 req_buffer.push_back(block->StartOffset);
1056 req_buffer.push_back(block->EndOffset);
1058 int req_enc_size = 0;
1059 const uint8 *req_enc_data = m_req_status.Encode(req_buffer, req_enc_size, changed);
1060 if (changed) {
1061 parent->AddTag(CECTag(EC_TAG_PARTFILE_REQ_STATUS, req_enc_size, (void *)req_enc_data));
1063 delete[] req_enc_data;
1066 // Source names
1068 // First count occurrence of all source names
1070 CECEmptyTag sourceNames(EC_TAG_PARTFILE_SOURCE_NAMES);
1071 typedef std::map<wxString, int> strIntMap;
1072 strIntMap nameMap;
1073 const CPartFile::SourceSet &sources = m_PartFile()->GetSourceList();
1074 for (CPartFile::SourceSet::const_iterator it = sources.begin(); it != sources.end(); ++it) {
1075 CUpDownClient *cur_src = *it;
1076 if (cur_src->GetRequestFile() != m_file || cur_src->GetClientFilename().Length() == 0) {
1077 continue;
1079 const wxString &name = cur_src->GetClientFilename();
1080 strIntMap::iterator itm = nameMap.find(name);
1081 if (itm == nameMap.end()) {
1082 nameMap[name] = 1;
1083 } else {
1084 itm->second++;
1088 // Go through our last list
1090 for (SourcenameItemMap::iterator it1 = m_sourcenameItemMap.begin(); it1 != m_sourcenameItemMap.end();) {
1091 SourcenameItemMap::iterator it2 = it1++;
1092 strIntMap::iterator itm = nameMap.find(it2->second.name);
1093 if (itm == nameMap.end()) {
1094 // name doesn't exist anymore, tell client to forget it
1095 CECTag tag(EC_TAG_PARTFILE_SOURCE_NAMES, it2->first);
1096 tag.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS, 0));
1097 sourceNames.AddTag(tag);
1098 // and forget it
1099 m_sourcenameItemMap.erase(it2);
1100 } else {
1101 // update count if it changed
1102 if (it2->second.count != itm->second) {
1103 CECTag tag(EC_TAG_PARTFILE_SOURCE_NAMES, it2->first);
1104 tag.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS, itm->second));
1105 sourceNames.AddTag(tag);
1106 it2->second.count = itm->second;
1108 // remove it from nameMap so that only new names are left there
1109 nameMap.erase(itm);
1113 // Add new names
1115 for (strIntMap::iterator it3 = nameMap.begin(); it3 != nameMap.end(); it3++) {
1116 int id = ++m_sourcenameID;
1117 CECIntTag tag(EC_TAG_PARTFILE_SOURCE_NAMES, id);
1118 tag.AddTag(CECTag(EC_TAG_PARTFILE_SOURCE_NAMES, it3->first));
1119 tag.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS, it3->second));
1120 sourceNames.AddTag(tag);
1121 // remember it
1122 m_sourcenameItemMap[id] = SourcenameItem(it3->first, it3->second);
1124 if (sourceNames.HasChildTags()) {
1125 parent->AddTag(sourceNames);
1130 void CPartFile_Encoder::ResetEncoder()
1132 CKnownFile_Encoder::ResetEncoder();
1133 m_gap_status.ResetEncoder();
1134 m_req_status.ResetEncoder();
1137 void CKnownFile_Encoder::Encode(CECTag *parent)
1140 // Source part frequencies
1142 // Reference to the availability list
1143 const ArrayOfUInts16& list = m_file->IsPartFile() ?
1144 ((CPartFile*)m_file)->m_SrcpartFrequency :
1145 m_file->m_AvailPartFrequency;
1146 // Don't add tag if available parts aren't populated yet.
1147 if (!list.empty()) {
1148 int part_enc_size;
1149 bool changed;
1150 const uint8 *part_enc_data = m_enc_data.Encode(list, part_enc_size, changed);
1151 if (changed) {
1152 parent->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS, part_enc_size, part_enc_data));
1154 delete[] part_enc_data;
1158 static CECPacket *GetStatsGraphs(const CECPacket *request)
1160 CECPacket *response = NULL;
1162 switch (request->GetDetailLevel()) {
1163 case EC_DETAIL_WEB:
1164 case EC_DETAIL_FULL: {
1165 double dTimestamp = 0.0;
1166 if (request->GetTagByName(EC_TAG_STATSGRAPH_LAST) != NULL) {
1167 dTimestamp = request->GetTagByName(EC_TAG_STATSGRAPH_LAST)->GetDoubleData();
1169 uint16 nScale = request->GetTagByNameSafe(EC_TAG_STATSGRAPH_SCALE)->GetInt();
1170 uint16 nMaxPoints = request->GetTagByNameSafe(EC_TAG_STATSGRAPH_WIDTH)->GetInt();
1171 uint32 *graphData;
1172 unsigned int numPoints = theApp->m_statistics->GetHistoryForWeb(nMaxPoints, (double)nScale, &dTimestamp, &graphData);
1173 if (numPoints) {
1174 response = new CECPacket(EC_OP_STATSGRAPHS);
1175 response->AddTag(CECTag(EC_TAG_STATSGRAPH_DATA, 4 * numPoints * sizeof(uint32), graphData));
1176 delete [] graphData;
1177 response->AddTag(CECTag(EC_TAG_STATSGRAPH_LAST, dTimestamp));
1178 } else {
1179 response = new CECPacket(EC_OP_FAILED);
1180 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("No points for graph.")));
1182 break;
1184 case EC_DETAIL_INC_UPDATE:
1185 case EC_DETAIL_UPDATE:
1186 case EC_DETAIL_CMD:
1187 // No graphs
1188 response = new CECPacket(EC_OP_FAILED);
1189 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Your client is not configured for this detail level.")));
1190 break;
1192 if (!response) {
1193 response = new CECPacket(EC_OP_FAILED);
1194 // Unknown reason
1197 return response;
1200 CECPacket *CECServerSocket::ProcessRequest2(const CECPacket *request)
1203 if ( !request ) {
1204 return 0;
1207 CECPacket *response = NULL;
1209 switch (request->GetOpCode()) {
1211 // Misc commands
1213 case EC_OP_SHUTDOWN:
1214 if (!theApp->IsOnShutDown()) {
1215 response = new CECPacket(EC_OP_NOOP);
1216 AddLogLineM(true, _("External Connection: shutdown requested"));
1217 #ifndef AMULE_DAEMON
1219 wxCloseEvent evt;
1220 evt.SetCanVeto(false);
1221 theApp->ShutDown(evt);
1223 #else
1224 theApp->ExitMainLoop();
1225 #endif
1226 } else {
1227 response = new CECPacket(EC_OP_FAILED);
1228 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Already shutting down.")));
1230 break;
1231 case EC_OP_ADD_LINK:
1232 for(unsigned int i = 0; i < request->GetTagCount();i++) {
1233 const CECTag *tag = request->GetTagByIndex(i);
1234 wxString link = tag->GetStringData();
1235 int category = 0;
1236 const CECTag *cattag = tag->GetTagByName(EC_TAG_PARTFILE_CAT);
1237 if (cattag) {
1238 category = cattag->GetInt();
1240 AddLogLineM(true, CFormat(_("ExternalConn: adding link '%s'.")) % link);
1241 if ( theApp->downloadqueue->AddLink(link, category) ) {
1242 response = new CECPacket(EC_OP_NOOP);
1243 } else {
1244 // Error messages are printed by the add function.
1245 response = new CECPacket(EC_OP_FAILED);
1246 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid link or already on list.")));
1249 break;
1251 // Status requests
1253 case EC_OP_STAT_REQ:
1254 response = Get_EC_Response_StatRequest(request, m_LoggerAccess);
1255 response->AddTag(CEC_ConnState_Tag(request->GetDetailLevel()));
1256 break;
1257 case EC_OP_GET_CONNSTATE:
1258 response = new CECPacket(EC_OP_MISC_DATA);
1259 response->AddTag(CEC_ConnState_Tag(request->GetDetailLevel()));
1260 break;
1264 case EC_OP_GET_SHARED_FILES:
1265 if ( request->GetDetailLevel() != EC_DETAIL_INC_UPDATE ) {
1266 response = Get_EC_Response_GetSharedFiles(request, m_FileEncoder);
1268 break;
1269 case EC_OP_GET_DLOAD_QUEUE:
1270 if ( request->GetDetailLevel() != EC_DETAIL_INC_UPDATE ) {
1271 response = Get_EC_Response_GetDownloadQueue(request, m_FileEncoder);
1273 break;
1275 // This will evolve into an update-all for inc tags
1277 case EC_OP_GET_UPDATE:
1278 if ( request->GetDetailLevel() == EC_DETAIL_INC_UPDATE ) {
1279 response = Get_EC_Response_GetUpdate(m_FileEncoder, m_obj_tagmap);
1281 break;
1282 case EC_OP_GET_ULOAD_QUEUE:
1283 response = Get_EC_Response_GetClientQueue(request, m_obj_tagmap, EC_OP_ULOAD_QUEUE);
1284 break;
1285 case EC_OP_GET_WAIT_QUEUE:
1286 response = Get_EC_Response_GetClientQueue(request, m_obj_tagmap, EC_OP_WAIT_QUEUE);
1287 break;
1288 case EC_OP_PARTFILE_REMOVE_NO_NEEDED:
1289 case EC_OP_PARTFILE_REMOVE_FULL_QUEUE:
1290 case EC_OP_PARTFILE_REMOVE_HIGH_QUEUE:
1291 case EC_OP_PARTFILE_CLEANUP_SOURCES:
1292 case EC_OP_PARTFILE_SWAP_A4AF_THIS:
1293 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO:
1294 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS:
1295 case EC_OP_PARTFILE_PAUSE:
1296 case EC_OP_PARTFILE_RESUME:
1297 case EC_OP_PARTFILE_STOP:
1298 case EC_OP_PARTFILE_PRIO_SET:
1299 case EC_OP_PARTFILE_DELETE:
1300 case EC_OP_PARTFILE_SET_CAT:
1301 response = Get_EC_Response_PartFile_Cmd(request);
1302 break;
1303 case EC_OP_SHAREDFILES_RELOAD:
1304 theApp->sharedfiles->Reload();
1305 response = new CECPacket(EC_OP_NOOP);
1306 break;
1307 case EC_OP_SHARED_SET_PRIO:
1308 response = Get_EC_Response_Set_SharedFile_Prio(request);
1309 break;
1310 case EC_OP_RENAME_FILE: {
1311 CMD4Hash fileHash = request->GetTagByNameSafe(EC_TAG_KNOWNFILE)->GetMD4Data();
1312 CKnownFile* file = theApp->knownfiles->FindKnownFileByID(fileHash);
1313 wxString newName = request->GetTagByNameSafe(EC_TAG_PARTFILE_NAME)->GetStringData();
1314 if (!file) {
1315 file = theApp->downloadqueue->GetFileByID(fileHash);
1317 if (!file) {
1318 response = new CECPacket(EC_OP_FAILED);
1319 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("File not found.")));
1320 break;
1322 if (newName.IsEmpty()) {
1323 response = new CECPacket(EC_OP_FAILED);
1324 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid file name.")));
1325 break;
1328 if (theApp->sharedfiles->RenameFile(file, CPath(newName))) {
1329 response = new CECPacket(EC_OP_NOOP);
1330 } else {
1331 response = new CECPacket(EC_OP_FAILED);
1332 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Unable to rename file.")));
1335 break;
1340 // Server commands
1342 case EC_OP_SERVER_ADD:
1343 response = Get_EC_Response_Server_Add(request);
1344 break;
1345 case EC_OP_SERVER_DISCONNECT:
1346 case EC_OP_SERVER_CONNECT:
1347 case EC_OP_SERVER_REMOVE:
1348 response = Get_EC_Response_Server(request);
1349 break;
1350 case EC_OP_GET_SERVER_LIST: {
1351 response = new CECPacket(EC_OP_SERVER_LIST);
1352 if (!thePrefs::GetNetworkED2K()) {
1353 // Kad only: just send an empty list
1354 break;
1356 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
1357 std::vector<const CServer*> servers = theApp->serverlist->CopySnapshot();
1358 for (
1359 std::vector<const CServer*>::const_iterator it = servers.begin();
1360 it != servers.end();
1361 ++it
1363 response->AddTag(CEC_Server_Tag(*it, detail_level));
1366 break;
1367 case EC_OP_SERVER_UPDATE_FROM_URL: {
1368 wxString url = request->GetTagByIndexSafe(0)->GetStringData();
1370 // Save the new url, and update the UI (if not amuled).
1371 Notify_ServersURLChanged(url);
1372 thePrefs::SetEd2kServersUrl(url);
1374 theApp->serverlist->UpdateServerMetFromURL(url);
1375 response = new CECPacket(EC_OP_NOOP);
1376 break;
1379 // IPFilter
1381 case EC_OP_IPFILTER_RELOAD:
1382 theApp->ipfilter->Reload();
1383 response = new CECPacket(EC_OP_NOOP);
1384 break;
1386 case EC_OP_IPFILTER_UPDATE: {
1387 wxString url = request->GetTagByIndexSafe(0)->GetStringData();
1388 if (url == wxEmptyString) {
1389 url = thePrefs::IPFilterURL();
1391 theApp->ipfilter->Update(url);
1392 response = new CECPacket(EC_OP_NOOP);
1393 break;
1396 // Search
1398 case EC_OP_SEARCH_START:
1399 response = Get_EC_Response_Search(request);
1400 break;
1402 case EC_OP_SEARCH_STOP:
1403 response = Get_EC_Response_Search_Stop(request);
1404 break;
1406 case EC_OP_SEARCH_RESULTS:
1407 if ( request->GetDetailLevel() == EC_DETAIL_INC_UPDATE ) {
1408 response = Get_EC_Response_Search_Results(m_obj_tagmap);
1409 } else {
1410 response = Get_EC_Response_Search_Results(request);
1412 break;
1414 case EC_OP_SEARCH_PROGRESS:
1415 response = new CECPacket(EC_OP_SEARCH_PROGRESS);
1416 response->AddTag(CECTag(EC_TAG_SEARCH_STATUS,
1417 theApp->searchlist->GetSearchProgress()));
1418 break;
1420 case EC_OP_DOWNLOAD_SEARCH_RESULT:
1421 response = Get_EC_Response_Search_Results_Download(request);
1422 break;
1424 // Preferences
1426 case EC_OP_GET_PREFERENCES:
1427 response = new CEC_Prefs_Packet(request->GetTagByNameSafe(EC_TAG_SELECT_PREFS)->GetInt(), request->GetDetailLevel());
1428 break;
1429 case EC_OP_SET_PREFERENCES:
1430 ((CEC_Prefs_Packet *)request)->Apply();
1431 theApp->glob_prefs->Save();
1432 if (thePrefs::IsFilteringClients()) {
1433 theApp->clientlist->FilterQueues();
1435 if (thePrefs::IsFilteringServers()) {
1436 theApp->serverlist->FilterServers();
1438 if (!thePrefs::GetNetworkED2K() && theApp->IsConnectedED2K()) {
1439 theApp->DisconnectED2K();
1441 if (!thePrefs::GetNetworkKademlia() && theApp->IsConnectedKad()) {
1442 theApp->StopKad();
1444 response = new CECPacket(EC_OP_NOOP);
1445 break;
1447 case EC_OP_CREATE_CATEGORY:
1448 if ( request->GetTagCount() == 1 ) {
1449 CEC_Category_Tag *tag = (CEC_Category_Tag *)request->GetTagByIndex(0);
1450 if (tag->Create()) {
1451 response = new CECPacket(EC_OP_NOOP);
1452 } else {
1453 response = new CECPacket(EC_OP_FAILED);
1454 response->AddTag(CECTag(EC_TAG_CATEGORY, theApp->glob_prefs->GetCatCount() - 1));
1455 response->AddTag(CECTag(EC_TAG_CATEGORY_PATH, tag->Path()));
1457 Notify_CategoryAdded();
1458 } else {
1459 response = new CECPacket(EC_OP_NOOP);
1461 break;
1462 case EC_OP_UPDATE_CATEGORY:
1463 if ( request->GetTagCount() == 1 ) {
1464 CEC_Category_Tag *tag = (CEC_Category_Tag *)request->GetTagByIndex(0);
1465 if (tag->Apply()) {
1466 response = new CECPacket(EC_OP_NOOP);
1467 } else {
1468 response = new CECPacket(EC_OP_FAILED);
1469 response->AddTag(CECTag(EC_TAG_CATEGORY, tag->GetInt()));
1470 response->AddTag(CECTag(EC_TAG_CATEGORY_PATH, tag->Path()));
1472 Notify_CategoryUpdate(tag->GetInt());
1473 } else {
1474 response = new CECPacket(EC_OP_NOOP);
1476 break;
1477 case EC_OP_DELETE_CATEGORY:
1478 if ( request->GetTagCount() == 1 ) {
1479 uint32 cat = request->GetTagByIndex(0)->GetInt();
1480 // this noes not only update the gui, but actually deletes the cat
1481 Notify_CategoryDelete(cat);
1483 response = new CECPacket(EC_OP_NOOP);
1484 break;
1487 // Logging
1489 case EC_OP_ADDLOGLINE:
1490 AddLogLineM( (request->GetTagByName(EC_TAG_LOG_TO_STATUS) != NULL), request->GetTagByNameSafe(EC_TAG_STRING)->GetStringData() );
1491 response = new CECPacket(EC_OP_NOOP);
1492 break;
1493 case EC_OP_ADDDEBUGLOGLINE:
1494 AddDebugLogLineM( (request->GetTagByName(EC_TAG_LOG_TO_STATUS) != NULL), logGeneral, request->GetTagByNameSafe(EC_TAG_STRING)->GetStringData() );
1495 response = new CECPacket(EC_OP_NOOP);
1496 break;
1497 case EC_OP_GET_LOG:
1498 response = new CECPacket(EC_OP_LOG);
1499 response->AddTag(CECTag(EC_TAG_STRING, theApp->GetLog(false)));
1500 break;
1501 case EC_OP_GET_DEBUGLOG:
1502 response = new CECPacket(EC_OP_DEBUGLOG);
1503 response->AddTag(CECTag(EC_TAG_STRING, theApp->GetDebugLog(false)));
1504 break;
1505 case EC_OP_RESET_LOG:
1506 theApp->GetLog(true);
1507 response = new CECPacket(EC_OP_NOOP);
1508 break;
1509 case EC_OP_RESET_DEBUGLOG:
1510 theApp->GetDebugLog(true);
1511 response = new CECPacket(EC_OP_NOOP);
1512 break;
1513 case EC_OP_GET_LAST_LOG_ENTRY:
1515 wxString tmp = theApp->GetLog(false);
1516 if (tmp.Last() == '\n') {
1517 tmp.RemoveLast();
1519 response = new CECPacket(EC_OP_LOG);
1520 response->AddTag(CECTag(EC_TAG_STRING, tmp.AfterLast('\n')));
1522 break;
1523 case EC_OP_GET_SERVERINFO:
1524 response = new CECPacket(EC_OP_SERVERINFO);
1525 response->AddTag(CECTag(EC_TAG_STRING, theApp->GetServerLog(false)));
1526 break;
1527 case EC_OP_CLEAR_SERVERINFO:
1528 theApp->GetServerLog(true);
1529 response = new CECPacket(EC_OP_NOOP);
1530 break;
1532 // Statistics
1534 case EC_OP_GET_STATSGRAPHS:
1535 response = GetStatsGraphs(request);
1536 break;
1537 case EC_OP_GET_STATSTREE: {
1538 theApp->m_statistics->UpdateStatsTree();
1539 response = new CECPacket(EC_OP_STATSTREE);
1540 CECTag* tree = theStats::GetECStatTree(request->GetTagByNameSafe(EC_TAG_STATTREE_CAPPING)->GetInt());
1541 if (tree) {
1542 response->AddTag(*tree);
1543 delete tree;
1545 if (request->GetDetailLevel() == EC_DETAIL_WEB) {
1546 response->AddTag(CECTag(EC_TAG_SERVER_VERSION, wxT(VERSION)));
1547 response->AddTag(CECTag(EC_TAG_USER_NICK, thePrefs::GetUserNick()));
1549 break;
1553 // Kad
1555 case EC_OP_KAD_START:
1556 response = Get_EC_Response_Kad_Connect(request);
1557 break;
1558 case EC_OP_KAD_STOP:
1559 theApp->StopKad();
1560 response = new CECPacket(EC_OP_NOOP);
1561 break;
1562 case EC_OP_KAD_UPDATE_FROM_URL: {
1563 wxString url = request->GetTagByIndexSafe(0)->GetStringData();
1565 // Save the new url, and update the UI (if not amuled).
1566 Notify_NodesURLChanged(url);
1567 thePrefs::SetKadNodesUrl(url);
1569 theApp->UpdateNotesDat(url);
1570 response = new CECPacket(EC_OP_NOOP);
1571 break;
1573 case EC_OP_KAD_BOOTSTRAP_FROM_IP:
1574 theApp->BootstrapKad(request->GetTagByIndexSafe(0)->GetInt(),
1575 request->GetTagByIndexSafe(1)->GetInt());
1576 response = new CECPacket(EC_OP_NOOP);
1577 break;
1580 // Networks
1582 case EC_OP_CONNECT:
1583 if (thePrefs::GetNetworkED2K()) {
1584 response = new CECPacket(EC_OP_STRINGS);
1585 if (theApp->IsConnectedED2K()) {
1586 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Already connected to eD2k.")));
1587 } else {
1588 theApp->serverconnect->ConnectToAnyServer();
1589 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Connecting to eD2k...")));
1592 if (thePrefs::GetNetworkKademlia()) {
1593 if (!response) {
1594 response = new CECPacket(EC_OP_STRINGS);
1596 if (theApp->IsConnectedKad()) {
1597 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Already connected to Kad.")));
1598 } else {
1599 theApp->StartKad();
1600 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Connecting to Kad...")));
1603 if (!response) {
1604 response = new CECPacket(EC_OP_FAILED);
1605 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("All networks are disabled.")));
1607 break;
1608 case EC_OP_DISCONNECT:
1609 if (theApp->IsConnected()) {
1610 response = new CECPacket(EC_OP_STRINGS);
1611 if (theApp->IsConnectedED2K()) {
1612 theApp->serverconnect->Disconnect();
1613 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Disconnected from eD2k.")));
1615 if (theApp->IsConnectedKad()) {
1616 theApp->StopKad();
1617 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Disconnected from Kad.")));
1619 } else {
1620 response = new CECPacket(EC_OP_NOOP);
1622 break;
1624 if (!response) {
1625 AddLogLineM(false, wxString::Format(_("External Connection: invalid opcode received: %#x"), request->GetOpCode()));
1626 wxFAIL;
1627 response = new CECPacket(EC_OP_FAILED);
1628 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid opcode (wrong protocol version?)")));
1630 return response;
1634 * Here notification-based EC. Notification will be sorted by priority for possible throttling.
1638 * Core general status
1640 ECStatusMsgSource::ECStatusMsgSource()
1642 m_last_ed2k_status_sent = 0xffffffff;
1643 m_last_kad_status_sent = 0xffffffff;
1644 m_server = (void *)0xffffffff;
1647 uint32 ECStatusMsgSource::GetEd2kStatus()
1649 if ( theApp->IsConnectedED2K() ) {
1650 return theApp->GetED2KID();
1651 } else if ( theApp->serverconnect->IsConnecting() ) {
1652 return 1;
1653 } else {
1654 return 0;
1658 uint32 ECStatusMsgSource::GetKadStatus()
1660 if ( theApp->IsConnectedKad() ) {
1661 return 1;
1662 } else if ( Kademlia::CKademlia::IsFirewalled() ) {
1663 return 2;
1664 } else if ( Kademlia::CKademlia::IsRunning() ) {
1665 return 3;
1667 return 0;
1670 CECPacket *ECStatusMsgSource::GetNextPacket()
1672 if ( (m_last_ed2k_status_sent != GetEd2kStatus()) ||
1673 (m_last_kad_status_sent != GetKadStatus()) ||
1674 (m_server != theApp->serverconnect->GetCurrentServer()) ) {
1676 m_last_ed2k_status_sent = GetEd2kStatus();
1677 m_last_kad_status_sent = GetKadStatus();
1678 m_server = theApp->serverconnect->GetCurrentServer();
1680 CECPacket *response = new CECPacket(EC_OP_STATS);
1681 response->AddTag(CEC_ConnState_Tag(EC_DETAIL_UPDATE));
1682 return response;
1684 return 0;
1688 * Downloading files
1690 ECPartFileMsgSource::ECPartFileMsgSource()
1692 for (unsigned int i = 0; i < theApp->downloadqueue->GetFileCount(); i++) {
1693 CPartFile *cur_file = theApp->downloadqueue->GetFileByIndex(i);
1694 PARTFILE_STATUS status = { true, false, false, false, true, cur_file };
1695 m_dirty_status[cur_file->GetFileHash()] = status;
1699 void ECPartFileMsgSource::SetDirty(CPartFile *file)
1701 CMD4Hash filehash = file->GetFileHash();
1702 if ( m_dirty_status.find(filehash) != m_dirty_status.end() ) {
1703 m_dirty_status[filehash].m_dirty = true;;
1707 void ECPartFileMsgSource::SetNew(CPartFile *file)
1709 CMD4Hash filehash = file->GetFileHash();
1710 wxASSERT ( m_dirty_status.find(filehash) == m_dirty_status.end() );
1711 PARTFILE_STATUS status = { true, false, false, false, true, file };
1712 m_dirty_status[filehash] = status;
1715 void ECPartFileMsgSource::SetCompleted(CPartFile *file)
1717 CMD4Hash filehash = file->GetFileHash();
1718 wxASSERT ( m_dirty_status.find(filehash) != m_dirty_status.end() );
1720 m_dirty_status[filehash].m_finished = true;
1723 void ECPartFileMsgSource::SetRemoved(CPartFile *file)
1725 CMD4Hash filehash = file->GetFileHash();
1726 wxASSERT ( m_dirty_status.find(filehash) != m_dirty_status.end() );
1728 m_dirty_status[filehash].m_removed = true;
1731 CECPacket *ECPartFileMsgSource::GetNextPacket()
1733 for(std::map<CMD4Hash, PARTFILE_STATUS>::iterator it = m_dirty_status.begin();
1734 it != m_dirty_status.end(); it++) {
1735 if ( it->second.m_new || it->second.m_dirty || it->second.m_removed) {
1736 CMD4Hash filehash = it->first;
1738 CPartFile *partfile = it->second.m_file;
1740 CECPacket *packet = new CECPacket(EC_OP_DLOAD_QUEUE);
1741 if ( it->second.m_removed ) {
1742 CECTag tag(EC_TAG_PARTFILE, filehash);
1743 packet->AddTag(tag);
1744 m_dirty_status.erase(it);
1745 } else {
1746 CEC_PartFile_Tag tag(partfile, it->second.m_new ? EC_DETAIL_FULL : EC_DETAIL_UPDATE);
1747 packet->AddTag(tag);
1749 m_dirty_status[filehash].m_new = false;
1750 m_dirty_status[filehash].m_dirty = false;
1752 return packet;
1755 return 0;
1759 * Shared files - similar to downloading
1761 ECKnownFileMsgSource::ECKnownFileMsgSource()
1763 for (unsigned int i = 0; i < theApp->sharedfiles->GetFileCount(); i++) {
1764 CKnownFile *cur_file = (CKnownFile *)theApp->sharedfiles->GetFileByIndex(i);
1765 KNOWNFILE_STATUS status = { true, false, false, true, cur_file };
1766 m_dirty_status[cur_file->GetFileHash()] = status;
1770 void ECKnownFileMsgSource::SetDirty(CKnownFile *file)
1772 CMD4Hash filehash = file->GetFileHash();
1773 if ( m_dirty_status.find(filehash) != m_dirty_status.end() ) {
1774 m_dirty_status[filehash].m_dirty = true;;
1778 void ECKnownFileMsgSource::SetNew(CKnownFile *file)
1780 CMD4Hash filehash = file->GetFileHash();
1781 wxASSERT ( m_dirty_status.find(filehash) == m_dirty_status.end() );
1782 KNOWNFILE_STATUS status = { true, false, false, true, file };
1783 m_dirty_status[filehash] = status;
1786 void ECKnownFileMsgSource::SetRemoved(CKnownFile *file)
1788 CMD4Hash filehash = file->GetFileHash();
1789 wxASSERT ( m_dirty_status.find(filehash) != m_dirty_status.end() );
1791 m_dirty_status[filehash].m_removed = true;
1794 CECPacket *ECKnownFileMsgSource::GetNextPacket()
1796 for(std::map<CMD4Hash, KNOWNFILE_STATUS>::iterator it = m_dirty_status.begin();
1797 it != m_dirty_status.end(); it++) {
1798 if ( it->second.m_new || it->second.m_dirty || it->second.m_removed) {
1799 CMD4Hash filehash = it->first;
1801 CKnownFile *partfile = it->second.m_file;
1803 CECPacket *packet = new CECPacket(EC_OP_SHARED_FILES);
1804 if ( it->second.m_removed ) {
1805 CECTag tag(EC_TAG_PARTFILE, filehash);
1806 packet->AddTag(tag);
1807 m_dirty_status.erase(it);
1808 } else {
1809 CEC_SharedFile_Tag tag(partfile, it->second.m_new ? EC_DETAIL_FULL : EC_DETAIL_UPDATE);
1810 packet->AddTag(tag);
1812 m_dirty_status[filehash].m_new = false;
1813 m_dirty_status[filehash].m_dirty = false;
1815 return packet;
1818 return 0;
1822 * Notification about search status
1824 ECSearchMsgSource::ECSearchMsgSource()
1828 CECPacket *ECSearchMsgSource::GetNextPacket()
1830 if ( m_dirty_status.empty() ) {
1831 return 0;
1834 CECPacket *response = new CECPacket(EC_OP_SEARCH_RESULTS);
1835 for(std::map<CMD4Hash, SEARCHFILE_STATUS>::iterator it = m_dirty_status.begin();
1836 it != m_dirty_status.end(); it++) {
1838 if ( it->second.m_new ) {
1839 response->AddTag(CEC_SearchFile_Tag(it->second.m_file, EC_DETAIL_FULL));
1840 it->second.m_new = false;
1841 } else if ( it->second.m_dirty ) {
1842 response->AddTag(CEC_SearchFile_Tag(it->second.m_file, EC_DETAIL_UPDATE));
1847 return response;
1850 void ECSearchMsgSource::FlushStatus()
1852 m_dirty_status.clear();
1855 void ECSearchMsgSource::SetDirty(CSearchFile *file)
1857 if ( m_dirty_status.count(file->GetFileHash()) ) {
1858 m_dirty_status[file->GetFileHash()].m_dirty = true;
1859 } else {
1860 m_dirty_status[file->GetFileHash()].m_new = true;
1861 m_dirty_status[file->GetFileHash()].m_dirty = true;
1862 m_dirty_status[file->GetFileHash()].m_child_dirty = true;
1863 m_dirty_status[file->GetFileHash()].m_file = file;
1867 void ECSearchMsgSource::SetChildDirty(CSearchFile *file)
1869 m_dirty_status[file->GetFileHash()].m_child_dirty = true;
1873 * Notification about uploading clients
1875 CECPacket *ECClientMsgSource::GetNextPacket()
1877 return 0;
1881 // Notification iface per-client
1883 ECNotifier::ECNotifier()
1887 ECNotifier::~ECNotifier()
1889 while (m_msg_source.begin() != m_msg_source.end())
1890 Remove_EC_Client(m_msg_source.begin()->first);
1893 CECPacket *ECNotifier::GetNextPacket(ECUpdateMsgSource *msg_source_array[])
1895 CECPacket *packet = 0;
1897 // priority 0 is highest
1899 for(int i = 0; i < EC_STATUS_LAST_PRIO; i++) {
1900 if ( (packet = msg_source_array[i]->GetNextPacket()) != 0 ) {
1901 break;
1904 return packet;
1907 CECPacket *ECNotifier::GetNextPacket(CECServerSocket *sock)
1910 // OnOutput is called for a first time before
1911 // socket is registered
1913 if ( m_msg_source.count(sock) ) {
1914 ECUpdateMsgSource **notifier_array = m_msg_source[sock];
1915 if ( !notifier_array ) {
1916 return 0;
1918 CECPacket *packet = GetNextPacket(notifier_array);
1919 printf("[EC] next update packet; opcode=%x\n",packet ? packet->GetOpCode() : 0xff);
1920 return packet;
1921 } else {
1922 return 0;
1927 // Interface to notification macros
1929 void ECNotifier::DownloadFile_SetDirty(CPartFile *file)
1931 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
1932 i != m_msg_source.end(); i++) {
1933 CECServerSocket *sock = i->first;
1934 if ( sock->HaveNotificationSupport() ) {
1935 ECUpdateMsgSource **notifier_array = i->second;
1936 ((ECPartFileMsgSource *)notifier_array[EC_PARTFILE])->SetDirty(file);
1939 NextPacketToSocket();
1942 void ECNotifier::DownloadFile_RemoveFile(CPartFile *file)
1944 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
1945 i != m_msg_source.end(); i++) {
1946 ECUpdateMsgSource **notifier_array = i->second;
1947 ((ECPartFileMsgSource *)notifier_array[EC_PARTFILE])->SetRemoved(file);
1949 NextPacketToSocket();
1952 void ECNotifier::DownloadFile_RemoveSource(CPartFile *)
1954 // per-partfile source list is not supported (yet), and IMHO quite useless
1957 void ECNotifier::DownloadFile_AddFile(CPartFile *file)
1959 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
1960 i != m_msg_source.end(); i++) {
1961 ECUpdateMsgSource **notifier_array = i->second;
1962 ((ECPartFileMsgSource *)notifier_array[EC_PARTFILE])->SetNew(file);
1964 NextPacketToSocket();
1967 void ECNotifier::DownloadFile_AddSource(CPartFile *)
1969 // per-partfile source list is not supported (yet), and IMHO quite useless
1972 void ECNotifier::SharedFile_AddFile(CKnownFile *file)
1974 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
1975 i != m_msg_source.end(); i++) {
1976 ECUpdateMsgSource **notifier_array = i->second;
1977 ((ECKnownFileMsgSource *)notifier_array[EC_KNOWN])->SetNew(file);
1979 NextPacketToSocket();
1982 void ECNotifier::SharedFile_RemoveFile(CKnownFile *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 ((ECKnownFileMsgSource *)notifier_array[EC_KNOWN])->SetRemoved(file);
1989 NextPacketToSocket();
1992 void ECNotifier::SharedFile_RemoveAllFiles()
1994 // need to figure out what to do here
1997 void ECNotifier::Add_EC_Client(CECServerSocket *sock)
1999 ECUpdateMsgSource **notifier_array = new ECUpdateMsgSource *[EC_STATUS_LAST_PRIO];
2000 notifier_array[EC_STATUS] = new ECStatusMsgSource();
2001 notifier_array[EC_SEARCH] = new ECSearchMsgSource();
2002 notifier_array[EC_PARTFILE] = new ECPartFileMsgSource();
2003 notifier_array[EC_CLIENT] = new ECClientMsgSource();
2004 notifier_array[EC_KNOWN] = new ECKnownFileMsgSource();
2006 m_msg_source[sock] = notifier_array;
2009 void ECNotifier::Remove_EC_Client(CECServerSocket *sock)
2011 if (m_msg_source.count(sock)) {
2012 ECUpdateMsgSource **notifier_array = m_msg_source[sock];
2014 m_msg_source.erase(sock);
2016 for(int i = 0; i < EC_STATUS_LAST_PRIO; i++) {
2017 delete notifier_array[i];
2019 delete [] notifier_array;
2023 void ECNotifier::NextPacketToSocket()
2025 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
2026 i != m_msg_source.end(); i++) {
2027 CECServerSocket *sock = i->first;
2028 if ( sock->HaveNotificationSupport() && !sock->DataPending() ) {
2029 ECUpdateMsgSource **notifier_array = i->second;
2030 CECPacket *packet = GetNextPacket(notifier_array);
2031 if ( packet ) {
2032 printf("[EC] sending update packet; opcode=%x\n",packet->GetOpCode());
2033 sock->SendPacket(packet);
2039 // File_checked_for_headers