Upstream tarball 9744
[amule.git] / src / amule.h
blob8212106163325819cd09af34f1fbff6c3cf7b903
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 int 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 int 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 int 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 */
379 // wxWidgets 2.8 requires special code for event handling and sockets.
380 // 2.9 doesn't, so standard event loop and sockets can be used
382 // Windows: aMuled compiles with 2.8 (without the special code),
383 // but works only with 2.9
385 #if !wxCHECK_VERSION(2, 9, 0)
386 #ifdef __WXMSW__
387 // MSW: can't run amuled with 2.8 anyway, just get it compiled
388 #define AMULED_DUMMY
389 #else
390 #define AMULED28
391 #endif
392 #endif
394 #ifdef AMULED28
395 #include <wx/socket.h>
397 class CSocketSet;
400 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
402 private:
403 CSocketSet *m_in_set, *m_out_set;
405 wxMutex m_lock;
406 public:
407 CAmuledGSocketFuncTable();
409 void AddSocket(GSocket *socket, GSocketEvent event);
410 void RemoveSocket(GSocket *socket, GSocketEvent event);
411 void RunSelect();
413 virtual bool OnInit();
414 virtual void OnExit();
415 virtual bool CanUseEventLoop();
416 virtual bool Init_Socket(GSocket *socket);
417 virtual void Destroy_Socket(GSocket *socket);
418 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
419 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
420 virtual void Enable_Events(GSocket *socket);
421 virtual void Disable_Events(GSocket *socket);
425 #endif // AMULED28
427 // no AppTraits used on Windows
428 #ifndef __WXMSW__
430 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
432 #include <wx/apptrait.h>
434 class CDaemonAppTraits : public wxConsoleAppTraits
436 private:
437 struct sigaction m_oldSignalChildAction;
438 struct sigaction m_newSignalChildAction;
440 #ifdef AMULED28
441 CAmuledGSocketFuncTable *m_table;
442 wxMutex m_lock;
443 std::list<wxObject *> m_sched_delete;
444 public:
445 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
446 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
447 virtual void ScheduleForDestroy(wxObject *object);
448 virtual void RemoveFromPendingDelete(wxObject *object);
450 void DeletePending();
451 #else // AMULED28
452 public:
453 CDaemonAppTraits();
454 #endif // !AMULED28
456 virtual int WaitForChild(wxExecuteData& execData);
458 #ifdef __WXMAC__
459 virtual wxStandardPathsBase& GetStandardPaths();
460 #endif
463 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
464 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
465 #endif // __WXMSW__
468 class CamuleDaemonApp : public CamuleApp
470 private:
471 #ifdef AMULED28
472 bool m_Exit;
473 CAmuledGSocketFuncTable *m_table;
474 #endif
475 bool OnInit();
476 int OnRun();
477 int OnExit();
479 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
481 #ifndef __WXMSW__
482 struct sigaction m_oldSignalChildAction;
483 struct sigaction m_newSignalChildAction;
484 public:
485 wxAppTraits *CreateTraits();
486 #endif // __WXMSW__
488 public:
490 #ifdef AMULED28
491 CamuleDaemonApp();
493 void ExitMainLoop() { m_Exit = true; }
494 #endif
496 #ifdef AMULED_DUMMY
497 void ExitMainLoop() {}
498 #endif
500 bool CopyTextToClipboard(wxString strText);
502 virtual int ShowAlert(wxString msg, wxString title, int flags);
504 DECLARE_EVENT_TABLE()
507 DECLARE_APP(CamuleDaemonApp)
508 #ifdef AMULE_CPP
509 CamuleDaemonApp *theApp;
510 #else
511 extern CamuleDaemonApp *theApp;
512 #endif
514 #endif /* ! AMULE_DAEMON */
516 #endif // AMULE_H
517 // File_checked_for_headers