Upstream tarball 10013
[amule.git] / src / ExternalConn.h
blob093bac6861b44cfe263a978ec37a662774b10ae8
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 class CPartFile_Encoder {
86 // RLE encoded data
87 // PartFileEncoderData class is used for decode only,
88 // while CPartFile_Encoder is used for encode only.
89 PartFileEncoderData m_enc_data;
91 CPartFile *m_file;
92 public:
93 // encoder side
94 CPartFile_Encoder(CPartFile *file = 0) { m_file = file; }
96 // encode - take data from m_file
97 void Encode(CECTag *parent_tag);
99 void ResetEncoder()
101 m_enc_data.ResetEncoder();
105 typedef CFileEncoderMap<CPartFile , CPartFile_Encoder, CDownloadQueue> CPartFile_Encoder_Map;
108 * Encode 'obtained parts' info to be sent to remote gui
110 class CKnownFile_Encoder {
111 RLE_Data m_enc_data;
112 CKnownFile *m_file;
113 public:
114 CKnownFile_Encoder(CKnownFile *file = 0) { m_file = file; }
116 // encode - take data from m_file
117 void Encode(CECTag *parent_tag);
119 void ResetEncoder()
121 m_enc_data.ResetEncoder();
125 typedef CFileEncoderMap<CKnownFile , CKnownFile_Encoder, CSharedFileList> CKnownFile_Encoder_Map;
127 template <class T, ec_tagname_t OP>
128 class CTagSet : public std::set<T> {
129 void InSet(const CECTag *tag, uint32)
131 this->insert(tag->GetInt());
133 void InSet(const CECTag *tag, CMD4Hash)
135 this->insert(tag->GetMD4Data());
137 public:
138 CTagSet(const CECPacket *request) : std::set<T>()
140 for (unsigned int i = 0;i < request->GetTagCount();i++) {
141 const CECTag *tag = request->GetTagByIndex(i);
142 if ( tag->GetTagName() == OP ) {
143 this->InSet(tag, T());
150 class CObjTagMap {
151 std::map<void *, CValueMap> m_obj_map;
152 public:
153 CValueMap &GetValueMap(void *object)
155 return m_obj_map[object];
158 size_t size()
160 return m_obj_map.size();
163 void RemoveDeleted(std::set<void *>& WXUNUSED(current_set))
165 for(std::map<void *, CValueMap>::iterator i = m_obj_map.begin(); i != m_obj_map.end(); i++) {
166 if ( !current_set.count(i->first) ) {
167 m_obj_map.erase(i->first);
175 class CECServerSocket;
176 class ECNotifier;
178 class ExternalConn : public wxEvtHandler
180 private:
181 typedef std::set<CECServerSocket *> SocketSet;
182 SocketSet socket_list;
184 public:
185 ExternalConn(amuleIPV4Address addr, wxString *msg);
186 ~ExternalConn();
188 wxSocketServer *m_ECServer;
189 ECNotifier *m_ec_notifier;
191 void AddSocket(CECServerSocket *s);
192 void RemoveSocket(CECServerSocket *s);
193 void KillAllSockets();
195 private:
196 // event handlers (these functions should _not_ be virtual)
197 void OnServerEvent(wxSocketEvent& event);
198 DECLARE_EVENT_TABLE()
201 class ECUpdateMsgSource {
202 public:
203 virtual ~ECUpdateMsgSource()
206 virtual CECPacket *GetNextPacket() = 0;
209 class ECPartFileMsgSource : public ECUpdateMsgSource {
210 typedef struct {
211 bool m_new;
212 bool m_comment_changed;
213 bool m_removed;
214 bool m_finished;
215 bool m_dirty;
216 CPartFile *m_file;
217 } PARTFILE_STATUS;
218 std::map<CMD4Hash, PARTFILE_STATUS> m_dirty_status;
219 public:
220 ECPartFileMsgSource();
222 void SetDirty(CPartFile *file);
223 void SetNew(CPartFile *file);
224 void SetCompleted(CPartFile *file);
225 void SetRemoved(CPartFile *file);
227 virtual CECPacket *GetNextPacket();
231 class ECKnownFileMsgSource : public ECUpdateMsgSource {
232 typedef struct {
233 bool m_new;
234 bool m_comment_changed;
235 bool m_removed;
236 bool m_dirty;
237 CKnownFile *m_file;
238 } KNOWNFILE_STATUS;
239 std::map<CMD4Hash, KNOWNFILE_STATUS> m_dirty_status;
240 public:
241 ECKnownFileMsgSource();
243 void SetDirty(CKnownFile *file);
244 void SetNew(CKnownFile *file);
245 void SetRemoved(CKnownFile *file);
247 virtual CECPacket *GetNextPacket();
250 class ECClientMsgSource : public ECUpdateMsgSource {
251 public:
252 virtual CECPacket *GetNextPacket();
255 class ECStatusMsgSource : public ECUpdateMsgSource {
256 uint32 m_last_ed2k_status_sent;
257 uint32 m_last_kad_status_sent;
258 void *m_server;
260 uint32 GetEd2kStatus();
261 uint32 GetKadStatus();
262 public:
263 ECStatusMsgSource();
265 virtual CECPacket *GetNextPacket();
268 class ECSearchMsgSource : public ECUpdateMsgSource {
269 typedef struct {
270 bool m_new;
271 bool m_child_dirty;
272 bool m_dirty;
273 CSearchFile *m_file;
274 } SEARCHFILE_STATUS;
275 std::map<CMD4Hash, SEARCHFILE_STATUS> m_dirty_status;
276 public:
277 ECSearchMsgSource();
279 void SetDirty(CSearchFile *file);
280 void SetChildDirty(CSearchFile *file);
282 void FlushStatus();
284 virtual CECPacket *GetNextPacket();
287 class ECNotifier {
289 // designated priority for each type of update
291 enum EC_SOURCE_PRIO {
292 EC_PARTFILE = 0,
293 EC_SEARCH,
294 EC_CLIENT,
295 EC_STATUS,
296 EC_KNOWN,
298 EC_STATUS_LAST_PRIO
301 //ECUpdateMsgSource *m_msg_source[EC_STATUS_LAST_PRIO];
302 std::map<CECServerSocket *, ECUpdateMsgSource **> m_msg_source;
304 void NextPacketToSocket();
306 CECPacket *GetNextPacket(ECUpdateMsgSource *msg_source_array[]);
307 public:
308 ECNotifier();
310 void Add_EC_Client(CECServerSocket *sock);
311 void Remove_EC_Client(CECServerSocket *sock);
313 CECPacket *GetNextPacket(CECServerSocket *sock);
316 // Interface to notification macros
318 void DownloadFile_SetDirty(CPartFile *file);
319 void DownloadFile_RemoveFile(CPartFile *file);
320 void DownloadFile_RemoveSource(CPartFile *file);
321 void DownloadFile_AddFile(CPartFile *file);
322 void DownloadFile_AddSource(CPartFile *file);
324 void Status_ConnectionState();
325 void Status_QueueCount();
326 void Status_UserCount();
328 void SharedFile_AddFile(CKnownFile *file);
329 void SharedFile_RemoveFile(CKnownFile *file);
330 void SharedFile_RemoveAllFiles();
335 #endif // EXTERNALCONN_H
336 // File_checked_for_headers