Upstream tarball 9597
[amule.git] / src / amule.h
blobde3398668bc07f565810c512e261dd1edc195be3
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-2008 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 #include <map>
37 #ifndef __WXMSW__
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 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
135 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
137 // Check ED2K and Kademlia state
138 bool IsFirewalled() const;
139 // Are we connected to at least one network?
140 bool IsConnected() const;
141 // Connection to ED2K
142 bool IsConnectedED2K() const;
144 // What about Kad? Is it running?
145 bool IsKadRunning() const;
146 // Connection to Kad
147 bool IsConnectedKad() const;
148 // Check Kad state (TCP)
149 bool IsFirewalledKad() const;
150 // Check Kad state (UDP)
151 bool IsFirewalledKadUDP() const;
152 // Kad stats
153 uint32 GetKadUsers() const;
154 uint32 GetKadFiles() const;
155 uint32 GetKadIndexedSources() const;
156 uint32 GetKadIndexedKeywords() const;
157 uint32 GetKadIndexedNotes() const;
158 uint32 GetKadIndexedLoad() const;
159 // True IP of machine
160 uint32 GetKadIPAdress() const;
161 // Buddy status
162 uint8 GetBuddyStatus() const;
163 uint32 GetBuddyIP() const;
164 uint32 GetBuddyPort() const;
166 // Check if we should callback this client
167 bool DoCallback( CUpDownClient *client );
169 // URL functions
170 wxString CreateMagnetLink(const CAbstractFile *f);
171 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool addcryptoptions = false);
172 wxString CreateED2kAICHLink(const CKnownFile* f);
174 // Misc functions
175 void OnlineSig(bool zero = false);
176 void Localize_mule();
177 void Trigger_New_version(wxString newMule);
179 // shakraw - new EC code using wxSocketBase
180 ExternalConn* ECServerHandler;
182 // return current (valid) public IP or 0 if unknown
183 // If ignorelocal is true, don't use m_localip
184 uint32 GetPublicIP(bool ignorelocal = false) const;
185 void SetPublicIP(const uint32 dwIP);
187 uint32 GetED2KID() const;
188 uint32 GetID() const;
190 // Other parts of the interface and such
191 CPreferences* glob_prefs;
192 CDownloadQueue* downloadqueue;
193 CUploadQueue* uploadqueue;
194 CServerConnect* serverconnect;
195 CSharedFileList* sharedfiles;
196 CServerList* serverlist;
197 CListenSocket* listensocket;
198 CClientList* clientlist;
199 CKnownFileList* knownfiles;
200 CSearchList* searchlist;
201 CClientCreditsList* clientcredits;
202 CFriendList* friendlist;
203 CClientUDPSocket* clientudp;
204 CStatistics* m_statistics;
205 CIPFilter* ipfilter;
206 UploadBandwidthThrottler* uploadBandwidthThrottler;
207 #ifdef ENABLE_UPNP
208 CUPnPControlPoint* m_upnp;
209 std::vector<CUPnPPortMapping> m_upnpMappings;
210 #endif
211 wxLocale m_locale;
213 void ShutDown();
215 wxString GetLog(bool reset = false);
216 wxString GetServerLog(bool reset = false);
217 wxString GetDebugLog(bool reset = false);
219 bool AddServer(CServer *srv, bool fromUser = false);
220 void AddServerMessageLine(wxString &msg);
221 #ifdef __DEBUG__
222 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
223 #endif
224 void SetOSFiles(const wxString new_path);
226 wxString ConfigDir;
228 const wxString& GetOSType() const { return OSType; }
230 void ShowUserCount();
232 void ShowConnectionState();
234 void StartKad();
235 void StopKad();
237 /** Bootstraps kad from the specified IP (must be in hostorder). */
238 void BootstrapKad(uint32 ip, uint16 port);
239 /** Updates the nodes.dat file from the specified url. */
240 void UpdateNotesDat(const wxString& str);
243 void DisconnectED2K();
245 bool CryptoAvailable() const;
247 protected:
248 // Used to detect a previous running instance of aMule
249 wxSingleInstanceChecker* m_singleInstance;
251 #ifdef __WXDEBUG__
253 * Handles asserts in a thread-safe manner.
255 virtual void OnAssertFailure(const wxChar* file, int line,
256 const wxChar* func, const wxChar* cond, const wxChar* msg);
257 #endif
259 void OnUDPDnsDone(CMuleInternalEvent& evt);
260 void OnSourceDnsDone(CMuleInternalEvent& evt);
261 void OnServerDnsDone(CMuleInternalEvent& evt);
263 void OnTCPTimer(CTimerEvent& evt);
264 void OnCoreTimer(CTimerEvent& evt);
266 void OnFinishedHashing(CHashingEvent& evt);
267 void OnFinishedAICHHashing(CHashingEvent& evt);
268 void OnFinishedCompletion(CCompletionEvent& evt);
269 void OnFinishedAllocation(CAllocFinishedEvent& evt);
270 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
271 void OnHashingShutdown(CMuleInternalEvent&);
272 void OnNotifyEvent(CMuleGUIEvent& evt);
274 void SetTimeOnTransfer();
276 APPState m_app_state;
278 wxString m_emulesig_path;
279 wxString m_amulesig_path;
281 char *strFullMuleVersion;
282 char *strOSDescription;
283 wxString OSType;
285 uint32 m_dwPublicIP;
287 long webserver_pid;
289 bool enable_daemon_fork;
290 wxString PidFile;
292 wxString server_msg;
294 CTimer* core_timer;
296 private:
297 virtual void OnUnhandledException();
299 void CheckNewVersion(uint32 result);
301 uint32 m_localip;
305 #ifndef AMULE_DAEMON
308 class CamuleGuiBase {
309 public:
310 CamuleGuiBase();
311 virtual ~CamuleGuiBase();
313 wxString m_FrameTitle;
314 CamuleDlg* amuledlg;
315 int m_FileDetailDialogActive;
317 bool CopyTextToClipboard( wxString strText );
319 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
320 virtual void ShowAlert(wxString msg, wxString title, int flags);
322 void AddGuiLogLine(const wxString& line);
323 protected:
325 * This list is used to contain log messages that are to be displayed
326 * on the GUI, when it is currently impossible to do so. This is in order
327 * to allows us to queue messages till after the dialog has been created.
329 std::list<wxString> m_logLines;
333 #ifndef CLIENT_GUI
336 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
339 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
341 int OnExit();
342 bool OnInit();
344 public:
346 virtual void ShowAlert(wxString msg, wxString title, int flags);
348 void ShutDown(wxCloseEvent &evt);
350 wxString GetLog(bool reset = false);
351 wxString GetServerLog(bool reset = false);
352 void AddServerMessageLine(wxString &msg);
353 DECLARE_EVENT_TABLE()
357 DECLARE_APP(CamuleGuiApp)
358 #ifdef AMULE_CPP
359 CamuleGuiApp *theApp;
360 #else
361 extern CamuleGuiApp *theApp;
362 #endif
365 #else /* !CLIENT_GUI */
368 #include "amule-remote-gui.h"
371 #endif // CLIENT_GUI
374 #define CALL_APP_DATA_LOCK
377 #else /* ! AMULE_DAEMON */
380 #include <wx/apptrait.h>
381 #include <wx/socket.h>
384 class CSocketSet;
387 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
389 private:
390 CSocketSet *m_in_set, *m_out_set;
392 wxMutex m_lock;
393 public:
394 CAmuledGSocketFuncTable();
396 void AddSocket(GSocket *socket, GSocketEvent event);
397 void RemoveSocket(GSocket *socket, GSocketEvent event);
398 void RunSelect();
400 virtual bool OnInit();
401 virtual void OnExit();
402 virtual bool CanUseEventLoop();
403 virtual bool Init_Socket(GSocket *socket);
404 virtual void Destroy_Socket(GSocket *socket);
405 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
406 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
407 virtual void Enable_Events(GSocket *socket);
408 virtual void Disable_Events(GSocket *socket);
412 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
415 class CDaemonAppTraits : public wxConsoleAppTraits
417 private:
418 CAmuledGSocketFuncTable *m_table;
419 wxMutex m_lock;
420 std::list<wxObject *> m_sched_delete;
421 #ifndef __WXMSW__
422 struct sigaction m_oldSignalChildAction;
423 struct sigaction m_newSignalChildAction;
424 #endif
426 public:
427 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
428 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
429 virtual void ScheduleForDestroy(wxObject *object);
430 virtual void RemoveFromPendingDelete(wxObject *object);
432 void DeletePending();
434 #ifndef __WXMSW__
435 virtual int WaitForChild(wxExecuteData& execData);
436 #endif
437 #ifdef __WXMAC__
438 virtual wxStandardPathsBase& GetStandardPaths();
439 #endif
443 #ifndef __WXMSW__
444 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
445 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
446 #endif // __WXMSW__
449 class CamuleDaemonApp : public CamuleApp
451 private:
452 bool m_Exit;
453 CAmuledGSocketFuncTable *m_table;
454 #ifndef __WXMSW__
455 struct sigaction m_oldSignalChildAction;
456 struct sigaction m_newSignalChildAction;
457 #endif // __WXMSW__
459 bool OnInit();
460 int OnRun();
461 int OnExit();
463 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
465 public:
466 CamuleDaemonApp();
468 void ExitMainLoop() { m_Exit = true; }
470 bool CopyTextToClipboard(wxString strText);
472 virtual void ShowAlert(wxString msg, wxString title, int flags);
474 DECLARE_EVENT_TABLE()
476 wxAppTraits *CreateTraits();
479 DECLARE_APP(CamuleDaemonApp)
480 #ifdef AMULE_CPP
481 CamuleDaemonApp *theApp;
482 #else
483 extern CamuleDaemonApp *theApp;
484 #endif
486 #endif /* ! AMULE_DAEMON */
488 #endif // AMULE_H
489 // File_checked_for_headers