DgmQueue.cpp
Go to the documentation of this file.
00001 #include <math.h>
00002 #include <errno.h>
00003 #include <time.h>
00004 
00005 #include "canon_vbc50i/libCanon/DgmQueue.h"
00006 
00007 
00008 DgmQueue::DgmQueue()
00009 {
00010         unsigned int i;
00011         pthread_mutex_init(&Qmtx,NULL);
00012         pthread_cond_init(&Qcnd,NULL);
00013         for (i=0;i<256;i++) interesting[i] = false;
00014         verbose = 0;
00015 }
00016 
00017 DgmQueue::~DgmQueue()
00018 {
00019 }
00020 
00021 void DgmQueue::addInteresting(unsigned int i)
00022 {
00023         lock();
00024         interesting[i] = true;
00025         unlock();
00026 }
00027 
00028 void DgmQueue::remInteresting(unsigned int i)
00029 {
00030         lock();
00031         interesting[i] = false;
00032         Q.erase(i);
00033         unlock();
00034 }
00035 
00036 void DgmQueue::resetInteresting()
00037 {
00038         unsigned int i;
00039         lock();
00040         for (i=0;i<256;i++) interesting[i] = false;
00041         Q.clear();
00042         unlock();
00043 }
00044 
00045 
00046 void DgmQueue::storeDgm(const Datagram & dgm)
00047 {
00048         lock();
00049         if (interesting[dgm.getId()]) {
00050                 //printf("T %08X Storing %d\n",(int)pthread_self(),dgm.getId());
00051                 Q.insert(Queue::value_type(dgm.getId(),dgm));
00052                 //printf("T %08X Signaling\n",(int)pthread_self());
00053                 pthread_cond_signal(&Qcnd);
00054         } else {
00055                 if (verbose > 0)
00056                         printf("T %08X Discarding %d\n",(int)pthread_self(),dgm.getId());
00057                 if (verbose > 1) 
00058                         dgm.print();
00059         }
00060         unlock();
00061 }
00062 
00063 bool DgmQueue::waitDgm(unsigned int id, Datagram & dgm, double maxwait_s)
00064 {
00065         //printf("T %08X Waiting %d\n",(int)pthread_self(),id);
00066         lock();
00067         struct timespec ts;
00068         clock_gettime(CLOCK_REALTIME, &ts);
00069         //printf("Current time is %d.%d",(int)ts.tv_sec,(int)ts.tv_nsec);
00070         double tmax = ts.tv_sec + ts.tv_nsec*1e-9 + maxwait_s;
00071         ts.tv_sec = (long int)floor(tmax);
00072         ts.tv_nsec = (long int)floor((tmax-ts.tv_sec)*1e9);
00073         //printf(", waiting till %d.%d\n",(int)ts.tv_sec,(int)ts.tv_nsec);
00074         int r;
00075         Queue::iterator it = Q.find(id);
00076         if (it == Q.end()) {
00077                 while (it == Q.end()) {
00078                         if (maxwait_s > 0) {
00079 
00080                                 //printf("T %08X TWaiting and unlocking\n",(int)pthread_self());
00081                                 r = pthread_cond_timedwait(&Qcnd,&Qmtx,&ts);
00082                                 //printf("T %08X cond wait exits with %d\n",(int)pthread_self(),r);
00083                                 //print();
00084                                 if (r != 0) {
00085                                         //printf("T %08X Timeout (%d)\n",(int)pthread_self(),r);
00086                                         unlock();
00087                                         return false;
00088                                 }
00089                         } else {
00090                                 //printf("T %08X Waiting and unlocking\n",(int)pthread_self());
00091                                 r = pthread_cond_wait(&Qcnd,&Qmtx);
00092                                 //printf("T %08X cond wait exits with %d\n",(int)pthread_self(),r);
00093                         }
00094                         //printf("Finding\n");
00095                         it = Q.find(id);
00096                         //printf("%s\n",(it==Q.end())?"Nothing":"Something");
00097                 }
00098         } 
00099         //printf("T %08X Received %d\n",(int)pthread_self(),id);
00100         dgm = it->second;
00101         Q.erase(it);
00102         unlock();
00103         return true;
00104 }
00105 
00106 void DgmQueue::print()
00107 {
00108         Queue::iterator it;
00109         printf("### QUEUE ######\n");
00110         for (it=Q.begin();it != Q.end();it++)
00111         {
00112                 printf("[ %d D %d ]\n",it->first,it->second.getId());
00113         }
00114         printf("################\n");
00115 }


canon_vbc50i
Author(s): Cedric Pradalier
autogenerated on Mon Jan 6 2014 11:18:27