2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 Kry ( elkry@sourceforge.net / http://www.amule.org )
5 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2008 Froenchenko Leonid (lfroen@gmail.com)
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "config.h" // Needed for VERSION
31 #include <ec/cpp/ECMuleSocket.h> // Needed for CECSocket
33 #include <common/Format.h> // Needed for CFormat
35 #include <common/ClientVersion.h>
37 #include "ExternalConn.h" // Interface declarations
38 #include "updownclient.h" // Needed for CUpDownClient
39 #include "Server.h" // Needed for CServer
40 #include "ServerList.h" // Needed for CServerList
41 #include "PartFile.h" // Needed for CPartFile
42 #include "ServerConnect.h" // Needed for CServerConnect
43 #include "UploadQueue.h" // Needed for CUploadQueue
44 #include "amule.h" // Needed for theApp
45 #include "SearchList.h" // Needed for GetSearchResults
46 #include "IPFilter.h" // Needed for CIPFilter
47 #include "ClientList.h"
48 #include "Preferences.h" // Needed for CPreferences
50 #include "GuiEvents.h" // Needed for Notify_* macros
51 #include "Statistics.h" // Needed for theStats
52 #include "KnownFileList.h" // Needed for CKnownFileList
53 #include "kademlia/kademlia/Kademlia.h"
56 //-------------------- CECServerSocket --------------------
58 class CECServerSocket
: public CECMuleSocket
61 CECServerSocket(ECNotifier
*notifier
);
62 virtual ~CECServerSocket();
64 virtual const CECPacket
*OnPacketReceived(const CECPacket
*packet
);
65 virtual void OnLost();
67 virtual void WriteDoneAndQueueEmpty();
69 ECNotifier
*m_ec_notifier
;
72 CPartFile_Encoder_Map m_part_encoder
;
73 CKnownFile_Encoder_Map m_shared_encoder
;
74 CObjTagMap m_obj_tagmap
;
78 CECServerSocket::CECServerSocket(ECNotifier
*notifier
)
81 m_authenticated(false),
86 wxASSERT(theApp
->ECServerHandler
);
87 theApp
->ECServerHandler
->AddSocket(this);
88 m_ec_notifier
= notifier
;
92 CECServerSocket::~CECServerSocket()
94 wxASSERT(theApp
->ECServerHandler
);
95 theApp
->ECServerHandler
->RemoveSocket(this);
99 const CECPacket
*CECServerSocket::OnPacketReceived(const CECPacket
*packet
)
101 const CECPacket
*reply
= NULL
;
103 if (!m_authenticated
) {
104 reply
= ExternalConn::Authenticate(packet
);
105 if (reply
->GetOpCode() != EC_OP_AUTH_OK
) {
107 AddLogLineM(false, _("Unauthorized access attempt. Connection closed."));
110 m_authenticated
= true;
113 reply
= ExternalConn::ProcessRequest2(
114 packet
, m_part_encoder
, m_shared_encoder
, m_obj_tagmap
);
120 void CECServerSocket::OnLost()
122 AddLogLineM(false,_("External connection closed."));
126 void CECServerSocket::WriteDoneAndQueueEmpty()
128 if ( HaveNotificationSupport() ) {
129 CECPacket
*packet
= m_ec_notifier
->GetNextPacket(this);
136 //-------------------- ExternalConn --------------------
144 BEGIN_EVENT_TABLE(ExternalConn
, wxEvtHandler
)
145 EVT_SOCKET(SERVER_ID
, ExternalConn::OnServerEvent
)
149 ExternalConn::ExternalConn(amuleIPV4Address addr
, wxString
*msg
)
153 // Are we allowed to accept External Connections?
154 if ( thePrefs::AcceptExternalConnections() ) {
155 // We must have a valid password, otherwise we will not allow EC connections
156 if (thePrefs::ECPassword().IsEmpty()) {
157 *msg
+= wxT("External connections disabled due to empty password!\n");
158 AddLogLineM(true, _("External connections disabled due to empty password!"));
163 m_ECServer
= new wxSocketServer(addr
, wxSOCKET_REUSEADDR
);
164 m_ECServer
->SetEventHandler(*this, SERVER_ID
);
165 m_ECServer
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
166 m_ECServer
->Notify(true);
168 int port
= addr
.Service();
169 wxString ip
= addr
.IPAddress();
170 if (m_ECServer
->Ok()) {
171 msgLocal
= wxT("*** TCP socket (ECServer) listening on ") + ip
+
172 wxString::Format(wxT(":%d"), port
);
173 *msg
+= msgLocal
+ wxT("\n");
174 AddLogLineM(false, msgLocal
);
176 msgLocal
= wxT("Could not listen for external connections at ") + ip
+
177 wxString::Format(wxT(":%d!"), port
);
178 *msg
+= msgLocal
+ wxT("\n");
179 AddLogLineM(false, msgLocal
);
182 *msg
+= wxT("External connections disabled in config file\n");
183 AddLogLineM(false,_("External connections disabled in config file"));
185 m_ec_notifier
= new ECNotifier();
189 ExternalConn::~ExternalConn()
193 delete m_ec_notifier
;
197 void ExternalConn::AddSocket(CECServerSocket
*s
)
200 socket_list
.insert(s
);
204 void ExternalConn::RemoveSocket(CECServerSocket
*s
)
207 socket_list
.erase(s
);
211 void ExternalConn::KillAllSockets()
213 AddDebugLogLineM(false, logGeneral
,
214 CFormat(wxT("ExternalConn::KillAllSockets(): %d sockets to destroy.")) %
216 SocketSet::iterator it
= socket_list
.begin();
217 while (it
!= socket_list
.end()) {
218 CECServerSocket
*s
= *(it
++);
226 void ExternalConn::OnServerEvent(wxSocketEvent
& WXUNUSED(event
))
228 CECServerSocket
*sock
= new CECServerSocket(m_ec_notifier
);
229 // Accept new connection if there is one in the pending
230 // connections queue, else exit. We use Accept(FALSE) for
231 // non-blocking accept (although if we got here, there
232 // should ALWAYS be a pending connection).
233 if ( m_ECServer
->AcceptWith(*sock
, false) ) {
234 AddLogLineM(false, _("New external connection accepted"));
237 AddLogLineM(false, _("ERROR: couldn't accept a new external connection"));
245 CECPacket
*ExternalConn::Authenticate(const CECPacket
*request
)
249 if (request
== NULL
) {
250 response
= new CECPacket(EC_OP_AUTH_FAIL
);
254 // Password must be specified if we are to allow remote connections
255 if ( thePrefs::ECPassword().IsEmpty() ) {
256 AddLogLineM(true, _("External connection refused due to empty password in preferences!"));
258 return new CECPacket(EC_OP_AUTH_FAIL
);
262 if (request
->GetOpCode() == EC_OP_AUTH_REQ
) {
263 const CECTag
*clientName
= request
->GetTagByName(EC_TAG_CLIENT_NAME
);
264 const CECTag
*clientVersion
= request
->GetTagByName(EC_TAG_CLIENT_VERSION
);
266 AddLogLineM(false, CFormat( _("Connecting client: %s %s") )
267 % ( clientName
? clientName
->GetStringData() : wxString(_("Unknown")) )
268 % ( clientVersion
? clientVersion
->GetStringData() : wxString(_("Unknown version")) ) );
269 const CECTag
*passwd
= request
->GetTagByName(EC_TAG_PASSWD_HASH
);
270 const CECTag
*protocol
= request
->GetTagByName(EC_TAG_PROTOCOL_VERSION
);
272 // For SVN versions, both client and server must use SVNDATE, and they must be the same
274 if (not vhash
.Decode(wxT(EC_VERSION_ID
))) {
275 response
= new CECPacket(EC_OP_AUTH_FAIL
);
276 response
->AddTag(CECTag(EC_TAG_STRING
, wxT("Fatal error, version hash is not a valid MD4-hash.")));
277 } else if (!request
->GetTagByName(EC_TAG_VERSION_ID
) || request
->GetTagByNameSafe(EC_TAG_VERSION_ID
)->GetMD4Data() != vhash
) {
278 response
= new CECPacket(EC_OP_AUTH_FAIL
);
279 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Incorrect EC version ID, there might be binary incompatibility. Use core and remote from same snapshot.")));
281 // For release versions, we don't want to allow connections from any arbitrary SVN client.
282 if (request
->GetTagByName(EC_TAG_VERSION_ID
)) {
283 response
= new CECPacket(EC_OP_AUTH_FAIL
);
284 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("You cannot connect to a release version from an arbitrary SVN version! *sigh* possible crash prevented")));
286 } else if (protocol
!= NULL
) {
287 uint16 proto_version
= protocol
->GetInt();
288 if (proto_version
== EC_CURRENT_PROTOCOL_VERSION
) {
291 if (!passh
.Decode(thePrefs::ECPassword())) {
292 AddLogLineM(false, wxT("EC Auth failed, invalid hash specificed as EC password: ") + thePrefs::ECPassword());
293 response
= new CECPacket(EC_OP_AUTH_FAIL
);
294 response
->AddTag(CECTag(EC_TAG_STRING
, wxT("Authentication failed, invalid hash specified as EC password.")));
295 } else if (passwd
&& passwd
->GetMD4Data() == passh
) {
296 response
= new CECPacket(EC_OP_AUTH_OK
);
299 AddLogLineM(false, wxT("EC Auth failed: (") + passwd
->GetMD4Data().Encode() + wxT(" != ") + passh
.Encode() + wxT(")."));
301 AddLogLineM(false, wxT("EC Auth failed. Password tag missing."));
304 response
= new CECPacket(EC_OP_AUTH_FAIL
);
305 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Authentication failed.")));
308 response
= new CECPacket(EC_OP_AUTH_FAIL
);
309 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid protocol version.") + wxString::Format(wxT("( %i != %i )"),proto_version
,EC_CURRENT_PROTOCOL_VERSION
)));
312 response
= new CECPacket(EC_OP_AUTH_FAIL
);
313 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Missing protocol version tag.")));
316 response
= new CECPacket(EC_OP_AUTH_FAIL
);
317 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid request, you should first authenticate.")));
320 response
->AddTag(CECTag(EC_TAG_SERVER_VERSION
, wxT(VERSION
)));
322 if (response
->GetOpCode() == EC_OP_AUTH_OK
) {
323 AddLogLineM(false, _("Access granted."));
325 AddLogLineM(false, wxGetTranslation(response
->GetTagByIndex(0)->GetStringData()));
331 CECPacket
*Get_EC_Response_StatRequest(const CECPacket
*request
)
333 CECPacket
*response
= new CECPacket(EC_OP_STATS
);
335 switch (request
->GetDetailLevel()) {
337 response
->AddTag(CECTag(EC_TAG_STATS_UP_OVERHEAD
, (uint32
)theStats::GetUpOverheadRate()));
338 response
->AddTag(CECTag(EC_TAG_STATS_DOWN_OVERHEAD
, (uint32
)theStats::GetDownOverheadRate()));
339 response
->AddTag(CECTag(EC_TAG_STATS_BANNED_COUNT
, /*(uint32)*/theStats::GetBannedCount()));
342 response
->AddTag(CECTag(EC_TAG_STATS_UL_SPEED
, (uint32
)theStats::GetUploadRate()));
343 response
->AddTag(CECTag(EC_TAG_STATS_DL_SPEED
, (uint32
)(theStats::GetDownloadRate())));
344 response
->AddTag(CECTag(EC_TAG_STATS_UL_SPEED_LIMIT
, (uint32
)(thePrefs::GetMaxUpload()*1024.0)));
345 response
->AddTag(CECTag(EC_TAG_STATS_DL_SPEED_LIMIT
, (uint32
)(thePrefs::GetMaxDownload()*1024.0)));
346 response
->AddTag(CECTag(EC_TAG_STATS_UL_QUEUE_LEN
, /*(uint32)*/theStats::GetWaitingUserCount()));
347 response
->AddTag(CECTag(EC_TAG_STATS_TOTAL_SRC_COUNT
, /*(uint32)*/theStats::GetFoundSources()));
350 uint32 totaluser
= 0, totalfile
= 0;
351 theApp
->serverlist
->GetUserFileStatus( totaluser
, totalfile
);
352 response
->AddTag(CECTag(EC_TAG_STATS_ED2K_USERS
, totaluser
));
353 response
->AddTag(CECTag(EC_TAG_STATS_KAD_USERS
, Kademlia::CKademlia::GetKademliaUsers()));
354 response
->AddTag(CECTag(EC_TAG_STATS_ED2K_FILES
, totalfile
));
355 response
->AddTag(CECTag(EC_TAG_STATS_KAD_FILES
, Kademlia::CKademlia::GetKademliaFiles()));
357 case EC_DETAIL_UPDATE
:
358 case EC_DETAIL_INC_UPDATE
:
365 CECPacket
*Get_EC_Response_GetSharedFiles(const CECPacket
*request
, CKnownFile_Encoder_Map
&encoders
)
367 wxASSERT(request
->GetOpCode() == EC_OP_GET_SHARED_FILES
);
369 CECPacket
*response
= new CECPacket(EC_OP_SHARED_FILES
);
371 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
373 // request can contain list of queried items
374 CTagSet
<CMD4Hash
, EC_TAG_KNOWNFILE
> queryitems(request
);
376 encoders
.UpdateEncoders(theApp
->sharedfiles
);
378 for (uint32 i
= 0; i
< theApp
->sharedfiles
->GetFileCount(); ++i
) {
379 CKnownFile
*cur_file
= (CKnownFile
*)theApp
->sharedfiles
->GetFileByIndex(i
);
381 if ( !cur_file
|| (!queryitems
.empty() && !queryitems
.count(cur_file
->GetFileHash())) ) {
385 CEC_SharedFile_Tag
filetag(cur_file
, detail_level
);
386 CKnownFile_Encoder
&enc
= encoders
[cur_file
];
387 if ( detail_level
!= EC_DETAIL_UPDATE
) {
390 enc
.Encode(&filetag
);
391 response
->AddTag(filetag
);
396 CECPacket
*Get_EC_Response_GetSharedFiles(CKnownFile_Encoder_Map
&encoders
, CObjTagMap
&tagmap
)
398 CECPacket
*response
= new CECPacket(EC_OP_SHARED_FILES
);
400 encoders
.UpdateEncoders(theApp
->sharedfiles
);
401 for (uint32 i
= 0; i
< theApp
->sharedfiles
->GetFileCount(); ++i
) {
402 CKnownFile
*cur_file
= (CKnownFile
*)theApp
->sharedfiles
->GetFileByIndex(i
);
405 // Hashes of tags are maintained on "per-object" basis. So, in this mode only
406 // same kind of objects can go into particular query type.
407 // Particulary here it means that files from download queue (aka partfiles)
408 // will not ne shown as shared files. Remote gui can do combine them if wishes
410 if ( !cur_file
|| cur_file
->IsPartFile() ) continue;
412 CValueMap
&valuemap
= tagmap
.GetValueMap(cur_file
);
413 CEC_SharedFile_Tag
filetag(cur_file
, valuemap
);
414 CKnownFile_Encoder
&enc
= encoders
[cur_file
];
415 enc
.Encode(&filetag
);
417 response
->AddTag(filetag
);
422 CECPacket
*Get_EC_Response_GetWaitQueue(const CECPacket
*request
)
424 wxASSERT(request
->GetOpCode() == EC_OP_GET_WAIT_QUEUE
);
426 CECPacket
*response
= new CECPacket(EC_OP_WAIT_QUEUE
);
428 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
431 // request can contain list of queried items
432 CTagSet
<uint32
, EC_TAG_CLIENT
> queryitems(request
);
434 const CClientPtrList
& uploading
= theApp
->uploadqueue
->GetWaitingList();
435 CClientPtrList::const_iterator it
= uploading
.begin();
436 for (; it
!= uploading
.end(); ++it
) {
437 CUpDownClient
* cur_client
= *it
;
439 if ( !cur_client
|| (!queryitems
.empty() && !queryitems
.count(cur_client
->GetUserIDHybrid())) ) {
442 CEC_UpDownClient_Tag
cli_tag(cur_client
, detail_level
);
444 response
->AddTag(cli_tag
);
450 CECPacket
*Get_EC_Response_GetWaitQueue(CObjTagMap
&tagmap
)
452 CECPacket
*response
= new CECPacket(EC_OP_WAIT_QUEUE
);
454 const CClientPtrList
& uploading
= theApp
->uploadqueue
->GetWaitingList();
455 CClientPtrList::const_iterator it
= uploading
.begin();
456 for (; it
!= uploading
.end(); ++it
) {
457 CUpDownClient
* cur_client
= *it
;
459 CValueMap
&valuemap
= tagmap
.GetValueMap(cur_client
);
460 CEC_UpDownClient_Tag
cli_tag(cur_client
, valuemap
);
462 response
->AddTag(cli_tag
);
468 CECPacket
*Get_EC_Response_GetUpQueue(const CECPacket
*request
)
470 wxASSERT(request
->GetOpCode() == EC_OP_GET_ULOAD_QUEUE
);
472 CECPacket
*response
= new CECPacket(EC_OP_ULOAD_QUEUE
);
474 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
477 // request can contain list of queried items
478 CTagSet
<uint32
, EC_TAG_CLIENT
> queryitems(request
);
481 const CClientPtrList
& uploading
= theApp
->uploadqueue
->GetUploadingList();
482 CClientPtrList::const_iterator it
= uploading
.begin();
483 for (; it
!= uploading
.end(); ++it
) {
484 CUpDownClient
* cur_client
= *it
;
486 if ( !cur_client
|| (!queryitems
.empty() && !queryitems
.count(cur_client
->GetUserIDHybrid())) ) {
490 CEC_UpDownClient_Tag
cli_tag(cur_client
, detail_level
);
491 response
->AddTag(cli_tag
);
498 CECPacket
*Get_EC_Response_GetUpQueue(CObjTagMap
&tagmap
)
500 CECPacket
*response
= new CECPacket(EC_OP_ULOAD_QUEUE
);
502 const CClientPtrList
& uploading
= theApp
->uploadqueue
->GetUploadingList();
503 CClientPtrList::const_iterator it
= uploading
.begin();
504 for (; it
!= uploading
.end(); ++it
) {
505 CUpDownClient
* cur_client
= *it
;
507 CValueMap
&valuemap
= tagmap
.GetValueMap(cur_client
);
508 CEC_UpDownClient_Tag
cli_tag(cur_client
, valuemap
);
510 response
->AddTag(cli_tag
);
517 CECPacket
*Get_EC_Response_GetDownloadQueue(CPartFile_Encoder_Map
&encoders
, CObjTagMap
&tagmap
)
519 CECPacket
*response
= new CECPacket(EC_OP_DLOAD_QUEUE
);
521 encoders
.UpdateEncoders(theApp
->downloadqueue
);
522 for (unsigned int i
= 0; i
< theApp
->downloadqueue
->GetFileCount(); i
++) {
523 CPartFile
*cur_file
= theApp
->downloadqueue
->GetFileByIndex(i
);
525 CValueMap
&valuemap
= tagmap
.GetValueMap(cur_file
);
526 CEC_PartFile_Tag
filetag(cur_file
, valuemap
);
527 CPartFile_Encoder
&enc
= encoders
[cur_file
];
528 enc
.Encode(&filetag
);
530 response
->AddTag(filetag
);
535 CECPacket
*Get_EC_Response_GetDownloadQueue(const CECPacket
*request
, CPartFile_Encoder_Map
&encoders
, bool detail
= false)
537 CECPacket
*response
= new CECPacket(EC_OP_DLOAD_QUEUE
);
539 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
541 // request can contain list of queried items
542 CTagSet
<CMD4Hash
, EC_TAG_PARTFILE
> queryitems(request
);
544 encoders
.UpdateEncoders(theApp
->downloadqueue
);
546 for (unsigned int i
= 0; i
< theApp
->downloadqueue
->GetFileCount(); i
++) {
547 CPartFile
*cur_file
= theApp
->downloadqueue
->GetFileByIndex(i
);
549 if ( !queryitems
.empty() && !queryitems
.count(cur_file
->GetFileHash()) ) {
553 CEC_PartFile_Tag
filetag(cur_file
, detail_level
, detail
);
555 CPartFile_Encoder
&enc
= encoders
[cur_file
];
556 if ( detail_level
!= EC_DETAIL_UPDATE
) {
559 enc
.Encode(&filetag
);
561 response
->AddTag(filetag
);
567 CECPacket
*Get_EC_Response_PartFile_Cmd(const CECPacket
*request
)
569 CECPacket
*response
= NULL
;
571 // request can contain multiple files.
572 for (unsigned int i
= 0; i
< request
->GetTagCount(); ++i
) {
573 const CECTag
*hashtag
= request
->GetTagByIndex(i
);
575 wxASSERT(hashtag
->GetTagName() == EC_TAG_PARTFILE
);
577 CMD4Hash hash
= hashtag
->GetMD4Data();
578 CPartFile
*pfile
= theApp
->downloadqueue
->GetFileByID( hash
);
581 AddLogLineM(false,CFormat(_("Remote PartFile command failed: FileHash not found: %s")) % hash
.Encode());
582 response
= new CECPacket(EC_OP_FAILED
);
583 response
->AddTag(CECTag(EC_TAG_STRING
, CFormat(wxString(wxTRANSLATE("FileHash not found: %s"))) % hash
.Encode()));
587 switch (request
->GetOpCode()) {
588 case EC_OP_PARTFILE_SWAP_A4AF_THIS
:
589 if ((pfile
->GetStatus(false) == PS_READY
) ||
590 (pfile
->GetStatus(false) == PS_EMPTY
)) {
591 CPartFile::SourceSet::const_iterator it
= pfile
->GetA4AFList().begin();
592 while ( it
!= pfile
->GetA4AFList().end() ) {
593 CUpDownClient
*cur_source
= *it
++;
595 cur_source
->SwapToAnotherFile(true, false, false, pfile
);
599 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO
:
600 pfile
->SetA4AFAuto(!pfile
->IsA4AFAuto());
602 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS
:
603 if ((pfile
->GetStatus(false) == PS_READY
) ||
604 (pfile
->GetStatus(false) == PS_EMPTY
)) {
605 CPartFile::SourceSet::const_iterator it
= pfile
->GetSourceList().begin();
606 while ( it
!= pfile
->GetSourceList().end() ) {
607 CUpDownClient
* cur_source
= *it
++;
609 cur_source
->SwapToAnotherFile(false, false, false, NULL
);
613 case EC_OP_PARTFILE_PAUSE
:
616 case EC_OP_PARTFILE_RESUME
:
618 pfile
->SavePartFile();
620 case EC_OP_PARTFILE_STOP
:
623 case EC_OP_PARTFILE_PRIO_SET
: {
624 uint8 prio
= hashtag
->GetTagByIndexSafe(0)->GetInt();
625 if ( prio
== PR_AUTO
) {
626 pfile
->SetAutoDownPriority(1);
628 pfile
->SetAutoDownPriority(0);
629 pfile
->SetDownPriority(prio
);
633 case EC_OP_PARTFILE_DELETE
:
634 if ( thePrefs::StartNextFile() && (pfile
->GetStatus() != PS_PAUSED
) ) {
635 theApp
->downloadqueue
->StartNextFile(pfile
);
640 case EC_OP_PARTFILE_SET_CAT
:
641 pfile
->SetCategory(hashtag
->GetTagByIndexSafe(0)->GetInt());
645 response
= new CECPacket(EC_OP_FAILED
);
646 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("OOPS! OpCode processing error!")));
651 response
= new CECPacket(EC_OP_NOOP
);
656 CECPacket
*Get_EC_Response_Server_Add(const CECPacket
*request
)
658 CECPacket
*response
= NULL
;
660 const CECTag
*srv_tag
= request
->GetTagByIndex(0);
662 wxString full_addr
= srv_tag
->GetTagByName(EC_TAG_SERVER_ADDRESS
)->GetStringData();
663 wxString name
= srv_tag
->GetTagByName(EC_TAG_SERVER_NAME
)->GetStringData();
665 wxString s_ip
= full_addr
.Left(full_addr
.Find(':'));
666 wxString s_port
= full_addr
.Mid(full_addr
.Find(':') + 1);
668 long port
= StrToULong(s_port
);
669 CServer
* toadd
= new CServer(port
, s_ip
);
670 toadd
->SetListName(name
.IsEmpty() ? full_addr
: name
);
672 if ( theApp
->AddServer(toadd
, true) ) {
673 response
= new CECPacket(EC_OP_NOOP
);
675 response
= new CECPacket(EC_OP_FAILED
);
676 response
->AddTag(CECTag(EC_TAG_STRING
, _("Server not added")));
683 CECPacket
*Get_EC_Response_Server(const CECPacket
*request
)
685 CECPacket
*response
= NULL
;
686 const CECTag
*srv_tag
= request
->GetTagByIndex(0);
689 srv
= theApp
->serverlist
->GetServerByIPTCP(srv_tag
->GetIPv4Data().IP(), srv_tag
->GetIPv4Data().m_port
);
690 // server tag passed, but server not found
692 response
= new CECPacket(EC_OP_FAILED
);
693 response
->AddTag(CECTag(EC_TAG_STRING
,
694 CFormat(wxString(wxTRANSLATE("server not found: %s"))) % srv_tag
->GetIPv4Data().StringIP()));
698 switch (request
->GetOpCode()) {
699 case EC_OP_SERVER_DISCONNECT
:
700 theApp
->serverconnect
->Disconnect();
701 response
= new CECPacket(EC_OP_NOOP
);
703 case EC_OP_SERVER_REMOVE
:
705 theApp
->serverlist
->RemoveServer(srv
);
706 response
= new CECPacket(EC_OP_NOOP
);
708 response
= new CECPacket(EC_OP_FAILED
);
709 response
->AddTag(CECTag(EC_TAG_STRING
,
710 wxTRANSLATE("need to define server to be removed")));
713 case EC_OP_SERVER_CONNECT
:
714 if (thePrefs::GetNetworkED2K()) {
716 theApp
->serverconnect
->ConnectToServer(srv
);
717 response
= new CECPacket(EC_OP_NOOP
);
719 theApp
->serverconnect
->ConnectToAnyServer();
720 response
= new CECPacket(EC_OP_NOOP
);
723 response
= new CECPacket(EC_OP_FAILED
);
724 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("eD2k is disabled in preferences.")));
729 response
= new CECPacket(EC_OP_FAILED
);
730 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("OOPS! OpCode processing error!")));
735 CECPacket
*Get_EC_Response_Search_Results(const CECPacket
*request
)
737 CECPacket
*response
= new CECPacket(EC_OP_SEARCH_RESULTS
);
739 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
741 // request can contain list of queried items
742 CTagSet
<CMD4Hash
, EC_TAG_SEARCHFILE
> queryitems(request
);
744 const CSearchResultList
& list
= theApp
->searchlist
->GetSearchResults(0xffffffff);
745 CSearchResultList::const_iterator it
= list
.begin();
746 while (it
!= list
.end()) {
747 CSearchFile
* sf
= *it
++;
748 if ( !queryitems
.empty() && !queryitems
.count(sf
->GetFileHash()) ) {
751 response
->AddTag(CEC_SearchFile_Tag(sf
, detail_level
));
756 CECPacket
*Get_EC_Response_Search_Results(CObjTagMap
&tagmap
)
758 CECPacket
*response
= new CECPacket(EC_OP_SEARCH_RESULTS
);
760 const CSearchResultList
& list
= theApp
->searchlist
->GetSearchResults(0xffffffff);
761 CSearchResultList::const_iterator it
= list
.begin();
762 while (it
!= list
.end()) {
763 CSearchFile
* sf
= *it
++;
764 CValueMap
&valuemap
= tagmap
.GetValueMap(sf
);
765 response
->AddTag(CEC_SearchFile_Tag(sf
, valuemap
));
770 CECPacket
*Get_EC_Response_Search_Results_Download(const CECPacket
*request
)
772 CECPacket
*response
= new CECPacket(EC_OP_STRINGS
);
773 for (unsigned int i
= 0;i
< request
->GetTagCount();i
++) {
774 const CECTag
*tag
= request
->GetTagByIndex(i
);
775 CMD4Hash hash
= tag
->GetMD4Data();
776 uint8 category
= tag
->GetTagByIndexSafe(0)->GetInt();
777 theApp
->searchlist
->AddFileToDownloadByHash(hash
, category
);
782 CECPacket
*Get_EC_Response_Search_Stop(const CECPacket
*WXUNUSED(request
))
784 CECPacket
*reply
= new CECPacket(EC_OP_MISC_DATA
);
785 theApp
->searchlist
->StopGlobalSearch();
789 CECPacket
*Get_EC_Response_Search(const CECPacket
*request
)
793 CEC_Search_Tag
*search_request
= (CEC_Search_Tag
*)request
->GetTagByIndex(0);
794 theApp
->searchlist
->RemoveResults(0xffffffff);
796 CSearchList::CSearchParams params
;
797 params
.searchString
= search_request
->SearchText();
798 params
.typeText
= search_request
->SearchFileType();
799 params
.extension
= search_request
->SearchExt();
800 params
.minSize
= search_request
->MinSize();
801 params
.maxSize
= search_request
->MaxSize();
802 params
.availability
= search_request
->Avail();
805 EC_SEARCH_TYPE search_type
= search_request
->SearchType();
806 SearchType core_search_type
= LocalSearch
;
807 switch (search_type
) {
808 case EC_SEARCH_GLOBAL
:
809 core_search_type
= GlobalSearch
;
811 if (core_search_type
!= GlobalSearch
) { // Not a global search obviously
812 core_search_type
= KadSearch
;
814 case EC_SEARCH_LOCAL
: {
815 uint32 search_id
= 0xffffffff;
816 wxString error
= theApp
->searchlist
->StartNewSearch(&search_id
, core_search_type
, params
);
817 if (!error
.IsEmpty()) {
820 response
= wxTRANSLATE("Search in progress. Refetch results in a moment!");
825 response
= wxTRANSLATE("WebSearch from remote interface makes no sense.");
829 CECPacket
*reply
= new CECPacket(EC_OP_FAILED
);
830 // error or search in progress
831 reply
->AddTag(CECTag(EC_TAG_STRING
, response
));
836 CECPacket
*Get_EC_Response_Set_SharedFile_Prio(const CECPacket
*request
)
838 CECPacket
*response
= new CECPacket(EC_OP_NOOP
);
839 for (unsigned int i
= 0;i
< request
->GetTagCount();i
++) {
840 const CECTag
*tag
= request
->GetTagByIndex(i
);
841 CMD4Hash hash
= tag
->GetMD4Data();
842 uint8 prio
= tag
->GetTagByIndexSafe(0)->GetInt();
843 CKnownFile
* cur_file
= theApp
->sharedfiles
->GetFileByID(hash
);
847 if (prio
== PR_AUTO
) {
848 cur_file
->SetAutoUpPriority(1);
849 cur_file
->UpdateAutoUpPriority();
851 cur_file
->SetAutoUpPriority(0);
852 cur_file
->SetUpPriority(prio
);
859 CECPacket
*Get_EC_Response_Kad_Connect(const CECPacket
*request
)
862 if (thePrefs::GetNetworkKademlia()) {
863 response
= new CECPacket(EC_OP_NOOP
);
864 if ( !Kademlia::CKademlia::IsRunning() ) {
865 Kademlia::CKademlia::Start();
866 theApp
->ShowConnectionState();
868 const CECTag
*addrtag
= request
->GetTagByIndex(0);
870 uint32 ip
= addrtag
->GetIPv4Data().IP();
871 uint16 port
= addrtag
->GetIPv4Data().m_port
;
872 Kademlia::CKademlia::Bootstrap(ip
, port
, true);
875 response
= new CECPacket(EC_OP_FAILED
);
876 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Kad is disabled in preferences.")));
882 // init with some default size
883 CPartFile_Encoder::GapBuffer
CPartFile_Encoder::m_gap_buffer(128);
886 CPartFile_Encoder::CPartFile_Encoder(CPartFile
*file
) :
887 m_enc_data(file
->GetPartCount(), file
->GetGapList().size() * 2)
893 CPartFile_Encoder::CPartFile_Encoder(int size
): m_enc_data(size
, 0)
898 CPartFile_Encoder::~CPartFile_Encoder()
903 CPartFile_Encoder::CPartFile_Encoder()
908 CPartFile_Encoder::CPartFile_Encoder(const CPartFile_Encoder
&obj
) : m_enc_data(obj
.m_enc_data
)
913 CPartFile_Encoder
&CPartFile_Encoder::operator=(const CPartFile_Encoder
&obj
)
916 m_enc_data
= obj
.m_enc_data
;
920 void CPartFile_Encoder::Encode(CECTag
*parent
)
922 const CPartFile::CGapPtrList
& gaplist
= m_file
->GetGapList();
923 const size_t gap_list_size
= gaplist
.size();
925 if ( m_gap_buffer
.size() < gap_list_size
* 2 ) {
926 m_gap_buffer
.clear();
927 m_gap_buffer
.resize(gap_list_size
* 2);
930 GapBuffer::iterator it
= m_gap_buffer
.begin();
932 CPartFile::CGapPtrList::const_iterator curr_pos
= gaplist
.begin();
933 for (; curr_pos
!= gaplist
.end(); ++curr_pos
) {
934 Gap_Struct
*curr
= *curr_pos
;
935 *it
++ = ENDIAN_HTONLL(curr
->start
);
936 *it
++ = ENDIAN_HTONLL(curr
->end
);
939 m_enc_data
.m_gap_status
.Realloc(gap_list_size
*2*sizeof(uint64
));
940 int gap_enc_size
= 0;
941 const unsigned char *gap_enc_data
= m_enc_data
.m_gap_status
.Encode((unsigned char *)&m_gap_buffer
[0], gap_enc_size
);
944 const unsigned char *part_enc_data
= m_enc_data
.m_part_status
.Encode(m_file
->m_SrcpartFrequency
, part_enc_size
);
947 parent
->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS
, part_enc_size
, part_enc_data
));
950 // Put data inside of tag in following order:
951 // [num_of_gaps] [gap_enc_data]
953 unsigned char *tagdata
;
954 CECTag
etag(EC_TAG_PARTFILE_GAP_STATUS
,
955 sizeof(uint32
) + gap_enc_size
, (void **)&tagdata
);
957 // real number of gaps - so remote size can realloc
958 RawPokeUInt32( tagdata
, ENDIAN_HTONL( gap_list_size
) );
959 tagdata
+= sizeof(uint32
);
960 memcpy(tagdata
, gap_enc_data
, gap_enc_size
);
962 parent
->AddTag(etag
);
964 it
= m_gap_buffer
.begin();
966 const CPartFile::CReqBlockPtrList
& requestedblocks
= m_file
->GetRequestedBlockList();
967 CPartFile::CReqBlockPtrList::const_iterator curr_pos2
= requestedblocks
.begin();
969 wxASSERT(m_gap_buffer
.size() >= requestedblocks
.size() * 2);
970 for ( ; curr_pos2
!= requestedblocks
.end(); ++curr_pos2
) {
971 Requested_Block_Struct
* block
= *curr_pos2
;
972 *it
++ = ENDIAN_HTONLL(block
->StartOffset
);
973 *it
++ = ENDIAN_HTONLL(block
->EndOffset
);
975 parent
->AddTag(CECTag(EC_TAG_PARTFILE_REQ_STATUS
,
976 requestedblocks
.size() * 2 * sizeof(uint64
), (void *)&m_gap_buffer
[0]));
980 CKnownFile_Encoder::CKnownFile_Encoder(CKnownFile
*file
) :
981 m_enc_data(file
->GetPartCount(), true)
986 CKnownFile_Encoder::CKnownFile_Encoder()
991 CKnownFile_Encoder::~CKnownFile_Encoder()
995 CKnownFile_Encoder::CKnownFile_Encoder(const CKnownFile_Encoder
&obj
) : m_enc_data(obj
.m_enc_data
)
1000 CKnownFile_Encoder
&CKnownFile_Encoder::operator=(const CKnownFile_Encoder
&obj
)
1002 m_file
= obj
.m_file
;
1003 m_enc_data
= obj
.m_enc_data
;
1007 void CKnownFile_Encoder::Encode(CECTag
*parent
)
1010 const unsigned char *part_enc_data
= m_enc_data
.Encode(m_file
->m_AvailPartFrequency
, part_enc_size
);
1012 parent
->AddTag(CECTag(EC_TAG_PARTFILE_PART_STATUS
, part_enc_size
, part_enc_data
));
1015 CECPacket
*GetStatsGraphs(const CECPacket
*request
)
1017 CECPacket
*response
= NULL
;
1019 switch (request
->GetDetailLevel()) {
1021 case EC_DETAIL_FULL
: {
1022 double dTimestamp
= 0.0;
1023 if (request
->GetTagByName(EC_TAG_STATSGRAPH_LAST
) != NULL
) {
1024 dTimestamp
= request
->GetTagByName(EC_TAG_STATSGRAPH_LAST
)->GetDoubleData();
1026 uint16 nScale
= request
->GetTagByNameSafe(EC_TAG_STATSGRAPH_SCALE
)->GetInt();
1027 uint16 nMaxPoints
= request
->GetTagByNameSafe(EC_TAG_STATSGRAPH_WIDTH
)->GetInt();
1029 unsigned int numPoints
= theApp
->m_statistics
->GetHistoryForWeb(nMaxPoints
, (double)nScale
, &dTimestamp
, &graphData
);
1031 response
= new CECPacket(EC_OP_STATSGRAPHS
);
1032 response
->AddTag(CECTag(EC_TAG_STATSGRAPH_DATA
, 4 * numPoints
* sizeof(uint32
), graphData
));
1033 delete [] graphData
;
1034 response
->AddTag(CECTag(EC_TAG_STATSGRAPH_LAST
, dTimestamp
));
1036 response
= new CECPacket(EC_OP_FAILED
);
1037 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("No points for graph.")));
1041 case EC_DETAIL_INC_UPDATE
:
1042 case EC_DETAIL_UPDATE
:
1045 response
= new CECPacket(EC_OP_FAILED
);
1046 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Your client is not configured for this detail level.")));
1050 response
= new CECPacket(EC_OP_FAILED
);
1057 CECPacket
*ExternalConn::ProcessRequest2(const CECPacket
*request
,
1058 CPartFile_Encoder_Map
&enc_part_map
, CKnownFile_Encoder_Map
&enc_shared_map
, CObjTagMap
&objmap
)
1065 CECPacket
*response
= NULL
;
1067 switch (request
->GetOpCode()) {
1071 case EC_OP_SHUTDOWN
:
1072 if (!theApp
->IsOnShutDown()) {
1073 response
= new CECPacket(EC_OP_NOOP
);
1074 AddLogLineM(true, _("External Connection: shutdown requested"));
1075 #ifndef AMULE_DAEMON
1078 evt
.SetCanVeto(false);
1079 theApp
->ShutDown(evt
);
1082 theApp
->ExitMainLoop();
1085 response
= new CECPacket(EC_OP_FAILED
);
1086 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Already shutting down.")));
1089 case EC_OP_ADD_LINK
:
1090 for(unsigned int i
= 0; i
< request
->GetTagCount();i
++) {
1091 const CECTag
*tag
= request
->GetTagByIndex(i
);
1092 wxString link
= tag
->GetStringData();
1093 int category
= tag
->GetTagByIndexSafe(0)->GetInt();
1094 AddLogLineM(true, CFormat(_("ExternalConn: adding link '%s'.")) % link
);
1095 if ( theApp
->downloadqueue
->AddLink(link
, category
) ) {
1096 response
= new CECPacket(EC_OP_NOOP
);
1098 // Error messages are printed by the add function.
1099 response
= new CECPacket(EC_OP_FAILED
);
1100 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid link or already on list.")));
1107 case EC_OP_STAT_REQ
:
1108 response
= Get_EC_Response_StatRequest(request
);
1109 case EC_OP_GET_CONNSTATE
:
1111 response
= new CECPacket(EC_OP_MISC_DATA
);
1113 response
->AddTag(CEC_ConnState_Tag(request
->GetDetailLevel()));
1118 case EC_OP_GET_SHARED_FILES
:
1119 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1120 response
= Get_EC_Response_GetSharedFiles(enc_shared_map
, objmap
);
1122 response
= Get_EC_Response_GetSharedFiles(request
, enc_shared_map
);
1125 case EC_OP_GET_DLOAD_QUEUE
:
1126 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1127 response
= Get_EC_Response_GetDownloadQueue(enc_part_map
, objmap
);
1129 response
= Get_EC_Response_GetDownloadQueue(request
, enc_part_map
);
1132 // transmit source names and comments only if file detail dialog is open
1133 case EC_OP_GET_DLOAD_QUEUE_DETAIL
:
1134 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1135 response
= Get_EC_Response_GetDownloadQueue(enc_part_map
, objmap
);
1137 response
= Get_EC_Response_GetDownloadQueue(request
, enc_part_map
, true);
1140 case EC_OP_GET_ULOAD_QUEUE
:
1141 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1142 response
= Get_EC_Response_GetUpQueue(objmap
);
1144 response
= Get_EC_Response_GetUpQueue(request
);
1147 case EC_OP_GET_WAIT_QUEUE
:
1148 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1149 response
= Get_EC_Response_GetWaitQueue(objmap
);
1151 response
= Get_EC_Response_GetWaitQueue(request
);
1154 case EC_OP_PARTFILE_REMOVE_NO_NEEDED
:
1155 case EC_OP_PARTFILE_REMOVE_FULL_QUEUE
:
1156 case EC_OP_PARTFILE_REMOVE_HIGH_QUEUE
:
1157 case EC_OP_PARTFILE_CLEANUP_SOURCES
:
1158 case EC_OP_PARTFILE_SWAP_A4AF_THIS
:
1159 case EC_OP_PARTFILE_SWAP_A4AF_THIS_AUTO
:
1160 case EC_OP_PARTFILE_SWAP_A4AF_OTHERS
:
1161 case EC_OP_PARTFILE_PAUSE
:
1162 case EC_OP_PARTFILE_RESUME
:
1163 case EC_OP_PARTFILE_STOP
:
1164 case EC_OP_PARTFILE_PRIO_SET
:
1165 case EC_OP_PARTFILE_DELETE
:
1166 case EC_OP_PARTFILE_SET_CAT
:
1167 response
= Get_EC_Response_PartFile_Cmd(request
);
1169 case EC_OP_SHAREDFILES_RELOAD
:
1170 theApp
->sharedfiles
->Reload();
1171 response
= new CECPacket(EC_OP_NOOP
);
1173 case EC_OP_SHARED_SET_PRIO
:
1174 response
= Get_EC_Response_Set_SharedFile_Prio(request
);
1176 case EC_OP_RENAME_FILE
: {
1177 CMD4Hash fileHash
= request
->GetTagByNameSafe(EC_TAG_KNOWNFILE
)->GetMD4Data();
1178 CKnownFile
* file
= theApp
->knownfiles
->FindKnownFileByID(fileHash
);
1179 wxString newName
= request
->GetTagByNameSafe(EC_TAG_PARTFILE_NAME
)->GetStringData();
1181 file
= theApp
->downloadqueue
->GetFileByID(fileHash
);
1184 response
= new CECPacket(EC_OP_FAILED
);
1185 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("File not found.")));
1188 if (newName
.IsEmpty()) {
1189 response
= new CECPacket(EC_OP_FAILED
);
1190 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid file name.")));
1194 if (theApp
->sharedfiles
->RenameFile(file
, CPath(newName
))) {
1195 response
= new CECPacket(EC_OP_NOOP
);
1197 response
= new CECPacket(EC_OP_FAILED
);
1198 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Unable to rename file.")));
1208 case EC_OP_SERVER_ADD
:
1209 response
= Get_EC_Response_Server_Add(request
);
1211 case EC_OP_SERVER_DISCONNECT
:
1212 case EC_OP_SERVER_CONNECT
:
1213 case EC_OP_SERVER_REMOVE
:
1214 response
= Get_EC_Response_Server(request
);
1216 case EC_OP_GET_SERVER_LIST
: {
1217 response
= new CECPacket(EC_OP_SERVER_LIST
);
1218 EC_DETAIL_LEVEL detail_level
= request
->GetDetailLevel();
1219 std::vector
<const CServer
*> servers
= theApp
->serverlist
->CopySnapshot();
1221 std::vector
<const CServer
*>::const_iterator it
= servers
.begin();
1222 it
!= servers
.end();
1225 response
->AddTag(CEC_Server_Tag(*it
, detail_level
));
1229 case EC_OP_SERVER_UPDATE_FROM_URL
: {
1230 wxString url
= request
->GetTagByIndexSafe(0)->GetStringData();
1232 // Save the new url, and update the UI (if not amuled).
1233 Notify_ServersURLChanged(url
);
1234 thePrefs::SetEd2kServersUrl(url
);
1236 theApp
->serverlist
->UpdateServerMetFromURL(url
);
1237 response
= new CECPacket(EC_OP_NOOP
);
1243 case EC_OP_IPFILTER_RELOAD
:
1244 theApp
->ipfilter
->Reload();
1245 response
= new CECPacket(EC_OP_NOOP
);
1250 case EC_OP_SEARCH_START
:
1251 response
= Get_EC_Response_Search(request
);
1254 case EC_OP_SEARCH_STOP
:
1255 response
= Get_EC_Response_Search_Stop(request
);
1258 case EC_OP_SEARCH_RESULTS
:
1259 if ( request
->GetDetailLevel() == EC_DETAIL_INC_UPDATE
) {
1260 response
= Get_EC_Response_Search_Results(objmap
);
1262 response
= Get_EC_Response_Search_Results(request
);
1266 case EC_OP_SEARCH_PROGRESS
:
1267 response
= new CECPacket(EC_OP_SEARCH_PROGRESS
);
1268 response
->AddTag(CECTag(EC_TAG_SEARCH_STATUS
,
1269 theApp
->searchlist
->GetSearchProgress()));
1272 case EC_OP_DOWNLOAD_SEARCH_RESULT
:
1273 response
= Get_EC_Response_Search_Results_Download(request
);
1278 case EC_OP_GET_PREFERENCES
:
1279 response
= new CEC_Prefs_Packet(request
->GetTagByNameSafe(EC_TAG_SELECT_PREFS
)->GetInt(), request
->GetDetailLevel());
1281 case EC_OP_SET_PREFERENCES
:
1282 ((CEC_Prefs_Packet
*)request
)->Apply();
1283 theApp
->glob_prefs
->Save();
1284 if (thePrefs::IsFilteringClients()) {
1285 theApp
->clientlist
->FilterQueues();
1287 if (thePrefs::IsFilteringServers()) {
1288 theApp
->serverlist
->FilterServers();
1290 if (!thePrefs::GetNetworkED2K() && theApp
->IsConnectedED2K()) {
1291 theApp
->DisconnectED2K();
1293 if (!thePrefs::GetNetworkKademlia() && theApp
->IsConnectedKad()) {
1296 response
= new CECPacket(EC_OP_NOOP
);
1299 case EC_OP_CREATE_CATEGORY
:
1300 if ( request
->GetTagCount() == 1 ) {
1301 ((CEC_Category_Tag
*)request
->GetTagByIndex(0))->Create();
1302 Notify_CategoryAdded();
1304 response
= new CECPacket(EC_OP_NOOP
);
1306 case EC_OP_UPDATE_CATEGORY
:
1307 if ( request
->GetTagCount() == 1 ) {
1308 CEC_Category_Tag
*tag
= (CEC_Category_Tag
*)request
->GetTagByIndex(0);
1310 Notify_CategoryUpdate(tag
->GetInt());
1312 response
= new CECPacket(EC_OP_NOOP
);
1314 case EC_OP_DELETE_CATEGORY
:
1315 if ( request
->GetTagCount() == 1 ) {
1316 uint32 cat
= request
->GetTagByIndex(0)->GetInt();
1317 Notify_CategoryDelete(cat
);
1319 response
= new CECPacket(EC_OP_NOOP
);
1325 case EC_OP_ADDLOGLINE
:
1326 AddLogLineM( (request
->GetTagByName(EC_TAG_LOG_TO_STATUS
) != NULL
), request
->GetTagByNameSafe(EC_TAG_STRING
)->GetStringData() );
1327 response
= new CECPacket(EC_OP_NOOP
);
1329 case EC_OP_ADDDEBUGLOGLINE
:
1330 AddDebugLogLineM( (request
->GetTagByName(EC_TAG_LOG_TO_STATUS
) != NULL
), logGeneral
, request
->GetTagByNameSafe(EC_TAG_STRING
)->GetStringData() );
1331 response
= new CECPacket(EC_OP_NOOP
);
1334 response
= new CECPacket(EC_OP_LOG
);
1335 response
->AddTag(CECTag(EC_TAG_STRING
, theApp
->GetLog(false)));
1337 case EC_OP_GET_DEBUGLOG
:
1338 response
= new CECPacket(EC_OP_DEBUGLOG
);
1339 response
->AddTag(CECTag(EC_TAG_STRING
, theApp
->GetDebugLog(false)));
1341 case EC_OP_RESET_LOG
:
1342 theApp
->GetLog(true);
1343 response
= new CECPacket(EC_OP_NOOP
);
1345 case EC_OP_RESET_DEBUGLOG
:
1346 theApp
->GetDebugLog(true);
1347 response
= new CECPacket(EC_OP_NOOP
);
1349 case EC_OP_GET_LAST_LOG_ENTRY
:
1351 wxString tmp
= theApp
->GetLog(false);
1352 if (tmp
.Last() == '\n') {
1355 response
= new CECPacket(EC_OP_LOG
);
1356 response
->AddTag(CECTag(EC_TAG_STRING
, tmp
.AfterLast('\n')));
1359 case EC_OP_GET_SERVERINFO
:
1360 response
= new CECPacket(EC_OP_SERVERINFO
);
1361 response
->AddTag(CECTag(EC_TAG_STRING
, theApp
->GetServerLog(false)));
1363 case EC_OP_CLEAR_SERVERINFO
:
1364 theApp
->GetServerLog(true);
1365 response
= new CECPacket(EC_OP_NOOP
);
1370 case EC_OP_GET_STATSGRAPHS
:
1371 response
= GetStatsGraphs(request
);
1373 case EC_OP_GET_STATSTREE
: {
1374 theApp
->m_statistics
->UpdateStatsTree();
1375 response
= new CECPacket(EC_OP_STATSTREE
);
1376 CECTag
* tree
= theStats::GetECStatTree(request
->GetTagByNameSafe(EC_TAG_STATTREE_CAPPING
)->GetInt());
1378 response
->AddTag(*tree
);
1381 if (request
->GetDetailLevel() == EC_DETAIL_WEB
) {
1382 response
->AddTag(CECTag(EC_TAG_SERVER_VERSION
, wxT(VERSION
)));
1383 response
->AddTag(CECTag(EC_TAG_USER_NICK
, thePrefs::GetUserNick()));
1391 case EC_OP_KAD_START
:
1392 response
= Get_EC_Response_Kad_Connect(request
);
1394 case EC_OP_KAD_STOP
:
1396 response
= new CECPacket(EC_OP_NOOP
);
1398 case EC_OP_KAD_UPDATE_FROM_URL
: {
1399 wxString url
= request
->GetTagByIndexSafe(0)->GetStringData();
1401 // Save the new url, and update the UI (if not amuled).
1402 Notify_NodesURLChanged(url
);
1403 thePrefs::SetKadNodesUrl(url
);
1405 theApp
->UpdateNotesDat(url
);
1406 response
= new CECPacket(EC_OP_NOOP
);
1409 case EC_OP_KAD_BOOTSTRAP_FROM_IP
:
1410 theApp
->BootstrapKad(request
->GetTagByIndexSafe(0)->GetInt(),
1411 request
->GetTagByIndexSafe(1)->GetInt());
1412 response
= new CECPacket(EC_OP_NOOP
);
1419 if (thePrefs::GetNetworkED2K()) {
1420 response
= new CECPacket(EC_OP_STRINGS
);
1421 if (theApp
->IsConnectedED2K()) {
1422 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Already connected to eD2k.")));
1424 theApp
->serverconnect
->ConnectToAnyServer();
1425 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Connecting to eD2k...")));
1428 if (thePrefs::GetNetworkKademlia()) {
1430 response
= new CECPacket(EC_OP_STRINGS
);
1432 if (theApp
->IsConnectedKad()) {
1433 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Already connected to Kad.")));
1436 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Connecting to Kad...")));
1440 response
= new CECPacket(EC_OP_FAILED
);
1441 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("All networks are disabled.")));
1444 case EC_OP_DISCONNECT
:
1445 if (theApp
->IsConnected()) {
1446 response
= new CECPacket(EC_OP_STRINGS
);
1447 if (theApp
->IsConnectedED2K()) {
1448 theApp
->serverconnect
->Disconnect();
1449 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Disconnected from eD2k.")));
1451 if (theApp
->IsConnectedKad()) {
1453 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Disconnected from Kad.")));
1456 response
= new CECPacket(EC_OP_NOOP
);
1461 AddLogLineM(false, wxString::Format(_("External Connection: invalid opcode received: %#x"), request
->GetOpCode()));
1463 response
= new CECPacket(EC_OP_FAILED
);
1464 response
->AddTag(CECTag(EC_TAG_STRING
, wxTRANSLATE("Invalid opcode (wrong protocol version?)")));
1471 * Here notification-based EC. Notification will be sorted by priority for possible throttling.
1475 * Core general status
1477 ECStatusMsgSource::ECStatusMsgSource()
1481 CECPacket
*ECStatusMsgSource::GetNextPacket()
1483 CECPacket
*response
= new CECPacket(EC_OP_STATS
);
1484 response
->AddTag(CEC_ConnState_Tag(EC_DETAIL_UPDATE
));
1491 ECPartFileMsgSource::ECPartFileMsgSource()
1493 PARTFILE_STATUS status
= { true, false, false, false };
1494 for (unsigned int i
= 0; i
< theApp
->downloadqueue
->GetFileCount(); i
++) {
1495 CPartFile
*cur_file
= theApp
->downloadqueue
->GetFileByIndex(i
);
1496 m_dirty_status
[cur_file
->GetFileHash()] = status
;
1500 void ECPartFileMsgSource::SetDirty(CPartFile
*file
)
1502 CMD4Hash filehash
= file
->GetFileHash();
1503 if ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() ) {
1505 // entry already present, meaning "dirty" flag is set
1509 PARTFILE_STATUS status
= { false, false, false, false };
1510 m_dirty_status
[filehash
] = status
;
1513 void ECPartFileMsgSource::SetNew(CPartFile
*file
)
1515 CMD4Hash filehash
= file
->GetFileHash();
1516 wxASSERT ( m_dirty_status
.find(filehash
) == m_dirty_status
.end() );
1517 PARTFILE_STATUS status
= { true, false, false, false };
1518 m_dirty_status
[filehash
] = status
;
1521 void ECPartFileMsgSource::SetCompleted(CPartFile
*file
)
1523 CMD4Hash filehash
= file
->GetFileHash();
1524 wxASSERT ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() );
1526 m_dirty_status
[filehash
].m_finished
= true;
1529 void ECPartFileMsgSource::SetRemoved(CPartFile
*file
)
1531 CMD4Hash filehash
= file
->GetFileHash();
1532 wxASSERT ( m_dirty_status
.find(filehash
) != m_dirty_status
.end() );
1534 m_dirty_status
[filehash
].m_removed
= true;
1537 CECPacket
*ECPartFileMsgSource::GetNextPacket()
1539 if ( m_dirty_status
.empty() ) {
1542 std::map
<CMD4Hash
, PARTFILE_STATUS
>::iterator it
= m_dirty_status
.begin();
1543 CMD4Hash filehash
= it
->first
;
1545 CPartFile
*partfile
= theApp
->downloadqueue
->GetFileByID(filehash
);
1547 CECPacket
*packet
= new CECPacket(EC_OP_DLOAD_QUEUE
);
1548 CEC_PartFile_Tag
tag(partfile
, EC_DETAIL_UPDATE
);
1549 packet
->AddTag(tag
);
1551 m_dirty_status
.erase(it
);
1557 * Notification about search status
1559 ECSearchMsgSource::ECSearchMsgSource()
1563 CECPacket
*ECSearchMsgSource::GetNextPacket()
1569 * Notification about uploading clients
1571 CECPacket
*ECClientMsgSource::GetNextPacket()
1577 // Notification iface per-client
1579 ECNotifier::ECNotifier()
1583 CECPacket
*ECNotifier::GetNextPacket(ECUpdateMsgSource
*msg_source_array
[])
1585 CECPacket
*packet
= 0;
1587 // priority 0 is highest
1589 for(int i
= 0; i
< EC_STATUS_LAST_PRIO
; i
++) {
1590 if ( (packet
= msg_source_array
[i
]->GetNextPacket()) != 0 ) {
1597 CECPacket
*ECNotifier::GetNextPacket(CECServerSocket
*sock
)
1599 ECUpdateMsgSource
**notifier_array
= m_msg_source
[sock
];
1601 // OnOutput is called for a first time before
1602 // socket is registered
1604 if ( !notifier_array
) {
1607 CECPacket
*packet
= GetNextPacket(notifier_array
);
1612 // Interface to notification macros
1614 void ECNotifier::DownloadFile_SetDirty(CPartFile
*file
)
1616 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
1617 i
!= m_msg_source
.end(); i
++) {
1618 ECUpdateMsgSource
**notifier_array
= i
->second
;
1619 ((ECPartFileMsgSource
*)notifier_array
[EC_PARTFILE
])->SetDirty(file
);
1621 NextPacketToSocket();
1624 void ECNotifier::DownloadFile_RemoveFile(CPartFile
*file
)
1626 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
1627 i
!= m_msg_source
.end(); i
++) {
1628 ECUpdateMsgSource
**notifier_array
= i
->second
;
1629 ((ECPartFileMsgSource
*)notifier_array
[EC_PARTFILE
])->SetRemoved(file
);
1633 void ECNotifier::DownloadFile_RemoveSource(CPartFile
*)
1635 // per-partfile source list is not supported (yet), and IMHO quite useless
1638 void ECNotifier::DownloadFile_AddFile(CPartFile
*file
)
1640 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
1641 i
!= m_msg_source
.end(); i
++) {
1642 ECUpdateMsgSource
**notifier_array
= i
->second
;
1643 ((ECPartFileMsgSource
*)notifier_array
[EC_PARTFILE
])->SetNew(file
);
1647 void ECNotifier::DownloadFile_AddSource(CPartFile
*)
1649 // per-partfile source list is not supported (yet), and IMHO quite useless
1652 void ECNotifier::Add_EC_Client(CECServerSocket
*sock
)
1654 ECUpdateMsgSource
**notifier_array
= new ECUpdateMsgSource
*[EC_STATUS_LAST_PRIO
];
1655 notifier_array
[EC_STATUS
] = new ECStatusMsgSource();
1656 notifier_array
[EC_SEARCH
] = new ECSearchMsgSource();
1657 notifier_array
[EC_PARTFILE
] = new ECPartFileMsgSource();
1658 notifier_array
[EC_CLIENT
] = new ECClientMsgSource();
1660 m_msg_source
[sock
] = notifier_array
;
1663 void ECNotifier::Remove_EC_Client(CECServerSocket
*sock
)
1665 ECUpdateMsgSource
**notifier_array
= m_msg_source
[sock
];
1666 m_msg_source
.erase(sock
);
1668 for(int i
= 0; i
< EC_STATUS_LAST_PRIO
; i
++) {
1669 delete notifier_array
[i
];
1671 delete [] notifier_array
;
1674 void ECNotifier::NextPacketToSocket()
1676 for(std::map
<CECServerSocket
*, ECUpdateMsgSource
**>::iterator i
= m_msg_source
.begin();
1677 i
!= m_msg_source
.end(); i
++) {
1678 CECServerSocket
*sock
= i
->first
;
1679 if ( sock
->HaveNotificationSupport() && !sock
->DataPending() ) {
1680 ECUpdateMsgSource
**notifier_array
= i
->second
;
1681 CECPacket
*packet
= GetNextPacket(notifier_array
);
1682 sock
->SendPacket(packet
);
1687 // File_checked_for_headers