source: flair-src/trunk/lib/FlairCore/src/ui_com.cpp@ 441

Last change on this file since 441 was 441, checked in by Sanahuja Guillaume, 3 years ago

update buffering

File size: 19.4 KB
RevLine 
[2]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[2]4// %flair:license}
5// created: 2011/05/01
6// filename: ui_com.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: classe permettant la lecture et l'ecriture sur socket UDT
14//
15//
16/*********************************************************************/
17
18#include "ui_com.h"
19#include <cstdlib>
20#include <string.h>
21#include <unistd.h>
22#include "Mutex.h"
23#include "SendData.h"
24#include "communication.h"
25#include "FrameworkManager.h"
26#include "zlib.h"
27#include <assert.h>
28#include "config.h"
29
30#ifdef __XENO__
31#include <pthread.h>
32#include <native/task.h>
33#include <native/task.h>
34#include <fcntl.h>
35#endif
36
37using std::string;
38using namespace flair::core;
39using namespace flair::gui;
40
[15]41ui_com::ui_com(const Object *parent, UDTSOCKET sock)
[213]42 : Thread(parent, "send", 2,16384*2) {
[15]43 // buffer envoi
44 send_buffer = (char *)malloc(3);
45 send_size = 3; // id(1)+period(2)
[2]46
[15]47 // mutex
48 send_mutex = NULL;
49 send_mutex = new Mutex(this, ObjectName());
[2]50
[15]51 socket_fd = sock;
52 connection_lost = false;
[2]53
54#ifdef __XENO__
[15]55 int status;
56 string tmp_name;
[2]57
[15]58 is_running = true;
[2]59
[15]60 // pipe
61 tmp_name = getFrameworkManager()->ObjectName() + "-" + ObjectName() + "-pipe";
62 // xenomai limitation
63 if (tmp_name.size() > 31)
64 Err("rt_pipe_create error (%s is too long)\n", tmp_name.c_str());
[2]65#ifdef RT_PIPE_SIZE
[15]66 status = rt_pipe_create(&pipe, tmp_name.c_str(), P_MINOR_AUTO, RT_PIPE_SIZE);
[2]67#else
[15]68 status = rt_pipe_create(&pipe, tmp_name.c_str(), P_MINOR_AUTO, 0);
[2]69#endif
70
[15]71 if (status != 0) {
[133]72 char errorMsg[256];
73 Err("rt_pipe_create error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
[15]74 // return -1;
75 }
[2]76
[15]77// start user side thread
[2]78#ifdef NRT_STACK_SIZE
[15]79 // Initialize thread creation attributes
80 pthread_attr_t attr;
81 if (pthread_attr_init(&attr) != 0) {
82 Err("pthread_attr_init error\n");
83 }
[2]84
[15]85 if (pthread_attr_setstacksize(&attr, NRT_STACK_SIZE) != 0) {
86 Err("pthread_attr_setstacksize error\n");
87 }
[2]88
[15]89 if (pthread_create(&thread, &attr, user_thread, (void *)this) < 0)
90#else // NRT_STACK_SIZE
91 if (pthread_create(&thread, NULL, user_thread, (void *)this) < 0)
92#endif // NRT_STACK_SIZE
93 {
94 Err("pthread_create error\n");
95 // return -1;
96 }
[2]97#ifdef NRT_STACK_SIZE
[15]98 if (pthread_attr_destroy(&attr) != 0) {
99 Err("pthread_attr_destroy error\n");
100 }
[2]101#endif
102
[15]103#endif //__XENO__
104 int timeout = 100;
105 if (UDT::setsockopt(socket_fd, 0, UDT_RCVTIMEO, &timeout, sizeof(int)) != 0)
106 Err("UDT::setsockopt error (UDT_RCVTIMEO)\n");
[247]107 /*
108 timeout=-1;
109 if (UDT::setsockopt(socket_fd, 0, UDT_SNDTIMEO, &timeout, sizeof(int)) != 0)
110 Err("UDT::setsockopt error (UDT_SNDTIMEO)\n");
111*/
[15]112 bool blocking = true;
113 if (UDT::setsockopt(socket_fd, 0, UDT_SNDSYN, &blocking, sizeof(bool)) != 0)
114 Err("UDT::setsockopt error (UDT_SNDSYN)\n");
[2]115
[15]116 if (UDT::setsockopt(socket_fd, 0, UDT_RCVSYN, &blocking, sizeof(bool)) != 0)
117 Err("UDT::setsockopt error (UDT_RCVSYN)\n");
118 //#endif //__XENO__
[2]119
[15]120 Start();
[2]121}
122
[15]123ui_com::~ui_com() {
[243]124//Printf("destruction ui_com\n");
[2]125
126#ifdef __XENO__
[15]127 is_running = false;
[2]128
[15]129 pthread_join(thread, NULL);
[2]130
[15]131 int status = rt_pipe_delete(&pipe);
[133]132 if (status != 0) {
133 char errorMsg[256];
134 Err("rt_pipe_delete error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
135 }
[2]136#endif
137
[15]138 SafeStop();
[2]139
[15]140 if (IsSuspended() == true)
141 Resume();
[2]142
[15]143 Join();
[247]144
145 char buf=CLOSING_CONNECTION;
146 Send(&buf,1);
147
[15]148 if (send_buffer != NULL)
149 free(send_buffer);
150 send_buffer = NULL;
[2]151
[243]152 //Printf("destruction ui_com ok\n");
[2]153}
154
[439]155//send, public part; dispatch to SendNRT (nrt) or rt_pipe (rt)
156//rt_pipe is read by user thread which calls SendNRT
[252]157void ui_com::Send(char *buf, ssize_t size,int ttl) {
[15]158 if (connection_lost == true)
159 return;
[2]160
[15]161 char *tosend = buf;
162 if (buf[0] == XML_HEADER) {
163 // cut xml header
164 tosend = strstr(buf, "<root");
165 size -= tosend - buf;
166 }
[2]167
168#ifdef __XENO__
[439]169 //data buf
170 ssize_t nb_write = rt_pipe_write(&pipe, tosend, size, P_NORMAL);
[15]171 if (nb_write < 0) {
[133]172 char errorMsg[256];
173 Err("rt_pipe_write error (%s)\n", strerror_r(-nb_write, errorMsg, sizeof(errorMsg)));
[15]174 } else if (nb_write != size) {
175 Err("rt_pipe_write error %i/%i\n", nb_write, size);
176 }
[439]177 //ttl
178 nb_write = rt_pipe_write(&pipe, &ttl, sizeof(ttl), P_NORMAL);
179 if (nb_write < 0) {
180 char errorMsg[256];
181 Err("rt_pipe_write error (%s)\n", strerror_r(-nb_write, errorMsg, sizeof(errorMsg)));
182 } else if (nb_write != sizeof(ttl)) {
183 Err("rt_pipe_write error %i/%i\n", nb_write, size);
184 }
[2]185#else //__XENO__
[439]186 SendNRT(tosend,size,ttl);
187#endif //__XENO__
188}
[2]189
[439]190//send, private part; called to effectively send to udt
191//this is always in nrt
192void ui_com::SendNRT(char *buf, ssize_t size,int ttl) {
193 ssize_t nb_write;
194 bool sendItCompressed = false;
195
[2]196#ifdef COMPRESS_FRAMES
[15]197 if (buf[0] == XML_HEADER) {
198 sendItCompressed = true;
199 }
[439]200#endif // COMPRESS_FRAMES
[2]201
[15]202 if (sendItCompressed) {
203 char *out;
204 ssize_t out_size;
[439]205 if (compressBuffer(buf, size, &out, &out_size, 9) == Z_OK) {
[15]206 size = out_size;
[252]207 nb_write = UDT::sendmsg(socket_fd, out, size,ttl, true);
[15]208 free(out);
209 } else {
210 Warn("Compress error, sending it uncompressed\n");
211 sendItCompressed = false;
[2]212 }
[15]213 }
[2]214
[15]215 if (!sendItCompressed) {
[439]216 nb_write = UDT::sendmsg(socket_fd, buf, size, ttl, true);
[15]217 }
[2]218
[247]219 //Printf("write %i %i\n",nb_write,size);
[15]220 if (nb_write < 0) {
221 Err("UDT::sendmsg error (%s)\n", UDT::getlasterror().getErrorMessage());
222 if (UDT::getlasterror().getErrorCode() == CUDTException::ECONNLOST ||
223 UDT::getlasterror().getErrorCode() == CUDTException::EINVSOCK) {
224 connection_lost = true;
[2]225 }
[15]226 } else if (nb_write != size) {
227 Err("%s, code %i (%ld/%ld)\n", UDT::getlasterror().getErrorMessage(),
228 UDT::getlasterror().getErrorCode(), nb_write, size);
229 }
[2]230}
231
[15]232ssize_t ui_com::Receive(char *buf, ssize_t buf_size) {
233 ssize_t bytesRead = UDT::recvmsg(socket_fd, buf, buf_size);
[2]234
[15]235 if (bytesRead < 0) {
236 if (UDT::getlasterror().getErrorCode() == CUDTException::ECONNLOST) {
237 Err("UDT::recvmsg error (%s)\n", UDT::getlasterror().getErrorMessage());
238 connection_lost = true;
[2]239 }
[15]240 }
[2]241
[15]242 return bytesRead;
[2]243}
244
[234]245void ui_com::CheckConnection(void) {
246 int32_t val,len;
247
248 int result=UDT::getsockopt(socket_fd,0,UDT_STATE,&val,&len);
249 if (result < 0) {
250 Printf("UDT::getsockopt error (%s)\n", UDT::getlasterror().getErrorMessage());
251 }
252 // printf("opt: %i %i %i\n",result,val,len);
253}
254
[439]255//todo: le run (rt) push les données une par une
256//le nrt recupere et decide quand envoyer :
257// -quand toutes les données d'une meme periode sont recues
258// -quand on a atteind le nb buffering
259// -le nrt alloue dynamiquement le buffer d'envoi
260//-> enelver l'alloc du buffer au startup (UpdateDataToSendSize)
[15]261void ui_com::Run(void) {
262 // check endianness
263 char header;
264 if (IsBigEndian()) {
[253]265 header = DATA_BIG_ENDIAN;
[15]266 Printf("System is big endian\n");
267 } else {
[253]268 header = DATA_LITTLE_ENDIAN;
[15]269 Printf("System is little endian\n");
270 }
[2]271
272#ifdef __XENO__
[15]273 WarnUponSwitches(true);
274 printf("\n"); // a revoir pourquoi??
275// sans ce printf, dans le simu_roll, le suspend ne fonctionne pas...
[2]276#endif
[15]277 // on attend d'avoir des choses à faire
278 Suspend();
[2]279
[15]280 while (!ToBeStopped()) {
281 size_t resume_id;
282 Time min = 0xffffffffffffffffULL;
[2]283
[15]284 // on recpuere l'id de la prochaine execution
285 send_mutex->GetMutex();
[441]286 resume_id = resumeTimes.size();
287 for (size_t i = 0; i < resumeTimes.size(); i++) {
288 if (resumeTimes.at(i) < min && datasToSend.at(i)->IsEnabled() == true) {
289 min = resumeTimes.at(i);
[15]290 resume_id = i;
291 }
292 }
[2]293
[15]294 // attente
[441]295 if (resume_id < resumeTimes.size()) {
296 Time time = resumeTimes.at(resume_id);
297 uint16_t resume_period = datasToSend.at(resume_id)->SendPeriod();
[15]298 send_mutex->ReleaseMutex();
299 // on dort jusqu'a la prochaine execution
300 SleepUntil(time);
[2]301
[15]302 // envoi des donnees
303 int offset = 3;
304 send_buffer[0] = header;
305 send_mutex->GetMutex();
[2]306
[441]307 for (size_t i = 0; i < datasToSend.size(); i++) {
308 if (datasToSend.at(i)->SendPeriod() == resume_period && datasToSend.at(i)->IsEnabled() == true) {
309 datasToSend.at(i)->CopyDatas(send_buffer + offset);
310 offset += datasToSend.at(i)->SendSize();
[15]311 }
312 }
313 if (offset != 3) {
314 memcpy(&send_buffer[1], &resume_period, sizeof(uint16_t));
[252]315 //printf("send %i %i %i %x %x\n",resume_period,offset,sizeof(uint16_t),send_buffer,&send_buffer[1]);
[15]316 // for(int i=0;i<offset;i++) printf("%x ",send_buffer[i]);
317 // printf("\n");
[252]318 Send(send_buffer, offset,resume_period);
[15]319 }
[2]320
[440]321 //test to push datas
[441]322 for (size_t i = 0; i < datasToSend.size(); i++) {
323 if (datasToSend.at(i)->SendPeriod() == resume_period && datasToSend.at(i)->IsEnabled() == true) {
324 PushDatasToSend(datasToSend.at(i));
[440]325 }
326 }
327 //end test
328
[15]329 // on planifie la prochaine execution
[441]330 for (size_t i = 0; i < datasToSend.size(); i++) {
331 if (datasToSend.at(i)->SendPeriod() == resume_period) {
332 resumeTimes.at(i) += datasToSend.at(i)->SendPeriod() * 1000000;
[2]333 }
[15]334 }
335 send_mutex->ReleaseMutex();
[157]336 //Printf("%i %lld\n",resume_period,GetTime()/1000000);
[15]337 } else {
338 send_mutex->ReleaseMutex();
339 // rien a faire, suspend
[157]340 //Printf("rien a faire suspend\n");
[15]341 Suspend();
[157]342 //Printf("wake\n");
[15]343 // on planifie la prochaine execution
344 Time time = GetTime();
345 send_mutex->GetMutex();
[441]346 for (size_t i = 0; i < datasToSend.size(); i++) {
347 resumeTimes.at(i) =
348 time + (Time)datasToSend.at(i)->SendPeriod() * 1000000;
[15]349 }
350 send_mutex->ReleaseMutex();
[2]351 }
[15]352 }
[2]353}
[440]354
355
356//called with send_mutex locked
357//in nrt
[441]358//TODO: RT part has to put datas in pipe, then user thread call this
359void ui_com::PushDatasToSend(const SendData *dataToSend) {
[440]360 PushedData_t* ptr=NULL;
361 //check if we already have one buffer for this couple period/nb_buffering
[441]362 for (size_t i = 0; i < pushedDatas.size(); i++) {
363 if (pushedDatas.at(i).period==dataToSend->SendPeriod() && pushedDatas.at(i).nb_buffering==dataToSend->NbBuffering()) {
364 ptr=&pushedDatas.at(i);
[440]365 }
366 }
367 if(ptr==NULL) {
[441]368 Warn("no match in buffers\n");
369 return;
[440]370 }
371
[441]372 dataToSend->CopyDatas(ptr->buf+ptr->actual_size);
373 ptr->actual_size+=dataToSend->SendSize();
374 Printf("nb buffered %i\n",ptr->actual_size);
375 Printf("pushed size %i period %i nb buffering %i\n",dataToSend->SendSize(),dataToSend->SendPeriod(),dataToSend->NbBuffering());
376 if(ptr->actual_size==ptr->final_size) {
377 Printf("ready to send\n");
378 //clean
379 ptr->actual_size=0;
380 }
[440]381}
382
383
[2]384#ifdef __XENO__
[15]385void *ui_com::user_thread(void *arg) {
386 int pipe_fd = -1;
387 string devname;
388 char *buf = NULL;
389 ui_com *caller = (ui_com *)arg;
390 int rv;
391 fd_set set;
392 struct timeval timeout;
[439]393 ssize_t nb_read;
394 int ttl;
[2]395
[15]396 buf = (char *)malloc(NRT_PIPE_SIZE);
397 if (buf == NULL) {
398 caller->Err("malloc error\n");
399 }
[2]400
[15]401 devname = NRT_PIPE_PATH + getFrameworkManager()->ObjectName() + "-" +
402 caller->ObjectName() + "-pipe";
403 while (pipe_fd < 0) {
404 pipe_fd = open(devname.c_str(), O_RDWR);
[133]405 if (pipe_fd < 0 && errno != ENOENT) {
406 char errorMsg[256];
407 caller->Err("open pipe_fd error (%s)\n", strerror_r(errno, errorMsg, sizeof(errorMsg)));
408 }
[15]409 usleep(1000);
410 }
[2]411
[15]412 while (1) {
413 FD_ZERO(&set); // clear the set
414 FD_SET(pipe_fd, &set); // add our file descriptor to the set
[2]415
[15]416 timeout.tv_sec = 0;
417 timeout.tv_usec = SELECT_TIMEOUT_MS * 1000;
[2]418
[15]419 rv = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
[2]420
[15]421 if (rv == -1) {
[439]422 if (caller->is_running == false && UDT::getlasterror().getErrorCode() == CUDTException::ETIMEOUT)
[15]423 break; // timeout
424 if (UDT::getlasterror().getErrorCode() != CUDTException::ETIMEOUT)
425 caller->Err("epoll_wait, %s, code %i\n",
[439]426 UDT::getlasterror().getErrorMessage(), UDT::getlasterror().getErrorCode());
[15]427 } else if (rv == 0) {
428 // printf("timeout\n"); // a timeout occured
429 if (caller->is_running == false)
430 break;
[2]431
[15]432 } else {
[439]433 //read data buf and ttl
434 //RT part has made 2 rt_pipe_write; they should be available in nrt
435 //(select ensure at least one data is ready)
[15]436 nb_read = read(pipe_fd, buf, NRT_PIPE_SIZE);
437 buf[nb_read] = 0;
[439]438 read(pipe_fd, &ttl, sizeof(ttl));
[15]439// printf("envoi\n%s\n",buf);
[2]440
[439]441 caller->SendNRT(buf, nb_read,ttl);
[2]442 }
[15]443 }
[2]444
[15]445 close(pipe_fd);
446 if (buf != NULL)
447 free(buf);
448 pthread_exit(0);
[2]449}
450#endif
451
[15]452int ui_com::compressBuffer(char *in, ssize_t in_size, char **out,
453 ssize_t *out_size, int level) {
454 int ret, flush;
455 unsigned have;
456 z_stream strm;
[2]457
[15]458 /* allocate deflate state */
459 strm.zalloc = Z_NULL;
460 strm.zfree = Z_NULL;
461 strm.opaque = Z_NULL;
462 ret = deflateInit(&strm, level);
463 if (ret != Z_OK)
464 return ret;
[2]465
[15]466 *out = (char *)malloc(COMPRESS_CHUNK);
467 if (!(*out))
468 return Z_BUF_ERROR;
[2]469
[15]470 strm.next_in = (unsigned char *)in;
471 strm.avail_out = COMPRESS_CHUNK;
472 strm.next_out = (unsigned char *)*out;
473 strm.avail_in = in_size;
474 flush = Z_FINISH;
[2]475
[15]476 ret = deflate(&strm, flush); /* no bad return value */
477 if (ret == Z_STREAM_ERROR) {
478 free(*out);
479 return ret;
480 }
[2]481
[15]482 have = COMPRESS_CHUNK - strm.avail_out;
483 *out_size = have;
484 // printf("%i -> %i\n",in_size,have);
485 /* clean up and return */
486 (void)deflateEnd(&strm);
[2]487
[15]488 if (strm.avail_out != 0) {
489 return Z_OK;
490 } else {
491 return Z_STREAM_ERROR;
492 }
[2]493}
494
[15]495int ui_com::uncompressBuffer(unsigned char *in, ssize_t in_size,
496 unsigned char **out, ssize_t *out_size) {
497 int ret;
498 // unsigned have;
499 z_stream strm;
[2]500
[15]501 /* allocate inflate state */
502 strm.zalloc = Z_NULL;
503 strm.zfree = Z_NULL;
504 strm.opaque = Z_NULL;
505 strm.avail_in = 0;
506 strm.next_in = Z_NULL;
507 ret = inflateInit(&strm);
508 if (ret != Z_OK)
509 return ret;
[2]510
[15]511 *out = (unsigned char *)malloc(COMPRESS_CHUNK);
512 if (!(*out))
513 return Z_BUF_ERROR;
[2]514
[15]515 strm.avail_in = in_size;
516 strm.next_in = in;
517 strm.avail_out = COMPRESS_CHUNK;
518 strm.next_out = *out;
[2]519
[15]520 ret = inflate(&strm, Z_NO_FLUSH);
521 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
522 switch (ret) {
523 case Z_NEED_DICT:
524 ret = Z_DATA_ERROR; /* and fall through */
525 case Z_DATA_ERROR:
526 case Z_MEM_ERROR:
[2]527 (void)inflateEnd(&strm);
[15]528 return ret;
529 }
530 // have = COMPRESS_CHUNK - strm.avail_out;
[2]531
[15]532 /* clean up and return */
533 (void)inflateEnd(&strm);
534 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
[2]535}
536
[15]537bool ui_com::ConnectionLost() { return connection_lost; }
[2]538
[15]539void ui_com::AddSendData(const SendData *obj) {
540 send_mutex->GetMutex();
[2]541
[441]542 resumeTimes.push_back(GetTime() + (Time)obj->SendPeriod() * 1000000);
[15]543
544 // on resume en meme temps tout ceux qui ont la meme periode
[441]545 for (size_t i = 0; i < datasToSend.size(); i++) {
546 if (datasToSend.at(i)->SendPeriod() == obj->SendPeriod()) {
547 resumeTimes.at(resumeTimes.size() - 1) = resumeTimes.at(i);
[15]548 break;
[2]549 }
[15]550 }
[2]551
[441]552 datasToSend.push_back(obj);
[2]553
[15]554 send_mutex->ReleaseMutex();
[2]555}
556
[441]557//TODO: check if it is RT compatible
[15]558void ui_com::UpdateSendData(const SendData *obj) {
559 // le mutex est deja pris par l'appellant
560 size_t id, i;
[2]561
[15]562 // on recupere l'id
[441]563 for (i = 0; i < datasToSend.size(); i++) {
564 if (datasToSend.at(i) == obj) {
[15]565 id = i;
566 break;
[2]567 }
[15]568 }
[2]569
[15]570 // on resume en meme temps tout ceux qui ont la meme periode
[441]571 for (i = 0; i < datasToSend.size(); i++) {
[15]572 if (i == id)
573 continue;
[441]574 if (datasToSend.at(i)->IsEnabled() == true && datasToSend.at(i)->SendPeriod() == obj->SendPeriod()) {
575 resumeTimes.at(id) = resumeTimes.at(i);
[15]576 break;
[2]577 }
[15]578 }
[2]579
[15]580 // si aucun match, on planifie l'execution
[441]581 if (i == datasToSend.size())
582 resumeTimes.at(id) = GetTime() + (Time)obj->SendPeriod() * 1000000;
[2]583
[441]584 if (IsSuspended() == true) Resume();
[2]585
[441]586//update all buffers if necessary, discard datas
587 std::vector<PushedData_t>::iterator pushedDatasIterator = pushedDatas.begin();
588 while(pushedDatasIterator != pushedDatas.end()) {
589 size_t bufSize=0;
590 for (size_t i = 0; i < datasToSend.size(); i++) {
591 if (datasToSend.at(i)->IsEnabled() && datasToSend.at(i)->SendPeriod() == (*pushedDatasIterator).period && datasToSend.at(i)->NbBuffering()==(*pushedDatasIterator).nb_buffering) {
592 bufSize+=datasToSend.at(i)->SendSize();
593 }
594 }
595 bufSize*=(*pushedDatasIterator).nb_buffering;
596 if(bufSize!=(*pushedDatasIterator).final_size && bufSize!=0) {
597 Printf("change buf size %i->%i\n",(*pushedDatasIterator).final_size,bufSize);
598 (*pushedDatasIterator).actual_size=0;
599 (*pushedDatasIterator).final_size=bufSize;
600 free((*pushedDatasIterator).buf);
601 (*pushedDatasIterator).buf=(char*)malloc(bufSize);
602 }
603 if(bufSize==0) {
604 Printf("delete buf\n");
605 free((*pushedDatasIterator).buf);
606 pushedDatasIterator = pushedDatas.erase(pushedDatasIterator);
607 } else {
608 ++pushedDatasIterator;
609 }
610 }
611
612 //check if we need a new buffer for this couple period/nb_buffering
613 if(obj->IsEnabled()) {
614 bool match=false;
615 for (size_t i = 0; i < pushedDatas.size(); i++) {
616 if (pushedDatas.at(i).period==obj->SendPeriod() && pushedDatas.at(i).nb_buffering==obj->NbBuffering()) {
617 Printf("match item %i, period %i nb buff %i\n",i,obj->SendPeriod(),obj->NbBuffering());
618 match=true;
619 }
620 }
621 //create new one
622 if(!match) {
623 printf("no match\n");
624 PushedData_t tmp;
625 tmp.period=obj->SendPeriod();
626 tmp.nb_buffering=obj->NbBuffering();
627 tmp.actual_size=0;
628 tmp.final_size=0;
629 for (size_t i = 0; i < datasToSend.size(); i++) {
630 if (datasToSend.at(i)->IsEnabled() && datasToSend.at(i)->SendPeriod() == obj->SendPeriod() && datasToSend.at(i)->NbBuffering()==obj->NbBuffering()) {
631 tmp.final_size+=datasToSend.at(i)->SendSize();
632 }
633 }
634 tmp.final_size*=obj->NbBuffering();
635 printf("final size %i\n",tmp.final_size);
636 tmp.buf=(char*)malloc(tmp.final_size);
637 if(tmp.buf!=NULL) {
638 pushedDatas.push_back(tmp);
639 } else {
640 Warn("could not allocate buffer of size %i\n",tmp.final_size);
641 }
642 }
643 }
644 Printf("nb buf %i\n",pushedDatas.size());
[15]645 return;
[2]646}
647
[439]648//allocate a buffer at startup for the worst case
649//(every data send with same pedriod)
650//TODO: dynamic size depending on what gcs asks?
[15]651void ui_com::UpdateDataToSendSize(void) {
652 send_mutex->GetMutex();
653 send_size = 3; // id(1)+period(2)
[441]654 for (size_t i = 0; i < datasToSend.size(); i++) {
655 if (datasToSend[i] != NULL)
656 send_size += datasToSend[i]->SendSize();
[15]657 }
[2]658
[15]659 // send_buffer=(char*)realloc((void*)send_buffer,send_size*sizeof(char));
660 if (send_buffer != NULL)
661 free(send_buffer);
662 send_buffer = (char *)malloc(send_size * sizeof(char));
663 send_mutex->ReleaseMutex();
[2]664}
665
[15]666void ui_com::RemoveSendData(const SendData *obj) {
667 // printf("remove_data_to_send %i\n",data_to_send.size());
[2]668
[15]669 send_mutex->GetMutex();
670 // on recupere l'id
[441]671 for (size_t i = 0; i < datasToSend.size(); i++) {
672 if (datasToSend.at(i) == obj) {
673 datasToSend.erase(datasToSend.begin() + i);
674 resumeTimes.erase(resumeTimes.begin() + i);
[15]675 // printf("remove_data_to_send %i ok\n",data_to_send.size());
676 break;
[2]677 }
[15]678 }
679 send_mutex->ReleaseMutex();
[2]680
[15]681 return;
[2]682}
683
[15]684void ui_com::Block(void) { send_mutex->GetMutex(); }
[2]685
[15]686void ui_com::UnBlock(void) { send_mutex->ReleaseMutex(); }
Note: See TracBrowser for help on using the repository browser.