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