Upstream tarball 10015
[amule.git] / src / ExternalConn.h
blobe9574bee1901b6e7b30e43b944bf8d00b47c2eab
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 Kry ( elkry@users.sourceforge.net / http://www.amule.org )
5 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 // Copyright (c) 2008 Froenchenko Leonid (lfroen@gmail.com)
7 //
8 // Any parts of this program derived from the xMule, lMule or eMule project,
9 // or contributed by third-party developers are copyrighted by their
10 // respective authors.
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #ifndef EXTERNALCONN_H
28 #define EXTERNALCONN_H
32 #include <ec/cpp/ECSpecialTags.h>
34 #include "amuleIPV4Address.h" // for amuleIPV4Address
35 #include "RLE.h" // for RLE
36 #include "DownloadQueue.h"
38 class wxSocketServer;
39 class wxSocketEvent;
42 // T - type of item
43 // E - type of encoder
44 // C - type of container in theApp
45 template <class T, class E, class C>
46 class CFileEncoderMap : public std::map<T *, E> {
47 public:
48 void UpdateEncoders(C *container)
50 // check if encoder contains files that no longer in container
51 // or, we have new files without encoder yet
52 std::set<T *> curr_files, dead_files;
53 for (unsigned int i = 0; i < container->GetFileCount(); i++) {
54 // cast for case of 'const'
55 T *cur_file = (T *)container->GetFileByIndex(i);
56 curr_files.insert(cur_file);
57 if ( this->count(cur_file) == 0 ) {
58 this->operator [](cur_file) = E(cur_file);
62 // curr_files set is created to minimize lookup time in download queue,
63 // since GetFileByID have loop inside leading to O(n), in this case
64 // it will mean O(n^2)
65 typename std::map<T *, E>::iterator i;
66 for(i = this->begin(); i != this->end(); i++) {
67 if ( curr_files.count(i->first) == 0 ) {
68 dead_files.insert(i->first);
71 typename std::set<T *>::iterator j;
72 for(j = dead_files.begin(); j != dead_files.end(); j++) {
73 this->erase(*j);
78 /*!
79 * PartStatus strings and gap lists are quite long - RLE encoding will help.
81 * Instead of sending each time full part-status string, send
82 * RLE encoded difference from previous one.
84 * PartFileEncoderData class is used for decode only,
85 * while CPartFile_Encoder is used for encode only.
87 class CPartFile_Encoder : public PartFileEncoderData {
88 CPartFile *m_file;
89 public:
90 // encoder side
91 CPartFile_Encoder(CPartFile *file = 0) { m_file = file; }
93 // encode - take data from m_file
94 void Encode(CECTag *parent_tag);
96 // Encoder may reset history if full info requested
97 void ResetEncoder();
100 typedef CFileEncoderMap<CPartFile , CPartFile_Encoder, CDownloadQueue> CPartFile_Encoder_Map;
103 * Encode 'obtained parts' info to be sent to remote gui
105 class CKnownFile_Encoder {
106 RLE_Data m_enc_data;
107 CKnownFile *m_file;
108 public:
109 CKnownFile_Encoder(CKnownFile *file = 0) { m_file = file; }
111 // encode - take data from m_file
112 void Encode(CECTag *parent_tag);
114 void ResetEncoder()
116 m_enc_data.ResetEncoder();
120 typedef CFileEncoderMap<CKnownFile , CKnownFile_Encoder, CSharedFileList> CKnownFile_Encoder_Map;
122 template <class T, ec_tagname_t OP>
123 class CTagSet : public std::set<T> {
124 void InSet(const CECTag *tag, uint32)
126 this->insert(tag->GetInt());
128 void InSet(const CECTag *tag, CMD4Hash)
130 this->insert(tag->GetMD4Data());
132 public:
133 CTagSet(const CECPacket *request) : std::set<T>()
135 for (unsigned int i = 0;i < request->GetTagCount();i++) {
136 const CECTag *tag = request->GetTagByIndex(i);
137 if ( tag->GetTagName() == OP ) {
138 this->InSet(tag, T());
145 class CObjTagMap {
146 std::map<void *, CValueMap> m_obj_map;
147 public:
148 CValueMap &GetValueMap(void *object)
150 return m_obj_map[object];
153 size_t size()
155 return m_obj_map.size();
158 void RemoveDeleted(std::set<void *>& WXUNUSED(current_set))
160 for(std::map<void *, CValueMap>::iterator i = m_obj_map.begin(); i != m_obj_map.end(); i++) {
161 if ( !current_set.count(i->first) ) {
162 m_obj_map.erase(i->first);
170 class CECServerSocket;
171 class ECNotifier;
173 class ExternalConn : public wxEvtHandler
175 private:
176 typedef std::set<CECServerSocket *> SocketSet;
177 SocketSet socket_list;
179 public:
180 ExternalConn(amuleIPV4Address addr, wxString *msg);
181 ~ExternalConn();
183 wxSocketServer *m_ECServer;
184 ECNotifier *m_ec_notifier;
186 void AddSocket(CECServerSocket *s);
187 void RemoveSocket(CECServerSocket *s);
188 void KillAllSockets();
190 private:
191 // event handlers (these functions should _not_ be virtual)
192 void OnServerEvent(wxSocketEvent& event);
193 DECLARE_EVENT_TABLE()
196 class ECUpdateMsgSource {
197 public:
198 virtual ~ECUpdateMsgSource()
201 virtual CECPacket *GetNextPacket() = 0;
204 class ECPartFileMsgSource : public ECUpdateMsgSource {
205 typedef struct {
206 bool m_new;
207 bool m_comment_changed;
208 bool m_removed;
209 bool m_finished;
210 bool m_dirty;
211 CPartFile *m_file;
212 } PARTFILE_STATUS;
213 std::map<CMD4Hash, PARTFILE_STATUS> m_dirty_status;
214 public:
215 ECPartFileMsgSource();
217 void SetDirty(CPartFile *file);
218 void SetNew(CPartFile *file);
219 void SetCompleted(CPartFile *file);
220 void SetRemoved(CPartFile *file);
222 virtual CECPacket *GetNextPacket();
226 class ECKnownFileMsgSource : public ECUpdateMsgSource {
227 typedef struct {
228 bool m_new;
229 bool m_comment_changed;
230 bool m_removed;
231 bool m_dirty;
232 CKnownFile *m_file;
233 } KNOWNFILE_STATUS;
234 std::map<CMD4Hash, KNOWNFILE_STATUS> m_dirty_status;
235 public:
236 ECKnownFileMsgSource();
238 void SetDirty(CKnownFile *file);
239 void SetNew(CKnownFile *file);
240 void SetRemoved(CKnownFile *file);
242 virtual CECPacket *GetNextPacket();
245 class ECClientMsgSource : public ECUpdateMsgSource {
246 public:
247 virtual CECPacket *GetNextPacket();
250 class ECStatusMsgSource : public ECUpdateMsgSource {
251 uint32 m_last_ed2k_status_sent;
252 uint32 m_last_kad_status_sent;
253 void *m_server;
255 uint32 GetEd2kStatus();
256 uint32 GetKadStatus();
257 public:
258 ECStatusMsgSource();
260 virtual CECPacket *GetNextPacket();
263 class ECSearchMsgSource : public ECUpdateMsgSource {
264 typedef struct {
265 bool m_new;
266 bool m_child_dirty;
267 bool m_dirty;
268 CSearchFile *m_file;
269 } SEARCHFILE_STATUS;
270 std::map<CMD4Hash, SEARCHFILE_STATUS> m_dirty_status;
271 public:
272 ECSearchMsgSource();
274 void SetDirty(CSearchFile *file);
275 void SetChildDirty(CSearchFile *file);
277 void FlushStatus();
279 virtual CECPacket *GetNextPacket();
282 class ECNotifier {
284 // designated priority for each type of update
286 enum EC_SOURCE_PRIO {
287 EC_PARTFILE = 0,
288 EC_SEARCH,
289 EC_CLIENT,
290 EC_STATUS,
291 EC_KNOWN,
293 EC_STATUS_LAST_PRIO
296 //ECUpdateMsgSource *m_msg_source[EC_STATUS_LAST_PRIO];
297 std::map<CECServerSocket *, ECUpdateMsgSource **> m_msg_source;
299 void NextPacketToSocket();
301 CECPacket *GetNextPacket(ECUpdateMsgSource *msg_source_array[]);
302 public:
303 ECNotifier();
305 void Add_EC_Client(CECServerSocket *sock);
306 void Remove_EC_Client(CECServerSocket *sock);
308 CECPacket *GetNextPacket(CECServerSocket *sock);
311 // Interface to notification macros
313 void DownloadFile_SetDirty(CPartFile *file);
314 void DownloadFile_RemoveFile(CPartFile *file);
315 void DownloadFile_RemoveSource(CPartFile *file);
316 void DownloadFile_AddFile(CPartFile *file);
317 void DownloadFile_AddSource(CPartFile *file);
319 void Status_ConnectionState();
320 void Status_QueueCount();
321 void Status_UserCount();
323 void SharedFile_AddFile(CKnownFile *file);
324 void SharedFile_RemoveFile(CKnownFile *file);
325 void SharedFile_RemoveAllFiles();
330 #endif // EXTERNALCONN_H
331 // File_checked_for_headers