[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 |
|
---|
| 37 | using std::string;
|
---|
| 38 | using namespace flair::core;
|
---|
| 39 | using namespace flair::gui;
|
---|
| 40 |
|
---|
[15] | 41 | ui_com::ui_com(const Object *parent, UDTSOCKET sock)
|
---|
| 42 | : Thread(parent, "send", 2) {
|
---|
| 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");
|
---|
[2] | 107 |
|
---|
[15] | 108 | bool blocking = true;
|
---|
| 109 | if (UDT::setsockopt(socket_fd, 0, UDT_SNDSYN, &blocking, sizeof(bool)) != 0)
|
---|
| 110 | Err("UDT::setsockopt error (UDT_SNDSYN)\n");
|
---|
[2] | 111 |
|
---|
[15] | 112 | if (UDT::setsockopt(socket_fd, 0, UDT_RCVSYN, &blocking, sizeof(bool)) != 0)
|
---|
| 113 | Err("UDT::setsockopt error (UDT_RCVSYN)\n");
|
---|
| 114 | //#endif //__XENO__
|
---|
[2] | 115 |
|
---|
[15] | 116 | Start();
|
---|
[2] | 117 | }
|
---|
| 118 |
|
---|
[15] | 119 | ui_com::~ui_com() {
|
---|
| 120 | // printf("destruction ui_com\n");
|
---|
[2] | 121 |
|
---|
| 122 | #ifdef __XENO__
|
---|
[15] | 123 | is_running = false;
|
---|
[2] | 124 |
|
---|
[15] | 125 | pthread_join(thread, NULL);
|
---|
[2] | 126 |
|
---|
[15] | 127 | int status = rt_pipe_delete(&pipe);
|
---|
[133] | 128 | if (status != 0) {
|
---|
| 129 | char errorMsg[256];
|
---|
| 130 | Err("rt_pipe_delete error (%s)\n", strerror_r(-status, errorMsg, sizeof(errorMsg)));
|
---|
| 131 | }
|
---|
[2] | 132 | #endif
|
---|
| 133 |
|
---|
[15] | 134 | SafeStop();
|
---|
[2] | 135 |
|
---|
[15] | 136 | if (IsSuspended() == true)
|
---|
| 137 | Resume();
|
---|
[2] | 138 |
|
---|
[15] | 139 | Join();
|
---|
[2] | 140 |
|
---|
[15] | 141 | if (send_buffer != NULL)
|
---|
| 142 | free(send_buffer);
|
---|
| 143 | send_buffer = NULL;
|
---|
[2] | 144 |
|
---|
[15] | 145 | // printf("destruction ui_com ok\n");
|
---|
[2] | 146 | }
|
---|
| 147 |
|
---|
[15] | 148 | void ui_com::Send(char *buf, ssize_t size) {
|
---|
| 149 | if (connection_lost == true)
|
---|
| 150 | return;
|
---|
[2] | 151 |
|
---|
[15] | 152 | char *tosend = buf;
|
---|
| 153 | ssize_t nb_write;
|
---|
[2] | 154 |
|
---|
[15] | 155 | if (buf[0] == XML_HEADER) {
|
---|
| 156 | // cut xml header
|
---|
| 157 | tosend = strstr(buf, "<root");
|
---|
| 158 | size -= tosend - buf;
|
---|
| 159 | }
|
---|
[2] | 160 |
|
---|
| 161 | #ifdef __XENO__
|
---|
[15] | 162 | nb_write = rt_pipe_write(&pipe, tosend, size, P_NORMAL);
|
---|
[2] | 163 |
|
---|
[15] | 164 | if (nb_write < 0) {
|
---|
[133] | 165 | char errorMsg[256];
|
---|
| 166 | Err("rt_pipe_write error (%s)\n", strerror_r(-nb_write, errorMsg, sizeof(errorMsg)));
|
---|
[15] | 167 | } else if (nb_write != size) {
|
---|
| 168 | Err("rt_pipe_write error %i/%i\n", nb_write, size);
|
---|
| 169 | }
|
---|
[2] | 170 | #else //__XENO__
|
---|
| 171 |
|
---|
| 172 | #ifdef COMPRESS_FRAMES
|
---|
[15] | 173 | bool sendItCompressed = false;
|
---|
| 174 | if (buf[0] == XML_HEADER) {
|
---|
| 175 | sendItCompressed = true;
|
---|
| 176 | }
|
---|
[2] | 177 |
|
---|
[15] | 178 | if (sendItCompressed) {
|
---|
| 179 | char *out;
|
---|
| 180 | ssize_t out_size;
|
---|
| 181 | if (compressBuffer(tosend, size, &out, &out_size, 9) == Z_OK) {
|
---|
| 182 | size = out_size;
|
---|
| 183 | nb_write = UDT::sendmsg(socket_fd, out, size, -1, true);
|
---|
| 184 | free(out);
|
---|
| 185 | } else {
|
---|
| 186 | Warn("Compress error, sending it uncompressed\n");
|
---|
| 187 | sendItCompressed = false;
|
---|
[2] | 188 | }
|
---|
[15] | 189 | }
|
---|
[2] | 190 |
|
---|
[15] | 191 | if (!sendItCompressed) {
|
---|
| 192 | nb_write = UDT::sendmsg(socket_fd, tosend, size, -1, true);
|
---|
| 193 | }
|
---|
[2] | 194 |
|
---|
[15] | 195 | #else // COMPRESS_FRAMES
|
---|
| 196 | nb_write = UDT::sendmsg(socket_fd, tosend, size, -1, true);
|
---|
| 197 | #endif // COMPRESS_FRAMES
|
---|
| 198 | // Printf("write %i %i\n",nb_write,size);
|
---|
| 199 | if (nb_write < 0) {
|
---|
| 200 | Err("UDT::sendmsg error (%s)\n", UDT::getlasterror().getErrorMessage());
|
---|
| 201 | if (UDT::getlasterror().getErrorCode() == CUDTException::ECONNLOST ||
|
---|
| 202 | UDT::getlasterror().getErrorCode() == CUDTException::EINVSOCK) {
|
---|
| 203 | connection_lost = true;
|
---|
[2] | 204 | }
|
---|
[15] | 205 | } else if (nb_write != size) {
|
---|
| 206 | Err("%s, code %i (%ld/%ld)\n", UDT::getlasterror().getErrorMessage(),
|
---|
| 207 | UDT::getlasterror().getErrorCode(), nb_write, size);
|
---|
| 208 | }
|
---|
[2] | 209 | #endif //__XENO__
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[15] | 212 | ssize_t ui_com::Receive(char *buf, ssize_t buf_size) {
|
---|
| 213 | ssize_t bytesRead = UDT::recvmsg(socket_fd, buf, buf_size);
|
---|
[2] | 214 |
|
---|
[15] | 215 | if (bytesRead < 0) {
|
---|
| 216 | if (UDT::getlasterror().getErrorCode() == CUDTException::ECONNLOST) {
|
---|
| 217 | Err("UDT::recvmsg error (%s)\n", UDT::getlasterror().getErrorMessage());
|
---|
| 218 | connection_lost = true;
|
---|
[2] | 219 | }
|
---|
[15] | 220 | }
|
---|
[2] | 221 |
|
---|
[15] | 222 | return bytesRead;
|
---|
[2] | 223 | }
|
---|
| 224 |
|
---|
[15] | 225 | void ui_com::Run(void) {
|
---|
| 226 | // check endianness
|
---|
| 227 | char header;
|
---|
| 228 | if (IsBigEndian()) {
|
---|
| 229 | header = DATAS_BIG_ENDIAN;
|
---|
| 230 | Printf("System is big endian\n");
|
---|
| 231 | } else {
|
---|
| 232 | header = DATAS_LITTLE_ENDIAN;
|
---|
| 233 | Printf("System is little endian\n");
|
---|
| 234 | }
|
---|
[2] | 235 |
|
---|
| 236 | #ifdef __XENO__
|
---|
[15] | 237 | WarnUponSwitches(true);
|
---|
| 238 | printf("\n"); // a revoir pourquoi??
|
---|
| 239 | // sans ce printf, dans le simu_roll, le suspend ne fonctionne pas...
|
---|
[2] | 240 | #endif
|
---|
[15] | 241 | // on attend d'avoir des choses à faire
|
---|
| 242 | Suspend();
|
---|
[2] | 243 |
|
---|
[15] | 244 | while (!ToBeStopped()) {
|
---|
| 245 | size_t resume_id;
|
---|
| 246 | Time min = 0xffffffffffffffffULL;
|
---|
[2] | 247 |
|
---|
[15] | 248 | // on recpuere l'id de la prochaine execution
|
---|
| 249 | send_mutex->GetMutex();
|
---|
| 250 | resume_id = resume_time.size();
|
---|
| 251 | for (size_t i = 0; i < resume_time.size(); i++) {
|
---|
| 252 | if (resume_time.at(i) < min && data_to_send.at(i)->IsEnabled() == true) {
|
---|
| 253 | min = resume_time.at(i);
|
---|
| 254 | resume_id = i;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
[2] | 257 |
|
---|
[15] | 258 | // attente
|
---|
| 259 | if (resume_id < resume_time.size()) {
|
---|
| 260 | Time time = resume_time.at(resume_id);
|
---|
| 261 | uint16_t resume_period = data_to_send.at(resume_id)->SendPeriod();
|
---|
| 262 | send_mutex->ReleaseMutex();
|
---|
| 263 | // on dort jusqu'a la prochaine execution
|
---|
| 264 | SleepUntil(time);
|
---|
[2] | 265 |
|
---|
[15] | 266 | // envoi des donnees
|
---|
| 267 | int offset = 3;
|
---|
| 268 | send_buffer[0] = header;
|
---|
| 269 | send_mutex->GetMutex();
|
---|
[2] | 270 |
|
---|
[15] | 271 | for (size_t i = 0; i < data_to_send.size(); i++) {
|
---|
| 272 | if (data_to_send.at(i)->SendPeriod() == resume_period &&
|
---|
| 273 | data_to_send.at(i)->IsEnabled() == true) {
|
---|
| 274 | data_to_send.at(i)->CopyDatas(send_buffer + offset);
|
---|
| 275 | offset += data_to_send.at(i)->SendSize();
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | if (offset != 3) {
|
---|
| 279 | memcpy(&send_buffer[1], &resume_period, sizeof(uint16_t));
|
---|
| 280 | // printf("send %i %i %i %x
|
---|
| 281 | // %x\n",resume_period,offset,sizeof(uint16_t),send_buffer,&send_buffer[1]);
|
---|
| 282 | // for(int i=0;i<offset;i++) printf("%x ",send_buffer[i]);
|
---|
| 283 | // printf("\n");
|
---|
| 284 | Send(send_buffer, offset);
|
---|
| 285 | }
|
---|
[2] | 286 |
|
---|
[15] | 287 | // on planifie la prochaine execution
|
---|
| 288 | for (size_t i = 0; i < data_to_send.size(); i++) {
|
---|
| 289 | if (data_to_send.at(i)->SendPeriod() == resume_period) {
|
---|
| 290 | resume_time.at(i) += data_to_send.at(i)->SendPeriod() * 1000000;
|
---|
[2] | 291 | }
|
---|
[15] | 292 | }
|
---|
| 293 | send_mutex->ReleaseMutex();
|
---|
| 294 | // Printf("%i %lld\n",resume_period,GetTime()/1000000);
|
---|
| 295 | } else {
|
---|
| 296 | send_mutex->ReleaseMutex();
|
---|
| 297 | // rien a faire, suspend
|
---|
| 298 | // Printf("rien a faire suspend\n");
|
---|
| 299 | Suspend();
|
---|
| 300 | // Printf("wake\n");
|
---|
| 301 | // on planifie la prochaine execution
|
---|
| 302 | Time time = GetTime();
|
---|
| 303 | send_mutex->GetMutex();
|
---|
| 304 | for (size_t i = 0; i < data_to_send.size(); i++) {
|
---|
| 305 | resume_time.at(i) =
|
---|
| 306 | time + (Time)data_to_send.at(i)->SendPeriod() * 1000000;
|
---|
| 307 | }
|
---|
| 308 | send_mutex->ReleaseMutex();
|
---|
[2] | 309 | }
|
---|
[15] | 310 | }
|
---|
[2] | 311 | }
|
---|
| 312 | #ifdef __XENO__
|
---|
[15] | 313 | void *ui_com::user_thread(void *arg) {
|
---|
| 314 | int pipe_fd = -1;
|
---|
| 315 | string devname;
|
---|
| 316 | char *buf = NULL;
|
---|
| 317 | ui_com *caller = (ui_com *)arg;
|
---|
| 318 | int rv;
|
---|
| 319 | fd_set set;
|
---|
| 320 | struct timeval timeout;
|
---|
| 321 | ssize_t nb_read, nb_write;
|
---|
[2] | 322 |
|
---|
[15] | 323 | buf = (char *)malloc(NRT_PIPE_SIZE);
|
---|
| 324 | if (buf == NULL) {
|
---|
| 325 | caller->Err("malloc error\n");
|
---|
| 326 | }
|
---|
[2] | 327 |
|
---|
[15] | 328 | devname = NRT_PIPE_PATH + getFrameworkManager()->ObjectName() + "-" +
|
---|
| 329 | caller->ObjectName() + "-pipe";
|
---|
| 330 | while (pipe_fd < 0) {
|
---|
| 331 | pipe_fd = open(devname.c_str(), O_RDWR);
|
---|
[133] | 332 | if (pipe_fd < 0 && errno != ENOENT) {
|
---|
| 333 | char errorMsg[256];
|
---|
| 334 | caller->Err("open pipe_fd error (%s)\n", strerror_r(errno, errorMsg, sizeof(errorMsg)));
|
---|
| 335 | }
|
---|
[15] | 336 | usleep(1000);
|
---|
| 337 | }
|
---|
[2] | 338 |
|
---|
[15] | 339 | while (1) {
|
---|
| 340 | FD_ZERO(&set); // clear the set
|
---|
| 341 | FD_SET(pipe_fd, &set); // add our file descriptor to the set
|
---|
[2] | 342 |
|
---|
[15] | 343 | timeout.tv_sec = 0;
|
---|
| 344 | timeout.tv_usec = SELECT_TIMEOUT_MS * 1000;
|
---|
[2] | 345 |
|
---|
[15] | 346 | rv = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
|
---|
[2] | 347 |
|
---|
[15] | 348 | if (rv == -1) {
|
---|
| 349 | if (caller->is_running == false &&
|
---|
| 350 | UDT::getlasterror().getErrorCode() == CUDTException::ETIMEOUT)
|
---|
| 351 | break; // timeout
|
---|
| 352 | if (UDT::getlasterror().getErrorCode() != CUDTException::ETIMEOUT)
|
---|
| 353 | caller->Err("epoll_wait, %s, code %i\n",
|
---|
| 354 | UDT::getlasterror().getErrorMessage(),
|
---|
| 355 | UDT::getlasterror().getErrorCode());
|
---|
| 356 | } else if (rv == 0) {
|
---|
| 357 | // printf("timeout\n"); // a timeout occured
|
---|
| 358 | if (caller->is_running == false)
|
---|
| 359 | break;
|
---|
[2] | 360 |
|
---|
[15] | 361 | } else {
|
---|
| 362 | nb_read = read(pipe_fd, buf, NRT_PIPE_SIZE);
|
---|
| 363 | buf[nb_read] = 0;
|
---|
| 364 | // printf("envoi\n%s\n",buf);
|
---|
[2] | 365 |
|
---|
| 366 | #ifdef COMPRESS_FRAMES
|
---|
[15] | 367 | char *out;
|
---|
| 368 | ssize_t out_size;
|
---|
[2] | 369 |
|
---|
[15] | 370 | if (buf[0] == XML_HEADER) {
|
---|
| 371 | if (compressBuffer(buf, nb_read, &out, &out_size, 9) == Z_OK) {
|
---|
| 372 | nb_read = out_size;
|
---|
| 373 | nb_write = UDT::sendmsg(caller->socket_fd, out, out_size, -1, true);
|
---|
| 374 | free(out);
|
---|
| 375 | } else {
|
---|
| 376 | caller->Warn("Compress error, sending it uncompressed\n");
|
---|
| 377 | nb_write = UDT::sendmsg(caller->socket_fd, buf, nb_read, -1, true);
|
---|
| 378 | }
|
---|
| 379 | } else {
|
---|
| 380 | nb_write = UDT::sendmsg(caller->socket_fd, buf, nb_read, -1, true);
|
---|
| 381 | }
|
---|
[2] | 382 | #else
|
---|
[15] | 383 | nb_write = UDT::sendmsg(caller->socket_fd, buf, nb_read, -1, true);
|
---|
[2] | 384 | #endif
|
---|
| 385 |
|
---|
[15] | 386 | if (nb_write < 0) {
|
---|
| 387 | caller->Err("UDT::sendmsg error (%s)\n",
|
---|
| 388 | UDT::getlasterror().getErrorMessage());
|
---|
| 389 | if (UDT::getlasterror().getErrorCode() == CUDTException::ECONNLOST ||
|
---|
| 390 | UDT::getlasterror().getErrorCode() == CUDTException::EINVSOCK) {
|
---|
| 391 | caller->connection_lost = true;
|
---|
[2] | 392 | }
|
---|
[15] | 393 | } else if (nb_write != nb_read) {
|
---|
| 394 | caller->Err("UDT::sendmsg error %i/%i\n", nb_write, nb_read);
|
---|
| 395 | }
|
---|
[2] | 396 | }
|
---|
[15] | 397 | }
|
---|
[2] | 398 |
|
---|
[15] | 399 | close(pipe_fd);
|
---|
| 400 | if (buf != NULL)
|
---|
| 401 | free(buf);
|
---|
| 402 | pthread_exit(0);
|
---|
[2] | 403 | }
|
---|
| 404 | #endif
|
---|
| 405 |
|
---|
[15] | 406 | int ui_com::compressBuffer(char *in, ssize_t in_size, char **out,
|
---|
| 407 | ssize_t *out_size, int level) {
|
---|
| 408 | int ret, flush;
|
---|
| 409 | unsigned have;
|
---|
| 410 | z_stream strm;
|
---|
[2] | 411 |
|
---|
[15] | 412 | /* allocate deflate state */
|
---|
| 413 | strm.zalloc = Z_NULL;
|
---|
| 414 | strm.zfree = Z_NULL;
|
---|
| 415 | strm.opaque = Z_NULL;
|
---|
| 416 | ret = deflateInit(&strm, level);
|
---|
| 417 | if (ret != Z_OK)
|
---|
| 418 | return ret;
|
---|
[2] | 419 |
|
---|
[15] | 420 | *out = (char *)malloc(COMPRESS_CHUNK);
|
---|
| 421 | if (!(*out))
|
---|
| 422 | return Z_BUF_ERROR;
|
---|
[2] | 423 |
|
---|
[15] | 424 | strm.next_in = (unsigned char *)in;
|
---|
| 425 | strm.avail_out = COMPRESS_CHUNK;
|
---|
| 426 | strm.next_out = (unsigned char *)*out;
|
---|
| 427 | strm.avail_in = in_size;
|
---|
| 428 | flush = Z_FINISH;
|
---|
[2] | 429 |
|
---|
[15] | 430 | ret = deflate(&strm, flush); /* no bad return value */
|
---|
| 431 | if (ret == Z_STREAM_ERROR) {
|
---|
| 432 | free(*out);
|
---|
| 433 | return ret;
|
---|
| 434 | }
|
---|
[2] | 435 |
|
---|
[15] | 436 | have = COMPRESS_CHUNK - strm.avail_out;
|
---|
| 437 | *out_size = have;
|
---|
| 438 | // printf("%i -> %i\n",in_size,have);
|
---|
| 439 | /* clean up and return */
|
---|
| 440 | (void)deflateEnd(&strm);
|
---|
[2] | 441 |
|
---|
[15] | 442 | if (strm.avail_out != 0) {
|
---|
| 443 | return Z_OK;
|
---|
| 444 | } else {
|
---|
| 445 | return Z_STREAM_ERROR;
|
---|
| 446 | }
|
---|
[2] | 447 | }
|
---|
| 448 |
|
---|
[15] | 449 | int ui_com::uncompressBuffer(unsigned char *in, ssize_t in_size,
|
---|
| 450 | unsigned char **out, ssize_t *out_size) {
|
---|
| 451 | int ret;
|
---|
| 452 | // unsigned have;
|
---|
| 453 | z_stream strm;
|
---|
[2] | 454 |
|
---|
[15] | 455 | /* allocate inflate state */
|
---|
| 456 | strm.zalloc = Z_NULL;
|
---|
| 457 | strm.zfree = Z_NULL;
|
---|
| 458 | strm.opaque = Z_NULL;
|
---|
| 459 | strm.avail_in = 0;
|
---|
| 460 | strm.next_in = Z_NULL;
|
---|
| 461 | ret = inflateInit(&strm);
|
---|
| 462 | if (ret != Z_OK)
|
---|
| 463 | return ret;
|
---|
[2] | 464 |
|
---|
[15] | 465 | *out = (unsigned char *)malloc(COMPRESS_CHUNK);
|
---|
| 466 | if (!(*out))
|
---|
| 467 | return Z_BUF_ERROR;
|
---|
[2] | 468 |
|
---|
[15] | 469 | strm.avail_in = in_size;
|
---|
| 470 | strm.next_in = in;
|
---|
| 471 | strm.avail_out = COMPRESS_CHUNK;
|
---|
| 472 | strm.next_out = *out;
|
---|
[2] | 473 |
|
---|
[15] | 474 | ret = inflate(&strm, Z_NO_FLUSH);
|
---|
| 475 | assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
---|
| 476 | switch (ret) {
|
---|
| 477 | case Z_NEED_DICT:
|
---|
| 478 | ret = Z_DATA_ERROR; /* and fall through */
|
---|
| 479 | case Z_DATA_ERROR:
|
---|
| 480 | case Z_MEM_ERROR:
|
---|
[2] | 481 | (void)inflateEnd(&strm);
|
---|
[15] | 482 | return ret;
|
---|
| 483 | }
|
---|
| 484 | // have = COMPRESS_CHUNK - strm.avail_out;
|
---|
[2] | 485 |
|
---|
[15] | 486 | /* clean up and return */
|
---|
| 487 | (void)inflateEnd(&strm);
|
---|
| 488 | return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
|
---|
[2] | 489 | }
|
---|
| 490 |
|
---|
[15] | 491 | bool ui_com::ConnectionLost() { return connection_lost; }
|
---|
[2] | 492 |
|
---|
[15] | 493 | void ui_com::AddSendData(const SendData *obj) {
|
---|
| 494 | send_mutex->GetMutex();
|
---|
[2] | 495 |
|
---|
[15] | 496 | resume_time.push_back(GetTime() + (Time)obj->SendPeriod() * 1000000);
|
---|
| 497 |
|
---|
| 498 | // on resume en meme temps tout ceux qui ont la meme periode
|
---|
| 499 | for (size_t i = 0; i < data_to_send.size(); i++) {
|
---|
| 500 | if (data_to_send.at(i)->SendPeriod() == obj->SendPeriod()) {
|
---|
| 501 | resume_time.at(resume_time.size() - 1) = resume_time.at(i);
|
---|
| 502 | break;
|
---|
[2] | 503 | }
|
---|
[15] | 504 | }
|
---|
[2] | 505 |
|
---|
[15] | 506 | data_to_send.push_back(obj);
|
---|
[2] | 507 |
|
---|
[15] | 508 | send_mutex->ReleaseMutex();
|
---|
[2] | 509 | }
|
---|
| 510 |
|
---|
[15] | 511 | void ui_com::UpdateSendData(const SendData *obj) {
|
---|
| 512 | // le mutex est deja pris par l'appellant
|
---|
| 513 | size_t id, i;
|
---|
[2] | 514 |
|
---|
[15] | 515 | // on recupere l'id
|
---|
| 516 | for (i = 0; i < data_to_send.size(); i++) {
|
---|
| 517 | if (data_to_send.at(i) == obj) {
|
---|
| 518 | id = i;
|
---|
| 519 | break;
|
---|
[2] | 520 | }
|
---|
[15] | 521 | }
|
---|
[2] | 522 |
|
---|
[15] | 523 | // on resume en meme temps tout ceux qui ont la meme periode
|
---|
| 524 | for (i = 0; i < data_to_send.size(); i++) {
|
---|
| 525 | if (i == id)
|
---|
| 526 | continue;
|
---|
| 527 | if (data_to_send.at(i)->IsEnabled() == true &&
|
---|
| 528 | data_to_send.at(i)->SendPeriod() == obj->SendPeriod()) {
|
---|
| 529 | resume_time.at(id) = resume_time.at(i);
|
---|
| 530 | break;
|
---|
[2] | 531 | }
|
---|
[15] | 532 | }
|
---|
[2] | 533 |
|
---|
[15] | 534 | // si aucun match, on planifie l'execution
|
---|
| 535 | if (i == data_to_send.size())
|
---|
| 536 | resume_time.at(id) = GetTime() + (Time)obj->SendPeriod() * 1000000;
|
---|
[2] | 537 |
|
---|
[15] | 538 | if (IsSuspended() == true)
|
---|
| 539 | Resume();
|
---|
[2] | 540 |
|
---|
[15] | 541 | return;
|
---|
[2] | 542 | }
|
---|
| 543 |
|
---|
[15] | 544 | void ui_com::UpdateDataToSendSize(void) {
|
---|
| 545 | send_mutex->GetMutex();
|
---|
| 546 | send_size = 3; // id(1)+period(2)
|
---|
| 547 | for (size_t i = 0; i < data_to_send.size(); i++) {
|
---|
| 548 | if (data_to_send[i] != NULL)
|
---|
| 549 | send_size += data_to_send[i]->SendSize();
|
---|
| 550 | }
|
---|
[2] | 551 |
|
---|
[15] | 552 | // send_buffer=(char*)realloc((void*)send_buffer,send_size*sizeof(char));
|
---|
| 553 | if (send_buffer != NULL)
|
---|
| 554 | free(send_buffer);
|
---|
| 555 | send_buffer = (char *)malloc(send_size * sizeof(char));
|
---|
| 556 | send_mutex->ReleaseMutex();
|
---|
[2] | 557 | }
|
---|
| 558 |
|
---|
[15] | 559 | void ui_com::RemoveSendData(const SendData *obj) {
|
---|
| 560 | // printf("remove_data_to_send %i\n",data_to_send.size());
|
---|
[2] | 561 |
|
---|
[15] | 562 | send_mutex->GetMutex();
|
---|
| 563 | // on recupere l'id
|
---|
| 564 | for (size_t i = 0; i < data_to_send.size(); i++) {
|
---|
| 565 | if (data_to_send.at(i) == obj) {
|
---|
| 566 | data_to_send.erase(data_to_send.begin() + i);
|
---|
| 567 | resume_time.erase(resume_time.begin() + i);
|
---|
| 568 | // printf("remove_data_to_send %i ok\n",data_to_send.size());
|
---|
| 569 | break;
|
---|
[2] | 570 | }
|
---|
[15] | 571 | }
|
---|
| 572 | send_mutex->ReleaseMutex();
|
---|
[2] | 573 |
|
---|
[15] | 574 | return;
|
---|
[2] | 575 | }
|
---|
| 576 |
|
---|
[15] | 577 | void ui_com::Block(void) { send_mutex->GetMutex(); }
|
---|
[2] | 578 |
|
---|
[15] | 579 | void ui_com::UnBlock(void) { send_mutex->ReleaseMutex(); }
|
---|