Remove unused variable
[amule.git] / src / amule.h
blob64cee8d8c22ecb9ee7d73b203817602267d42d2f
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2011 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.
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 __WINDOWS__
38 #include <signal.h>
39 // #include <wx/unix/execute.h>
40 #endif // __WINDOWS__
42 #ifdef HAVE_CONFIG_H
43 # include "config.h" // Needed for ASIO_SOCKETS
44 #endif
47 class CAbstractFile;
48 class CKnownFile;
49 class ExternalConn;
50 class CamuleDlg;
51 class CPreferences;
52 class CDownloadQueue;
53 class CUploadQueue;
54 class CServerConnect;
55 class CSharedFileList;
56 class CServer;
57 class CFriend;
58 class CMD4Hash;
59 class CServerList;
60 class CListenSocket;
61 class CClientList;
62 class CKnownFileList;
63 class CCanceledFileList;
64 class CSearchList;
65 class CClientCreditsList;
66 class CFriendList;
67 class CClientUDPSocket;
68 class CIPFilter;
69 class UploadBandwidthThrottler;
70 #ifdef ASIO_SOCKETS
71 class CAsioService;
72 #else
73 class wxSocketEvent;
74 #endif
75 #ifdef ENABLE_UPNP
76 class CUPnPControlPoint;
77 class CUPnPPortMapping;
78 #endif
79 class CStatistics;
80 class wxCommandEvent;
81 class wxCloseEvent;
82 class wxFFileOutputStream;
83 class CTimer;
84 class CTimerEvent;
85 class wxSingleInstanceChecker;
86 class CHashingEvent;
87 class CMuleInternalEvent;
88 class CCompletionEvent;
89 class CAllocFinishedEvent;
90 class wxExecuteData;
91 class CLoggingEvent;
94 namespace MuleNotify {
95 class CMuleGUIEvent;
98 using MuleNotify::CMuleGUIEvent;
101 namespace Kademlia {
102 class CUInt128;
106 #ifdef AMULE_DAEMON
107 #define AMULE_APP_BASE wxAppConsole
108 #define CORE_TIMER_PERIOD 300
109 #else
110 #define AMULE_APP_BASE wxApp
111 #define CORE_TIMER_PERIOD 100
112 #endif
114 #define CONNECTED_ED2K (1<<0)
115 #define CONNECTED_KAD_NOT (1<<1)
116 #define CONNECTED_KAD_OK (1<<2)
117 #define CONNECTED_KAD_FIREWALLED (1<<3)
120 // Base class common to amule, aamuled and amulegui
121 class CamuleAppCommon
123 private:
124 // Used to detect a previous running instance of aMule
125 wxSingleInstanceChecker* m_singleInstance;
127 bool CheckPassedLink(const wxString &in, wxString &out, int cat);
128 protected:
129 wxString FullMuleVersion;
130 wxString OSDescription;
131 wxString OSType;
132 bool enable_daemon_fork;
133 bool ec_config;
134 bool m_skipConnectionDialog;
135 bool m_geometryEnabled;
136 wxString m_geometryString;
137 wxString m_logFile;
138 wxString m_appName;
139 wxString m_PidFile;
141 bool InitCommon(int argc, wxChar ** argv);
142 void RefreshSingleInstanceChecker();
143 bool CheckMuleDirectory(const wxString& desc, const class CPath& directory, const wxString& alternative, class CPath& outDir);
144 public:
145 wxString m_configFile;
147 CamuleAppCommon();
148 ~CamuleAppCommon();
149 void AddLinksFromFile();
150 // URL functions
151 wxString CreateMagnetLink(const CAbstractFile *f);
152 wxString CreateED2kLink(const CAbstractFile* f, bool add_source = false, bool use_hostname = false, bool add_cryptoptions = false, bool add_AICH = false);
153 // Who am I ?
154 #ifdef AMULE_DAEMON
155 bool IsDaemon() const { return true; }
156 #else
157 bool IsDaemon() const { return false; }
158 #endif
160 #ifdef CLIENT_GUI
161 bool IsRemoteGui() const { return true; }
162 #else
163 bool IsRemoteGui() const { return false; }
164 #endif
166 const wxString& GetMuleAppName() const { return m_appName; }
167 const wxString GetFullMuleVersion() const;
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 #if wxUSE_ON_FATAL_EXCEPTION
186 void OnFatalException();
187 #endif
188 bool ReinitializeNetwork(wxString *msg);
190 // derived classes may override those
191 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
193 #ifndef ASIO_SOCKETS
194 // Socket handlers
195 void ListenSocketHandler(wxSocketEvent& event);
196 void UDPSocketHandler(wxSocketEvent& event);
197 #endif
199 virtual int ShowAlert(wxString msg, wxString title, int flags) = 0;
201 // Barry - To find out if app is running or shutting/shut down
202 bool IsRunning() const { return (m_app_state == APP_STATE_RUNNING); }
203 bool IsOnShutDown() const { return (m_app_state == APP_STATE_SHUTTINGDOWN); }
205 // Check ED2K and Kademlia state
206 bool IsFirewalled() const;
207 // Are we connected to at least one network?
208 bool IsConnected() const;
209 // Connection to ED2K
210 bool IsConnectedED2K() const;
212 // What about Kad? Is it running?
213 bool IsKadRunning() const;
214 // Connection to Kad
215 bool IsConnectedKad() const;
216 // Check Kad state (TCP)
217 bool IsFirewalledKad() const;
218 // Check Kad state (UDP)
219 bool IsFirewalledKadUDP() const;
220 // Check Kad state (LAN mode)
221 bool IsKadRunningInLanMode() const;
222 // Kad stats
223 uint32 GetKadUsers() const;
224 uint32 GetKadFiles() const;
225 uint32 GetKadIndexedSources() const;
226 uint32 GetKadIndexedKeywords() const;
227 uint32 GetKadIndexedNotes() const;
228 uint32 GetKadIndexedLoad() const;
229 // True IP of machine
230 uint32 GetKadIPAdress() const;
231 // Buddy status
232 uint8 GetBuddyStatus() const;
233 uint32 GetBuddyIP() const;
234 uint32 GetBuddyPort() const;
235 // Kad ID
236 const Kademlia::CUInt128& GetKadID() const;
238 // Check if we should callback this client
239 bool CanDoCallback(uint32 clientServerIP, uint16 clientServerPort);
241 // Misc functions
242 void OnlineSig(bool zero = false);
243 void Localize_mule();
244 void Trigger_New_version(wxString newMule);
246 // shakraw - new EC code using wxSocketBase
247 ExternalConn* ECServerHandler;
249 // return current (valid) public IP or 0 if unknown
250 // If ignorelocal is true, don't use m_localip
251 uint32 GetPublicIP(bool ignorelocal = false) const;
252 void SetPublicIP(const uint32 dwIP);
254 uint32 GetED2KID() const;
255 uint32 GetID() const;
257 // Other parts of the interface and such
258 CPreferences* glob_prefs;
259 CDownloadQueue* downloadqueue;
260 CUploadQueue* uploadqueue;
261 CServerConnect* serverconnect;
262 CSharedFileList* sharedfiles;
263 CServerList* serverlist;
264 CListenSocket* listensocket;
265 CClientList* clientlist;
266 CKnownFileList* knownfiles;
267 CCanceledFileList* canceledfiles;
268 CSearchList* searchlist;
269 CClientCreditsList* clientcredits;
270 CFriendList* friendlist;
271 CClientUDPSocket* clientudp;
272 CStatistics* m_statistics;
273 CIPFilter* ipfilter;
274 UploadBandwidthThrottler* uploadBandwidthThrottler;
275 #ifdef ASIO_SOCKETS
276 CAsioService* m_AsioService;
277 #endif
278 #ifdef ENABLE_UPNP
279 CUPnPControlPoint* m_upnp;
280 std::vector<CUPnPPortMapping> m_upnpMappings;
281 #endif
282 wxLocale m_locale;
284 void ShutDown();
286 wxString GetLog(bool reset = false);
287 wxString GetServerLog(bool reset = false);
288 wxString GetDebugLog(bool reset = false);
290 bool AddServer(CServer *srv, bool fromUser = false);
291 void AddServerMessageLine(wxString &msg);
292 #ifdef __DEBUG__
293 void AddSocketDeleteDebug(uint32 socket_pointer, uint32 creation_time);
294 #endif
295 void SetOSFiles(const wxString& new_path);
297 const wxString& GetOSType() const { return OSType; }
299 void ShowUserCount();
301 void ShowConnectionState(bool forceUpdate = false);
303 void StartKad();
304 void StopKad();
306 /** Bootstraps kad from the specified IP (must be in hostorder). */
307 void BootstrapKad(uint32 ip, uint16 port);
308 /** Updates the nodes.dat file from the specified url. */
309 void UpdateNotesDat(const wxString& str);
312 void DisconnectED2K();
314 bool CryptoAvailable() const;
316 protected:
318 #ifdef __WXDEBUG__
320 * Handles asserts in a thread-safe manner.
322 virtual void OnAssertFailure(const wxChar* file, int line,
323 const wxChar* func, const wxChar* cond, const wxChar* msg);
324 #endif
326 void OnUDPDnsDone(CMuleInternalEvent& evt);
327 void OnSourceDnsDone(CMuleInternalEvent& evt);
328 void OnServerDnsDone(CMuleInternalEvent& evt);
330 void OnTCPTimer(CTimerEvent& evt);
331 void OnCoreTimer(CTimerEvent& evt);
333 void OnFinishedHashing(CHashingEvent& evt);
334 void OnFinishedAICHHashing(CHashingEvent& evt);
335 void OnFinishedCompletion(CCompletionEvent& evt);
336 void OnFinishedAllocation(CAllocFinishedEvent& evt);
337 void OnFinishedHTTPDownload(CMuleInternalEvent& evt);
338 void OnHashingShutdown(CMuleInternalEvent&);
339 void OnNotifyEvent(CMuleGUIEvent& evt);
341 void SetTimeOnTransfer();
343 APPState m_app_state;
345 wxString m_emulesig_path;
346 wxString m_amulesig_path;
348 uint32 m_dwPublicIP;
350 long webserver_pid;
352 wxString server_msg;
354 CTimer* core_timer;
356 private:
357 virtual void OnUnhandledException();
359 void CheckNewVersion(uint32 result);
361 uint32 m_localip;
365 #ifndef AMULE_DAEMON
368 class CamuleGuiBase {
369 public:
370 CamuleGuiBase();
371 virtual ~CamuleGuiBase();
373 wxString m_FrameTitle;
374 CamuleDlg* amuledlg;
376 bool CopyTextToClipboard( wxString strText );
377 void ResetTitle();
379 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
380 virtual int ShowAlert(wxString msg, wxString title, int flags);
382 void AddGuiLogLine(const wxString& line);
383 protected:
385 * This list is used to contain log messages that are to be displayed
386 * on the GUI, when it is currently impossible to do so. This is in order
387 * to allows us to queue messages till after the dialog has been created.
389 std::list<wxString> m_logLines;
393 #ifndef CLIENT_GUI
396 class CamuleGuiApp : public CamuleApp, public CamuleGuiBase
399 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
401 int OnExit();
402 bool OnInit();
404 public:
406 virtual int ShowAlert(wxString msg, wxString title, int flags);
408 void ShutDown(wxCloseEvent &evt);
410 wxString GetLog(bool reset = false);
411 wxString GetServerLog(bool reset = false);
412 void AddServerMessageLine(wxString &msg);
413 DECLARE_EVENT_TABLE()
417 DECLARE_APP(CamuleGuiApp)
418 extern CamuleGuiApp *theApp;
421 #else /* !CLIENT_GUI */
424 #include "amule-remote-gui.h"
427 #endif // CLIENT_GUI
430 #define CALL_APP_DATA_LOCK
433 #else /* ! AMULE_DAEMON */
435 // wxWidgets 2.8 requires special code for event handling and sockets.
436 // 2.9 doesn't, so standard event loop and sockets can be used
438 // Windows: aMuled compiles with 2.8 (without the special code),
439 // but works only with 2.9
441 #if !wxCHECK_VERSION(2, 9, 0)
442 // wx 2.8 needs a hand-made event loop in any case
443 #define AMULED28_EVENTLOOP
445 // wx 2.8 also needs extra socket code, unless we have ASIO sockets
447 #ifdef HAVE_CONFIG_H
448 # include "config.h" // defines ASIO_SOCKETS
449 #endif
451 #ifndef ASIO_SOCKETS
452 // MSW: can't run amuled with 2.8 without ASIO sockets, just get it compiled
453 #ifndef __WINDOWS__
454 #define AMULED28_SOCKETS
455 #endif
456 #endif
457 #endif
459 #ifdef AMULED28_SOCKETS
460 #include <wx/socket.h>
462 class CSocketSet;
465 class CAmuledGSocketFuncTable : public GSocketGUIFunctionsTable
467 private:
468 CSocketSet *m_in_set, *m_out_set;
470 wxMutex m_lock;
471 public:
472 CAmuledGSocketFuncTable();
474 void AddSocket(GSocket *socket, GSocketEvent event);
475 void RemoveSocket(GSocket *socket, GSocketEvent event);
476 void RunSelect();
478 virtual bool OnInit();
479 virtual void OnExit();
480 virtual bool CanUseEventLoop();
481 virtual bool Init_Socket(GSocket *socket);
482 virtual void Destroy_Socket(GSocket *socket);
483 virtual void Install_Callback(GSocket *socket, GSocketEvent event);
484 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
485 virtual void Enable_Events(GSocket *socket);
486 virtual void Disable_Events(GSocket *socket);
490 #endif // AMULED28_SOCKETS
492 // AppTrait functionality is required for 2.8 wx sockets
493 // Otherwise it's used to prevent zombie child processes,
494 // which stops working with wx 2.9.5.
495 // So disable it there (no idea if this has a noticeable impact).
497 #if !wxCHECK_VERSION(2, 9, 5) && !defined(__WINDOWS__ )
498 #define AMULED_APPTRAITS
499 #endif
501 #ifdef AMULED_APPTRAITS
503 typedef std::map<int, class wxEndProcessData *> EndProcessDataMap;
505 #include <wx/apptrait.h>
507 class CDaemonAppTraits : public wxConsoleAppTraits
509 private:
510 struct sigaction m_oldSignalChildAction;
511 struct sigaction m_newSignalChildAction;
513 #ifdef AMULED28_SOCKETS
514 CAmuledGSocketFuncTable *m_table;
515 wxMutex m_lock;
516 std::list<wxObject *> m_sched_delete;
517 public:
518 CDaemonAppTraits(CAmuledGSocketFuncTable *table);
519 virtual GSocketGUIFunctionsTable* GetSocketGUIFunctionsTable();
520 virtual void ScheduleForDestroy(wxObject *object);
521 virtual void RemoveFromPendingDelete(wxObject *object);
523 void DeletePending();
524 #else // AMULED28_SOCKETS
525 public:
526 CDaemonAppTraits();
527 #endif // !AMULED28_SOCKETS
529 virtual int WaitForChild(wxExecuteData& execData);
531 #if defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
532 virtual wxStandardPathsBase& GetStandardPaths();
533 #endif
536 void OnSignalChildHandler(int signal, siginfo_t *siginfo, void *ucontext);
537 pid_t AmuleWaitPid(pid_t pid, int *status, int options, wxString *msg);
539 #endif // AMULED_APPTRAITS
542 class CamuleDaemonApp : public CamuleApp
544 private:
545 #ifdef AMULED28_EVENTLOOP
546 bool m_Exit;
547 #endif
548 #ifdef AMULED28_SOCKETS
549 CAmuledGSocketFuncTable *m_table;
550 #endif
551 bool OnInit();
552 int OnRun();
553 int OnExit();
555 virtual int InitGui(bool geometry_enable, wxString &geometry_string);
557 #ifdef AMULED_APPTRAITS
558 struct sigaction m_oldSignalChildAction;
559 struct sigaction m_newSignalChildAction;
560 public:
561 wxAppTraits *CreateTraits();
562 #endif // AMULED_APPTRAITS
564 public:
566 #ifdef AMULED28_EVENTLOOP
567 CamuleDaemonApp();
569 void ExitMainLoop() { m_Exit = true; }
570 #endif
572 bool CopyTextToClipboard(wxString strText);
574 virtual int ShowAlert(wxString msg, wxString title, int flags);
576 DECLARE_EVENT_TABLE()
579 DECLARE_APP(CamuleDaemonApp)
580 extern CamuleDaemonApp *theApp;
582 #endif /* ! AMULE_DAEMON */
584 #endif // AMULE_H
585 // File_checked_for_headers