Fix build with Boost from source
[amule.git] / src / amule.h
blobc849f621d7716edccb377377b546a78c4755c019
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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
27 #ifndef AMULE_H
28 #define AMULE_H
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
36 #include <map>
37 #ifndef __WINDOWS__
38 #include <signal.h>
39 // #include <wx/unix/execute.h>
40 #endif // __WINDOWS__
43 class CAbstractFile;
44 class CKnownFile;
45 class ExternalConn;
46 class CamuleDlg;
47 class CPreferences;
48 class CDownloadQueue;
49 class CUploadQueue;
50 class CServerConnect;
51 class CSharedFileList;
52 class CServer;
53 class CFriend;
54 class CMD4Hash;
55 class CServerList;
56 class CListenSocket;
57 class CClientList;
58 class CKnownFileList;
59 class CCanceledFileList;
60 class CSearchList;
61 class CClientCreditsList;
62 class CFriendList;
63 class CClientUDPSocket;
64 class CIPFilter;
65 class UploadBandwidthThrottler;
66 class CAsioService;
67 #ifdef ENABLE_UPNP
68 class CUPnPControlPoint;
69 class CUPnPPortMapping;
70 #endif
71 class CStatistics;
72 class wxSocketEvent;
73 class wxCommandEvent;
74 class wxCloseEvent;
75 class wxFFileOutputStream;
76 class CTimer;
77 class CTimerEvent;
78 class wxSingleInstanceChecker;
79 class CHashingEvent;
80 class CMuleInternalEvent;
81 class CCompletionEvent;
82 class CAllocFinishedEvent;
83 class wxExecuteData;
84 class CLoggingEvent;
87 namespace MuleNotify {
88 class CMuleGUIEvent;
92 using MuleNotify::CMuleGUIEvent;
95 #ifdef AMULE_DAEMON
96 #define AMULE_APP_BASE wxAppConsole
97 #define CORE_TIMER_PERIOD 300
98 #else
99 #define AMULE_APP_BASE wxApp
100 #define CORE_TIMER_PERIOD 100
101 #endif
103 #define CONNECTED_ED2K (1<<0)
104 #define CONNECTED_KAD_NOT (1<<1)
105 #define CONNECTED_KAD_OK (1<<2)
106 #define CONNECTED_KAD_FIREWALLED (1<<3)
109 // Base class common to amule, aamuled and amulegui
110 class CamuleAppCommon
112 private:
113 // Used to detect a previous running instance of aMule
114 wxSingleInstanceChecker* m_singleInstance;
116 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
117 protected:
118 wxString FullMuleVersion;
119 wxString OSDescription;
120 wxString OSType;
121 bool enable_daemon_fork;
122 bool ec_config;
123 bool m_skipConnectionDialog;
124 bool m_geometryEnabled;
125 wxString m_geometryString;
126 wxString m_logFile;
127 wxString m_appName;
128 wxString m_PidFile;
130 bool InitCommon(int argc, wxChar ** argv);
131 void RefreshSingleInstanceChecker();
132 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
133 public:
134 wxString m_configFile;
136 CamuleAppCommon();
137 ~CamuleAppCommon();
138 void AddLinksFromFile();
139 // URL functions
140 wxString CreateMagnetLink(const CAbstractFile *f);
141 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool add_cryptoptions = false, bool add_AICH = false);
142 // Who am I ?
143 #ifdef AMULE_DAEMON
144 bool IsDaemon() const { return true; }
145 #else
146 bool IsDaemon() const { return false; }
147 #endif
149 #ifdef CLIENT_GUI
150 bool IsRemoteGui() const { return true; }
151 #else
152 bool IsRemoteGui() const { return false; }
153 #endif
155 const wxString& GetMuleAppName() const { return m_appName; }
156 const wxString GetFullMuleVersion() const;
159 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
161 private:
162 enum APPState {
163 APP_STATE_RUNNING = 0,
164 APP_STATE_SHUTTINGDOWN,
165 APP_STATE_STARTING
168 public:
169 CamuleApp();
170 virtual ~CamuleApp();
172 virtual bool OnInit();
173 int OnExit();
174 #if wxUSE_ON_FATAL_EXCEPTION
175 void OnFatalException();
176 #endif
177 bool ReinitializeNetwork(wxString *msg);
179 // derived classes may override those
180 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
182 // Socket handlers
183 void ListenSocketHandler(wxSocketEvent& event);
184 void ServerSocketHandler(wxSocketEvent& event);
185 void UDPSocketHandler(wxSocketEvent& event);
187 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
189 // Barry - To find out if app is running or shutting/shut down
190 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
191 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
193 // Check ED2K and Kademlia state
194 bool IsFirewalled() const;
195 // Are we connected to at least one network?
196 bool IsConnected() const;
197 // Connection to ED2K
198 bool IsConnectedED2K() const;
200 // What about Kad? Is it running?
201 bool IsKadRunning() const;
202 // Connection to Kad
203 bool IsConnectedKad() const;
204 // Check Kad state (TCP)
205 bool IsFirewalledKad() const;
206 // Check Kad state (UDP)
207 bool IsFirewalledKadUDP() const;
208 // Check Kad state (LAN mode)
209 bool IsKadRunningInLanMode() const;
210 // Kad stats
211 uint32 GetKadUsers() const;
212 uint32 GetKadFiles() const;
213 uint32 GetKadIndexedSources() const;
214 uint32 GetKadIndexedKeywords() const;
215 uint32 GetKadIndexedNotes() const;
216 uint32 GetKadIndexedLoad() const;
217 // True IP of machine
218 uint32 GetKadIPAdress() const;
219 // Buddy status
220 uint8 GetBuddyStatus() const;
221 uint32 GetBuddyIP() const;
222 uint32 GetBuddyPort() const;
224 // Check if we should callback this client
225 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
227 // Misc functions
228 void OnlineSig(bool zero = false);
229 void Localize_mule();
230 void Trigger_New_version(wxString newMule);
232 // shakraw - new EC code using wxSocketBase
233 ExternalConn* ECServerHandler;
235 // return current (valid) public IP or 0 if unknown
236 // If ignorelocal is true, don't use m_localip
237 uint32 GetPublicIP(bool ignorelocal = false) const;
238 void SetPublicIP(const uint32 dwIP);
240 uint32 GetED2KID() const;
241 uint32 GetID() const;
243 // Other parts of the interface and such
244 CPreferences* glob_prefs;
245 CDownloadQueue* downloadqueue;
246 CUploadQueue* uploadqueue;
247 CServerConnect* serverconnect;
248 CSharedFileList* sharedfiles;
249 CServerList* serverlist;
250 CListenSocket* listensocket;
251 CClientList* clientlist;
252 CKnownFileList* knownfiles;
253 CCanceledFileList* canceledfiles;
254 CSearchList* searchlist;
255 CClientCreditsList* clientcredits;
256 CFriendList* friendlist;
257 CClientUDPSocket* clientudp;
258 CStatistics* m_statistics;
259 CIPFilter* ipfilter;
260 UploadBandwidthThrottler* uploadBandwidthThrottler;
261 CAsioService* m_AsioService;
262 #ifdef ENABLE_UPNP
263 CUPnPControlPoint* m_upnp;
264 std::vector<CUPnPPortMapping> m_upnpMappings;
265 #endif
266 wxLocale m_locale;
268 void ShutDown();
270 wxString GetLog(bool reset = false);
271 wxString GetServerLog(bool reset = false);
272 wxString GetDebugLog(bool reset = false);
274 bool AddServer(CServer *srv, bool fromUser = false);
275 void AddServerMessageLine(wxString &msg);
276 #ifdef __DEBUG__
277 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
278 #endif
279 void SetOSFiles(const wxString& new_path);
281 const wxString& GetOSType() const { return OSType; }
283 void ShowUserCount();
285 void ShowConnectionState(bool forceUpdate = false);
287 void StartKad();
288 void StopKad();
290 /** Bootstraps kad from the specified IP (must be in hostorder). */
291 void BootstrapKad(uint32 ip, uint16 port);
292 /** Updates the nodes.dat file from the specified url. */
293 void UpdateNotesDat(const wxString& str);
296 void DisconnectED2K();
298 bool CryptoAvailable() const;
300 protected:
302 #ifdef __WXDEBUG__
304 * Handles asserts in a thread-safe manner.
306 virtual void OnAssertFailure(const wxChar* file, int line,
307 const wxChar* func, const wxChar* cond, const wxChar* msg);
308 #endif
310 void OnUDPDnsDone(CMuleInternalEvent& evt);
311 void OnSourceDnsDone(CMuleInternalEvent& evt);
312 void OnServerDnsDone(CMuleInternalEvent& evt);
314 void OnTCPTimer(CTimerEvent& evt);
315 void OnCoreTimer(CTimerEvent& evt);
317 void OnFinishedHashing(CHashingEvent& evt);
318 void OnFinishedAICHHashing(CHashingEvent& evt);
319 void OnFinishedCompletion(CCompletionEvent& evt);
320 void OnFinishedAllocation(CAllocFinishedEvent& evt);
321 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
322 void OnHashingShutdown(CMuleInternalEvent&);
323 void OnNotifyEvent(CMuleGUIEvent& evt);
325 void SetTimeOnTransfer();
327 APPState m_app_state;
329 wxString m_emulesig_path;
330 wxString m_amulesig_path;
332 uint32 m_dwPublicIP;
334 long webserver_pid;
336 wxString server_msg;
338 CTimer* core_timer;
340 private:
341 virtual void OnUnhandledException();
343 void CheckNewVersion(uint32 result);
345 uint32 m_localip;
349 #ifndef AMULE_DAEMON
352 class CamuleGuiBase {
353 public:
354 CamuleGuiBase();
355 virtual ~CamuleGuiBase();
357 wxString m_FrameTitle;
358 CamuleDlg* amuledlg;
359 int m_FileDetailDialogActive;
361 bool CopyTextToClipboard( wxString strText );
362 void ResetTitle();
364 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
365 virtual int ShowAlert(wxString msg, wxString title, int flags);
367 void AddGuiLogLine(const wxString& line);
368 protected:
370 * This list is used to contain log messages that are to be displayed
371 * on the GUI, when it is currently impossible to do so. This is in order
372 * to allows us to queue messages till after the dialog has been created.
374 std::list<wxString> m_logLines;
378 #ifndef CLIENT_GUI
381 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
384 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
386 int OnExit();
387 bool OnInit();
389 public:
391 virtual int ShowAlert(wxString msg, wxString title, int flags);
393 void ShutDown(wxCloseEvent &evt);
395 wxString GetLog(bool reset = false);
396 wxString GetServerLog(bool reset = false);
397 void AddServerMessageLine(wxString &msg);
398 DECLARE_EVENT_TABLE()
402 DECLARE_APP(CamuleGuiApp)
403 extern CamuleGuiApp *theApp;
406 #else /* !CLIENT_GUI */
409 #include "amule-remote-gui.h"
412 #endif // CLIENT_GUI
415 #define CALL_APP_DATA_LOCK
418 #else /* ! AMULE_DAEMON */
420 // wxWidgets 2.8 requires special code for event handling and sockets.
421 // 2.9 doesn't, so standard event loop and sockets can be used
423 // Windows: aMuled compiles with 2.8 (without the special code),
424 // but works only with 2.9
426 #if !wxCHECK_VERSION(2, 9, 0)
427 // wx 2.8 needs a hand-made event loop in any case
428 #define AMULED28_EVENTLOOP
430 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
432 #ifdef HAVE_CONFIG_H
433 # include "config.h" // defines ASIO_SOCKETS
434 #endif
436 #ifndef ASIO_SOCKETS
437 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
438 #ifndef __WINDOWS__
439 #define AMULED28_SOCKETS
440 #endif
441 #endif
442 #endif
444 #ifdef AMULED28_SOCKETS
445 #include <wx/socket.h>
447 class CSocketSet;
450 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
452 private:
453 CSocketSet *m_in_set, *m_out_set;
455 wxMutex m_lock;
456 public:
457 CAmuledGSocketFuncTable();
459 void AddSocket(GSocket *socket, GSocketEvent event);
460 void RemoveSocket(GSocket *socket, GSocketEvent event);
461 void RunSelect();
463 virtual bool OnInit();
464 virtual void OnExit();
465 virtual bool CanUseEventLoop();
466 virtual bool Init_Socket(GSocket *socket);
467 virtual void Destroy_Socket(GSocket *socket);
468 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
469 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
470 virtual void Enable_Events(GSocket *socket);
471 virtual void Disable_Events(GSocket *socket);
475 #endif // AMULED28_SOCKETS
477 // AppTrait functionality is required for 2.8 wx sockets
478 // Otherwise it's used to prevent zombie child processes,
479 // which stops working with wx 2.9.5.
480 // So disable it there (no idea if this has a noticeable impact).
482 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__ )
483 #define AMULED_APPTRAITS
484 #endif
486 #ifdef AMULED_APPTRAITS
488 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
490 #include <wx/apptrait.h>
492 class CDaemonAppTraits : public wxConsoleAppTraits
494 private:
495 struct sigaction m_oldSignalChildAction;
496 struct sigaction m_newSignalChildAction;
498 #ifdef AMULED28_SOCKETS
499 CAmuledGSocketFuncTable *m_table;
500 wxMutex m_lock;
501 std::list<wxObject *> m_sched_delete;
502 public:
503 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
504 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
505 virtual void ScheduleForDestroy(wxObject *object);
506 virtual void RemoveFromPendingDelete(wxObject *object);
508 void DeletePending();
509 #else // AMULED28_SOCKETS
510 public:
511 CDaemonAppTraits();
512 #endif // !AMULED28_SOCKETS
514 virtual int WaitForChild(wxExecuteData& execData);
516 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
517 virtual wxStandardPathsBase& GetStandardPaths();
518 #endif
521 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
522 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
524 #endif // AMULED_APPTRAITS
527 class CamuleDaemonApp : public CamuleApp
529 private:
530 #ifdef AMULED28_EVENTLOOP
531 bool m_Exit;
532 #endif
533 #ifdef AMULED28_SOCKETS
534 CAmuledGSocketFuncTable *m_table;
535 #endif
536 bool OnInit();
537 int OnRun();
538 int OnExit();
540 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
542 #ifdef AMULED_APPTRAITS
543 struct sigaction m_oldSignalChildAction;
544 struct sigaction m_newSignalChildAction;
545 public:
546 wxAppTraits *CreateTraits();
547 #endif // AMULED_APPTRAITS
549 public:
551 #ifdef AMULED28_EVENTLOOP
552 CamuleDaemonApp();
554 void ExitMainLoop() { m_Exit = true; }
555 #endif
557 bool CopyTextToClipboard(wxString strText);
559 virtual int ShowAlert(wxString msg, wxString title, int flags);
561 DECLARE_EVENT_TABLE()
564 DECLARE_APP(CamuleDaemonApp)
565 extern CamuleDaemonApp *theApp;
567 #endif /* ! AMULE_DAEMON */
569 #endif // AMULE_H
570 // File_checked_for_headers