|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include "stdafx.h" |
|
22 #include "statlist.h" |
|
23 |
|
24 //////////////////////////////////////////////////////////////////////////////////////// |
|
25 // The one and only connection list |
|
26 STATDLLList aList; |
|
27 |
|
28 ////////////////////////////////////////////////////////////////////////////////////////// |
|
29 // Constructor |
|
30 STATDLLList::STATDLLList() |
|
31 : ListStart(0) |
|
32 { |
|
33 InitializeCriticalSection(&CriticalSection); |
|
34 |
|
35 memset(szVersion, 0, sizeof(szVersion)); |
|
36 sprintf(szVersion, "%s.%s.%s", STAT_VERSION_MAJOR, STAT_VERSION_MINOR, STAT_VERSION_PATCH); |
|
37 |
|
38 } |
|
39 |
|
40 |
|
41 ////////////////////////////////////////////////////////////////////////////////////////// |
|
42 // Destructor |
|
43 // |
|
44 STATDLLList::~STATDLLList() |
|
45 { |
|
46 // release list members (passing 0 will return the last member in the list) |
|
47 STATDLLMember* pMember = Member(0); |
|
48 while(pMember) |
|
49 { |
|
50 RemoveListMember((DWORD)pMember); |
|
51 pMember = Member(0); |
|
52 } |
|
53 |
|
54 StopLogging(); |
|
55 DeleteCriticalSection(&CriticalSection); |
|
56 } |
|
57 |
|
58 |
|
59 ////////////////////////////////////////////////////////////////////////////////////////// |
|
60 // Turns on logging for STAT multiple connection management. |
|
61 // |
|
62 int |
|
63 STATDLLList::SetLogging(const char *path) |
|
64 { |
|
65 bool valid = false; |
|
66 if (!oRep.active()) |
|
67 { |
|
68 // split path into folder/file |
|
69 char folder[MAX_PATH + 1] = {0}; |
|
70 strcpy(folder, path); |
|
71 char *ptr = strrchr(folder, '\\'); |
|
72 if (ptr) |
|
73 { |
|
74 char file[MAX_PATH + 1] = {0}; |
|
75 strcpy(file, ptr + 1); |
|
76 (*ptr) = (char)0; |
|
77 |
|
78 if (*file) |
|
79 { |
|
80 if (oRep.init(STAT_APPNAME, RPT_ALL, RPT_FILE, folder, file)) |
|
81 { |
|
82 oRep.dash(); |
|
83 oRep.header("%s", STAT_APPNAME); |
|
84 valid = true; |
|
85 } |
|
86 else |
|
87 strcpy(szErrorText, "Connection logging failed (file system error)."); |
|
88 } |
|
89 else |
|
90 strcpy(szErrorText, "Connection logging failed (invalid filename)."); |
|
91 } |
|
92 else |
|
93 strcpy(szErrorText, "Connection logging failed (invalid path)."); |
|
94 } |
|
95 else |
|
96 strcpy(szErrorText, "Connection logging is already initialised."); |
|
97 |
|
98 return valid; |
|
99 } |
|
100 |
|
101 |
|
102 ////////////////////////////////////////////////////////////////////////////////////////// |
|
103 // Turns off logging |
|
104 // |
|
105 void |
|
106 STATDLLList::StopLogging() |
|
107 { |
|
108 if (oRep.active()) |
|
109 oRep.kill(); |
|
110 } |
|
111 |
|
112 |
|
113 ////////////////////////////////////////////////////////////////////////////////////////// |
|
114 // Open a connection with a new list member |
|
115 // |
|
116 DWORD |
|
117 STATDLLList::CreateListMember(const STATCONNECTTYPE iConnectType, const char *pszPlatformType) |
|
118 { |
|
119 bool valid = false; |
|
120 |
|
121 (*szErrorText) = (char)0; |
|
122 oRep.info("Creating new list member (%d : %s)...", iConnectType, pszPlatformType); |
|
123 |
|
124 STATDLLMember *ptr = new STATDLLMember(iConnectType, pszPlatformType, &oRep); |
|
125 if (ptr) |
|
126 { |
|
127 if (ptr->GetErrorCode() == ITS_OK) |
|
128 { |
|
129 // add to list |
|
130 if (ListStart) |
|
131 { |
|
132 ptr->lPrevConnection = Member(0); // returns last member in list |
|
133 ptr->lPrevConnection->lNextConnection = ptr; |
|
134 ptr->lNextConnection = NULL; |
|
135 } |
|
136 else |
|
137 { |
|
138 ListStart = ptr; |
|
139 } |
|
140 |
|
141 // WalkList(); |
|
142 |
|
143 oRep.info("Member %ld (%d : %s) created", ptr, iConnectType, pszPlatformType); |
|
144 valid = true; |
|
145 } |
|
146 else |
|
147 { |
|
148 // save the error thrown |
|
149 strcpy(szErrorText, ptr->GetErrorText()); |
|
150 delete ptr; |
|
151 ptr = NULL; |
|
152 } |
|
153 } |
|
154 |
|
155 if (!ptr || !valid) |
|
156 oRep.error("ERROR: Could not create new member"); |
|
157 |
|
158 return (DWORD)ptr; |
|
159 } |
|
160 |
|
161 |
|
162 ////////////////////////////////////////////////////////////////////////////////////////// |
|
163 // Find and return a member connection |
|
164 // If 0 or bad pointer is passed, will return the last position in the list |
|
165 // If nothing in list, will return NULL |
|
166 // |
|
167 STATDLLMember* |
|
168 STATDLLList::Member(const DWORD dwHandle) |
|
169 { |
|
170 STATDLLMember *current = 0, *next = 0; |
|
171 next = ListStart; |
|
172 while (next) |
|
173 { |
|
174 current = next; |
|
175 if (current == (STATDLLMember *)dwHandle) |
|
176 break; |
|
177 |
|
178 next = current->lNextConnection; |
|
179 } |
|
180 |
|
181 return current; |
|
182 } |
|
183 |
|
184 |
|
185 ////////////////////////////////////////////////////////////////////////////////////////// |
|
186 // Close a member connection |
|
187 // |
|
188 bool |
|
189 STATDLLList::RemoveListMember(const DWORD dwHandle) |
|
190 { |
|
191 STATDLLMember *current = Member(dwHandle); |
|
192 oRep.info("Removing list member %ld...", dwHandle); |
|
193 bool valid = false; |
|
194 |
|
195 // remove from list |
|
196 if (current) |
|
197 { |
|
198 // something before |
|
199 if (current->lPrevConnection) |
|
200 current->lPrevConnection->lNextConnection = current->lNextConnection; |
|
201 else |
|
202 ListStart = current->lNextConnection; |
|
203 |
|
204 // something after |
|
205 if (current->lNextConnection) |
|
206 current->lNextConnection->lPrevConnection = current->lPrevConnection; |
|
207 |
|
208 delete current; |
|
209 oRep.info("Member removed"); |
|
210 |
|
211 // WalkList(); |
|
212 |
|
213 valid = true; |
|
214 } |
|
215 else |
|
216 oRep.error("ERROR: Could not remove"); |
|
217 |
|
218 return valid; |
|
219 } |
|
220 |
|
221 |
|
222 ////////////////////////////////////////////////////////////////////////////////////////// |
|
223 // Private Methods |
|
224 ////////////////////////////////////////////////////////////////////////////////////////// |
|
225 |
|
226 ////////////////////////////////////////////////////////////////////////////////////////// |
|
227 // walks the list for debug purposes |
|
228 // |
|
229 void |
|
230 STATDLLList::WalkList() |
|
231 { |
|
232 if (ListStart) |
|
233 oRep.dash(); |
|
234 |
|
235 STATDLLMember *current = 0, *next = 0; |
|
236 next = ListStart; |
|
237 while (next) |
|
238 { |
|
239 current = next; |
|
240 |
|
241 oRep.info("Member details: Previous:%ld this:%ld Next:%ld", |
|
242 current->lPrevConnection, current, current->lNextConnection); |
|
243 |
|
244 next = current->lNextConnection; |
|
245 } |
|
246 |
|
247 if (ListStart) |
|
248 oRep.dash(); |
|
249 } |