2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 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>
51 class CSharedFileList
;
60 class CClientCreditsList
;
62 class CClientUDPSocket
;
64 class UploadBandwidthThrottler
;
66 class CUPnPControlPoint
;
67 class CUPnPPortMapping
;
72 class wxFFileOutputStream
;
76 class wxSingleInstanceChecker
;
78 class CMuleInternalEvent
;
79 class CCompletionEvent
;
80 class CAllocFinishedEvent
;
85 namespace MuleNotify
{
90 using MuleNotify::CMuleGUIEvent
;
94 #define AMULE_APP_BASE wxAppConsole
95 #define CORE_TIMER_PERIOD 300
97 #define AMULE_APP_BASE wxApp
98 #define CORE_TIMER_PERIOD 100
101 #define CONNECTED_ED2K (1<<0)
102 #define CONNECTED_KAD_NOT (1<<1)
103 #define CONNECTED_KAD_OK (1<<2)
104 #define CONNECTED_KAD_FIREWALLED (1<<3)
107 class CamuleApp
: public AMULE_APP_BASE
111 APP_STATE_RUNNING
= 0,
112 APP_STATE_SHUTTINGDOWN
,
118 virtual ~CamuleApp();
120 virtual bool OnInit();
122 void OnFatalException();
123 bool ReinitializeNetwork(wxString
*msg
);
125 // derived classes may override those
126 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
129 void ListenSocketHandler(wxSocketEvent
& event
);
130 void ServerSocketHandler(wxSocketEvent
& event
);
131 void UDPSocketHandler(wxSocketEvent
& event
);
133 virtual int ShowAlert(wxString msg
, wxString title
, int flags
) = 0;
135 // Barry - To find out if app is running or shutting/shut down
136 bool IsRunning() const { return (m_app_state
== APP_STATE_RUNNING
); }
137 bool IsOnShutDown() const { return (m_app_state
== APP_STATE_SHUTTINGDOWN
); }
139 // Check ED2K and Kademlia state
140 bool IsFirewalled() const;
141 // Are we connected to at least one network?
142 bool IsConnected() const;
143 // Connection to ED2K
144 bool IsConnectedED2K() const;
146 // What about Kad? Is it running?
147 bool IsKadRunning() const;
149 bool IsConnectedKad() const;
150 // Check Kad state (TCP)
151 bool IsFirewalledKad() const;
152 // Check Kad state (UDP)
153 bool IsFirewalledKadUDP() const;
155 uint32
GetKadUsers() const;
156 uint32
GetKadFiles() const;
157 uint32
GetKadIndexedSources() const;
158 uint32
GetKadIndexedKeywords() const;
159 uint32
GetKadIndexedNotes() const;
160 uint32
GetKadIndexedLoad() const;
161 // True IP of machine
162 uint32
GetKadIPAdress() const;
164 uint8
GetBuddyStatus() const;
165 uint32
GetBuddyIP() const;
166 uint32
GetBuddyPort() const;
168 // Check if we should callback this client
169 bool CanDoCallback(CUpDownClient
*client
);
172 wxString
CreateMagnetLink(const CAbstractFile
*f
);
173 wxString
CreateED2kLink(const CAbstractFile
* f
, bool add_source
= false, bool use_hostname
= false, bool addcryptoptions
= false);
174 wxString
CreateED2kAICHLink(const CKnownFile
* f
);
177 void OnlineSig(bool zero
= false);
178 void Localize_mule();
179 void Trigger_New_version(wxString newMule
);
181 // shakraw - new EC code using wxSocketBase
182 ExternalConn
* ECServerHandler
;
184 // return current (valid) public IP or 0 if unknown
185 // If ignorelocal is true, don't use m_localip
186 uint32
GetPublicIP(bool ignorelocal
= false) const;
187 void SetPublicIP(const uint32 dwIP
);
189 uint32
GetED2KID() const;
190 uint32
GetID() const;
192 // Other parts of the interface and such
193 CPreferences
* glob_prefs
;
194 CDownloadQueue
* downloadqueue
;
195 CUploadQueue
* uploadqueue
;
196 CServerConnect
* serverconnect
;
197 CSharedFileList
* sharedfiles
;
198 CServerList
* serverlist
;
199 CListenSocket
* listensocket
;
200 CClientList
* clientlist
;
201 CKnownFileList
* knownfiles
;
202 CSearchList
* searchlist
;
203 CClientCreditsList
* clientcredits
;
204 CFriendList
* friendlist
;
205 CClientUDPSocket
* clientudp
;
206 CStatistics
* m_statistics
;
208 UploadBandwidthThrottler
* uploadBandwidthThrottler
;
210 CUPnPControlPoint
* m_upnp
;
211 std::vector
<CUPnPPortMapping
> m_upnpMappings
;
217 wxString
GetLog(bool reset
= false);
218 wxString
GetServerLog(bool reset
= false);
219 wxString
GetDebugLog(bool reset
= false);
221 bool AddServer(CServer
*srv
, bool fromUser
= false);
222 void AddServerMessageLine(wxString
&msg
);
224 void AddSocketDeleteDebug(uint32 socket_pointer
, uint32 creation_time
);
226 void SetOSFiles(const wxString new_path
);
230 const wxString
& GetOSType() const { return OSType
; }
232 void ShowUserCount();
234 void ShowConnectionState();
239 /** Bootstraps kad from the specified IP (must be in hostorder). */
240 void BootstrapKad(uint32 ip
, uint16 port
);
241 /** Updates the nodes.dat file from the specified url. */
242 void UpdateNotesDat(const wxString
& str
);
245 void DisconnectED2K();
247 bool CryptoAvailable() const;
250 // Used to detect a previous running instance of aMule
251 wxSingleInstanceChecker
* m_singleInstance
;
255 * Handles asserts in a thread-safe manner.
257 virtual void OnAssertFailure(const wxChar
* file
, int line
,
258 const wxChar
* func
, const wxChar
* cond
, const wxChar
* msg
);
261 void OnUDPDnsDone(CMuleInternalEvent
& evt
);
262 void OnSourceDnsDone(CMuleInternalEvent
& evt
);
263 void OnServerDnsDone(CMuleInternalEvent
& evt
);
265 void OnTCPTimer(CTimerEvent
& evt
);
266 void OnCoreTimer(CTimerEvent
& evt
);
268 void OnFinishedHashing(CHashingEvent
& evt
);
269 void OnFinishedAICHHashing(CHashingEvent
& evt
);
270 void OnFinishedCompletion(CCompletionEvent
& evt
);
271 void OnFinishedAllocation(CAllocFinishedEvent
& evt
);
272 void OnFinishedHTTPDownload(CMuleInternalEvent
& evt
);
273 void OnHashingShutdown(CMuleInternalEvent
&);
274 void OnNotifyEvent(CMuleGUIEvent
& evt
);
276 void SetTimeOnTransfer();
278 APPState m_app_state
;
280 wxString m_emulesig_path
;
281 wxString m_amulesig_path
;
283 char *strFullMuleVersion
;
284 char *strOSDescription
;
291 bool enable_daemon_fork
;
299 virtual void OnUnhandledException();
301 void CheckNewVersion(uint32 result
);
310 class CamuleGuiBase
{
313 virtual ~CamuleGuiBase();
315 wxString m_FrameTitle
;
317 int m_FileDetailDialogActive
;
319 bool CopyTextToClipboard( wxString strText
);
321 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
322 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
324 void AddGuiLogLine(const wxString
& line
);
327 * This list is used to contain log messages that are to be displayed
328 * on the GUI, when it is currently impossible to do so. This is in order
329 * to allows us to queue messages till after the dialog has been created.
331 std::list
<wxString
> m_logLines
;
338 class CamuleGuiApp
: public CamuleApp
, public CamuleGuiBase
341 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
348 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
350 void ShutDown(wxCloseEvent
&evt
);
352 wxString
GetLog(bool reset
= false);
353 wxString
GetServerLog(bool reset
= false);
354 void AddServerMessageLine(wxString
&msg
);
355 DECLARE_EVENT_TABLE()
359 DECLARE_APP(CamuleGuiApp
)
361 CamuleGuiApp
*theApp
;
363 extern CamuleGuiApp
*theApp
;
367 #else /* !CLIENT_GUI */
370 #include "amule-remote-gui.h"
376 #define CALL_APP_DATA_LOCK
379 #else /* ! AMULE_DAEMON */
381 // wxWidgets 2.8 requires special code for event handling and sockets.
382 // 2.9 doesn't, so standard event loop and sockets can be used
384 // Windows: aMuled compiles with 2.8 (without the special code),
385 // but works only with 2.9
387 #if !wxCHECK_VERSION(2, 9, 0)
389 // MSW: can't run amuled with 2.8 anyway, just get it compiled
397 #include <wx/socket.h>
402 class CAmuledGSocketFuncTable
: public GSocketGUIFunctionsTable
405 CSocketSet
*m_in_set
, *m_out_set
;
409 CAmuledGSocketFuncTable();
411 void AddSocket(GSocket
*socket
, GSocketEvent event
);
412 void RemoveSocket(GSocket
*socket
, GSocketEvent event
);
415 virtual bool OnInit();
416 virtual void OnExit();
417 virtual bool CanUseEventLoop();
418 virtual bool Init_Socket(GSocket
*socket
);
419 virtual void Destroy_Socket(GSocket
*socket
);
420 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
);
421 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
);
422 virtual void Enable_Events(GSocket
*socket
);
423 virtual void Disable_Events(GSocket
*socket
);
429 // no AppTraits used on Windows
432 typedef std::map
<int, class wxEndProcessData
*> EndProcessDataMap
;
434 #include <wx/apptrait.h>
436 class CDaemonAppTraits
: public wxConsoleAppTraits
439 struct sigaction m_oldSignalChildAction
;
440 struct sigaction m_newSignalChildAction
;
443 CAmuledGSocketFuncTable
*m_table
;
445 std::list
<wxObject
*> m_sched_delete
;
447 CDaemonAppTraits(CAmuledGSocketFuncTable
*table
);
448 virtual GSocketGUIFunctionsTable
* GetSocketGUIFunctionsTable();
449 virtual void ScheduleForDestroy(wxObject
*object
);
450 virtual void RemoveFromPendingDelete(wxObject
*object
);
452 void DeletePending();
458 virtual int WaitForChild(wxExecuteData
& execData
);
460 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
461 virtual wxStandardPathsBase
& GetStandardPaths();
465 void OnSignalChildHandler(int signal
, siginfo_t
*siginfo
, void *ucontext
);
466 pid_t
AmuleWaitPid(pid_t pid
, int *status
, int options
, wxString
*msg
);
470 class CamuleDaemonApp
: public CamuleApp
475 CAmuledGSocketFuncTable
*m_table
;
481 virtual int InitGui(bool geometry_enable
, wxString
&geometry_string
);
484 struct sigaction m_oldSignalChildAction
;
485 struct sigaction m_newSignalChildAction
;
487 wxAppTraits
*CreateTraits();
495 void ExitMainLoop() { m_Exit
= true; }
499 void ExitMainLoop() {}
502 bool CopyTextToClipboard(wxString strText
);
504 virtual int ShowAlert(wxString msg
, wxString title
, int flags
);
506 DECLARE_EVENT_TABLE()
509 DECLARE_APP(CamuleDaemonApp
)
511 CamuleDaemonApp
*theApp
;
513 extern CamuleDaemonApp
*theApp
;
516 #endif /* ! AMULE_DAEMON */
519 // File_checked_for_headers