rllib  1
rldataprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rldataprovider.cpp - description
3  -------------------
4  begin : Fri Dec 20 2002
5  copyright : (C) 2002 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rldataprovider.h"
17 #include <stdio.h>
18 #include <string.h>
19 
20 rlDataProvider::rlDataProvider(int numInteger, int numFloat, int numString)
21 {
22  int i;
23 
24  num_integer = numInteger;
25  num_float = numFloat;
26  num_string = numString;
27  if(num_integer < 0) num_integer = 0;
28  if(num_float < 0) num_float = 0;
29  if(num_string < 0) num_string = 0;
30  ints = NULL;
31  floats = NULL;
32  strings = NULL;
33  if(num_integer > 0)
34  {
35  ints = new int[num_integer];
36  for(i=0; i<num_integer; i++) ints[i] = 0;
37  }
38  if(num_float > 0)
39  {
40  floats = new float[num_float];
41  for(i=0; i<num_float; i++) floats[i] = 0.0f;
42  }
43  if(num_string > 0)
44  {
45  strings = new CHARPTR[num_string];
46  for(i=0; i<num_string; i++)
47  {
48  strings[i] = new char [1];
49  *strings[i] = '\0';
50  }
51  }
52 }
53 
55 {
56  int i;
57 
58  if(ints != NULL) delete [] ints;
59  if(floats != NULL) delete [] floats;
60  for(i=0; i<num_string; i++)
61  {
62  if(strings[i] != NULL) delete [] strings[i];
63  }
64  if(strings != NULL) delete [] strings;
65 }
66 
68 {
69  int ret;
70 
71  if(id < 0) return -1;
72  if(id >= num_integer) return -1;
73  mutex.lock();
74  ret = ints[id];
75  mutex.unlock();
76  return ret;
77 }
78 
80 {
81  float ret;
82 
83  if(id < 0) return 0.0f;
84  if(id >= num_float) return 0.0f;
85  mutex.lock();
86  ret = floats[id];
87  mutex.unlock();
88  return ret;
89 }
90 
91 int rlDataProvider::getIntArray(int id, int *array, int nmax)
92 {
93  int ret;
94  const char *cptr = getString(id);
95  if(cptr == NULL) return -1;
96 
97  ret = 0;
98  while(1)
99  {
100  if(cptr == NULL || *cptr == '\0') break;
101  if(*cptr != ',' && *cptr != '\"')
102  {
103  sscanf(cptr,"%d",&array[ret++]);
104  cptr = strchr(cptr,',');
105  if(cptr != NULL) cptr++;
106  if(ret >= nmax) break;
107  }
108  else
109  {
110  cptr++;
111  }
112  }
113  return ret;
114 }
115 
116 int rlDataProvider::getFloatArray(int id, float *array, int nmax)
117 {
118  int ret;
119  const char *cptr = getString(id);
120  if(cptr == NULL) return -1;
121 
122  ret = 0;
123  while(1)
124  {
125  if(cptr == NULL || *cptr == '\0') break;
126  if(*cptr != ',' && *cptr != '\"')
127  {
128  sscanf(cptr,"%f",&array[ret++]);
129  cptr = strchr(cptr,',');
130  if(cptr != NULL) cptr++;
131  if(ret >= nmax) break;
132  }
133  else
134  {
135  cptr++;
136  }
137  }
138  return ret;
139 }
140 
141 const char *rlDataProvider::getString(int id)
142 {
143  const char *ret;
144 
145  if(id < 0) return NULL;
146  if(id >= num_string) return NULL;
147  mutex.lock();
148  ret = strings[id];
149  mutex.unlock();
150  return ret;
151 }
152 
153 int rlDataProvider::setInt(int id, int i)
154 {
155  if(id < 0) return -1;
156  if(id >= num_integer) return -1;
157  mutex.lock();
158  ints[id] = i;
159  mutex.unlock();
160  return 0;
161 }
162 
163 int rlDataProvider::setFloat(int id, float f)
164 {
165  if(id < 0) return -1;
166  if(id >= num_float) return -1;
167  mutex.lock();
168  floats[id] = f;
169  mutex.unlock();
170  return 0;
171 }
172 
173 int rlDataProvider::setIntArray(int id, int *array, int num)
174 {
175  char buf[rl_PRINTF_LENGTH],val[100];
176  int nmax,ipos,ind;
177 
178  if(num <= 0) return -1;
179  nmax = rl_PRINTF_LENGTH - 1;
180  buf[0] = '\0';
181  ipos = strlen(buf);
182  for(ind=0; ind<num; ind++)
183  {
184  sprintf(val,"%d,",array[ind]);
185  if(ipos+(int)strlen(val) >= nmax) return -1;
186  strcat(buf,val);
187  ipos = strlen(buf);
188  }
189  return setString(id,buf);
190 }
191 
192 int rlDataProvider::setFloatArray(int id, float *array, int num)
193 {
194  char buf[rl_PRINTF_LENGTH],val[100];
195  int nmax,ipos,ind;
196 
197  if(num <= 0) return -1;
198  nmax = rl_PRINTF_LENGTH - 1;
199  buf[0] = '\0';
200  ipos = strlen(buf);
201  for(ind=0; ind<num; ind++)
202  {
203  sprintf(val,"%f,",array[ind]);
204  if(ipos+(int)strlen(val) >= nmax) return -1;
205  strcat(buf,val);
206  ipos = strlen(buf);
207  }
208  return setString(id,buf);
209 }
210 
211 int rlDataProvider::setString(int id, const char *str)
212 {
213  if(id < 0) return -1;
214  if(id >= num_string) return -1;
215  mutex.lock();
216  if(strings[id] != NULL) delete [] strings[id];
217  strings[id] = new char[strlen(str)+1];
218  strcpy(strings[id],str);
219  mutex.unlock();
220  return 0;
221 }
222 
224 {
225  int ret;
226 
227  if(id < 0) return -1;
228  if(id >= num_integer) return -1;
229  mutex.lock();
230  ret = ints[id];
231  ints[id] = -1;
232  mutex.unlock();
233  return ret;
234 }
235 
237 {
238  if(id < 0) return -1;
239  if(id >= num_integer) return -1;
240  while(1)
241  {
242  mutex.lock();
243  if(ints[id] == -1)
244  {
245  ints[id] = i;
246  mutex.unlock();
247  return 0;
248  }
249  mutex.unlock();
250  rlsleep(10);
251  }
252 }
253 
255 {
256  int ret;
257 
258  ret = setInt(0,i);
260  return ret;
261 }
262 
264 {
266  return getInt(0);
267 }
268 
270 {
271  int id,ival,ret;
272  float fval;
273  char cval[rl_PRINTF_LENGTH];
274  const char *cptr;
275  rlInterpreter interpreter;
276 
277  while(socket->isConnected())
278  {
279  ret = socket->readStr(interpreter.line,rl_PRINTF_LENGTH-1);
280  if(ret <= 0) break;
281  if (interpreter.isCommand("int("))
282  {
283  sscanf(interpreter.line,"int(%d,%d)",&id,&ival);
284  setInt(id,ival);
285  }
286  else if(interpreter.isCommand("intAndReset("))
287  {
288  sscanf(interpreter.line,"intAndReset(%d,%d)",&id,&ival);
289  setIntAndWaitForReset(id,ival);
290  }
291  else if(interpreter.isCommand("float("))
292  {
293  sscanf(interpreter.line,"float(%d,%f)",&id,&fval);
294  setFloat(id,fval);
295  }
296  else if(interpreter.isCommand("string("))
297  {
298  sscanf(interpreter.line,"string(%d,",&id);
299  interpreter.copyStringParam(cval,0);
300  setString(id,cval);
301  }
302  else if(interpreter.isCommand("getInt("))
303  {
304  sscanf(interpreter.line,"getInt(%d)",&id);
305  socket->printf("intResult(%d)\n",getInt(id));
306  }
307  else if(interpreter.isCommand("getIntAndReset("))
308  {
309  sscanf(interpreter.line,"getIntAndReset(%d)",&id);
310  socket->printf("intResult(%d)\n",getIntAndReset(id));
311  }
312  else if(interpreter.isCommand("getInt0Semaphore("))
313  {
314  socket->printf("intResult(%d)\n",getInt0Semaphore());
315  }
316  else if(interpreter.isCommand("getFloat("))
317  {
318  sscanf(interpreter.line,"getFloat(%d)",&id);
319  socket->printf("floatResult(%f)\n",getFloat(id));
320  }
321  else if(interpreter.isCommand("getString("))
322  {
323  sscanf(interpreter.line,"getString(%d)",&id);
324  cptr = getString(id);
325  if(cptr == NULL) socket->printf("stringResult(\"\")\n");
326  else socket->printf("stringResult(\"%s\")\n",cptr);
327  }
328  }
329  return 0;
330 }
331 
332 //------------------------------------------------------------------------------------
333 #define CLIENT_TIMEOUT 1000
334 
336 {
337  rlwsa();
338 }
339 
341 {
342 }
343 
344 int rlDataProviderClient::getInt(rlSocket *socket, int id, int *status)
345 {
346  int ret;
347 
348  *status = -1;
349  if(socket->isConnected() == 0) return 0;
350  socket->printf("getInt(%d)\n",id);
352  if(ret == 0) // timeout
353  {
354  socket->disconnect();
355  socket->connect();
356  *status = -2;
357  return 0;
358  }
359  if(interpreter.isCommand("intResult("))
360  {
361  sscanf(interpreter.line,"intResult(%d)",&ret);
362  *status = 0;
363  return ret;
364  }
365  return 0;
366 }
367 
368 float rlDataProviderClient::getFloat(rlSocket *socket, int id, int *status)
369 {
370  float ret;
371  int iret;
372 
373  *status = -1;
374  if(socket->isConnected() == 0) return 0.0f;
375  socket->printf("getFloat(%d)\n",id);
377  if(iret == 0) // timeout
378  {
379  socket->disconnect();
380  socket->connect();
381  *status = -2;
382  return 0;
383  }
384  if(interpreter.isCommand("floatResult("))
385  {
386  sscanf(interpreter.line,"floatResult(%f)",&ret);
387  *status = 0;
388  return ret;
389  }
390  return 0.0f;
391 }
392 
393 int rlDataProviderClient::getIntArray(rlSocket *socket, int id, int *array, int nmax)
394 {
395  int status,ret;
396  const char *cptr = getString(socket,id,&status);
397  if(status != 0) return -1;
398 
399  ret = 0;
400  while(1)
401  {
402  if(cptr == NULL || *cptr == '\0') break;
403  if(*cptr != ',' && *cptr != '\"')
404  {
405  sscanf(cptr,"%d",&array[ret++]);
406  cptr = strchr(cptr,',');
407  if(cptr != NULL) cptr++;
408  if(ret >= nmax) break;
409  }
410  else
411  {
412  cptr++;
413  }
414  }
415  return ret;
416 }
417 
418 int rlDataProviderClient::getFloatArray(rlSocket *socket, int id, float *array, int nmax)
419 {
420  int status,ret;
421  const char *cptr = getString(socket,id,&status);
422  if(status != 0) return -1;
423 
424  ret = 0;
425  while(1)
426  {
427  if(cptr == NULL || *cptr == '\0') break;
428  if(*cptr != ',' && *cptr != '\"')
429  {
430  sscanf(cptr,"%f",&array[ret++]);
431  cptr = strchr(cptr,',');
432  if(cptr != NULL) cptr++;
433  if(ret >= nmax) break;
434  }
435  else
436  {
437  cptr++;
438  }
439  }
440  return ret;
441 }
442 
443 const char *rlDataProviderClient::getString(rlSocket *socket, int id, int *status)
444 {
445  int ret;
446 
447  *status = -1;
448  if(socket->isConnected() == 0) return NULL;
449  socket->printf("getString(%d)\n",id);
451  if(ret == 0) // timeout
452  {
453  socket->disconnect();
454  socket->connect();
455  *status = -2;
456  return 0;
457  }
458  if(interpreter.isCommand("stringResult("))
459  {
461  *status = 0;
462  return cret;
463  }
464  return NULL;
465 }
466 
467 int rlDataProviderClient::setInt(rlSocket *socket, int id, int i)
468 {
469  if(socket->isConnected() == 0) return -1;
470  return socket->printf("int(%d,%d)\n",id,i);
471 }
472 
473 int rlDataProviderClient::setFloat(rlSocket *socket, int id, float f)
474 {
475  if(socket->isConnected() == 0) return -1;
476  return socket->printf("float(%d,%d)\n",id,f);
477 }
478 
479 int rlDataProviderClient::setIntArray(rlSocket *socket, int id, int *i, int num)
480 {
481  char buf[rl_PRINTF_LENGTH],val[100];
482  int nmax,ipos,ind;
483 
484  if(num <= 0) return -1;
485  nmax = rl_PRINTF_LENGTH - strlen("\")\n") - 1;
486  sprintf(buf,"string(%d,\"",id);
487  ipos = strlen(buf);
488  for(ind=0; ind<num; ind++)
489  {
490  sprintf(val,"%d,",i[ind]);
491  if(ipos+(int)strlen(val) >= nmax) return -1;
492  strcat(buf,val);
493  ipos = strlen(buf);
494  }
495  strcat(buf,"\"\n");
496  return socket->printf(buf);
497 }
498 
499 int rlDataProviderClient::setFloatArray(rlSocket *socket, int id, float *f, int num)
500 {
501  char buf[rl_PRINTF_LENGTH],val[100];
502  int nmax,ipos,ind;
503 
504  if(num <= 0) return -1;
505  nmax = rl_PRINTF_LENGTH - strlen("\")\n") - 1;
506  sprintf(buf,"string(%d,\"",id);
507  ipos = strlen(buf);
508  for(ind=0; ind<num; ind++)
509  {
510  sprintf(val,"%f,",f[ind]);
511  if(ipos+(int)strlen(val) >= nmax) return -1;
512  strcat(buf,val);
513  ipos = strlen(buf);
514  }
515  strcat(buf,"\"\n");
516  return socket->printf(buf);
517 }
518 
519 int rlDataProviderClient::setString(rlSocket *socket, int id, const char *str)
520 {
521  if(socket->isConnected() == 0) return -1;
522  return socket->printf("string(%d,\"%d\")\n",id,str);
523 }
524 
525 int rlDataProviderClient::getIntAndReset(rlSocket *socket, int id, int *status)
526 {
527  int ret;
528 
529  *status = -1;
530  if(socket->isConnected() == 0) return 0;
531  socket->printf("getIntAndReset(%d)\n",id);
533  if(ret == 0) // timeout
534  {
535  socket->disconnect();
536  socket->connect();
537  *status = -2;
538  return 0;
539  }
540  if(interpreter.isCommand("intResult("))
541  {
542  sscanf(interpreter.line,"intResult(%d)",&ret);
543  *status = 0;
544  return ret;
545  }
546  return 0;
547 }
548 
550 {
551  if(socket->isConnected() == 0) return -1;
552  return socket->printf("intAndReset(%d,%d)\n",id,i);
553 }
554 
556 {
557  int ret;
558 
559  *status = -1;
560  if(socket->isConnected() == 0) return 0;
561  socket->printf("getInt0Semaphore()\n");
562  ret = socket->readStr(interpreter.line,rl_PRINTF_LENGTH-1,CLIENT_TIMEOUT*60*60); // 1 hour timeout
563  if(ret == 0) { *status = -2; return 0; } // timeout
564  if(interpreter.isCommand("intResult("))
565  {
566  sscanf(interpreter.line,"intResult(%d)",&ret);
567  *status = 0;
568  return ret;
569  }
570  return 0;
571 }
572 
573 //------------------------------------------------------------------------------------
574 typedef struct
575 {
577  int socket;
578 }WORKER_DATA;
579 
580 static void *workerThread(void *arg)
581 {
582  WORKER_DATA *worker_data;
583  THREAD_PARAM *p = (THREAD_PARAM *) arg;
584  if(arg == NULL) return NULL;
585  worker_data = (WORKER_DATA *) p->user;
586  rlSocket socket(worker_data->socket);
587 
588  worker_data->thread->provider->run(&socket);
589 
590  return NULL;
591 }
592 
593 static void *rlDataProviderAccept(void *arg)
594 {
595  int s;
596  rlThread worker;
597  WORKER_DATA worker_data;
598  rlDataProviderThreads *provider_threads;
599  THREAD_PARAM *p = (THREAD_PARAM *) arg;
600  if(arg == NULL) return NULL;
601  provider_threads = (rlDataProviderThreads *) p->user;
602  if(provider_threads == NULL) return NULL;
603  rlSocket socket("localhost",provider_threads->port,0);
604 
605  while(1)
606  {
607  s = socket.connect();
608  if(s == -1) break;
609  worker_data.thread = provider_threads;
610  worker_data.socket = s;
611  worker.create(workerThread,&worker_data);
612  rlsleep(100);
613  }
614  return NULL;
615 }
616 
618 {
619  rlwsa();
620  port = Port;
621  provider = Provider;
622 }
623 
625 {
626 }
627 
629 {
630  if(port <= 0) return;
631  if(port >= 256*256) return;
632  if(provider == NULL) return;
634 }
void * user
Definition: rlthread.h:30
int lock()
Definition: rlthread.cpp:105
int setFloat(int id, float f)
int getFloatArray(rlSocket *socket, int id, float *array, int nmax)
int create(void *(*func)(void *), void *argument)
Definition: rlthread.cpp:35
rlSemaphore int0semaphore
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int getIntAndReset(int id)
const char * getString(rlSocket *socket, int id, int *status)
static void * rlDataProviderAccept(void *arg)
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
float getFloat(int id)
char cret[rl_PRINTF_LENGTH]
const char * getString(int id)
int setString(int id, const char *str)
int getInt(int id)
int run(rlSocket *socket)
rlDataProviderThreads(int Port, rlDataProvider *Provider)
int setInt(rlSocket *socket, int id, int i)
int setIntArray(rlSocket *socket, int id, int *i, int num)
int isCommand(const char *command)
virtual ~rlDataProvider()
int setIntAndWaitForReset(rlSocket *socket, int id, int i)
int getInt(rlSocket *socket, int id, int *status)
void rlsleep(long msec)
Definition: rlwthread.cpp:396
int unlock()
Definition: rlthread.cpp:110
rlInterpreter interpreter
#define CLIENT_TIMEOUT
static void * workerThread(void *arg)
int incrementSemaphore()
Definition: rlthread.cpp:131
int setString(rlSocket *socket, int id, const char *str)
int waitSemaphore()
Definition: rlthread.cpp:126
int setInt0Semaphore(int i)
int getIntArray(rlSocket *socket, int id, int *array, int nmax)
int getInt0Semaphore(rlSocket *socket, int *status)
int connect()
Definition: rlsocket.cpp:321
float getFloat(rlSocket *socket, int id, int *status)
int disconnect()
Definition: rlsocket.cpp:545
int isConnected()
Definition: rlsocket.cpp:559
rlDataProvider * provider
rlDataProviderThreads * thread
int setIntAndWaitForReset(int id, int i)
void copyStringParam(char *destination, int index)
int setFloat(rlSocket *socket, int id, float f)
int setFloatArray(int id, float *f, int num)
int setIntArray(int id, int *i, int num)
int rlwsa()
Definition: rlsocket.cpp:68
int setInt(int id, int i)
int getFloatArray(int id, float *f, int nmax)
int getIntArray(int id, int *i, int nmax)
int getIntAndReset(rlSocket *socket, int id, int *status)
rlDataProvider(int numInteger, int numFloat=0, int numString=0)
int printf(const char *format,...)
Definition: rlsocket.cpp:586
int setFloatArray(rlSocket *socket, int id, float *f, int num)