Upstream tarball 20080304
[amule.git] / src / amule.h
blob2c0c703b348c9c2b958123c20b1f7a7c7922e26d
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002 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.
20 //
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 #ifndef __WXMSW__
37 #include <map>
38 #include <signal.h>
39 #include <wx/unix/execute.h>
40 #endif // __WXMSW__
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 CSearchList;
60 class CClientCreditsList;
61 class CFriendList;
62 class CClientUDPSocket;
63 class CIPFilter;
64 class UploadBandwidthThrottler;
65 #ifdef ENABLE_UPNP
66 class CUPnPControlPoint;
67 class CUPnPPortMapping;
68 #endif
69 class CStatistics;
70 class wxSocketEvent;
71 class wxCommandEvent;
72 class wxFFileOutputStream;
73 class CUpDownClient;
74 class CTimer;
75 class CTimerEvent;
76 class wxSingleInstanceChecker;
77 class CHashingEvent;
78 class CMuleInternalEvent;
79 class CCompletionEvent;
80 class wxExecuteData;
81 class CLoggingEvent;
84 namespace MuleNotify {
85 class CMuleGUIEvent;
89 using MuleNotify::CMuleGUIEvent;
92 #ifdef AMULE_DAEMON
93 #define AMULE_APP_BASE wxAppConsole
94 #else
95 #define AMULE_APP_BASE wxApp
96 #endif
98 #define CONNECTED_ED2K (1<<0)
99 #define CONNECTED_KAD_NOT (1<<1)
100 #define CONNECTED_KAD_OK (1<<2)
101 #define CONNECTED_KAD_FIREWALLED (1<<3)
104 class CamuleApp : public AMULE_APP_BASE
106 private:
107 enum APPState {
108 APP_STATE_RUNNING = 0,
109 APP_STATE_SHUTTINGDOWN,
110 APP_STATE_STARTING
113 public:
114 CamuleApp();
115 virtual ~CamuleApp();
117 virtual bool OnInit();
118 int OnExit();
119 void OnFatalException();
120 bool ReinitializeNetwork(wxString *msg);
122 // derived classes may override those
123 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
125 // Socket handlers
126 void ListenSocketHandler(wxSocketEvent& event);
127 void ServerSocketHandler(wxSocketEvent& event);
128 void UDPSocketHandler(wxSocketEvent& event);
130 virtual void ShowAlert(wxString msg, wxString title, int flags) = 0;
132 // Barry - To find out if app is running or shutting/shut down
133 const bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
134 const bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
136 // Check ED2K and Kademlia state
137 bool IsFirewalled();
138 // Check Kad state
139 bool IsFirewalledKad();
140 // Check if we should callback this client
141 bool DoCallback( CUpDownClient *client );
143 // Connection to ED2K
144 bool IsConnectedED2K();
145 // Connection to Kad
146 bool IsConnectedKad();
147 // Are we connected to at least one network?
148 bool IsConnected();
150 // What about Kad? Is it running?
151 bool IsKadRunning();
153 // URL functions
154 wxString CreateMagnetLink(const CAbstractFile *f);
155 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool addcryptoptions = false);
156 wxString CreateED2kAICHLink(const CKnownFile* f);
158 // Misc functions
159 void OnlineSig(bool zero = false);
160 void Localize_mule();
161 void Trigger_New_version(wxString newMule);
163 // shakraw - new EC code using wxSocketBase
164 ExternalConn* ECServerHandler;
166 // return current (valid) public IP or 0 if unknown
167 // If ignorelocal is true, don't use m_localip
168 uint32 GetPublicIP(bool ignorelocal = false) const;
169 void SetPublicIP(const uint32 dwIP);
171 uint32 GetED2KID() const;
172 uint32 GetID() const;
174 // Other parts of the interface and such
175 CPreferences* glob_prefs;
176 CDownloadQueue* downloadqueue;
177 CUploadQueue* uploadqueue;
178 CServerConnect* serverconnect;
179 CSharedFileList* sharedfiles;
180 CServerList* serverlist;
181 CListenSocket* listensocket;
182 CClientList* clientlist;
183 CKnownFileList* knownfiles;
184 CSearchList* searchlist;
185 CClientCreditsList* clientcredits;
186 CFriendList* friendlist;
187 CClientUDPSocket* clientudp;
188 CStatistics* m_statistics;
189 CIPFilter* ipfilter;
190 UploadBandwidthThrottler* uploadBandwidthThrottler;
191 #ifdef ENABLE_UPNP
192 CUPnPControlPoint* m_upnp;
193 std::vector<CUPnPPortMapping> m_upnpMappings;
194 #endif
195 wxLocale m_locale;
197 void ShutDown();
199 wxString GetLog(bool reset = false);
200 wxString GetServerLog(bool reset = false);
201 wxString GetDebugLog(bool reset = false);
203 bool AddServer(CServer *srv, bool fromUser = false);
204 void AddServerMessageLine(wxString &msg);
205 #ifdef __DEBUG__
206 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
207 #endif
208 void SetOSFiles(const wxString new_path);
210 wxString ConfigDir;
212 void AddLogLine(const wxString &msg);
214 const wxString& GetOSType() const { return OSType; }
216 void ShowUserCount();
218 void ShowConnectionState();
220 void StartKad();
221 void StopKad();
223 /** Bootstraps kad from the specified IP (must be in hostorder). */
224 void BootstrapKad(uint32 ip, uint16 port);
225 /** Updates the nodes.dat file from the specified url. */
226 void UpdateNotesDat(const wxString& str);
229 void DisconnectED2K();
231 bool CryptoAvailable() const;
233 //! TODO: Move to CLogger
234 wxFFileOutputStream* applog;
235 protected:
236 // Used to detect a previous running instance of aMule
237 wxSingleInstanceChecker* m_singleInstance;
239 #ifdef __WXDEBUG__
241 * Handles asserts in a thread-safe manner.
243 virtual void OnAssertFailure(const wxChar* file, int line,
244 const wxChar* func, const wxChar* cond, const wxChar* msg);
245 #endif
248 * This class is used to contain log messages that are to be displayed
249 * on the GUI, when it is currently impossible to do so. This is in order
250 * to allows us to queue messages till after the dialog has been created.
252 struct QueuedLogLine
254 //! The text line to be displayed
255 wxString line;
256 //! True if the line should be shown on the status bar, false otherwise.
257 bool show;
260 void OnUDPDnsDone(CMuleInternalEvent& evt);
261 void OnSourceDnsDone(CMuleInternalEvent& evt);
262 void OnServerDnsDone(CMuleInternalEvent& evt);
264 void OnTCPTimer(CTimerEvent& evt);
265 void OnCoreTimer(CTimerEvent& evt);
267 void OnFinishedHashing(CHashingEvent& evt);
268 void OnFinishedAICHHashing(CHashingEvent& evt);
269 void OnFinishedCompletion(CCompletionEvent& evt);
270 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
271 void OnHashingShutdown(CMuleInternalEvent&);
272 void OnNotifyEvent(CMuleGUIEvent& evt);
274 void SetTimeOnTransfer();
276 std::list<QueuedLogLine> m_logLines;
278 APPState m_app_state;
280 wxString m_emulesig_path;
281 wxString m_amulesig_path;
283 char *strFullMuleVersion;
284 char *strOSDescription;
285 wxString OSType;
287 uint32 m_dwPublicIP;
289 long webserver_pid;
291 bool enable_stdout_log;
292 bool enable_daemon_fork;
293 wxString server_msg;
295 CTimer* core_timer;
297 private:
298 virtual void OnUnhandledException();
300 void CheckNewVersion(uint32 result);
302 uint32 m_localip;
306 #ifndef AMULE_DAEMON
309 class CamuleGuiBase {
310 public:
311 CamuleGuiBase();
312 virtual ~CamuleGuiBase();
314 wxString m_FrameTitle;
315 CamuleDlg* amuledlg;
316 int m_FileDetailDialogActive;
318 bool CopyTextToClipboard( wxString strText );
320 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
321 virtual void ShowAlert(wxString msg, wxString title, int flags);
325 #ifndef CLIENT_GUI
328 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
331 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
333 int OnExit();
334 bool OnInit();
336 public:
338 virtual void ShowAlert(wxString msg, wxString title, int flags);
340 void ShutDown(wxCloseEvent &evt);
341 void OnLoggingEvent(CLoggingEvent& evt);
343 wxString GetLog(bool reset = false);
344 wxString GetServerLog(bool reset = false);
345 void AddServerMessageLine(wxString &msg);
346 DECLARE_EVENT_TABLE()
350 DECLARE_APP(CamuleGuiApp)
351 #ifdef AMULE_CPP
352 CamuleGuiApp *theApp;
353 #else
354 extern CamuleGuiApp *theApp;
355 #endif
358 #else /* !CLIENT_GUI */
361 #include "amule-remote-gui.h"
364 #endif // CLIENT_GUI
367 #define CALL_APP_DATA_LOCK
370 #else /* ! AMULE_DAEMON */
373 #include <wx/apptrait.h>
374 #include <wx/socket.h>
377 class CSocketSet;
380 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
382 private:
383 CSocketSet *m_in_set, *m_out_set;
385 wxMutex m_lock;
386 public:
387 CAmuledGSocketFuncTable();
389 void AddSocket(GSocket *socket, GSocketEvent event);
390 void RemoveSocket(GSocket *socket, GSocketEvent event);
391 void RunSelect();
393 virtual bool OnInit();
394 virtual void OnExit();
395 virtual bool CanUseEventLoop();
396 virtual bool Init_Socket(GSocket *socket);
397 virtual void Destroy_Socket(GSocket *socket);
398 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
399 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
400 virtual void Enable_Events(GSocket *socket);
401 virtual void Disable_Events(GSocket *socket);
405 typedef std::map<int, wxEndProcessData *> EndProcessDataMap;
408 class CDaemonAppTraits : public wxConsoleAppTraits
410 private:
411 CAmuledGSocketFuncTable *m_table;
412 wxMutex m_lock;
413 std::list<wxObject *> m_sched_delete;
414 #ifndef __WXMSW__
415 struct sigaction m_oldSignalChildAction;
416 struct sigaction m_newSignalChildAction;
417 #endif
419 public:
420 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
421 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
422 virtual void ScheduleForDestroy(wxObject *object);
423 virtual void RemoveFromPendingDelete(wxObject *object);
425 void DeletePending();
427 #ifndef __WXMSW__
428 virtual int WaitForChild(wxExecuteData& execData);
429 #endif
430 #ifdef __WXMAC__
431 virtual wxStandardPathsBase& GetStandardPaths();
432 #endif
436 #ifndef __WXMSW__
437 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
438 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
439 #endif // __WXMSW__
442 class CamuleDaemonApp : public CamuleApp
444 private:
445 bool m_Exit;
446 CAmuledGSocketFuncTable *m_table;
447 #ifndef __WXMSW__
448 struct sigaction m_oldSignalChildAction;
449 struct sigaction m_newSignalChildAction;
450 #endif // __WXMSW__
452 bool OnInit();
453 int OnRun();
454 int OnExit();
456 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
458 public:
459 CamuleDaemonApp();
461 void ExitMainLoop() { m_Exit = true; }
463 bool CopyTextToClipboard(wxString strText);
465 virtual void ShowAlert(wxString msg, wxString title, int flags);
467 void OnLoggingEvent(CLoggingEvent& evt);
469 DECLARE_EVENT_TABLE()
471 wxAppTraits *CreateTraits();
474 DECLARE_APP(CamuleDaemonApp)
475 #ifdef AMULE_CPP
476 CamuleDaemonApp *theApp;
477 #else
478 extern CamuleDaemonApp *theApp;
479 #endif
481 #endif /* ! AMULE_DAEMON */
483 #endif // AMULE_H
484 // File_checked_for_headers