1 /***************************************************************************
2 csearchmanager.h - description
3 -------------------
4 begin : Thu May 27 2004
5 copyright : (C) 2004 by Mathias Küster
6 email : mathen@users.berlios.de
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
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. *
15 * *
16 ***************************************************************************/
17
18 #ifndef CSEARCHMANAGER_H
19 #define CSEARCHMANAGER_H
20
21 /**
22 *@author Mathias Küster
23 *
24 * This whole class is mainly designed for automatically entering, doing
25 * the search, and leaving, hubs, from some list. I doubt any other
26 * client does this nor any hub would want this behaviour.
27 *
28 * What this class can not do is match search results to searches, nor can
29 * it manage a changing list of searches. Results are either sent to
30 * CDownloadManager for auto searches or via a single callback to one
31 * search window. There is a list of searches, but that can only be set
32 * when starting the search.
33 *
34 * Valknut may have been modified to allow more than one search window
35 * but it does not allow more than one search to be running at a time.
36 */
37
38 #include <time.h>
39
40 #include <dclib/dcos.h>
41 #include <dclib/core/cstring.h>
42 #include <dclib/core/cconnection.h>
43 #include <dclib/core/csingleton.h>
44 #include <dclib/cclient.h>
45 #include <dclib/csearchsocket.h>
46 #include <dclib/core/cstringlist.h>
47 #include <dclib/core/ccallback.h>
48
49 /** */
50 class CSearchClient : public CClient {
51 public:
52 /** */
CSearchClient()53 CSearchClient() {
54 m_bSearchRemove = false;
55 m_bSearchEnable = false;
56 m_pCurrentSearchObject = 0;
57 m_tSearchTimeout = 0;
58 };
59 /** */
~CSearchClient()60 virtual ~CSearchClient() {};
61
62 /** */
63 bool m_bSearchRemove;
64 /** */
65 bool m_bSearchEnable;
66 /** */
67 time_t m_tSearchTimeout;
68 /** */
69 CDCMessage * m_pCurrentSearchObject;
70 };
71
72 /** search modes */
73 enum eSearchMode {
74 esmCONNECTEDSINGLE = 0,
75 esmCONNECTEDALL,
76 esmPUBLIC,
77 esmBOOKMARK
78 };
79
80 enum eSearchState {
81 esNONE = 0,
82 esSEARCH,
83 esTIMEOUT,
84 esSTOP
85 };
86
87 /** */
88 enum eSearchError {
89 eseNONE = 0,
90 eseALREADYRUN
91 };
92
93 /** */
94 enum eSearchType {
95 estyNONE = 0,
96 estySINGLE,
97 estyMULTI,
98 estyEXTERNAL
99 };
100
101 /** */
102 class CSearchManager : public CSingleton<CSearchManager> {
103 public:
104 /** */
105 CSearchManager();
106 /** */
107 virtual ~CSearchManager();
108
109 /** */
110 int CallBackManager();
111 /** */
112 int CallBackClient( CClient * Client, CDCMessage * DCMessage );
113 /** */
114 int CallBackSearchSocket( CMessageSearchResult* );
115
116 /** */
117 eSearchError StartSearch( eSearchMode mode, eSearchType type, CList<CDCMessage> * querylist, CStringList<CString> * serverlist );
118 /** */
119 void StopSearch();
120
121 /** */
122 bool HandleSearch( CDCMessage * message );
123
124 /** */
SearchType()125 enum eSearchType SearchType() { return m_eSearchType; }
126 /** */
MaxClients()127 long MaxClients() { return m_nMaxClients; }
128 /** */
MaxClients(long n)129 void MaxClients( long n ) { m_nMaxClients = n; }
130 /** */
EnableTag(bool b)131 void EnableTag( bool b ) { m_bEnableTag = b; }
132
133 /** */
IsSearch()134 bool IsSearch() { return (m_eSearchType != estyNONE); }
135
136 /** */
SearchState()137 eSearchState SearchState() { eSearchState e; m_Mutex.Lock(); e = m_eSearchState; m_Mutex.UnLock(); return e; }
138 /** */
SearchState(eSearchState e)139 void SearchState( eSearchState e ) { m_Mutex.Lock(); m_eSearchState = e; m_Mutex.UnLock(); }
140
141 /** */
142 void SetCallBackFunction( _CCallback1<CDCMessage*> * callback );
143 /** */
144 _CCallback1<CDCMessage*> * GetCallBackFunction();
145
146 /** */
StartTime()147 time_t StartTime() { return m_tStartTime; };
148 /** */
149 long HubCount();
150 /** */
HubIndex()151 long HubIndex() { return m_nHubIndex; };
152 /** */
HubError()153 long HubError() { return m_nHubError; };
154
155 protected:
156
157 private:
158 /** */
159 bool AddClients();
160 /** */
161 bool AddClient();
162 /** */
163 void UpdateClients();
164 /** */
165 bool RemoveClients();
166 /** */
167 void DisconnectClients();
168 /** */
169 bool DoSearch( CSearchClient * HubSearchClient );
170 /** */
171 bool SendObject( CDCMessage * DCMessage );
172
173 /** manager callback */
174 _CCallback0 * m_pCallback;
175 /** */
176 CMutex m_Mutex;
177 /** client list */
178 CList<CSearchClient> * m_pClientList;
179
180 /** max simultan clients */
181 long m_nMaxClients;
182 /** current hub index */
183 long m_nHubIndex;
184 /** hub with errors */
185 long m_nHubError;
186 /** current hub */
187 CString * m_sCurrentHub;
188
189 /** hub list */
190 CStringList<CString> * m_pHubList;
191 /** */
192 CList<CDCMessage> * m_pSearchList;
193 /** only for passive searches */
194 CDCMessage * m_pCurrentSearchObject;
195
196 /** */
197 bool m_bEnableTag;
198 /** */
199 bool m_bHandleUserList;
200
201 /** */
202 CSearchSocket m_SearchSocket;
203
204 /** starttime */
205 time_t m_tStartTime;
206 /** timeout */
207 time_t m_tTimeoutTime;
208
209 /** search type */
210 enum eSearchType m_eSearchType;
211 /** search state */
212 enum eSearchState m_eSearchState;
213 /** search mode */
214 enum eSearchMode m_eSearchMode;
215 /** client mode */
216 enum eClientMode m_eClientMode;
217
218 /** callback function */
219 _CCallback1<CDCMessage*> * m_pParentCallback;
220 /** mutex for changing callback */
221 CMutex m_CallbackMutex;
222 };
223
224 /** */
SetCallBackFunction(_CCallback1<CDCMessage * > * callback)225 inline void CSearchManager::SetCallBackFunction( _CCallback1<CDCMessage*> * callback )
226 { m_CallbackMutex.Lock(); m_pParentCallback = callback; m_CallbackMutex.UnLock(); }
227 /** */
GetCallBackFunction()228 inline _CCallback1<CDCMessage*> * CSearchManager::GetCallBackFunction()
229 { return m_pParentCallback; }
230
231 #endif
232