2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include <wx/app.h> // Needed for wxApp
32 #include <wx/intl.h> // Needed for wxLocale
35 #include "Types.h" // Needed for int32, uint16 and uint64
39 // #include <wx/unix/execute.h>
43 # include "config.h" // Needed for ASIO_SOCKETS
55 class CSharedFileList
;
63 class CCanceledFileList
;
65 class CClientCreditsList
;
67 class CClientUDPSocket
;
69 class UploadBandwidthThrottler
;
76 class CUPnPControlPoint
;
77 class CUPnPPortMapping
;
82 class wxFFileOutputStream
;
85 class wxSingleInstanceChecker
;
87 class CMuleInternalEvent
;
88 class CCompletionEvent
;
89 class CAllocFinishedEvent
;
94 namespace MuleNotify
{
98 using MuleNotify::CMuleGUIEvent
;
107 #define AMULE_APP_BASE wxAppConsole
108 #define CORE_TIMER_PERIOD 300
110 #define AMULE_APP_BASE wxApp
111 #define CORE_TIMER_PERIOD 100
114 #define CONNECTED_ED2K (1<<0)
115 #define CONNECTED_KAD_NOT (1<<1)
116 #define CONNECTED_KAD_OK (1<<2)
117 #define CONNECTED_KAD_FIREWALLED (1<<3)
120 // Base class common to amule, aamuled and amulegui
121 class CamuleAppCommon
124 // Used to detect a previous running instance of aMule
125 wxSingleInstanceChecker
* m_singleInstance
;
127 bool CheckPassedLink(const wxString
&in
, wxString
&out
, int cat
);
129 wxString FullMuleVersion
;
130 wxString OSDescription
;
132 bool enable_daemon_fork
;
134 bool m_skipConnectionDialog
;
135 bool m_geometryEnabled
;
136 wxString m_geometryString
;
141 bool InitCommon(int argc
, wxChar
** argv
);
142 void RefreshSingleInstanceChecker();
143 bool CheckMuleDirectory(const wxString
& desc
, const class CPath
& directory
, const wxString
& alternative
, class CPath
& outDir
);
145 wxString m_configFile
;
149 void AddLinksFromFile();
151 wxString
CreateMagnetLink(const CAbstractFile
*f
);
152 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool add_cryptoptions
= false, bool add_AICH
= false);
155 bool IsDaemon() const { return true; }
157 bool IsDaemon() const { return false; }
161 bool IsRemoteGui() const { return true; }
163 bool IsRemoteGui() const { return false; }
166 const wxString
& GetMuleAppName() const { return m_appName
; }
167 const wxString
GetFullMuleVersion() const;
170 class CamuleApp
: public AMULE_APP_BASE
, public CamuleAppCommon
174 APP_STATE_RUNNING
= 0,
175 APP_STATE_SHUTTINGDOWN
,
181 virtual ~CamuleApp();
183 virtual bool OnInit();
185 #if wxUSE_ON_FATAL_EXCEPTION
186 void OnFatalException();
188 bool ReinitializeNetwork(wxString
*msg
);
190 // derived classes may override those
191 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
195 void ListenSocketHandler(wxSocketEvent
& event
);
196 void UDPSocketHandler(wxSocketEvent
& event
);
199 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
201 // Barry - To find out if app is running or shutting/shut down
202 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
203 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
205 // Check ED2K and Kademlia state
206 bool IsFirewalled() const;
207 // Are we connected to at least one network?
208 bool IsConnected() const;
209 // Connection to ED2K
210 bool IsConnectedED2K() const;
212 // What about Kad? Is it running?
213 bool IsKadRunning() const;
215 bool IsConnectedKad() const;
216 // Check Kad state (TCP)
217 bool IsFirewalledKad() const;
218 // Check Kad state (UDP)
219 bool IsFirewalledKadUDP() const;
220 // Check Kad state (LAN mode)
221 bool IsKadRunningInLanMode() const;
223 uint32
GetKadUsers() const;
224 uint32
GetKadFiles() const;
225 uint32
GetKadIndexedSources() const;
226 uint32
GetKadIndexedKeywords() const;
227 uint32
GetKadIndexedNotes() const;
228 uint32
GetKadIndexedLoad() const;
229 // True IP of machine
230 uint32
GetKadIPAdress() const;
232 uint8
GetBuddyStatus() const;
233 uint32
GetBuddyIP() const;
234 uint32
GetBuddyPort() const;
236 const Kademlia::CUInt128
& GetKadID() const;
238 // Check if we should callback this client
239 bool CanDoCallback(uint32 clientServerIP
, uint16 clientServerPort
);
242 void OnlineSig(bool zero
= false);
243 void Localize_mule();
244 void Trigger_New_version(wxString newMule
);
246 // shakraw - new EC code using wxSocketBase
247 ExternalConn
* ECServerHandler
;
249 // return current (valid) public IP or 0 if unknown
250 // If ignorelocal is true, don't use m_localip
251 uint32
GetPublicIP(bool ignorelocal
= false) const;
252 void SetPublicIP(const uint32 dwIP
);
254 uint32
GetED2KID() const;
255 uint32
GetID() const;
257 // Other parts of the interface and such
258 CPreferences
* glob_prefs
;
259 CDownloadQueue
* downloadqueue
;
260 CUploadQueue
* uploadqueue
;
261 CServerConnect
* serverconnect
;
262 CSharedFileList
* sharedfiles
;
263 CServerList
* serverlist
;
264 CListenSocket
* listensocket
;
265 CClientList
* clientlist
;
266 CKnownFileList
* knownfiles
;
267 CCanceledFileList
* canceledfiles
;
268 CSearchList
* searchlist
;
269 CClientCreditsList
* clientcredits
;
270 CFriendList
* friendlist
;
271 CClientUDPSocket
* clientudp
;
272 CStatistics
* m_statistics
;
274 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
276 CAsioService
* m_AsioService
;
279 CUPnPControlPoint
* m_upnp
;
280 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
286 wxString
GetLog(bool reset
= false);
287 wxString
GetServerLog(bool reset
= false);
288 wxString
GetDebugLog(bool reset
= false);
290 bool AddServer(CServer
*srv
, bool fromUser
= false);
291 void AddServerMessageLine(wxString
&msg
);
293 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
295 void SetOSFiles(const wxString
& new_path
);
297 const wxString
& GetOSType() const { return OSType
; }
299 void ShowUserCount();
301 void ShowConnectionState(bool forceUpdate
= false);
306 /** Bootstraps kad from the specified IP (must be in hostorder). */
307 void BootstrapKad(uint32 ip
, uint16 port
);
308 /** Updates the nodes.dat file from the specified url. */
309 void UpdateNotesDat(const wxString
& str
);
312 void DisconnectED2K();
314 bool CryptoAvailable() const;
320 * Handles asserts in a thread-safe manner.
322 virtual void OnAssertFailure(const wxChar
* file
, int line
,
323 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
326 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
327 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
328 void OnServerDnsDone(CMuleInternalEvent
& evt
);
330 void OnTCPTimer(CTimerEvent
& evt
);
331 void OnCoreTimer(CTimerEvent
& evt
);
333 void OnFinishedHashing(CHashingEvent
& evt
);
334 void OnFinishedAICHHashing(CHashingEvent
& evt
);
335 void OnFinishedCompletion(CCompletionEvent
& evt
);
336 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
337 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
338 void OnHashingShutdown(CMuleInternalEvent
&);
339 void OnNotifyEvent(CMuleGUIEvent
& evt
);
341 void SetTimeOnTransfer();
343 APPState m_app_state
;
345 wxString m_emulesig_path
;
346 wxString m_amulesig_path
;
357 virtual void OnUnhandledException();
359 void CheckNewVersion(uint32 result
);
368 class CamuleGuiBase
{
371 virtual ~CamuleGuiBase();
373 wxString m_FrameTitle
;
376 bool CopyTextToClipboard( wxString strText
);
379 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
380 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
382 void AddGuiLogLine(const wxString
& line
);
385 * This list is used to contain log messages that are to be displayed
386 * on the GUI, when it is currently impossible to do so. This is in order
387 * to allows us to queue messages till after the dialog has been created.
389 std::list
<wxString
> m_logLines
;
396 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
399 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
406 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
408 void ShutDown(wxCloseEvent
&evt
);
410 wxString
GetLog(bool reset
= false);
411 wxString
GetServerLog(bool reset
= false);
412 void AddServerMessageLine(wxString
&msg
);
413 DECLARE_EVENT_TABLE()
417 DECLARE_APP(CamuleGuiApp
)
418 extern CamuleGuiApp
*theApp
;
421 #else /* !CLIENT_GUI */
424 #include "amule-remote-gui.h"
430 #define CALL_APP_DATA_LOCK
433 #else /* ! AMULE_DAEMON */
435 // wxWidgets 2.8 requires special code for event handling and sockets.
436 // 2.9 doesn't, so standard event loop and sockets can be used
438 // Windows: aMuled compiles with 2.8 (without the special code),
439 // but works only with 2.9
441 #if !wxCHECK_VERSION(2, 9, 0)
442 // wx 2.8 needs a hand-made event loop in any case
443 #define AMULED28_EVENTLOOP
445 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
448 # include "config.h" // defines ASIO_SOCKETS
452 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
454 #define AMULED28_SOCKETS
459 #ifdef AMULED28_SOCKETS
460 #include <wx/socket.h>
465 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
468 CSocketSet
*m_in_set
, *m_out_set
;
472 CAmuledGSocketFuncTable();
474 void AddSocket(GSocket
*socket
, GSocketEvent event
);
475 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
478 virtual bool OnInit();
479 virtual void OnExit();
480 virtual bool CanUseEventLoop();
481 virtual bool Init_Socket(GSocket
*socket
);
482 virtual void Destroy_Socket(GSocket
*socket
);
483 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
484 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
485 virtual void Enable_Events(GSocket
*socket
);
486 virtual void Disable_Events(GSocket
*socket
);
490 #endif // AMULED28_SOCKETS
492 // AppTrait functionality is required for 2.8 wx sockets
493 // Otherwise it's used to prevent zombie child processes,
494 // which stops working with wx 2.9.5.
495 // So disable it there (no idea if this has a noticeable impact).
497 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__)
498 #define AMULED_APPTRAITS
501 #ifdef AMULED_APPTRAITS
503 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
505 #include <wx/apptrait.h>
507 class CDaemonAppTraits
: public wxConsoleAppTraits
510 struct sigaction m_oldSignalChildAction
;
511 struct sigaction m_newSignalChildAction
;
513 #ifdef AMULED28_SOCKETS
514 CAmuledGSocketFuncTable
*m_table
;
516 std::list
<wxObject
*> m_sched_delete
;
518 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
519 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
520 virtual void ScheduleForDestroy(wxObject
*object
);
521 virtual void RemoveFromPendingDelete(wxObject
*object
);
523 void DeletePending();
524 #else // AMULED28_SOCKETS
527 #endif // !AMULED28_SOCKETS
529 virtual int WaitForChild(wxExecuteData
& execData
);
531 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
532 virtual wxStandardPathsBase
& GetStandardPaths();
536 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
537 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
539 #endif // AMULED_APPTRAITS
542 class CamuleDaemonApp
: public CamuleApp
545 #ifdef AMULED28_EVENTLOOP
548 #ifdef AMULED28_SOCKETS
549 CAmuledGSocketFuncTable
*m_table
;
555 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
557 #ifdef AMULED_APPTRAITS
558 struct sigaction m_oldSignalChildAction
;
559 struct sigaction m_newSignalChildAction
;
561 wxAppTraits
*CreateTraits();
562 #endif // AMULED_APPTRAITS
566 #ifdef AMULED28_EVENTLOOP
569 void ExitMainLoop() { m_Exit
= true; }
572 bool CopyTextToClipboard(wxString strText
);
574 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
576 DECLARE_EVENT_TABLE()
579 DECLARE_APP(CamuleDaemonApp
)
580 extern CamuleDaemonApp
*theApp
;
582 #endif /* ! AMULE_DAEMON */
585 // File_checked_for_headers