HCE project C++ developers source code library  1.1.1
HCE project developer library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TResStatHandler.h
Go to the documentation of this file.
1 #ifndef TRESSTATHANDLER_H
2 #define TRESSTATHANDLER_H
3 #include <new>
4 #include <string.h>
5 #include <BaseHandler.h>
6 #include <Thread.h>
7 #include <AStorageHandler.h>
8 #include <UpdateExtendedTypes.h>
9 
10 template <class T>
11 class TResStatHandler:public BaseHandler, private Thread
12 {
13  public:
14  TResStatHandler(T *storage, AStorageHandler *statisticHandler):BaseHandler(140)
15  {
16  this->statisticHandler = statisticHandler;
17  this->storage = storage;
18  queue = NULL;
19  queuePos = 0;
20  queueSize = 0;
21  current = 0;
22  inProgress = false;
23  statisticHandler->setValue("AdminSiteClearRequestCounter", 0ll);
24  statisticHandler->setValue("AdminSiteClearResourceCounter", 0ll);
25  statisticHandler->setValue("AdminResListDelRequestCounter", 0ll);
26  statisticHandler->setValue("AdminResListDelResourceCounter", 0ll);
27  statisticHandler->setValue("AdminResListRequestCounter", 0ll);
28  }
30  {
31  if(queue)free(queue);
32  }
33  void* handle(char code, ...)
34  {
35  setError(0, NULL);
36  switch(code)
37  {
38  case 'a':
39  {
40  statisticHandler->addToValue("AdminResListRequestCounter", 1ll);
41  size_t size = 8192;
42  char *ret = (char*)malloc(size + sizeof(int));
43  unsigned int *curSize = (unsigned int*)ret;
44  *curSize = 0;
45  u_int32_t count;
46  u_int8_t *res = storage->getResourceList(count);
47  for(u_int32_t i = 0; i < count; i ++)
48  {
49  if(size < (size_t)*curSize + 64)
50  {
51  size <<= 1;
52  ret = (char*)realloc(ret, size);
53  curSize = (unsigned int*)ret;
54  }
55  *curSize += sprintf(ret + sizeof(int) + *curSize, "%llu %u ", *(u_int64_t*)(res + i * (sizeof(u_int64_t) + sizeof(u_int8_t))), (u_int32_t)(*(res + i * (sizeof(u_int64_t) + sizeof(u_int8_t)) + sizeof(u_int64_t))));
56  }
57  if(res)free(res);
58  ret[sizeof(int) + *curSize] = '\0';
59  return ret;
60  break;
61  }
62  case 'b':
63  {
64  statisticHandler->addToValue("AdminResListDelRequestCounter", 1ll);
65  va_list list;
66  va_start(list, code);
67  int *ret = (int*)malloc(sizeof(int));
68  *ret = 0;
69  char *resources = va_arg(list, char*);
70  String str(resources);
71  str.urlDecode();
72  resources = (char*)str.c_str();
73  if(resources)
74  {
75  int count = 0;
76  int len = strlen(resources) / 16;
77  char **endptr = NULL;
78  if (len > 0)
79  {
80  u_int64_t *ids = new u_int64_t[len];
81  while(*resources && count < len)
82  {
83  while(*resources && (*resources < '0' || *resources > '9'))resources++;
84  if(*resources)
85  {
86  ids[count++] = (u_int64_t)strtoull(resources, endptr, 10);
87  resources = *endptr;
88  if (!resources)
89  {
90  break;
91  }
92  }
93  }
94  *ret = storage->delResourceList(ids, count);
95  statisticHandler->addToValue("AdminResListDelResourceCounter", (long long)(*ret));
96  delete []ids;
97  }
98  }
99 /*
100  statisticHandler->addToValue("AdminResListDelRequestCounter", 1ll);
101  va_list list;
102  va_start(list, code);
103  int *ret = (int*)malloc(sizeof(int));
104  *ret = 0;
105  char *resources = va_arg(list, char*);
106  String str(resources);
107  str.urlDecode();
108  resources = (char*)str.c_str();
109  if(resources)
110  {
111  int count = atoi(resources);
112  if(count)
113  {
114  u_int64_t *ids = new u_int64_t[count];
115  int i = 0;
116  while(*resources >= '0' && *resources <='9')resources++;
117  while(*resources && i < count)
118  {
119  while(*resources && (*resources < '0' || *resources > '9'))resources++;
120  if(*resources)
121  {
122  ids[i++] = (u_int64_t)strtoull(resources, NULL, 10);
123  }
124  while(*resources >= '0' && *resources <='9')resources++;
125  }
126  count = i;
127  *ret = storage->delResourceList(ids, count);
128  statisticHandler->addToValue("AdminResListDelResourceCounter", (long long)(*ret));
129  delete []ids;
130  }
131  }
132  va_end(list);
133  return ret;
134  break;
135 */
136  }
137  case 'c':
138  {
139  statisticHandler->addToValue("AdminSiteClearRequestCounter", 1ll);
140  va_list list;
141  va_start(list, code);
142  int *ret = (int*)malloc(sizeof(int));
143  u_int32_t siteId = va_arg(list, u_int32_t);
144  mutex.lock();
145  for(u_int32_t i = 0; i < queuePos; i++)
146  if(queue[i] == siteId)
147  {
148  *ret = 0;
149 //.. FIX MUTEX UNLOCKING
150  mutex.unlock();
151  return ret;
152  }
153  if(queuePos == queueSize)
154  {
155  queueSize = queueSize == 0 ? 1: (queueSize << 1);
156  queue = (u_int32_t*)realloc(queue, sizeof(u_int32_t) * queueSize);
157  }
158  queue[queuePos++] = siteId;
159  *ret = queuePos;
160  mutex.unlock();
161  if(!inProgress)
162  {
163  start();
164  detach();
165  }
166  va_end(list);
167  return ret;
168  break;
169  }
170  case 'd':
171  {
172  char *ret = (char*)malloc(128);
173  va_list list;
174  va_start(list, code);
175  u_int32_t siteId = va_arg(list, u_int32_t);
176  if(!siteId)
177  {
178  sprintf(ret, "v1=%u&v2=%u&v3=%u", inProgress?1:0, queuePos, current);//inProgress, count in queue
179  }
180  else
181  {
182  bool find = false;
183  u_int32_t foundPos = 0;
184  mutex.lock();
185  if(inProgress && current == siteId)find = true;
186  else
187  {
188  for(u_int32_t i = 0; i < queuePos && !find; i++)
189  if(queue[i] == siteId)
190  {
191  find = true;
192  foundPos = i;
193  }
194  }
195  mutex.unlock();
196  sprintf(ret, "v1=%u&v2=%u&v3=%u", find?1:0, foundPos, current);//inProgress, count in queue
197  }
198  return ret;
199  }
200  case 'e':
201  {
202  bool *ret = (bool*)malloc(1);
203  va_list list;
204  va_start(list, code);
205  char *encodeExtendedTypes = va_arg(list, char *);
206  if (!encodeExtendedTypes)
207  {
208  sprintf((char *)ret, "%c", 0);
209  }
210  else
211  {
212  String decodeString (encodeExtendedTypes);
213  decodeString.urlDecode();
214  if (decodeString.c_str());
215  {
216  UpdatedExtendedTypes updateExtendedTypes;
217  string s (decodeString.c_str());
218  updateExtendedTypes.parse (s);
219  *ret = storage->updateResourcesParams(updateExtendedTypes.getUpdatedTypesList());
220  }
221  }
222  return ret;
223  }
224 
225 // ADD new handle code here
226  default:
227  {
228  setError(1, "Wrong command code");
229  break;
230  }
231  }
232  return NULL;
233  }
234  private:
235  T *storage;
236  AStorageHandler *statisticHandler;
237  u_int32_t *queue;
238  u_int32_t queuePos;
239  u_int32_t queueSize;
240  u_int32_t current;
241  bool inProgress;
242  Mutex mutex;
243 
244  void *run()
245  {
246  inProgress = true;
247  u_int32_t siteId;
248  do
249  {
250  siteId = u_int32_t(-1);
251  mutex.lock();
252  if(queuePos)siteId = queue[--queuePos];
253  mutex.unlock();
254  if(siteId != u_int32_t(-1))
255  {
256  current = siteId;
257  statisticHandler->addToValue("AdminSiteClearResourceCounter", (long long)storage->delBySite(siteId));
258  }
259  }while(siteId != u_int32_t(-1));
260  inProgress = false;
261  return NULL;
262  }
263 };
264 
265 #endif