Upstream tarball 9828
[amule.git] / src / amule.h
blob18c8cd9337fd503883d01262bf84012a34020086
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 #define CORE_TIMER_PERIOD 300
96 #else
97 #define AMULE_APP_BASE wxApp
98 #define CORE_TIMER_PERIOD 100
99 #endif
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
109 private:
110 enum APPState {
111 APP_STATE_RUNNING = 0,
112 APP_STATE_SHUTTINGDOWN,
113 APP_STATE_STARTING
116 public:
117 CamuleApp();
118 virtual ~CamuleApp();
120 virtual bool OnInit();
121 int OnExit();
122 void OnFatalException();
123 bool ReinitializeNetwork(wxString *msg);
125 // derived classes may override those
126 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
128 // Socket handlers
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;
148 // Connection to Kad
149 bool IsConnectedKad() const;
150 // Check Kad state (TCP)
151 bool IsFirewalledKad() const;
152 // Check Kad state (UDP)
153 bool IsFirewalledKadUDP() const;
154 // Kad stats
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;
163 // Buddy status
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);
171 // URL functions
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);
176 // Misc functions
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;
207 CIPFilter* ipfilter;
208 UploadBandwidthThrottler* uploadBandwidthThrottler;
209 #ifdef ENABLE_UPNP
210 CUPnPControlPoint* m_upnp;
211 std::vector<CUPnPPortMapping> m_upnpMappings;
212 #endif
213 wxLocale m_locale;
215 void ShutDown();
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);
223 #ifdef __DEBUG__
224 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
225 #endif
226 void SetOSFiles(const wxString new_path);
228 wxString ConfigDir;
230 const wxString& GetOSType() const { return OSType; }
232 void ShowUserCount();
234 void ShowConnectionState();
236 void StartKad();
237 void StopKad();
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;
249 protected:
250 // Used to detect a previous running instance of aMule
251 wxSingleInstanceChecker* m_singleInstance;
253 #ifdef __WXDEBUG__
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);
259 #endif
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;
285 wxString OSType;
287 uint32 m_dwPublicIP;
289 long webserver_pid;
291 bool enable_daemon_fork;
292 wxString PidFile;
294 wxString server_msg;
296 CTimer* core_timer;
298 private:
299 virtual void OnUnhandledException();
301 void CheckNewVersion(uint32 result);
303 uint32 m_localip;
307 #ifndef AMULE_DAEMON
310 class CamuleGuiBase {
311 public:
312 CamuleGuiBase();
313 virtual ~CamuleGuiBase();
315 wxString m_FrameTitle;
316 CamuleDlg* amuledlg;
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);
325 protected:
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;
335 #ifndef CLIENT_GUI
338 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
341 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
343 int OnExit();
344 bool OnInit();
346 public:
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)
360 #ifdef AMULE_CPP
361 CamuleGuiApp *theApp;
362 #else
363 extern CamuleGuiApp *theApp;
364 #endif
367 #else /* !CLIENT_GUI */
370 #include "amule-remote-gui.h"
373 #endif // CLIENT_GUI
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)
388 #ifdef __WXMSW__
389 // MSW: can't run amuled with 2.8 anyway, just get it compiled
390 #define AMULED_DUMMY
391 #else
392 #define AMULED28
393 #endif
394 #endif
396 #ifdef AMULED28
397 #include <wx/socket.h>
399 class CSocketSet;
402 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
404 private:
405 CSocketSet *m_in_set, *m_out_set;
407 wxMutex m_lock;
408 public:
409 CAmuledGSocketFuncTable();
411 void AddSocket(GSocket *socket, GSocketEvent event);
412 void RemoveSocket(GSocket *socket, GSocketEvent event);
413 void RunSelect();
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);
427 #endif // AMULED28
429 // no AppTraits used on Windows
430 #ifndef __WXMSW__
432 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
434 #include <wx/apptrait.h>
436 class CDaemonAppTraits : public wxConsoleAppTraits
438 private:
439 struct sigaction m_oldSignalChildAction;
440 struct sigaction m_newSignalChildAction;
442 #ifdef AMULED28
443 CAmuledGSocketFuncTable *m_table;
444 wxMutex m_lock;
445 std::list<wxObject *> m_sched_delete;
446 public:
447 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
448 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
449 virtual void ScheduleForDestroy(wxObject *object);
450 virtual void RemoveFromPendingDelete(wxObject *object);
452 void DeletePending();
453 #else // AMULED28
454 public:
455 CDaemonAppTraits();
456 #endif // !AMULED28
458 virtual int WaitForChild(wxExecuteData& execData);
460 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
461 virtual wxStandardPathsBase& GetStandardPaths();
462 #endif
465 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
466 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
467 #endif // __WXMSW__
470 class CamuleDaemonApp : public CamuleApp
472 private:
473 #ifdef AMULED28
474 bool m_Exit;
475 CAmuledGSocketFuncTable *m_table;
476 #endif
477 bool OnInit();
478 int OnRun();
479 int OnExit();
481 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
483 #ifndef __WXMSW__
484 struct sigaction m_oldSignalChildAction;
485 struct sigaction m_newSignalChildAction;
486 public:
487 wxAppTraits *CreateTraits();
488 #endif // __WXMSW__
490 public:
492 #ifdef AMULED28
493 CamuleDaemonApp();
495 void ExitMainLoop() { m_Exit = true; }
496 #endif
498 #ifdef AMULED_DUMMY
499 void ExitMainLoop() {}
500 #endif
502 bool CopyTextToClipboard(wxString strText);
504 virtual int ShowAlert(wxString msg, wxString title, int flags);
506 DECLARE_EVENT_TABLE()
509 DECLARE_APP(CamuleDaemonApp)
510 #ifdef AMULE_CPP
511 CamuleDaemonApp *theApp;
512 #else
513 extern CamuleDaemonApp *theApp;
514 #endif
516 #endif /* ! AMULE_DAEMON */
518 #endif // AMULE_H
519 // File_checked_for_headers