Enable "Reset Log" button in the Remote GUI
[amule.git] / src / ExternalConn.cpp
blobfc27ebb9428e886e91da3885b4629b32c031484e
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();
207 void ResetLog() { m_LoggerAccess.Reset(); }
208 private:
209 ECNotifier *m_ec_notifier;
211 const CECPacket *Authenticate(const CECPacket *);
213 enum {
214 CONN_INIT,
215 CONN_SALT_SENT,
216 CONN_ESTABLISHED,
217 CONN_FAILED
218 } m_conn_state;
220 uint64_t m_passwd_salt;
221 CLoggerAccess m_LoggerAccess;
222 CFileEncoderMap m_FileEncoder;
223 CObjTagMap m_obj_tagmap;
224 CECPacket *ProcessRequest2(const CECPacket *request);
226 virtual bool IsAuthorized() { return m_conn_state == CONN_ESTABLISHED; }
230 CECServerSocket::CECServerSocket(ECNotifier *notifier)
232 CECMuleSocket(true),
233 m_conn_state(CONN_INIT),
234 m_passwd_salt(GetRandomUint64())
236 wxASSERT(theApp->ECServerHandler);
237 theApp->ECServerHandler->AddSocket(this);
238 m_ec_notifier = notifier;
242 CECServerSocket::~CECServerSocket()
244 wxASSERT(theApp->ECServerHandler);
245 theApp->ECServerHandler->RemoveSocket(this);
249 const CECPacket *CECServerSocket::OnPacketReceived(const CECPacket *packet, uint32 trueSize)
251 packet->DebugPrint(true, trueSize);
253 const CECPacket *reply = NULL;
255 if (m_conn_state == CONN_FAILED) {
256 // Client didn't close the socket when authentication failed.
257 AddLogLineN(_("Client sent packet after authentication failed."));
258 CloseSocket();
261 if (m_conn_state != CONN_ESTABLISHED) {
262 // This is called twice:
263 // 1) send salt
264 // 2) verify password
265 reply = Authenticate(packet);
266 } else {
267 reply = ProcessRequest2(packet);
269 return reply;
273 void CECServerSocket::OnLost()
275 AddLogLineN(_("External connection closed."));
276 theApp->ECServerHandler->m_ec_notifier->Remove_EC_Client(this);
277 DestroySocket();
280 void CECServerSocket::WriteDoneAndQueueEmpty()
282 if ( HaveNotificationSupport() && (m_conn_state == CONN_ESTABLISHED) ) {
283 CECPacket *packet = m_ec_notifier->GetNextPacket(this);
284 if ( packet ) {
285 SendPacket(packet);
287 } else {
288 //printf("[EC] %p: WriteDoneAndQueueEmpty but notification disabled\n", this);
292 //-------------------- ExternalConn --------------------
294 enum
295 { // id for sockets
296 SERVER_ID = 1000
300 BEGIN_EVENT_TABLE(ExternalConn, wxEvtHandler)
301 EVT_SOCKET(SERVER_ID, ExternalConn::OnServerEvent)
302 END_EVENT_TABLE()
305 ExternalConn::ExternalConn(amuleIPV4Address addr, wxString *msg)
307 wxString msgLocal;
308 m_ECServer = NULL;
309 // Are we allowed to accept External Connections?
310 if ( thePrefs::AcceptExternalConnections() ) {
311 // We must have a valid password, otherwise we will not allow EC connections
312 if (thePrefs::ECPassword().IsEmpty()) {
313 *msg += wxT("External connections disabled due to empty password!\n");
314 AddLogLineC(_("External connections disabled due to empty password!"));
315 return;
318 // Create the socket
319 m_ECServer = new wxSocketServer(addr, wxSOCKET_REUSEADDR);
320 m_ECServer->SetEventHandler(*this, SERVER_ID);
321 m_ECServer->SetNotify(wxSOCKET_CONNECTION_FLAG);
322 m_ECServer->Notify(true);
324 int port = addr.Service();
325 wxString ip = addr.IPAddress();
326 if (m_ECServer->Ok()) {
327 msgLocal = CFormat(wxT("*** TCP socket (ECServer) listening on %s:%d")) % ip % port;
328 *msg += msgLocal + wxT("\n");
329 AddLogLineN(msgLocal);
330 } else {
331 msgLocal = CFormat(wxT("Could not listen for external connections at %s:%d!")) % ip % port;
332 *msg += msgLocal + wxT("\n");
333 AddLogLineN(msgLocal);
335 } else {
336 *msg += wxT("External connections disabled in config file\n");
337 AddLogLineN(_("External connections disabled in config file"));
339 m_ec_notifier = new ECNotifier();
343 ExternalConn::~ExternalConn()
345 KillAllSockets();
346 delete m_ECServer;
347 delete m_ec_notifier;
351 void ExternalConn::AddSocket(CECServerSocket *s)
353 wxASSERT(s);
354 socket_list.insert(s);
358 void ExternalConn::RemoveSocket(CECServerSocket *s)
360 wxASSERT(s);
361 socket_list.erase(s);
365 void ExternalConn::KillAllSockets()
367 AddDebugLogLineN(logGeneral,
368 CFormat(wxT("ExternalConn::KillAllSockets(): %d sockets to destroy.")) %
369 socket_list.size());
370 SocketSet::iterator it = socket_list.begin();
371 while (it != socket_list.end()) {
372 CECServerSocket *s = *(it++);
373 s->Close();
374 s->Destroy();
379 void ExternalConn::ResetAllLogs()
381 SocketSet::iterator it = socket_list.begin();
382 while (it != socket_list.end()) {
383 CECServerSocket *s = *(it++);
384 s->ResetLog();
389 void ExternalConn::OnServerEvent(wxSocketEvent& WXUNUSED(event))
391 CECServerSocket *sock = new CECServerSocket(m_ec_notifier);
392 // Accept new connection if there is one in the pending
393 // connections queue, else exit. We use Accept(FALSE) for
394 // non-blocking accept (although if we got here, there
395 // should ALWAYS be a pending connection).
396 if ( m_ECServer->AcceptWith(*sock, false) ) {
397 AddLogLineN(_("New external connection accepted"));
398 } else {
399 delete sock;
400 AddLogLineN(_("ERROR: couldn't accept a new external connection"));
406 // Authentication
408 const CECPacket *CECServerSocket::Authenticate(const CECPacket *request)
410 CECPacket *response;
412 if (request == NULL) {
413 return new CECPacket(EC_OP_AUTH_FAIL);
416 // Password must be specified if we are to allow remote connections
417 if ( thePrefs::ECPassword().IsEmpty() ) {
418 AddLogLineC(_("External connection refused due to empty password in preferences!"));
420 return new CECPacket(EC_OP_AUTH_FAIL);
423 if ((m_conn_state == CONN_INIT) && (request->GetOpCode() == EC_OP_AUTH_REQ) ) {
424 const CECTag *clientName = request->GetTagByName(EC_TAG_CLIENT_NAME);
425 const CECTag *clientVersion = request->GetTagByName(EC_TAG_CLIENT_VERSION);
427 AddLogLineN(CFormat( _("Connecting client: %s %s") )
428 % ( clientName ? clientName->GetStringData() : wxString(_("Unknown")) )
429 % ( clientVersion ? clientVersion->GetStringData() : wxString(_("Unknown version")) ) );
430 const CECTag *protocol = request->GetTagByName(EC_TAG_PROTOCOL_VERSION);
431 #ifdef EC_VERSION_ID
432 // For SVN versions, both client and server must use SVNDATE, and they must be the same
433 CMD4Hash vhash;
434 if (!vhash.Decode(wxT(EC_VERSION_ID))) {
435 response = new CECPacket(EC_OP_AUTH_FAIL);
436 response->AddTag(CECTag(EC_TAG_STRING, wxT("Fatal error, version hash is not a valid MD4-hash.")));
437 } else if (!request->GetTagByName(EC_TAG_VERSION_ID) || request->GetTagByNameSafe(EC_TAG_VERSION_ID)->GetMD4Data() != vhash) {
438 response = new CECPacket(EC_OP_AUTH_FAIL);
439 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Incorrect EC version ID, there might be binary incompatibility. Use core and remote from same snapshot.")));
440 #else
441 // For release versions, we don't want to allow connections from any arbitrary SVN client.
442 if (request->GetTagByName(EC_TAG_VERSION_ID)) {
443 response = new CECPacket(EC_OP_AUTH_FAIL);
444 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("You cannot connect to a release version from an arbitrary development snapshot! *sigh* possible crash prevented")));
445 #endif
446 } else if (protocol != NULL) {
447 uint16 proto_version = protocol->GetInt();
448 if (proto_version == EC_CURRENT_PROTOCOL_VERSION) {
449 response = new CECPacket(EC_OP_AUTH_SALT);
450 response->AddTag(CECTag(EC_TAG_PASSWD_SALT, m_passwd_salt));
451 m_conn_state = CONN_SALT_SENT;
453 // So far ok, check capabilities of client
455 bool canZLIB = false, canUTF8numbers = false;
456 if (request->GetTagByName(EC_TAG_CAN_ZLIB)) {
457 canZLIB = true;
458 m_my_flags |= EC_FLAG_ZLIB;
460 if (request->GetTagByName(EC_TAG_CAN_UTF8_NUMBERS)) {
461 canUTF8numbers = true;
462 m_my_flags |= EC_FLAG_UTF8_NUMBERS;
464 m_haveNotificationSupport = request->GetTagByName(EC_TAG_CAN_NOTIFY) != NULL;
465 AddDebugLogLineN(logEC, CFormat(wxT("Client capabilities: ZLIB: %s UTF8 numbers: %s Push notification: %s") )
466 % (canZLIB ? wxT("yes") : wxT("no"))
467 % (canUTF8numbers ? wxT("yes") : wxT("no"))
468 % (m_haveNotificationSupport ? wxT("yes") : wxT("no")));
469 } else {
470 response = new CECPacket(EC_OP_AUTH_FAIL);
471 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid protocol version.")
472 + CFormat(wxT("( %i != %i )")) % proto_version % EC_CURRENT_PROTOCOL_VERSION));
474 } else {
475 response = new CECPacket(EC_OP_AUTH_FAIL);
476 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Missing protocol version tag.")));
478 } else if ((m_conn_state == CONN_SALT_SENT) && (request->GetOpCode() == EC_OP_AUTH_PASSWD)) {
479 const CECTag *passwd = request->GetTagByName(EC_TAG_PASSWD_HASH);
480 CMD4Hash passh;
482 if (!passh.Decode(thePrefs::ECPassword())) {
483 wxString err = wxTRANSLATE("Authentication failed: invalid hash specified as EC password.");
484 AddLogLineN(wxString(wxGetTranslation(err))
485 + wxT(" ") + thePrefs::ECPassword());
486 response = new CECPacket(EC_OP_AUTH_FAIL);
487 response->AddTag(CECTag(EC_TAG_STRING, err));
488 } else {
489 wxString saltHash = MD5Sum(CFormat(wxT("%lX")) % m_passwd_salt).GetHash();
490 wxString saltStr = CFormat(wxT("%lX")) % m_passwd_salt;
492 passh.Decode(MD5Sum(thePrefs::ECPassword().Lower() + saltHash).GetHash());
494 if (passwd && passwd->GetMD4Data() == passh) {
495 response = new CECPacket(EC_OP_AUTH_OK);
496 response->AddTag(CECTag(EC_TAG_SERVER_VERSION, wxT(VERSION)));
497 } else {
498 wxString err;
499 if (passwd) {
500 err = wxTRANSLATE("Authentication failed: wrong password.");
501 } else {
502 err = wxTRANSLATE("Authentication failed: missing password.");
505 response = new CECPacket(EC_OP_AUTH_FAIL);
506 response->AddTag(CECTag(EC_TAG_STRING, err));
507 AddLogLineN(wxGetTranslation(err));
510 } else {
511 response = new CECPacket(EC_OP_AUTH_FAIL);
512 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid request, please authenticate first.")));
515 if (response->GetOpCode() == EC_OP_AUTH_OK) {
516 m_conn_state = CONN_ESTABLISHED;
517 AddLogLineN(_("Access granted."));
518 // Establish notification handler if client supports it
519 if (HaveNotificationSupport()) {
520 theApp->ECServerHandler->m_ec_notifier->Add_EC_Client(this);
522 } else if (response->GetOpCode() == EC_OP_AUTH_FAIL) {
523 // Log message sent to client
524 if (response->GetFirstTagSafe()->IsString()) {
525 AddLogLineN(CFormat(_("Sent error message \"%s\" to client.")) % wxGetTranslation(response->GetFirstTagSafe()->GetStringData()));
527 // Access denied!
528 amuleIPV4Address address;
529 GetPeer(address);
530 AddLogLineN(CFormat(_("Unauthorized access attempt from %s. Connection closed.")) % address.IPAddress() );
531 m_conn_state = CONN_FAILED;
534 return response;
537 // Make a Logger tag (if there are any logging messages) and add it to the response
538 static void AddLoggerTag(CECPacket *response, CLoggerAccess &LoggerAccess)
540 if (LoggerAccess.HasString()) {
541 CECEmptyTag tag(EC_TAG_STATS_LOGGER_MESSAGE);
542 // Tag structure is fix: tag carries nothing, inside are the strings
543 // maximum of 200 log lines per message
544 int entries = 0;
545 wxString line;
546 while (entries < 200 && LoggerAccess.GetString(line)) {
547 tag.AddTag(CECTag(EC_TAG_STRING, line));
548 entries++;
550 response->AddTag(tag);
551 //printf("send Log tag %d %d\n", FirstEntry, entries);
555 static CECPacket *Get_EC_Response_StatRequest(const CECPacket *request, CLoggerAccess &LoggerAccess)
557 CECPacket *response = new CECPacket(EC_OP_STATS);
559 switch (request->GetDetailLevel()) {
560 case EC_DETAIL_FULL:
561 response->AddTag(CECTag(EC_TAG_STATS_UP_OVERHEAD, (uint32)theStats::GetUpOverheadRate()));
562 response->AddTag(CECTag(EC_TAG_STATS_DOWN_OVERHEAD, (uint32)theStats::GetDownOverheadRate()));
563 response->AddTag(CECTag(EC_TAG_STATS_BANNED_COUNT, /*(uint32)*/theStats::GetBannedCount()));
564 AddLoggerTag(response, LoggerAccess);
565 case EC_DETAIL_WEB:
566 case EC_DETAIL_CMD:
567 response->AddTag(CECTag(EC_TAG_STATS_UL_SPEED, (uint32)theStats::GetUploadRate()));
568 response->AddTag(CECTag(EC_TAG_STATS_DL_SPEED, (uint32)(theStats::GetDownloadRate())));
569 response->AddTag(CECTag(EC_TAG_STATS_UL_SPEED_LIMIT, (uint32)(thePrefs::GetMaxUpload()*1024.0)));
570 response->AddTag(CECTag(EC_TAG_STATS_DL_SPEED_LIMIT, (uint32)(thePrefs::GetMaxDownload()*1024.0)));
571 response->AddTag(CECTag(EC_TAG_STATS_UL_QUEUE_LEN, /*(uint32)*/theStats::GetWaitingUserCount()));
572 response->AddTag(CECTag(EC_TAG_STATS_TOTAL_SRC_COUNT, /*(uint32)*/theStats::GetFoundSources()));
573 // User/Filecounts
575 uint32 totaluser = 0, totalfile = 0;
576 theApp->serverlist->GetUserFileStatus( totaluser, totalfile );
577 response->AddTag(CECTag(EC_TAG_STATS_ED2K_USERS, totaluser));
578 response->AddTag(CECTag(EC_TAG_STATS_KAD_USERS, Kademlia::CKademlia::GetKademliaUsers()));
579 response->AddTag(CECTag(EC_TAG_STATS_ED2K_FILES, totalfile));
580 response->AddTag(CECTag(EC_TAG_STATS_KAD_FILES, Kademlia::CKademlia::GetKademliaFiles()));
582 // Kad stats
583 if (Kademlia::CKademlia::IsConnected()) {
584 response->AddTag(CECTag(EC_TAG_STATS_KAD_FIREWALLED_UDP, Kademlia::CUDPFirewallTester::IsFirewalledUDP(true)));
585 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_SOURCES, Kademlia::CKademlia::GetIndexed()->m_totalIndexSource));
586 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_KEYWORDS, Kademlia::CKademlia::GetIndexed()->m_totalIndexKeyword));
587 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_NOTES, Kademlia::CKademlia::GetIndexed()->m_totalIndexNotes));
588 response->AddTag(CECTag(EC_TAG_STATS_KAD_INDEXED_LOAD, Kademlia::CKademlia::GetIndexed()->m_totalIndexLoad));
589 response->AddTag(CECTag(EC_TAG_STATS_KAD_IP_ADRESS, wxUINT32_SWAP_ALWAYS(Kademlia::CKademlia::GetPrefs()->GetIPAddress())));
590 response->AddTag(CECTag(EC_TAG_STATS_KAD_IN_LAN_MODE, Kademlia::CKademlia::IsRunningInLANMode()));
591 response->AddTag(CECTag(EC_TAG_STATS_BUDDY_STATUS, theApp->clientlist->GetBuddyStatus()));
592 uint32 BuddyIP = 0;
593 uint16 BuddyPort = 0;
594 CUpDownClient * Buddy = theApp->clientlist->GetBuddy();
595 if (Buddy) {
596 BuddyIP = Buddy->GetIP();
597 BuddyPort = Buddy->GetUDPPort();
599 response->AddTag(CECTag(EC_TAG_STATS_BUDDY_IP, BuddyIP));
600 response->AddTag(CECTag(EC_TAG_STATS_BUDDY_PORT, BuddyPort));
602 case EC_DETAIL_UPDATE:
603 case EC_DETAIL_INC_UPDATE:
604 break;
607 return response;
610 static CECPacket *Get_EC_Response_GetSharedFiles(const CECPacket *request, CFileEncoderMap &encoders)
612 wxASSERT(request->GetOpCode() == EC_OP_GET_SHARED_FILES);
614 CECPacket *response = new CECPacket(EC_OP_SHARED_FILES);
616 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
618 // request can contain list of queried items
619 CTagSet<uint32, EC_TAG_KNOWNFILE> queryitems(request);
621 encoders.UpdateEncoders();
623 for (uint32 i = 0; i < theApp->sharedfiles->GetFileCount(); ++i) {
624 CKnownFile *cur_file = (CKnownFile *)theApp->sharedfiles->GetFileByIndex(i);
626 if ( !cur_file || (!queryitems.empty() && !queryitems.count(cur_file->ECID())) ) {
627 continue;
630 CEC_SharedFile_Tag filetag(cur_file, detail_level);
631 CKnownFile_Encoder *enc = encoders[cur_file->ECID()];
632 if ( detail_level != EC_DETAIL_UPDATE ) {
633 enc->ResetEncoder();
635 enc->Encode(&filetag);
636 response->AddTag(filetag);
638 return response;
641 static CECPacket *Get_EC_Response_GetUpdate(CFileEncoderMap &encoders, CObjTagMap &tagmap)
643 CECPacket *response = new CECPacket(EC_OP_SHARED_FILES);
645 encoders.UpdateEncoders();
646 for (CFileEncoderMap::iterator it = encoders.begin(); it != encoders.end(); ++it) {
647 const CKnownFile *cur_file = it->second->GetFile();
648 CValueMap &valuemap = tagmap.GetValueMap(cur_file->ECID());
649 if (cur_file->IsCPartFile()) {
650 CEC_PartFile_Tag filetag((const CPartFile*) cur_file, EC_DETAIL_INC_UPDATE, &valuemap);
651 // Add information if partfile is shared
652 filetag.AddTag(EC_TAG_PARTFILE_SHARED, it->second->IsShared(), &valuemap);
654 CPartFile_Encoder * enc = (CPartFile_Encoder *) encoders[cur_file->ECID()];
655 enc->Encode(&filetag);
656 response->AddTag(filetag);
657 } else {
658 CEC_SharedFile_Tag filetag(cur_file, EC_DETAIL_INC_UPDATE, &valuemap);
659 CKnownFile_Encoder * enc = encoders[cur_file->ECID()];
660 enc->Encode(&filetag);
661 response->AddTag(filetag);
664 // Add clients
665 CECEmptyTag clients(EC_TAG_CLIENT);
666 const CClientList::IDMap& clientList = theApp->clientlist->GetClientList();
667 for (CClientList::IDMap::const_iterator it = clientList.begin(); it != clientList.end(); it++) {
668 const CUpDownClient* cur_client = it->second;
669 CValueMap &valuemap = tagmap.GetValueMap(cur_client->ECID());
670 clients.AddTag(CEC_UpDownClient_Tag(cur_client, EC_DETAIL_INC_UPDATE, &valuemap));
672 response->AddTag(clients);
674 return response;
677 static CECPacket *Get_EC_Response_GetClientQueue(const CECPacket *request, CObjTagMap &tagmap, int op)
679 CECPacket *response = new CECPacket(op);
681 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
684 // request can contain list of queried items
685 // (not for incremental update of course)
686 CTagSet<uint32, EC_TAG_CLIENT> queryitems(request);
688 const CClientPtrList& clients = theApp->uploadqueue->GetUploadingList();
689 CClientPtrList::const_iterator it = clients.begin();
690 for (; it != clients.end(); ++it) {
691 CUpDownClient* cur_client = *it;
693 if (!cur_client) { // shouldn't happen
694 continue;
696 if (!queryitems.empty() && !queryitems.count(cur_client->ECID())) {
697 continue;
699 CValueMap *valuemap = NULL;
700 if (detail_level == EC_DETAIL_INC_UPDATE) {
701 valuemap = &tagmap.GetValueMap(cur_client->ECID());
703 CEC_UpDownClient_Tag cli_tag(cur_client, detail_level, valuemap);
705 response->AddTag(cli_tag);
708 return response;
712 static CECPacket *Get_EC_Response_GetDownloadQueue(const CECPacket *request, CFileEncoderMap &encoders)
714 CECPacket *response = new CECPacket(EC_OP_DLOAD_QUEUE);
716 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
718 // request can contain list of queried items
719 CTagSet<uint32, EC_TAG_PARTFILE> queryitems(request);
721 encoders.UpdateEncoders();
723 for (unsigned int i = 0; i < theApp->downloadqueue->GetFileCount(); i++) {
724 CPartFile *cur_file = theApp->downloadqueue->GetFileByIndex(i);
726 if ( !queryitems.empty() && !queryitems.count(cur_file->ECID()) ) {
727 continue;
730 CEC_PartFile_Tag filetag(cur_file, detail_level);
732 CPartFile_Encoder * enc = (CPartFile_Encoder *) encoders[cur_file->ECID()];
733 if ( detail_level != EC_DETAIL_UPDATE ) {
734 enc->ResetEncoder();
736 enc->Encode(&filetag);
738 response->AddTag(filetag);
740 return response;
744 static CECPacket *Get_EC_Response_PartFile_Cmd(const CECPacket *request)
746 CECPacket *response = NULL;
748 // request can contain multiple files.
749 for (CECPacket::const_iterator it1 = request->begin(); it1 != request->end(); it1++) {
750 const CECTag &hashtag = *it1;
752 wxASSERT(hashtag.GetTagName() == EC_TAG_PARTFILE);
754 CMD4Hash hash = hashtag.GetMD4Data();
755 CPartFile *pfile = theApp->downloadqueue->GetFileByID( hash );
757 if ( !pfile ) {
758 AddLogLineN(CFormat(_("Remote PartFile command failed: FileHash not found: %s")) % hash.Encode());
759 response = new CECPacket(EC_OP_FAILED);
760 response->AddTag(CECTag(EC_TAG_STRING, CFormat(wxString(wxTRANSLATE("FileHash not found: %s"))) % hash.Encode()));
761 //return response;
762 break;
764 switch (request->GetOpCode()) {
765 case EC_OP_PARTFILE_SWAP_A4AF_THIS:
766 if ((pfile->GetStatus(false) == PS_READY) ||
767 (pfile->GetStatus(false) == PS_EMPTY)) {
768 CKnownFile::SourceSet::const_iterator it = pfile->GetA4AFList().begin();
769 while ( it != pfile->GetA4AFList().end() ) {
770 CUpDownClient *cur_source = *it++;
772 cur_source->SwapToAnotherFile(true, false, false, pfile);
775 break;
776 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO:
777 pfile->SetA4AFAuto(!pfile->IsA4AFAuto());
778 break;
779 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS:
780 if ((pfile->GetStatus(false) == PS_READY) ||
781 (pfile->GetStatus(false) == PS_EMPTY)) {
782 CKnownFile::SourceSet::const_iterator it = pfile->GetSourceList().begin();
783 while ( it != pfile->GetSourceList().end() ) {
784 CUpDownClient* cur_source = *it++;
786 cur_source->SwapToAnotherFile(false, false, false, NULL);
789 break;
790 case EC_OP_PARTFILE_PAUSE:
791 pfile->PauseFile();
792 break;
793 case EC_OP_PARTFILE_RESUME:
794 pfile->ResumeFile();
795 pfile->SavePartFile();
796 break;
797 case EC_OP_PARTFILE_STOP:
798 pfile->StopFile();
799 break;
800 case EC_OP_PARTFILE_PRIO_SET: {
801 uint8 prio = hashtag.GetFirstTagSafe()->GetInt();
802 if ( prio == PR_AUTO ) {
803 pfile->SetAutoDownPriority(1);
804 } else {
805 pfile->SetAutoDownPriority(0);
806 pfile->SetDownPriority(prio);
809 break;
810 case EC_OP_PARTFILE_DELETE:
811 if ( thePrefs::StartNextFile() && (pfile->GetStatus() != PS_PAUSED) ) {
812 theApp->downloadqueue->StartNextFile(pfile);
814 pfile->Delete();
815 break;
817 case EC_OP_PARTFILE_SET_CAT:
818 pfile->SetCategory(hashtag.GetFirstTagSafe()->GetInt());
819 break;
821 default:
822 response = new CECPacket(EC_OP_FAILED);
823 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("OOPS! OpCode processing error!")));
824 break;
827 if (!response) {
828 response = new CECPacket(EC_OP_NOOP);
830 return response;
833 static CECPacket *Get_EC_Response_Server_Add(const CECPacket *request)
835 CECPacket *response = NULL;
837 wxString full_addr = request->GetTagByNameSafe(EC_TAG_SERVER_ADDRESS)->GetStringData();
838 wxString name = request->GetTagByNameSafe(EC_TAG_SERVER_NAME)->GetStringData();
840 wxString s_ip = full_addr.Left(full_addr.Find(':'));
841 wxString s_port = full_addr.Mid(full_addr.Find(':') + 1);
843 long port = StrToULong(s_port);
844 CServer* toadd = new CServer(port, s_ip);
845 toadd->SetListName(name.IsEmpty() ? full_addr : name);
847 if ( theApp->AddServer(toadd, true) ) {
848 response = new CECPacket(EC_OP_NOOP);
849 } else {
850 response = new CECPacket(EC_OP_FAILED);
851 response->AddTag(CECTag(EC_TAG_STRING, _("Server not added")));
852 delete toadd;
855 return response;
858 static CECPacket *Get_EC_Response_Server(const CECPacket *request)
860 CECPacket *response = NULL;
861 const CECTag *srv_tag = request->GetFirstTagSafe();
862 CServer *srv = 0;
863 if ( srv_tag ) {
864 srv = theApp->serverlist->GetServerByIPTCP(srv_tag->GetIPv4Data().IP(), srv_tag->GetIPv4Data().m_port);
865 // server tag passed, but server not found
866 if ( !srv ) {
867 response = new CECPacket(EC_OP_FAILED);
868 response->AddTag(CECTag(EC_TAG_STRING,
869 CFormat(wxString(wxTRANSLATE("server not found: %s"))) % srv_tag->GetIPv4Data().StringIP()));
870 return response;
873 switch (request->GetOpCode()) {
874 case EC_OP_SERVER_DISCONNECT:
875 theApp->serverconnect->Disconnect();
876 response = new CECPacket(EC_OP_NOOP);
877 break;
878 case EC_OP_SERVER_REMOVE:
879 if ( srv ) {
880 theApp->serverlist->RemoveServer(srv);
881 response = new CECPacket(EC_OP_NOOP);
882 } else {
883 response = new CECPacket(EC_OP_FAILED);
884 response->AddTag(CECTag(EC_TAG_STRING,
885 wxTRANSLATE("need to define server to be removed")));
887 break;
888 case EC_OP_SERVER_CONNECT:
889 if (thePrefs::GetNetworkED2K()) {
890 if ( srv ) {
891 theApp->serverconnect->ConnectToServer(srv);
892 response = new CECPacket(EC_OP_NOOP);
893 } else {
894 theApp->serverconnect->ConnectToAnyServer();
895 response = new CECPacket(EC_OP_NOOP);
897 } else {
898 response = new CECPacket(EC_OP_FAILED);
899 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("eD2k is disabled in preferences.")));
901 break;
903 if (!response) {
904 response = new CECPacket(EC_OP_FAILED);
905 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("OOPS! OpCode processing error!")));
907 return response;
910 static CECPacket *Get_EC_Response_Search_Results(const CECPacket *request)
912 CECPacket *response = new CECPacket(EC_OP_SEARCH_RESULTS);
914 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
916 // request can contain list of queried items
917 CTagSet<uint32, EC_TAG_SEARCHFILE> queryitems(request);
919 const CSearchResultList& list = theApp->searchlist->GetSearchResults(0xffffffff);
920 CSearchResultList::const_iterator it = list.begin();
921 while (it != list.end()) {
922 CSearchFile* sf = *it++;
923 if ( !queryitems.empty() && !queryitems.count(sf->ECID()) ) {
924 continue;
926 response->AddTag(CEC_SearchFile_Tag(sf, detail_level));
928 return response;
931 static CECPacket *Get_EC_Response_Search_Results(CObjTagMap &tagmap)
933 CECPacket *response = new CECPacket(EC_OP_SEARCH_RESULTS);
935 const CSearchResultList& list = theApp->searchlist->GetSearchResults(0xffffffff);
936 CSearchResultList::const_iterator it = list.begin();
937 while (it != list.end()) {
938 CSearchFile* sf = *it++;
939 CValueMap &valuemap = tagmap.GetValueMap(sf->ECID());
940 response->AddTag(CEC_SearchFile_Tag(sf, EC_DETAIL_INC_UPDATE, &valuemap));
941 /* Here we could add the children, if amulegui were able to merge them.
942 if (sf->HasChildren()) {
943 const CSearchResultList& children = sf->GetChildren();
944 for (size_t i = 0; i < children.size(); ++i) {
945 CSearchFile* sfc = children.at(i);
946 CValueMap &valuemap1 = tagmap.GetValueMap(sfc->ECID());
947 response->AddTag(CEC_SearchFile_Tag(sfc, EC_DETAIL_INC_UPDATE, &valuemap1));
952 return response;
955 static CECPacket *Get_EC_Response_Search_Results_Download(const CECPacket *request)
957 CECPacket *response = new CECPacket(EC_OP_STRINGS);
958 for (CECPacket::const_iterator it = request->begin(); it != request->end(); it++) {
959 const CECTag &tag = *it;
960 CMD4Hash hash = tag.GetMD4Data();
961 uint8 category = tag.GetFirstTagSafe()->GetInt();
962 theApp->searchlist->AddFileToDownloadByHash(hash, category);
964 return response;
967 static CECPacket *Get_EC_Response_Search_Stop(const CECPacket *WXUNUSED(request))
969 CECPacket *reply = new CECPacket(EC_OP_MISC_DATA);
970 theApp->searchlist->StopGlobalSearch();
971 return reply;
974 static CECPacket *Get_EC_Response_Search(const CECPacket *request)
976 wxString response;
978 CEC_Search_Tag *search_request = (CEC_Search_Tag *)request->GetFirstTagSafe();
979 theApp->searchlist->RemoveResults(0xffffffff);
981 CSearchList::CSearchParams params;
982 params.searchString = search_request->SearchText();
983 params.typeText = search_request->SearchFileType();
984 params.extension = search_request->SearchExt();
985 params.minSize = search_request->MinSize();
986 params.maxSize = search_request->MaxSize();
987 params.availability = search_request->Avail();
990 EC_SEARCH_TYPE search_type = search_request->SearchType();
991 SearchType core_search_type = LocalSearch;
992 uint32 op = EC_OP_FAILED;
993 switch (search_type) {
994 case EC_SEARCH_GLOBAL:
995 core_search_type = GlobalSearch;
996 case EC_SEARCH_KAD:
997 if (core_search_type != GlobalSearch) { // Not a global search obviously
998 core_search_type = KadSearch;
1000 case EC_SEARCH_LOCAL: {
1001 uint32 search_id = 0xffffffff;
1002 wxString error = theApp->searchlist->StartNewSearch(&search_id, core_search_type, params);
1003 if (!error.IsEmpty()) {
1004 response = error;
1005 } else {
1006 response = wxTRANSLATE("Search in progress. Refetch results in a moment!");
1007 op = EC_OP_STRINGS;
1009 break;
1011 case EC_SEARCH_WEB:
1012 response = wxTRANSLATE("WebSearch from remote interface makes no sense.");
1013 break;
1016 CECPacket *reply = new CECPacket(op);
1017 // error or search in progress
1018 reply->AddTag(CECTag(EC_TAG_STRING, response));
1020 return reply;
1023 static CECPacket *Get_EC_Response_Set_SharedFile_Prio(const CECPacket *request)
1025 CECPacket *response = new CECPacket(EC_OP_NOOP);
1026 for (CECPacket::const_iterator it = request->begin(); it != request->end(); it++) {
1027 const CECTag &tag = *it;
1028 CMD4Hash hash = tag.GetMD4Data();
1029 uint8 prio = tag.GetFirstTagSafe()->GetInt();
1030 CKnownFile* cur_file = theApp->sharedfiles->GetFileByID(hash);
1031 if ( !cur_file ) {
1032 continue;
1034 if (prio == PR_AUTO) {
1035 cur_file->SetAutoUpPriority(1);
1036 cur_file->UpdateAutoUpPriority();
1037 } else {
1038 cur_file->SetAutoUpPriority(0);
1039 cur_file->SetUpPriority(prio);
1041 Notify_SharedFilesUpdateItem(cur_file);
1044 return response;
1047 void CPartFile_Encoder::Encode(CECTag *parent)
1050 // Source part frequencies
1052 CKnownFile_Encoder::Encode(parent);
1055 // Gaps
1057 const CGapList& gaplist = m_PartFile()->GetGapList();
1058 const size_t gap_list_size = gaplist.size();
1059 ArrayOfUInts64 gaps;
1060 gaps.reserve(gap_list_size * 2);
1062 for (CGapList::const_iterator curr_pos = gaplist.begin();
1063 curr_pos != gaplist.end(); ++curr_pos) {
1064 gaps.push_back(curr_pos.start());
1065 gaps.push_back(curr_pos.end());
1068 int gap_enc_size = 0;
1069 bool changed;
1070 const uint8 *gap_enc_data = m_gap_status.Encode(gaps, gap_enc_size, changed);
1071 if (changed) {
1072 parent->AddTag(CECTag(EC_TAG_PARTFILE_GAP_STATUS, gap_enc_size, (void *)gap_enc_data));
1074 delete[] gap_enc_data;
1077 // Requested blocks
1079 ArrayOfUInts64 req_buffer;
1080 const CPartFile::CReqBlockPtrList& requestedblocks = m_PartFile()->GetRequestedBlockList();
1081 CPartFile::CReqBlockPtrList::const_iterator curr_pos2 = requestedblocks.begin();
1083 for ( ; curr_pos2 != requestedblocks.end(); ++curr_pos2 ) {
1084 Requested_Block_Struct* block = *curr_pos2;
1085 req_buffer.push_back(block->StartOffset);
1086 req_buffer.push_back(block->EndOffset);
1088 int req_enc_size = 0;
1089 const uint8 *req_enc_data = m_req_status.Encode(req_buffer, req_enc_size, changed);
1090 if (changed) {
1091 parent->AddTag(CECTag(EC_TAG_PARTFILE_REQ_STATUS, req_enc_size, (void *)req_enc_data));
1093 delete[] req_enc_data;
1096 // Source names
1098 // First count occurrence of all source names
1100 CECEmptyTag sourceNames(EC_TAG_PARTFILE_SOURCE_NAMES);
1101 typedef std::map<wxString, int> strIntMap;
1102 strIntMap nameMap;
1103 const CPartFile::SourceSet &sources = m_PartFile()->GetSourceList();
1104 for (CPartFile::SourceSet::const_iterator it = sources.begin(); it != sources.end(); ++it) {
1105 CUpDownClient *cur_src = *it;
1106 if (cur_src->GetRequestFile() != m_file || cur_src->GetClientFilename().Length() == 0) {
1107 continue;
1109 const wxString &name = cur_src->GetClientFilename();
1110 strIntMap::iterator itm = nameMap.find(name);
1111 if (itm == nameMap.end()) {
1112 nameMap[name] = 1;
1113 } else {
1114 itm->second++;
1118 // Go through our last list
1120 for (SourcenameItemMap::iterator it1 = m_sourcenameItemMap.begin(); it1 != m_sourcenameItemMap.end();) {
1121 SourcenameItemMap::iterator it2 = it1++;
1122 strIntMap::iterator itm = nameMap.find(it2->second.name);
1123 if (itm == nameMap.end()) {
1124 // name doesn't exist anymore, tell client to forget it
1125 CECTag tag(EC_TAG_PARTFILE_SOURCE_NAMES, it2->first);
1126 tag.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS, 0));
1127 sourceNames.AddTag(tag);
1128 // and forget it
1129 m_sourcenameItemMap.erase(it2);
1130 } else {
1131 // update count if it changed
1132 if (it2->second.count != itm->second) {
1133 CECTag tag(EC_TAG_PARTFILE_SOURCE_NAMES, it2->first);
1134 tag.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS, itm->second));
1135 sourceNames.AddTag(tag);
1136 it2->second.count = itm->second;
1138 // remove it from nameMap so that only new names are left there
1139 nameMap.erase(itm);
1143 // Add new names
1145 for (strIntMap::iterator it3 = nameMap.begin(); it3 != nameMap.end(); it3++) {
1146 int id = ++m_sourcenameID;
1147 CECIntTag tag(EC_TAG_PARTFILE_SOURCE_NAMES, id);
1148 tag.AddTag(CECTag(EC_TAG_PARTFILE_SOURCE_NAMES, it3->first));
1149 tag.AddTag(CECIntTag(EC_TAG_PARTFILE_SOURCE_NAMES_COUNTS, it3->second));
1150 sourceNames.AddTag(tag);
1151 // remember it
1152 m_sourcenameItemMap[id] = SourcenameItem(it3->first, it3->second);
1154 if (sourceNames.HasChildTags()) {
1155 parent->AddTag(sourceNames);
1160 void CPartFile_Encoder::ResetEncoder()
1162 CKnownFile_Encoder::ResetEncoder();
1163 m_gap_status.ResetEncoder();
1164 m_req_status.ResetEncoder();
1167 void CKnownFile_Encoder::Encode(CECTag *parent)
1170 // Source part frequencies
1172 // Reference to the availability list
1173 const ArrayOfUInts16& list = m_file->IsPartFile() ?
1174 ((CPartFile*)m_file)->m_SrcpartFrequency :
1175 m_file->m_AvailPartFrequency;
1176 // Don't add tag if available parts aren't populated yet.
1177 if (!list.empty()) {
1178 int part_enc_size;
1179 bool changed;
1180 const uint8 *part_enc_data = m_enc_data.Encode(list, part_enc_size, changed);
1181 if (changed) {
1182 parent->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS, part_enc_size, part_enc_data));
1184 delete[] part_enc_data;
1188 static CECPacket *GetStatsGraphs(const CECPacket *request)
1190 CECPacket *response = NULL;
1192 switch (request->GetDetailLevel()) {
1193 case EC_DETAIL_WEB:
1194 case EC_DETAIL_FULL: {
1195 double dTimestamp = 0.0;
1196 if (request->GetTagByName(EC_TAG_STATSGRAPH_LAST) != NULL) {
1197 dTimestamp = request->GetTagByName(EC_TAG_STATSGRAPH_LAST)->GetDoubleData();
1199 uint16 nScale = request->GetTagByNameSafe(EC_TAG_STATSGRAPH_SCALE)->GetInt();
1200 uint16 nMaxPoints = request->GetTagByNameSafe(EC_TAG_STATSGRAPH_WIDTH)->GetInt();
1201 uint32 *graphData;
1202 unsigned int numPoints = theApp->m_statistics->GetHistoryForWeb(nMaxPoints, (double)nScale, &dTimestamp, &graphData);
1203 if (numPoints) {
1204 response = new CECPacket(EC_OP_STATSGRAPHS);
1205 response->AddTag(CECTag(EC_TAG_STATSGRAPH_DATA, 4 * numPoints * sizeof(uint32), graphData));
1206 delete [] graphData;
1207 response->AddTag(CECTag(EC_TAG_STATSGRAPH_LAST, dTimestamp));
1208 } else {
1209 response = new CECPacket(EC_OP_FAILED);
1210 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("No points for graph.")));
1212 break;
1214 case EC_DETAIL_INC_UPDATE:
1215 case EC_DETAIL_UPDATE:
1216 case EC_DETAIL_CMD:
1217 // No graphs
1218 response = new CECPacket(EC_OP_FAILED);
1219 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Your client is not configured for this detail level.")));
1220 break;
1222 if (!response) {
1223 response = new CECPacket(EC_OP_FAILED);
1224 // Unknown reason
1227 return response;
1230 CECPacket *CECServerSocket::ProcessRequest2(const CECPacket *request)
1233 if ( !request ) {
1234 return 0;
1237 CECPacket *response = NULL;
1239 switch (request->GetOpCode()) {
1241 // Misc commands
1243 case EC_OP_SHUTDOWN:
1244 if (!theApp->IsOnShutDown()) {
1245 response = new CECPacket(EC_OP_NOOP);
1246 AddLogLineC(_("External Connection: shutdown requested"));
1247 #ifndef AMULE_DAEMON
1249 wxCloseEvent evt;
1250 evt.SetCanVeto(false);
1251 theApp->ShutDown(evt);
1253 #else
1254 theApp->ExitMainLoop();
1255 #endif
1256 } else {
1257 response = new CECPacket(EC_OP_FAILED);
1258 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Already shutting down.")));
1260 break;
1261 case EC_OP_ADD_LINK:
1262 for (CECPacket::const_iterator it = request->begin(); it != request->end(); it++) {
1263 const CECTag &tag = *it;
1264 wxString link = tag.GetStringData();
1265 int category = 0;
1266 const CECTag *cattag = tag.GetTagByName(EC_TAG_PARTFILE_CAT);
1267 if (cattag) {
1268 category = cattag->GetInt();
1270 AddLogLineC(CFormat(_("ExternalConn: adding link '%s'.")) % link);
1271 if ( theApp->downloadqueue->AddLink(link, category) ) {
1272 response = new CECPacket(EC_OP_NOOP);
1273 } else {
1274 // Error messages are printed by the add function.
1275 response = new CECPacket(EC_OP_FAILED);
1276 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid link or already on list.")));
1279 break;
1281 // Status requests
1283 case EC_OP_STAT_REQ:
1284 response = Get_EC_Response_StatRequest(request, m_LoggerAccess);
1285 response->AddTag(CEC_ConnState_Tag(request->GetDetailLevel()));
1286 break;
1287 case EC_OP_GET_CONNSTATE:
1288 response = new CECPacket(EC_OP_MISC_DATA);
1289 response->AddTag(CEC_ConnState_Tag(request->GetDetailLevel()));
1290 break;
1294 case EC_OP_GET_SHARED_FILES:
1295 if ( request->GetDetailLevel() != EC_DETAIL_INC_UPDATE ) {
1296 response = Get_EC_Response_GetSharedFiles(request, m_FileEncoder);
1298 break;
1299 case EC_OP_GET_DLOAD_QUEUE:
1300 if ( request->GetDetailLevel() != EC_DETAIL_INC_UPDATE ) {
1301 response = Get_EC_Response_GetDownloadQueue(request, m_FileEncoder);
1303 break;
1305 // This will evolve into an update-all for inc tags
1307 case EC_OP_GET_UPDATE:
1308 if ( request->GetDetailLevel() == EC_DETAIL_INC_UPDATE ) {
1309 response = Get_EC_Response_GetUpdate(m_FileEncoder, m_obj_tagmap);
1311 break;
1312 case EC_OP_GET_ULOAD_QUEUE:
1313 response = Get_EC_Response_GetClientQueue(request, m_obj_tagmap, EC_OP_ULOAD_QUEUE);
1314 break;
1315 case EC_OP_PARTFILE_REMOVE_NO_NEEDED:
1316 case EC_OP_PARTFILE_REMOVE_FULL_QUEUE:
1317 case EC_OP_PARTFILE_REMOVE_HIGH_QUEUE:
1318 case EC_OP_PARTFILE_CLEANUP_SOURCES:
1319 case EC_OP_PARTFILE_SWAP_A4AF_THIS:
1320 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO:
1321 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS:
1322 case EC_OP_PARTFILE_PAUSE:
1323 case EC_OP_PARTFILE_RESUME:
1324 case EC_OP_PARTFILE_STOP:
1325 case EC_OP_PARTFILE_PRIO_SET:
1326 case EC_OP_PARTFILE_DELETE:
1327 case EC_OP_PARTFILE_SET_CAT:
1328 response = Get_EC_Response_PartFile_Cmd(request);
1329 break;
1330 case EC_OP_SHAREDFILES_RELOAD:
1331 theApp->sharedfiles->Reload();
1332 response = new CECPacket(EC_OP_NOOP);
1333 break;
1334 case EC_OP_SHARED_SET_PRIO:
1335 response = Get_EC_Response_Set_SharedFile_Prio(request);
1336 break;
1337 case EC_OP_RENAME_FILE: {
1338 CMD4Hash fileHash = request->GetTagByNameSafe(EC_TAG_KNOWNFILE)->GetMD4Data();
1339 wxString newName = request->GetTagByNameSafe(EC_TAG_PARTFILE_NAME)->GetStringData();
1340 // search first in downloadqueue - it might be in known files as well
1341 CKnownFile* file = theApp->downloadqueue->GetFileByID(fileHash);
1342 if (!file) {
1343 file = theApp->knownfiles->FindKnownFileByID(fileHash);
1345 if (!file) {
1346 response = new CECPacket(EC_OP_FAILED);
1347 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("File not found.")));
1348 break;
1350 if (newName.IsEmpty()) {
1351 response = new CECPacket(EC_OP_FAILED);
1352 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid file name.")));
1353 break;
1356 if (theApp->sharedfiles->RenameFile(file, CPath(newName))) {
1357 response = new CECPacket(EC_OP_NOOP);
1358 } else {
1359 response = new CECPacket(EC_OP_FAILED);
1360 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Unable to rename file.")));
1363 break;
1368 // Server commands
1370 case EC_OP_SERVER_ADD:
1371 response = Get_EC_Response_Server_Add(request);
1372 break;
1373 case EC_OP_SERVER_DISCONNECT:
1374 case EC_OP_SERVER_CONNECT:
1375 case EC_OP_SERVER_REMOVE:
1376 response = Get_EC_Response_Server(request);
1377 break;
1378 case EC_OP_GET_SERVER_LIST: {
1379 response = new CECPacket(EC_OP_SERVER_LIST);
1380 if (!thePrefs::GetNetworkED2K()) {
1381 // Kad only: just send an empty list
1382 break;
1384 EC_DETAIL_LEVEL detail_level = request->GetDetailLevel();
1385 std::vector<const CServer*> servers = theApp->serverlist->CopySnapshot();
1386 for (
1387 std::vector<const CServer*>::const_iterator it = servers.begin();
1388 it != servers.end();
1389 ++it
1391 response->AddTag(CEC_Server_Tag(*it, detail_level));
1394 break;
1395 case EC_OP_SERVER_UPDATE_FROM_URL: {
1396 wxString url = request->GetFirstTagSafe()->GetStringData();
1398 // Save the new url, and update the UI (if not amuled).
1399 Notify_ServersURLChanged(url);
1400 thePrefs::SetEd2kServersUrl(url);
1402 theApp->serverlist->UpdateServerMetFromURL(url);
1403 response = new CECPacket(EC_OP_NOOP);
1404 break;
1407 // IPFilter
1409 case EC_OP_IPFILTER_RELOAD:
1410 theApp->ipfilter->Reload();
1411 response = new CECPacket(EC_OP_NOOP);
1412 break;
1414 case EC_OP_IPFILTER_UPDATE: {
1415 wxString url = request->GetFirstTagSafe()->GetStringData();
1416 if (url.IsEmpty()) {
1417 url = thePrefs::IPFilterURL();
1419 theApp->ipfilter->Update(url);
1420 response = new CECPacket(EC_OP_NOOP);
1421 break;
1424 // Search
1426 case EC_OP_SEARCH_START:
1427 response = Get_EC_Response_Search(request);
1428 break;
1430 case EC_OP_SEARCH_STOP:
1431 response = Get_EC_Response_Search_Stop(request);
1432 break;
1434 case EC_OP_SEARCH_RESULTS:
1435 if ( request->GetDetailLevel() == EC_DETAIL_INC_UPDATE ) {
1436 response = Get_EC_Response_Search_Results(m_obj_tagmap);
1437 } else {
1438 response = Get_EC_Response_Search_Results(request);
1440 break;
1442 case EC_OP_SEARCH_PROGRESS:
1443 response = new CECPacket(EC_OP_SEARCH_PROGRESS);
1444 response->AddTag(CECTag(EC_TAG_SEARCH_STATUS,
1445 theApp->searchlist->GetSearchProgress()));
1446 break;
1448 case EC_OP_DOWNLOAD_SEARCH_RESULT:
1449 response = Get_EC_Response_Search_Results_Download(request);
1450 break;
1452 // Preferences
1454 case EC_OP_GET_PREFERENCES:
1455 response = new CEC_Prefs_Packet(request->GetTagByNameSafe(EC_TAG_SELECT_PREFS)->GetInt(), request->GetDetailLevel());
1456 break;
1457 case EC_OP_SET_PREFERENCES:
1458 ((CEC_Prefs_Packet *)request)->Apply();
1459 theApp->glob_prefs->Save();
1460 if (thePrefs::IsFilteringClients()) {
1461 theApp->clientlist->FilterQueues();
1463 if (thePrefs::IsFilteringServers()) {
1464 theApp->serverlist->FilterServers();
1466 if (!thePrefs::GetNetworkED2K() && theApp->IsConnectedED2K()) {
1467 theApp->DisconnectED2K();
1469 if (!thePrefs::GetNetworkKademlia() && theApp->IsConnectedKad()) {
1470 theApp->StopKad();
1472 response = new CECPacket(EC_OP_NOOP);
1473 break;
1475 case EC_OP_CREATE_CATEGORY:
1476 if ( request->GetTagCount() == 1 ) {
1477 CEC_Category_Tag *tag = (CEC_Category_Tag *)request->GetFirstTagSafe();
1478 if (tag->Create()) {
1479 response = new CECPacket(EC_OP_NOOP);
1480 } else {
1481 response = new CECPacket(EC_OP_FAILED);
1482 response->AddTag(CECTag(EC_TAG_CATEGORY, theApp->glob_prefs->GetCatCount() - 1));
1483 response->AddTag(CECTag(EC_TAG_CATEGORY_PATH, tag->Path()));
1485 Notify_CategoryAdded();
1486 } else {
1487 response = new CECPacket(EC_OP_NOOP);
1489 break;
1490 case EC_OP_UPDATE_CATEGORY:
1491 if ( request->GetTagCount() == 1 ) {
1492 CEC_Category_Tag *tag = (CEC_Category_Tag *)request->GetFirstTagSafe();
1493 if (tag->Apply()) {
1494 response = new CECPacket(EC_OP_NOOP);
1495 } else {
1496 response = new CECPacket(EC_OP_FAILED);
1497 response->AddTag(CECTag(EC_TAG_CATEGORY, tag->GetInt()));
1498 response->AddTag(CECTag(EC_TAG_CATEGORY_PATH, tag->Path()));
1500 Notify_CategoryUpdate(tag->GetInt());
1501 } else {
1502 response = new CECPacket(EC_OP_NOOP);
1504 break;
1505 case EC_OP_DELETE_CATEGORY:
1506 if ( request->GetTagCount() == 1 ) {
1507 uint32 cat = request->GetFirstTagSafe()->GetInt();
1508 // this noes not only update the gui, but actually deletes the cat
1509 Notify_CategoryDelete(cat);
1511 response = new CECPacket(EC_OP_NOOP);
1512 break;
1515 // Logging
1517 case EC_OP_ADDLOGLINE:
1518 if (request->GetTagByName(EC_TAG_LOG_TO_STATUS) != NULL) {
1519 AddLogLineC(request->GetTagByNameSafe(EC_TAG_STRING)->GetStringData());
1520 } else {
1521 AddLogLineN(request->GetTagByNameSafe(EC_TAG_STRING)->GetStringData());
1523 response = new CECPacket(EC_OP_NOOP);
1524 break;
1525 case EC_OP_ADDDEBUGLOGLINE:
1526 if (request->GetTagByName(EC_TAG_LOG_TO_STATUS) != NULL) {
1527 AddDebugLogLineC(logGeneral, request->GetTagByNameSafe(EC_TAG_STRING)->GetStringData());
1528 } else {
1529 AddDebugLogLineN(logGeneral, request->GetTagByNameSafe(EC_TAG_STRING)->GetStringData());
1531 response = new CECPacket(EC_OP_NOOP);
1532 break;
1533 case EC_OP_GET_LOG:
1534 response = new CECPacket(EC_OP_LOG);
1535 response->AddTag(CECTag(EC_TAG_STRING, theApp->GetLog(false)));
1536 break;
1537 case EC_OP_GET_DEBUGLOG:
1538 response = new CECPacket(EC_OP_DEBUGLOG);
1539 response->AddTag(CECTag(EC_TAG_STRING, theApp->GetDebugLog(false)));
1540 break;
1541 case EC_OP_RESET_LOG:
1542 theApp->GetLog(true);
1543 response = new CECPacket(EC_OP_NOOP);
1544 break;
1545 case EC_OP_RESET_DEBUGLOG:
1546 theApp->GetDebugLog(true);
1547 response = new CECPacket(EC_OP_NOOP);
1548 break;
1549 case EC_OP_GET_LAST_LOG_ENTRY:
1551 wxString tmp = theApp->GetLog(false);
1552 if (tmp.Last() == '\n') {
1553 tmp.RemoveLast();
1555 response = new CECPacket(EC_OP_LOG);
1556 response->AddTag(CECTag(EC_TAG_STRING, tmp.AfterLast('\n')));
1558 break;
1559 case EC_OP_GET_SERVERINFO:
1560 response = new CECPacket(EC_OP_SERVERINFO);
1561 response->AddTag(CECTag(EC_TAG_STRING, theApp->GetServerLog(false)));
1562 break;
1563 case EC_OP_CLEAR_SERVERINFO:
1564 theApp->GetServerLog(true);
1565 response = new CECPacket(EC_OP_NOOP);
1566 break;
1568 // Statistics
1570 case EC_OP_GET_STATSGRAPHS:
1571 response = GetStatsGraphs(request);
1572 break;
1573 case EC_OP_GET_STATSTREE: {
1574 theApp->m_statistics->UpdateStatsTree();
1575 response = new CECPacket(EC_OP_STATSTREE);
1576 CECTag* tree = theStats::GetECStatTree(request->GetTagByNameSafe(EC_TAG_STATTREE_CAPPING)->GetInt());
1577 if (tree) {
1578 response->AddTag(*tree);
1579 delete tree;
1581 if (request->GetDetailLevel() == EC_DETAIL_WEB) {
1582 response->AddTag(CECTag(EC_TAG_SERVER_VERSION, wxT(VERSION)));
1583 response->AddTag(CECTag(EC_TAG_USER_NICK, thePrefs::GetUserNick()));
1585 break;
1589 // Kad
1591 case EC_OP_KAD_START:
1592 if (thePrefs::GetNetworkKademlia()) {
1593 response = new CECPacket(EC_OP_NOOP);
1594 if ( !Kademlia::CKademlia::IsRunning() ) {
1595 Kademlia::CKademlia::Start();
1596 theApp->ShowConnectionState();
1598 } else {
1599 response = new CECPacket(EC_OP_FAILED);
1600 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Kad is disabled in preferences.")));
1602 break;
1603 case EC_OP_KAD_STOP:
1604 theApp->StopKad();
1605 theApp->ShowConnectionState();
1606 response = new CECPacket(EC_OP_NOOP);
1607 break;
1608 case EC_OP_KAD_UPDATE_FROM_URL: {
1609 wxString url = request->GetFirstTagSafe()->GetStringData();
1611 // Save the new url, and update the UI (if not amuled).
1612 Notify_NodesURLChanged(url);
1613 thePrefs::SetKadNodesUrl(url);
1615 theApp->UpdateNotesDat(url);
1616 response = new CECPacket(EC_OP_NOOP);
1617 break;
1619 case EC_OP_KAD_BOOTSTRAP_FROM_IP:
1620 if (thePrefs::GetNetworkKademlia()) {
1621 theApp->BootstrapKad(request->GetTagByNameSafe(EC_TAG_BOOTSTRAP_IP)->GetInt(),
1622 request->GetTagByNameSafe(EC_TAG_BOOTSTRAP_PORT)->GetInt());
1623 theApp->ShowConnectionState();
1624 response = new CECPacket(EC_OP_NOOP);
1625 } else {
1626 response = new CECPacket(EC_OP_FAILED);
1627 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Kad is disabled in preferences.")));
1629 break;
1632 // Networks
1633 // These requests are currently used only in the text client
1635 case EC_OP_CONNECT:
1636 if (thePrefs::GetNetworkED2K()) {
1637 response = new CECPacket(EC_OP_STRINGS);
1638 if (theApp->IsConnectedED2K()) {
1639 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Already connected to eD2k.")));
1640 } else {
1641 theApp->serverconnect->ConnectToAnyServer();
1642 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Connecting to eD2k...")));
1645 if (thePrefs::GetNetworkKademlia()) {
1646 if (!response) {
1647 response = new CECPacket(EC_OP_STRINGS);
1649 if (theApp->IsConnectedKad()) {
1650 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Already connected to Kad.")));
1651 } else {
1652 theApp->StartKad();
1653 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Connecting to Kad...")));
1656 if (response) {
1657 theApp->ShowConnectionState();
1658 } else {
1659 response = new CECPacket(EC_OP_FAILED);
1660 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("All networks are disabled.")));
1662 break;
1663 case EC_OP_DISCONNECT:
1664 if (theApp->IsConnected()) {
1665 response = new CECPacket(EC_OP_STRINGS);
1666 if (theApp->IsConnectedED2K()) {
1667 theApp->serverconnect->Disconnect();
1668 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Disconnected from eD2k.")));
1670 if (theApp->IsConnectedKad()) {
1671 theApp->StopKad();
1672 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Disconnected from Kad.")));
1674 theApp->ShowConnectionState();
1675 } else {
1676 response = new CECPacket(EC_OP_NOOP);
1678 break;
1680 if (!response) {
1681 AddLogLineN(CFormat(_("External Connection: invalid opcode received: %#x")) % request->GetOpCode());
1682 wxFAIL;
1683 response = new CECPacket(EC_OP_FAILED);
1684 response->AddTag(CECTag(EC_TAG_STRING, wxTRANSLATE("Invalid opcode (wrong protocol version?)")));
1686 return response;
1690 * Here notification-based EC. Notification will be sorted by priority for possible throttling.
1694 * Core general status
1696 ECStatusMsgSource::ECStatusMsgSource()
1698 m_last_ed2k_status_sent = 0xffffffff;
1699 m_last_kad_status_sent = 0xffffffff;
1700 m_server = (void *)0xffffffff;
1703 uint32 ECStatusMsgSource::GetEd2kStatus()
1705 if ( theApp->IsConnectedED2K() ) {
1706 return theApp->GetED2KID();
1707 } else if ( theApp->serverconnect->IsConnecting() ) {
1708 return 1;
1709 } else {
1710 return 0;
1714 uint32 ECStatusMsgSource::GetKadStatus()
1716 if ( theApp->IsConnectedKad() ) {
1717 return 1;
1718 } else if ( Kademlia::CKademlia::IsFirewalled() ) {
1719 return 2;
1720 } else if ( Kademlia::CKademlia::IsRunning() ) {
1721 return 3;
1723 return 0;
1726 CECPacket *ECStatusMsgSource::GetNextPacket()
1728 if ( (m_last_ed2k_status_sent != GetEd2kStatus()) ||
1729 (m_last_kad_status_sent != GetKadStatus()) ||
1730 (m_server != theApp->serverconnect->GetCurrentServer()) ) {
1732 m_last_ed2k_status_sent = GetEd2kStatus();
1733 m_last_kad_status_sent = GetKadStatus();
1734 m_server = theApp->serverconnect->GetCurrentServer();
1736 CECPacket *response = new CECPacket(EC_OP_STATS);
1737 response->AddTag(CEC_ConnState_Tag(EC_DETAIL_UPDATE));
1738 return response;
1740 return 0;
1744 * Downloading files
1746 ECPartFileMsgSource::ECPartFileMsgSource()
1748 for (unsigned int i = 0; i < theApp->downloadqueue->GetFileCount(); i++) {
1749 CPartFile *cur_file = theApp->downloadqueue->GetFileByIndex(i);
1750 PARTFILE_STATUS status = { true, false, false, false, true, cur_file };
1751 m_dirty_status[cur_file->GetFileHash()] = status;
1755 void ECPartFileMsgSource::SetDirty(CPartFile *file)
1757 CMD4Hash filehash = file->GetFileHash();
1758 if ( m_dirty_status.find(filehash) != m_dirty_status.end() ) {
1759 m_dirty_status[filehash].m_dirty = true;;
1763 void ECPartFileMsgSource::SetNew(CPartFile *file)
1765 CMD4Hash filehash = file->GetFileHash();
1766 wxASSERT ( m_dirty_status.find(filehash) == m_dirty_status.end() );
1767 PARTFILE_STATUS status = { true, false, false, false, true, file };
1768 m_dirty_status[filehash] = status;
1771 void ECPartFileMsgSource::SetCompleted(CPartFile *file)
1773 CMD4Hash filehash = file->GetFileHash();
1774 wxASSERT ( m_dirty_status.find(filehash) != m_dirty_status.end() );
1776 m_dirty_status[filehash].m_finished = true;
1779 void ECPartFileMsgSource::SetRemoved(CPartFile *file)
1781 CMD4Hash filehash = file->GetFileHash();
1782 wxASSERT ( m_dirty_status.find(filehash) != m_dirty_status.end() );
1784 m_dirty_status[filehash].m_removed = true;
1787 CECPacket *ECPartFileMsgSource::GetNextPacket()
1789 for(std::map<CMD4Hash, PARTFILE_STATUS>::iterator it = m_dirty_status.begin();
1790 it != m_dirty_status.end(); it++) {
1791 if ( it->second.m_new || it->second.m_dirty || it->second.m_removed) {
1792 CMD4Hash filehash = it->first;
1794 CPartFile *partfile = it->second.m_file;
1796 CECPacket *packet = new CECPacket(EC_OP_DLOAD_QUEUE);
1797 if ( it->second.m_removed ) {
1798 CECTag tag(EC_TAG_PARTFILE, filehash);
1799 packet->AddTag(tag);
1800 m_dirty_status.erase(it);
1801 } else {
1802 CEC_PartFile_Tag tag(partfile, it->second.m_new ? EC_DETAIL_FULL : EC_DETAIL_UPDATE);
1803 packet->AddTag(tag);
1805 m_dirty_status[filehash].m_new = false;
1806 m_dirty_status[filehash].m_dirty = false;
1808 return packet;
1811 return 0;
1815 * Shared files - similar to downloading
1817 ECKnownFileMsgSource::ECKnownFileMsgSource()
1819 for (unsigned int i = 0; i < theApp->sharedfiles->GetFileCount(); i++) {
1820 CKnownFile *cur_file = (CKnownFile *)theApp->sharedfiles->GetFileByIndex(i);
1821 KNOWNFILE_STATUS status = { true, false, false, true, cur_file };
1822 m_dirty_status[cur_file->GetFileHash()] = status;
1826 void ECKnownFileMsgSource::SetDirty(CKnownFile *file)
1828 CMD4Hash filehash = file->GetFileHash();
1829 if ( m_dirty_status.find(filehash) != m_dirty_status.end() ) {
1830 m_dirty_status[filehash].m_dirty = true;;
1834 void ECKnownFileMsgSource::SetNew(CKnownFile *file)
1836 CMD4Hash filehash = file->GetFileHash();
1837 wxASSERT ( m_dirty_status.find(filehash) == m_dirty_status.end() );
1838 KNOWNFILE_STATUS status = { true, false, false, true, file };
1839 m_dirty_status[filehash] = status;
1842 void ECKnownFileMsgSource::SetRemoved(CKnownFile *file)
1844 CMD4Hash filehash = file->GetFileHash();
1845 wxASSERT ( m_dirty_status.find(filehash) != m_dirty_status.end() );
1847 m_dirty_status[filehash].m_removed = true;
1850 CECPacket *ECKnownFileMsgSource::GetNextPacket()
1852 for(std::map<CMD4Hash, KNOWNFILE_STATUS>::iterator it = m_dirty_status.begin();
1853 it != m_dirty_status.end(); it++) {
1854 if ( it->second.m_new || it->second.m_dirty || it->second.m_removed) {
1855 CMD4Hash filehash = it->first;
1857 CKnownFile *partfile = it->second.m_file;
1859 CECPacket *packet = new CECPacket(EC_OP_SHARED_FILES);
1860 if ( it->second.m_removed ) {
1861 CECTag tag(EC_TAG_PARTFILE, filehash);
1862 packet->AddTag(tag);
1863 m_dirty_status.erase(it);
1864 } else {
1865 CEC_SharedFile_Tag tag(partfile, it->second.m_new ? EC_DETAIL_FULL : EC_DETAIL_UPDATE);
1866 packet->AddTag(tag);
1868 m_dirty_status[filehash].m_new = false;
1869 m_dirty_status[filehash].m_dirty = false;
1871 return packet;
1874 return 0;
1878 * Notification about search status
1880 ECSearchMsgSource::ECSearchMsgSource()
1884 CECPacket *ECSearchMsgSource::GetNextPacket()
1886 if ( m_dirty_status.empty() ) {
1887 return 0;
1890 CECPacket *response = new CECPacket(EC_OP_SEARCH_RESULTS);
1891 for(std::map<CMD4Hash, SEARCHFILE_STATUS>::iterator it = m_dirty_status.begin();
1892 it != m_dirty_status.end(); it++) {
1894 if ( it->second.m_new ) {
1895 response->AddTag(CEC_SearchFile_Tag(it->second.m_file, EC_DETAIL_FULL));
1896 it->second.m_new = false;
1897 } else if ( it->second.m_dirty ) {
1898 response->AddTag(CEC_SearchFile_Tag(it->second.m_file, EC_DETAIL_UPDATE));
1903 return response;
1906 void ECSearchMsgSource::FlushStatus()
1908 m_dirty_status.clear();
1911 void ECSearchMsgSource::SetDirty(CSearchFile *file)
1913 if ( m_dirty_status.count(file->GetFileHash()) ) {
1914 m_dirty_status[file->GetFileHash()].m_dirty = true;
1915 } else {
1916 m_dirty_status[file->GetFileHash()].m_new = true;
1917 m_dirty_status[file->GetFileHash()].m_dirty = true;
1918 m_dirty_status[file->GetFileHash()].m_child_dirty = true;
1919 m_dirty_status[file->GetFileHash()].m_file = file;
1923 void ECSearchMsgSource::SetChildDirty(CSearchFile *file)
1925 m_dirty_status[file->GetFileHash()].m_child_dirty = true;
1929 * Notification about uploading clients
1931 CECPacket *ECClientMsgSource::GetNextPacket()
1933 return 0;
1937 // Notification iface per-client
1939 ECNotifier::ECNotifier()
1943 ECNotifier::~ECNotifier()
1945 while (m_msg_source.begin() != m_msg_source.end())
1946 Remove_EC_Client(m_msg_source.begin()->first);
1949 CECPacket *ECNotifier::GetNextPacket(ECUpdateMsgSource *msg_source_array[])
1951 CECPacket *packet = 0;
1953 // priority 0 is highest
1955 for(int i = 0; i < EC_STATUS_LAST_PRIO; i++) {
1956 if ( (packet = msg_source_array[i]->GetNextPacket()) != 0 ) {
1957 break;
1960 return packet;
1963 CECPacket *ECNotifier::GetNextPacket(CECServerSocket *sock)
1966 // OnOutput is called for a first time before
1967 // socket is registered
1969 if ( m_msg_source.count(sock) ) {
1970 ECUpdateMsgSource **notifier_array = m_msg_source[sock];
1971 if ( !notifier_array ) {
1972 return 0;
1974 CECPacket *packet = GetNextPacket(notifier_array);
1975 printf("[EC] next update packet; opcode=%x\n",packet ? packet->GetOpCode() : 0xff);
1976 return packet;
1977 } else {
1978 return 0;
1983 // Interface to notification macros
1985 void ECNotifier::DownloadFile_SetDirty(CPartFile *file)
1987 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
1988 i != m_msg_source.end(); i++) {
1989 CECServerSocket *sock = i->first;
1990 if ( sock->HaveNotificationSupport() ) {
1991 ECUpdateMsgSource **notifier_array = i->second;
1992 ((ECPartFileMsgSource *)notifier_array[EC_PARTFILE])->SetDirty(file);
1995 NextPacketToSocket();
1998 void ECNotifier::DownloadFile_RemoveFile(CPartFile *file)
2000 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
2001 i != m_msg_source.end(); i++) {
2002 ECUpdateMsgSource **notifier_array = i->second;
2003 ((ECPartFileMsgSource *)notifier_array[EC_PARTFILE])->SetRemoved(file);
2005 NextPacketToSocket();
2008 void ECNotifier::DownloadFile_RemoveSource(CPartFile *)
2010 // per-partfile source list is not supported (yet), and IMHO quite useless
2013 void ECNotifier::DownloadFile_AddFile(CPartFile *file)
2015 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
2016 i != m_msg_source.end(); i++) {
2017 ECUpdateMsgSource **notifier_array = i->second;
2018 ((ECPartFileMsgSource *)notifier_array[EC_PARTFILE])->SetNew(file);
2020 NextPacketToSocket();
2023 void ECNotifier::DownloadFile_AddSource(CPartFile *)
2025 // per-partfile source list is not supported (yet), and IMHO quite useless
2028 void ECNotifier::SharedFile_AddFile(CKnownFile *file)
2030 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
2031 i != m_msg_source.end(); i++) {
2032 ECUpdateMsgSource **notifier_array = i->second;
2033 ((ECKnownFileMsgSource *)notifier_array[EC_KNOWN])->SetNew(file);
2035 NextPacketToSocket();
2038 void ECNotifier::SharedFile_RemoveFile(CKnownFile *file)
2040 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
2041 i != m_msg_source.end(); i++) {
2042 ECUpdateMsgSource **notifier_array = i->second;
2043 ((ECKnownFileMsgSource *)notifier_array[EC_KNOWN])->SetRemoved(file);
2045 NextPacketToSocket();
2048 void ECNotifier::SharedFile_RemoveAllFiles()
2050 // need to figure out what to do here
2053 void ECNotifier::Add_EC_Client(CECServerSocket *sock)
2055 ECUpdateMsgSource **notifier_array = new ECUpdateMsgSource *[EC_STATUS_LAST_PRIO];
2056 notifier_array[EC_STATUS] = new ECStatusMsgSource();
2057 notifier_array[EC_SEARCH] = new ECSearchMsgSource();
2058 notifier_array[EC_PARTFILE] = new ECPartFileMsgSource();
2059 notifier_array[EC_CLIENT] = new ECClientMsgSource();
2060 notifier_array[EC_KNOWN] = new ECKnownFileMsgSource();
2062 m_msg_source[sock] = notifier_array;
2065 void ECNotifier::Remove_EC_Client(CECServerSocket *sock)
2067 if (m_msg_source.count(sock)) {
2068 ECUpdateMsgSource **notifier_array = m_msg_source[sock];
2070 m_msg_source.erase(sock);
2072 for(int i = 0; i < EC_STATUS_LAST_PRIO; i++) {
2073 delete notifier_array[i];
2075 delete [] notifier_array;
2079 void ECNotifier::NextPacketToSocket()
2081 for(std::map<CECServerSocket *, ECUpdateMsgSource **>::iterator i = m_msg_source.begin();
2082 i != m_msg_source.end(); i++) {
2083 CECServerSocket *sock = i->first;
2084 if ( sock->HaveNotificationSupport() && !sock->DataPending() ) {
2085 ECUpdateMsgSource **notifier_array = i->second;
2086 CECPacket *packet = GetNextPacket(notifier_array);
2087 if ( packet ) {
2088 printf("[EC] sending update packet; opcode=%x\n",packet->GetOpCode());
2089 sock->SendPacket(packet);
2095 // File_checked_for_headers