Menu

Statistics.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2009-2010 Philipp Werner <philipp.werner@st.ovgu.de>
00004  * All rights reserved.
00005  *
00006  *    Redistribution and use in source and binary forms, with or without
00007  *    modification, are permitted provided that the following conditions
00008  *    are met:
00009  *
00010  *    * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *
00013  *    * Redistributions in binary form must reproduce the above copyright
00014  *      notice, this list of conditions and the following disclaimer in
00015  *      the documentation and/or other materials provided with the
00016  *      distribution.
00017  *
00018  *    * Neither the name of the copyright holders nor the names of
00019  *      contributors may be used to endorse or promote products derived
00020  *      from this software without specific prior written permission.
00021  *
00022  *
00023  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00025  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00026  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *
00036  * $Id$
00037  *
00038  ******************************************************************************/
00039 
00040 #ifndef __STATISTICS_H_2F8C97C9EC89D7__
00041 #define __STATISTICS_H_2F8C97C9EC89D7__
00042 
00043 
00044 #include <string.h>
00045 
00046 #ifndef __AVR__
00047 #include "boost/thread/mutex.hpp"
00048 #endif
00049 
00050 
00051 namespace famouso {
00052     namespace mw {
00053         namespace afp {
00054             namespace defrag {
00055 
00056 
00071                 struct FragmentStats {
00073                     unsigned int expected;
00074 
00076                     unsigned int currently_expected;
00077 
00078 
00079 
00081                     unsigned int received;
00082 
00084                     unsigned int incorrect_header;
00085 
00087                     unsigned int outdated;
00088 
00090                     unsigned int used;
00091 
00093                     unsigned int duplicates;
00094 
00096                     FragmentStats() {
00097                         reset();
00098                     }
00099 
00101                     void reset() {
00102                         memset(this, 0, sizeof(*this));
00103                     }
00104                 };
00105 
00106 
00114                 struct EventStats {
00115 
00117                     unsigned int complete;
00118 
00120                     unsigned int incomplete;
00121 
00123                     EventStats() {
00124                         reset();
00125                     }
00126 
00128                     void reset() {
00129                         memset(this, 0, sizeof(*this));
00130                     }
00131                 };
00132 
00133 
00134 
00139                 template <class Tag>
00140                 class Statistics {
00141                         // TODO: mutex als policy
00142 
00143                         static FragmentStats & fragment() {
00144                             static FragmentStats s;
00145                             return s;
00146                         }
00147 
00148                         static EventStats & event() {
00149                             static EventStats s;
00150                             return s;
00151                         }
00152 
00153 #ifndef __AVR__
00154                         static boost::mutex & mutex() {
00155                             static boost::mutex m;
00156                             return m;
00157                         }
00158 #endif
00159 
00160                         static void inc(unsigned int & val) {
00161 #ifndef __AVR__
00162                             mutex().lock();
00163                             val++;
00164                             mutex().unlock();
00165 #else
00166                             val++;
00167 #endif
00168                         }
00169 
00170                         static void add(unsigned int & val, unsigned int to_add) {
00171 #ifndef __AVR__
00172                             mutex().lock();
00173                             val += to_add;
00174                             mutex().unlock();
00175 #else
00176                             val += to_add;
00177 #endif
00178                         }
00179 
00180                         static void sub(unsigned int & val, unsigned int to_sub) {
00181 #ifndef __AVR__
00182                             mutex().lock();
00183                             val -= to_sub;
00184                             mutex().unlock();
00185 #else
00186                             val -= to_sub;
00187 #endif
00188                         }
00189 
00190                     public:
00191 
00193                         static void fragment_received() {
00194                             inc(fragment().received);
00195                         }
00196 
00198                         static void fragment_incorrect_header() {
00199                             inc(fragment().incorrect_header);
00200                         }
00201 
00203                         static void fragment_outdated() {
00204                             inc(fragment().outdated);
00205                         }
00206 
00208                         static void fragment_duplicate() {
00209                             inc(fragment().duplicates);
00210                         }
00211 
00213                         static void fragment_used() {
00214                             inc(fragment().used);
00215                         }
00216 
00218                         static void fragments_expected(unsigned int n) {
00219                             add(fragment().expected, n);
00220                         }
00221 
00223                         static void fragments_currently_expected_add(unsigned int n) {
00224                             add(fragment().currently_expected, n);
00225                         }
00226 
00228                         static void fragments_currently_expected_sub(unsigned int n) {
00229                             sub(fragment().currently_expected, n);
00230                         }
00231 
00232 
00234                         static void event_complete()   {
00235                             inc(event().complete);
00236                         }
00237 
00239                         static void event_incomplete() {
00240                             inc(event().incomplete);
00241                         }
00242 
00243 
00245                         static void get_fragment_stats(FragmentStats & fs) {
00246 #ifndef __AVR__
00247                             mutex().lock();
00248                             memcpy(&fs, &fragment(), sizeof(fs));
00249                             mutex().unlock();
00250 #else
00251                             memcpy(&fs, &fragment(), sizeof(fs));
00252 #endif
00253                         }
00254 
00256                         static void get_event_stats(EventStats & es) {
00257 #ifndef __AVR__
00258                             mutex().lock();
00259                             memcpy(&es, &event(), sizeof(es));
00260                             mutex().unlock();
00261 #else
00262                             memcpy(&es, &event(), sizeof(es));
00263 #endif
00264                         }
00265                 };
00266 
00267 
00268 
00273                 class NoStatistics {
00274                     public:
00275 
00277                         static void fragment_received() {}
00278 
00280                         static void fragment_incorrect_header() {}
00281 
00283                         static void fragment_outdated() {}
00284 
00286                         static void fragment_duplicate() {}
00287 
00289                         static void fragment_used() {}
00290 
00292                         static void fragments_expected(unsigned int n) {}
00293 
00295                         static void fragments_currently_expected_add(unsigned int n) {}
00296 
00298                         static void fragments_currently_expected_sub(unsigned int n) {}
00299 
00300 
00302                         static void event_complete()   {}
00303 
00305                         static void event_incomplete() {}
00306 
00307 
00309                         static void get_fragment_stats(FragmentStats & fs) {
00310                         }
00311 
00313                         static void get_event_stats(EventStats & es) {
00314                         }
00315                 };
00316 
00317 
00318 
00319             } // namespace defrag
00320         } // namespace afp
00321     } // namespace mw
00322 } // namespace famouso
00323 
00324 
00325 #endif // __STATISTICS_H_2F8C97C9EC89D7__
00326