Upstream tarball 10153
[amule.git] / src / amule.h
blob924ab879bb61528cd9a485c524f1726668e91489
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 CCanceledFileList;
60 class CSearchList;
61 class CClientCreditsList;
62 class CFriendList;
63 class CClientUDPSocket;
64 class CIPFilter;
65 class UploadBandwidthThrottler;
66 #ifdef ENABLE_UPNP
67 class CUPnPControlPoint;
68 class CUPnPPortMapping;
69 #endif
70 class CStatistics;
71 class wxSocketEvent;
72 class wxCommandEvent;
73 class wxCloseEvent;
74 class wxFFileOutputStream;
75 class CUpDownClient;
76 class CTimer;
77 class CTimerEvent;
78 class wxSingleInstanceChecker;
79 class CHashingEvent;
80 class CMuleInternalEvent;
81 class CCompletionEvent;
82 class CAllocFinishedEvent;
83 class wxExecuteData;
84 class CLoggingEvent;
87 namespace MuleNotify {
88 class CMuleGUIEvent;
92 using MuleNotify::CMuleGUIEvent;
95 #ifdef AMULE_DAEMON
96 #define AMULE_APP_BASE wxAppConsole
97 #define CORE_TIMER_PERIOD 300
98 #else
99 #define AMULE_APP_BASE wxApp
100 #define CORE_TIMER_PERIOD 100
101 #endif
103 #define CONNECTED_ED2K (1<<0)
104 #define CONNECTED_KAD_NOT (1<<1)
105 #define CONNECTED_KAD_OK (1<<2)
106 #define CONNECTED_KAD_FIREWALLED (1<<3)
109 // Base class common to amule, aamuled and amulegui
110 class CamuleAppCommon
112 private:
113 // Used to detect a previous running instance of aMule
114 wxSingleInstanceChecker* m_singleInstance;
116 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
117 protected:
118 char *strFullMuleVersion;
119 char *strOSDescription;
120 wxString OSType;
121 bool enable_daemon_fork;
122 bool ec_config;
123 bool m_skipConnectionDialog;
124 bool m_geometryEnabled;
125 wxString m_geometryString;
126 wxString m_logFile;
127 wxString m_appName;
128 wxString m_PidFile;
130 bool InitCommon(int argc, wxChar ** argv);
131 void RefreshSingleInstanceChecker();
132 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
133 public:
134 wxString ConfigDir;
135 wxString m_configFile;
137 CamuleAppCommon();
138 ~CamuleAppCommon();
139 void AddLinksFromFile();
140 // URL functions
141 wxString CreateMagnetLink(const CAbstractFile *f);
142 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool addcryptoptions = false);
143 wxString CreateED2kAICHLink(const CKnownFile* f);
144 // Who am I ?
145 #ifdef AMULE_DAEMON
146 bool IsDaemon() { return true; }
147 #else
148 bool IsDaemon() { return false; }
149 #endif
151 #ifdef CLIENT_GUI
152 bool IsRemoteGui() { return true; }
153 #else
154 bool IsRemoteGui() { return false; }
155 #endif
157 const wxString& GetMuleAppName() { return m_appName; }
162 * Returns a description of the version of aMule being used.
164 * @return A detailed description of the aMule version, including application
165 * name and wx information.
167 * This should become a CamuleAppCommon member once certain files get unlocked.
169 #define GetFullMuleVersion() (theApp->GetMuleAppName() + wxT(" ") + GetMuleVersion())
171 class CamuleApp : public AMULE_APP_BASE, public CamuleAppCommon
173 private:
174 enum APPState {
175 APP_STATE_RUNNING = 0,
176 APP_STATE_SHUTTINGDOWN,
177 APP_STATE_STARTING
180 public:
181 CamuleApp();
182 virtual ~CamuleApp();
184 virtual bool OnInit();
185 int OnExit();
186 void OnFatalException();
187 bool ReinitializeNetwork(wxString *msg);
189 // derived classes may override those
190 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
192 // Socket handlers
193 void ListenSocketHandler(wxSocketEvent& event);
194 void ServerSocketHandler(wxSocketEvent& event);
195 void UDPSocketHandler(wxSocketEvent& event);
197 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
199 // Barry - To find out if app is running or shutting/shut down
200 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
201 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
203 // Check ED2K and Kademlia state
204 bool IsFirewalled() const;
205 // Are we connected to at least one network?
206 bool IsConnected() const;
207 // Connection to ED2K
208 bool IsConnectedED2K() const;
210 // What about Kad? Is it running?
211 bool IsKadRunning() const;
212 // Connection to Kad
213 bool IsConnectedKad() const;
214 // Check Kad state (TCP)
215 bool IsFirewalledKad() const;
216 // Check Kad state (UDP)
217 bool IsFirewalledKadUDP() const;
218 // Kad stats
219 uint32 GetKadUsers() const;
220 uint32 GetKadFiles() const;
221 uint32 GetKadIndexedSources() const;
222 uint32 GetKadIndexedKeywords() const;
223 uint32 GetKadIndexedNotes() const;
224 uint32 GetKadIndexedLoad() const;
225 // True IP of machine
226 uint32 GetKadIPAdress() const;
227 // Buddy status
228 uint8 GetBuddyStatus() const;
229 uint32 GetBuddyIP() const;
230 uint32 GetBuddyPort() const;
232 // Check if we should callback this client
233 bool CanDoCallback(CUpDownClient *client);
235 // Misc functions
236 void OnlineSig(bool zero = false);
237 void Localize_mule();
238 void Trigger_New_version(wxString newMule);
240 // shakraw - new EC code using wxSocketBase
241 ExternalConn* ECServerHandler;
243 // return current (valid) public IP or 0 if unknown
244 // If ignorelocal is true, don't use m_localip
245 uint32 GetPublicIP(bool ignorelocal = false) const;
246 void SetPublicIP(const uint32 dwIP);
248 uint32 GetED2KID() const;
249 uint32 GetID() const;
251 // Other parts of the interface and such
252 CPreferences* glob_prefs;
253 CDownloadQueue* downloadqueue;
254 CUploadQueue* uploadqueue;
255 CServerConnect* serverconnect;
256 CSharedFileList* sharedfiles;
257 CServerList* serverlist;
258 CListenSocket* listensocket;
259 CClientList* clientlist;
260 CKnownFileList* knownfiles;
261 CCanceledFileList* canceledfiles;
262 CSearchList* searchlist;
263 CClientCreditsList* clientcredits;
264 CFriendList* friendlist;
265 CClientUDPSocket* clientudp;
266 CStatistics* m_statistics;
267 CIPFilter* ipfilter;
268 UploadBandwidthThrottler* uploadBandwidthThrottler;
269 #ifdef ENABLE_UPNP
270 CUPnPControlPoint* m_upnp;
271 std::vector<CUPnPPortMapping> m_upnpMappings;
272 #endif
273 wxLocale m_locale;
275 void ShutDown();
277 wxString GetLog(bool reset = false);
278 wxString GetServerLog(bool reset = false);
279 wxString GetDebugLog(bool reset = false);
281 bool AddServer(CServer *srv, bool fromUser = false);
282 void AddServerMessageLine(wxString &msg);
283 #ifdef __DEBUG__
284 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
285 #endif
286 void SetOSFiles(const wxString new_path);
288 const wxString& GetOSType() const { return OSType; }
290 void ShowUserCount();
292 void ShowConnectionState();
294 void StartKad();
295 void StopKad();
297 /** Bootstraps kad from the specified IP (must be in hostorder). */
298 void BootstrapKad(uint32 ip, uint16 port);
299 /** Updates the nodes.dat file from the specified url. */
300 void UpdateNotesDat(const wxString& str);
303 void DisconnectED2K();
305 bool CryptoAvailable() const;
307 protected:
309 #ifdef __WXDEBUG__
311 * Handles asserts in a thread-safe manner.
313 virtual void OnAssertFailure(const wxChar* file, int line,
314 const wxChar* func, const wxChar* cond, const wxChar* msg);
315 #endif
317 void OnUDPDnsDone(CMuleInternalEvent& evt);
318 void OnSourceDnsDone(CMuleInternalEvent& evt);
319 void OnServerDnsDone(CMuleInternalEvent& evt);
321 void OnTCPTimer(CTimerEvent& evt);
322 void OnCoreTimer(CTimerEvent& evt);
324 void OnFinishedHashing(CHashingEvent& evt);
325 void OnFinishedAICHHashing(CHashingEvent& evt);
326 void OnFinishedCompletion(CCompletionEvent& evt);
327 void OnFinishedAllocation(CAllocFinishedEvent& evt);
328 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
329 void OnHashingShutdown(CMuleInternalEvent&);
330 void OnNotifyEvent(CMuleGUIEvent& evt);
332 void SetTimeOnTransfer();
334 APPState m_app_state;
336 wxString m_emulesig_path;
337 wxString m_amulesig_path;
339 uint32 m_dwPublicIP;
341 long webserver_pid;
343 wxString server_msg;
345 CTimer* core_timer;
347 private:
348 virtual void OnUnhandledException();
350 void CheckNewVersion(uint32 result);
352 uint32 m_localip;
356 #ifndef AMULE_DAEMON
359 class CamuleGuiBase {
360 public:
361 CamuleGuiBase();
362 virtual ~CamuleGuiBase();
364 wxString m_FrameTitle;
365 CamuleDlg* amuledlg;
366 int m_FileDetailDialogActive;
368 bool CopyTextToClipboard( wxString strText );
370 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
371 virtual int ShowAlert(wxString msg, wxString title, int flags);
373 void AddGuiLogLine(const wxString& line);
374 protected:
376 * This list is used to contain log messages that are to be displayed
377 * on the GUI, when it is currently impossible to do so. This is in order
378 * to allows us to queue messages till after the dialog has been created.
380 std::list<wxString> m_logLines;
384 #ifndef CLIENT_GUI
387 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
390 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
392 int OnExit();
393 bool OnInit();
395 public:
397 virtual int ShowAlert(wxString msg, wxString title, int flags);
399 void ShutDown(wxCloseEvent &evt);
401 wxString GetLog(bool reset = false);
402 wxString GetServerLog(bool reset = false);
403 void AddServerMessageLine(wxString &msg);
404 DECLARE_EVENT_TABLE()
408 DECLARE_APP(CamuleGuiApp)
409 extern CamuleGuiApp *theApp;
412 #else /* !CLIENT_GUI */
415 #include "amule-remote-gui.h"
418 #endif // CLIENT_GUI
421 #define CALL_APP_DATA_LOCK
424 #else /* ! AMULE_DAEMON */
426 // wxWidgets 2.8 requires special code for event handling and sockets.
427 // 2.9 doesn't, so standard event loop and sockets can be used
429 // Windows: aMuled compiles with 2.8 (without the special code),
430 // but works only with 2.9
432 #if !wxCHECK_VERSION(2, 9, 0)
433 #ifdef __WXMSW__
434 // MSW: can't run amuled with 2.8 anyway, just get it compiled
435 #define AMULED_DUMMY
436 #else
437 #define AMULED28
438 #endif
439 #endif
441 #ifdef AMULED28
442 #include <wx/socket.h>
444 class CSocketSet;
447 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
449 private:
450 CSocketSet *m_in_set, *m_out_set;
452 wxMutex m_lock;
453 public:
454 CAmuledGSocketFuncTable();
456 void AddSocket(GSocket *socket, GSocketEvent event);
457 void RemoveSocket(GSocket *socket, GSocketEvent event);
458 void RunSelect();
460 virtual bool OnInit();
461 virtual void OnExit();
462 virtual bool CanUseEventLoop();
463 virtual bool Init_Socket(GSocket *socket);
464 virtual void Destroy_Socket(GSocket *socket);
465 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
466 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
467 virtual void Enable_Events(GSocket *socket);
468 virtual void Disable_Events(GSocket *socket);
472 #endif // AMULED28
474 // no AppTraits used on Windows
475 #ifndef __WXMSW__
477 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
479 #include <wx/apptrait.h>
481 class CDaemonAppTraits : public wxConsoleAppTraits
483 private:
484 struct sigaction m_oldSignalChildAction;
485 struct sigaction m_newSignalChildAction;
487 #ifdef AMULED28
488 CAmuledGSocketFuncTable *m_table;
489 wxMutex m_lock;
490 std::list<wxObject *> m_sched_delete;
491 public:
492 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
493 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
494 virtual void ScheduleForDestroy(wxObject *object);
495 virtual void RemoveFromPendingDelete(wxObject *object);
497 void DeletePending();
498 #else // AMULED28
499 public:
500 CDaemonAppTraits();
501 #endif // !AMULED28
503 virtual int WaitForChild(wxExecuteData& execData);
505 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
506 virtual wxStandardPathsBase& GetStandardPaths();
507 #endif
510 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
511 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
512 #endif // __WXMSW__
515 class CamuleDaemonApp : public CamuleApp
517 private:
518 #ifdef AMULED28
519 bool m_Exit;
520 CAmuledGSocketFuncTable *m_table;
521 #endif
522 bool OnInit();
523 int OnRun();
524 int OnExit();
526 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
528 #ifndef __WXMSW__
529 struct sigaction m_oldSignalChildAction;
530 struct sigaction m_newSignalChildAction;
531 public:
532 wxAppTraits *CreateTraits();
533 #endif // __WXMSW__
535 public:
537 #ifdef AMULED28
538 CamuleDaemonApp();
540 void ExitMainLoop() { m_Exit = true; }
541 #endif
543 #ifdef AMULED_DUMMY
544 void ExitMainLoop() {}
545 #endif
547 bool CopyTextToClipboard(wxString strText);
549 virtual int ShowAlert(wxString msg, wxString title, int flags);
551 DECLARE_EVENT_TABLE()
554 DECLARE_APP(CamuleDaemonApp)
555 extern CamuleDaemonApp *theApp;
557 #endif /* ! AMULE_DAEMON */
559 #endif // AMULE_H
560 // File_checked_for_headers