Upstream tarball 20080626
[amule.git] / src / amule.h
blob158e06b76f4ebbe81663512fe75f2654bdacdd3a
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 CAllocFinishedEvent;
81 class wxExecuteData;
82 class CLoggingEvent;
85 namespace MuleNotify {
86 class CMuleGUIEvent;
90 using MuleNotify::CMuleGUIEvent;
93 #ifdef AMULE_DAEMON
94 #define AMULE_APP_BASE wxAppConsole
95 #else
96 #define AMULE_APP_BASE wxApp
97 #endif
99 #define CONNECTED_ED2K (1<<0)
100 #define CONNECTED_KAD_NOT (1<<1)
101 #define CONNECTED_KAD_OK (1<<2)
102 #define CONNECTED_KAD_FIREWALLED (1<<3)
105 class CamuleApp : public AMULE_APP_BASE
107 private:
108 enum APPState {
109 APP_STATE_RUNNING = 0,
110 APP_STATE_SHUTTINGDOWN,
111 APP_STATE_STARTING
114 public:
115 CamuleApp();
116 virtual ~CamuleApp();
118 virtual bool OnInit();
119 int OnExit();
120 void OnFatalException();
121 bool ReinitializeNetwork(wxString *msg);
123 // derived classes may override those
124 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
126 // Socket handlers
127 void ListenSocketHandler(wxSocketEvent& event);
128 void ServerSocketHandler(wxSocketEvent& event);
129 void UDPSocketHandler(wxSocketEvent& event);
131 virtual void ShowAlert(wxString msg, wxString title, int flags) = 0;
133 // Barry - To find out if app is running or shutting/shut down
134 const bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
135 const bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
137 // Check ED2K and Kademlia state
138 bool IsFirewalled();
139 // Check Kad state
140 bool IsFirewalledKad();
141 // Check if we should callback this client
142 bool DoCallback( CUpDownClient *client );
144 // Connection to ED2K
145 bool IsConnectedED2K();
146 // Connection to Kad
147 bool IsConnectedKad();
148 // Are we connected to at least one network?
149 bool IsConnected();
151 // What about Kad? Is it running?
152 bool IsKadRunning();
154 // URL functions
155 wxString CreateMagnetLink(const CAbstractFile *f);
156 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool addcryptoptions = false);
157 wxString CreateED2kAICHLink(const CKnownFile* f);
159 // Misc functions
160 void OnlineSig(bool zero = false);
161 void Localize_mule();
162 void Trigger_New_version(wxString newMule);
164 // shakraw - new EC code using wxSocketBase
165 ExternalConn* ECServerHandler;
167 // return current (valid) public IP or 0 if unknown
168 // If ignorelocal is true, don't use m_localip
169 uint32 GetPublicIP(bool ignorelocal = false) const;
170 void SetPublicIP(const uint32 dwIP);
172 uint32 GetED2KID() const;
173 uint32 GetID() const;
175 // Other parts of the interface and such
176 CPreferences* glob_prefs;
177 CDownloadQueue* downloadqueue;
178 CUploadQueue* uploadqueue;
179 CServerConnect* serverconnect;
180 CSharedFileList* sharedfiles;
181 CServerList* serverlist;
182 CListenSocket* listensocket;
183 CClientList* clientlist;
184 CKnownFileList* knownfiles;
185 CSearchList* searchlist;
186 CClientCreditsList* clientcredits;
187 CFriendList* friendlist;
188 CClientUDPSocket* clientudp;
189 CStatistics* m_statistics;
190 CIPFilter* ipfilter;
191 UploadBandwidthThrottler* uploadBandwidthThrottler;
192 #ifdef ENABLE_UPNP
193 CUPnPControlPoint* m_upnp;
194 std::vector<CUPnPPortMapping> m_upnpMappings;
195 #endif
196 wxLocale m_locale;
198 void ShutDown();
200 wxString GetLog(bool reset = false);
201 wxString GetServerLog(bool reset = false);
202 wxString GetDebugLog(bool reset = false);
204 bool AddServer(CServer *srv, bool fromUser = false);
205 void AddServerMessageLine(wxString &msg);
206 #ifdef __DEBUG__
207 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
208 #endif
209 void SetOSFiles(const wxString new_path);
211 wxString ConfigDir;
213 void AddLogLine(const wxString &msg);
215 const wxString& GetOSType() const { return OSType; }
217 void ShowUserCount();
219 void ShowConnectionState();
221 void StartKad();
222 void StopKad();
224 /** Bootstraps kad from the specified IP (must be in hostorder). */
225 void BootstrapKad(uint32 ip, uint16 port);
226 /** Updates the nodes.dat file from the specified url. */
227 void UpdateNotesDat(const wxString& str);
230 void DisconnectED2K();
232 bool CryptoAvailable() const;
234 //! TODO: Move to CLogger
235 wxFFileOutputStream* applog;
236 protected:
237 // Used to detect a previous running instance of aMule
238 wxSingleInstanceChecker* m_singleInstance;
240 #ifdef __WXDEBUG__
242 * Handles asserts in a thread-safe manner.
244 virtual void OnAssertFailure(const wxChar* file, int line,
245 const wxChar* func, const wxChar* cond, const wxChar* msg);
246 #endif
249 * This class is used to contain log messages that are to be displayed
250 * on the GUI, when it is currently impossible to do so. This is in order
251 * to allows us to queue messages till after the dialog has been created.
253 struct QueuedLogLine
255 //! The text line to be displayed
256 wxString line;
257 //! True if the line should be shown on the status bar, false otherwise.
258 bool show;
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 std::list<QueuedLogLine> m_logLines;
280 APPState m_app_state;
282 wxString m_emulesig_path;
283 wxString m_amulesig_path;
285 char *strFullMuleVersion;
286 char *strOSDescription;
287 wxString OSType;
289 uint32 m_dwPublicIP;
291 long webserver_pid;
293 bool enable_stdout_log;
294 bool enable_daemon_fork;
295 wxString server_msg;
297 CTimer* core_timer;
299 private:
300 virtual void OnUnhandledException();
302 void CheckNewVersion(uint32 result);
304 uint32 m_localip;
308 #ifndef AMULE_DAEMON
311 class CamuleGuiBase {
312 public:
313 CamuleGuiBase();
314 virtual ~CamuleGuiBase();
316 wxString m_FrameTitle;
317 CamuleDlg* amuledlg;
318 int m_FileDetailDialogActive;
320 bool CopyTextToClipboard( wxString strText );
322 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
323 virtual void ShowAlert(wxString msg, wxString title, int flags);
327 #ifndef CLIENT_GUI
330 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
333 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
335 int OnExit();
336 bool OnInit();
338 public:
340 virtual void ShowAlert(wxString msg, wxString title, int flags);
342 void ShutDown(wxCloseEvent &evt);
343 void OnLoggingEvent(CLoggingEvent& evt);
345 wxString GetLog(bool reset = false);
346 wxString GetServerLog(bool reset = false);
347 void AddServerMessageLine(wxString &msg);
348 DECLARE_EVENT_TABLE()
352 DECLARE_APP(CamuleGuiApp)
353 #ifdef AMULE_CPP
354 CamuleGuiApp *theApp;
355 #else
356 extern CamuleGuiApp *theApp;
357 #endif
360 #else /* !CLIENT_GUI */
363 #include "amule-remote-gui.h"
366 #endif // CLIENT_GUI
369 #define CALL_APP_DATA_LOCK
372 #else /* ! AMULE_DAEMON */
375 #include <wx/apptrait.h>
376 #include <wx/socket.h>
379 class CSocketSet;
382 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
384 private:
385 CSocketSet *m_in_set, *m_out_set;
387 wxMutex m_lock;
388 public:
389 CAmuledGSocketFuncTable();
391 void AddSocket(GSocket *socket, GSocketEvent event);
392 void RemoveSocket(GSocket *socket, GSocketEvent event);
393 void RunSelect();
395 virtual bool OnInit();
396 virtual void OnExit();
397 virtual bool CanUseEventLoop();
398 virtual bool Init_Socket(GSocket *socket);
399 virtual void Destroy_Socket(GSocket *socket);
400 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
401 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
402 virtual void Enable_Events(GSocket *socket);
403 virtual void Disable_Events(GSocket *socket);
407 typedef std::map<int, wxEndProcessData *> EndProcessDataMap;
410 class CDaemonAppTraits : public wxConsoleAppTraits
412 private:
413 CAmuledGSocketFuncTable *m_table;
414 wxMutex m_lock;
415 std::list<wxObject *> m_sched_delete;
416 #ifndef __WXMSW__
417 struct sigaction m_oldSignalChildAction;
418 struct sigaction m_newSignalChildAction;
419 #endif
421 public:
422 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
423 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
424 virtual void ScheduleForDestroy(wxObject *object);
425 virtual void RemoveFromPendingDelete(wxObject *object);
427 void DeletePending();
429 #ifndef __WXMSW__
430 virtual int WaitForChild(wxExecuteData& execData);
431 #endif
432 #ifdef __WXMAC__
433 virtual wxStandardPathsBase& GetStandardPaths();
434 #endif
438 #ifndef __WXMSW__
439 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
440 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
441 #endif // __WXMSW__
444 class CamuleDaemonApp : public CamuleApp
446 private:
447 bool m_Exit;
448 CAmuledGSocketFuncTable *m_table;
449 #ifndef __WXMSW__
450 struct sigaction m_oldSignalChildAction;
451 struct sigaction m_newSignalChildAction;
452 #endif // __WXMSW__
454 bool OnInit();
455 int OnRun();
456 int OnExit();
458 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
460 public:
461 CamuleDaemonApp();
463 void ExitMainLoop() { m_Exit = true; }
465 bool CopyTextToClipboard(wxString strText);
467 virtual void ShowAlert(wxString msg, wxString title, int flags);
469 void OnLoggingEvent(CLoggingEvent& evt);
471 DECLARE_EVENT_TABLE()
473 wxAppTraits *CreateTraits();
476 DECLARE_APP(CamuleDaemonApp)
477 #ifdef AMULE_CPP
478 CamuleDaemonApp *theApp;
479 #else
480 extern CamuleDaemonApp *theApp;
481 #endif
483 #endif /* ! AMULE_DAEMON */
485 #endif // AMULE_H
486 // File_checked_for_headers