rllib  1
Public Member Functions | Private Types | Private Attributes | List of all members
rlDataProvider Class Reference

#include <rldataprovider.h>

Collaboration diagram for rlDataProvider:
Collaboration graph
[legend]

Public Member Functions

 rlDataProvider (int numInteger, int numFloat=0, int numString=0)
 
virtual ~rlDataProvider ()
 
int getInt (int id)
 
float getFloat (int id)
 
int getIntArray (int id, int *i, int nmax)
 
int getFloatArray (int id, float *f, int nmax)
 
const char * getString (int id)
 
int setInt (int id, int i)
 
int setFloat (int id, float f)
 
int setIntArray (int id, int *i, int num)
 
int setFloatArray (int id, float *f, int num)
 
int setString (int id, const char *str)
 
int getIntAndReset (int id)
 
int setIntAndWaitForReset (int id, int i)
 
int setInt0Semaphore (int i)
 
int getInt0Semaphore ()
 
int run (rlSocket *socket)
 

Private Types

typedef char * CHARPTR
 

Private Attributes

int * ints
 
float * floats
 
char ** strings
 
int num_integer
 
int num_float
 
int num_string
 
rlMutex mutex
 
rlSemaphore int0semaphore
 

Detailed Description

This class is a container for data.
You can get/set the data identified by id.
The run method starts a separate thread that makes rlDataProvider available over TCP.

Definition at line 29 of file rldataprovider.h.

Member Typedef Documentation

◆ CHARPTR

typedef char* rlDataProvider::CHARPTR
private

Definition at line 50 of file rldataprovider.h.

Constructor & Destructor Documentation

◆ rlDataProvider()

rlDataProvider::rlDataProvider ( int  numInteger,
int  numFloat = 0,
int  numString = 0 
)

Definition at line 20 of file rldataprovider.cpp.

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 }

◆ ~rlDataProvider()

rlDataProvider::~rlDataProvider ( )
virtual

Definition at line 54 of file rldataprovider.cpp.

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 }

Member Function Documentation

◆ getFloat()

float rlDataProvider::getFloat ( int  id)

Definition at line 79 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
int unlock()
Definition: rlthread.cpp:110

◆ getFloatArray()

int rlDataProvider::getFloatArray ( int  id,
float *  f,
int  nmax 
)

Definition at line 116 of file rldataprovider.cpp.

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 }
const char * getString(int id)

◆ getInt()

int rlDataProvider::getInt ( int  id)

Definition at line 67 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
int unlock()
Definition: rlthread.cpp:110

◆ getInt0Semaphore()

int rlDataProvider::getInt0Semaphore ( )

Definition at line 263 of file rldataprovider.cpp.

264 {
266  return getInt(0);
267 }
rlSemaphore int0semaphore
int getInt(int id)
int waitSemaphore()
Definition: rlthread.cpp:126

◆ getIntAndReset()

int rlDataProvider::getIntAndReset ( int  id)

Definition at line 223 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
int unlock()
Definition: rlthread.cpp:110

◆ getIntArray()

int rlDataProvider::getIntArray ( int  id,
int *  i,
int  nmax 
)

Definition at line 91 of file rldataprovider.cpp.

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 }
const char * getString(int id)

◆ getString()

const char * rlDataProvider::getString ( int  id)

Definition at line 141 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
int unlock()
Definition: rlthread.cpp:110

◆ run()

int rlDataProvider::run ( rlSocket socket)

Definition at line 269 of file rldataprovider.cpp.

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 }
int setFloat(int id, float f)
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int getIntAndReset(int id)
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
float getFloat(int id)
const char * getString(int id)
int setString(int id, const char *str)
int getInt(int id)
int isCommand(const char *command)
int isConnected()
Definition: rlsocket.cpp:559
int setIntAndWaitForReset(int id, int i)
void copyStringParam(char *destination, int index)
int setInt(int id, int i)
int printf(const char *format,...)
Definition: rlsocket.cpp:586

◆ setFloat()

int rlDataProvider::setFloat ( int  id,
float  f 
)

Definition at line 163 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
int unlock()
Definition: rlthread.cpp:110

◆ setFloatArray()

int rlDataProvider::setFloatArray ( int  id,
float *  f,
int  num 
)

Definition at line 192 of file rldataprovider.cpp.

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 }
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int setString(int id, const char *str)

◆ setInt()

int rlDataProvider::setInt ( int  id,
int  i 
)

Definition at line 153 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
int unlock()
Definition: rlthread.cpp:110

◆ setInt0Semaphore()

int rlDataProvider::setInt0Semaphore ( int  i)

Definition at line 254 of file rldataprovider.cpp.

255 {
256  int ret;
257 
258  ret = setInt(0,i);
260  return ret;
261 }
rlSemaphore int0semaphore
int incrementSemaphore()
Definition: rlthread.cpp:131
int setInt(int id, int i)

◆ setIntAndWaitForReset()

int rlDataProvider::setIntAndWaitForReset ( int  id,
int  i 
)

Definition at line 236 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
void rlsleep(long msec)
Definition: rlwthread.cpp:396
int unlock()
Definition: rlthread.cpp:110

◆ setIntArray()

int rlDataProvider::setIntArray ( int  id,
int *  i,
int  num 
)

Definition at line 173 of file rldataprovider.cpp.

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 }
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
int setString(int id, const char *str)

◆ setString()

int rlDataProvider::setString ( int  id,
const char *  str 
)

Definition at line 211 of file rldataprovider.cpp.

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 }
int lock()
Definition: rlthread.cpp:105
int unlock()
Definition: rlthread.cpp:110

Member Data Documentation

◆ floats

float* rlDataProvider::floats
private

Definition at line 52 of file rldataprovider.h.

◆ int0semaphore

rlSemaphore rlDataProvider::int0semaphore
private

Definition at line 56 of file rldataprovider.h.

◆ ints

int* rlDataProvider::ints
private

Definition at line 51 of file rldataprovider.h.

◆ mutex

rlMutex rlDataProvider::mutex
private

Definition at line 55 of file rldataprovider.h.

◆ num_float

int rlDataProvider::num_float
private

Definition at line 54 of file rldataprovider.h.

◆ num_integer

int rlDataProvider::num_integer
private

Definition at line 54 of file rldataprovider.h.

◆ num_string

int rlDataProvider::num_string
private

Definition at line 54 of file rldataprovider.h.

◆ strings

char** rlDataProvider::strings
private

Definition at line 53 of file rldataprovider.h.


The documentation for this class was generated from the following files: