Jack2  1.9.10
JackTools.h
00001 /*
00002   Copyright (C) 2006-2008 Grame
00003 
00004   This program is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU Lesser General Public License as published by
00006   the Free Software Foundation; either version 2.1 of the License, or
00007   (at your option) any later version.
00008 
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU Lesser General Public License for more details.
00013 
00014   You should have received a copy of the GNU Lesser General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #ifndef __JackTools__
00021 #define __JackTools__
00022 
00023 #ifdef WIN32
00024 #include <windows.h>
00025 #define DIR_SEPARATOR '\\'
00026 #else
00027 #define DIR_SEPARATOR '/'
00028 #include <sys/stat.h>
00029 #include <sys/types.h>
00030 #include <unistd.h>
00031 #include <dirent.h>
00032 #endif
00033 
00034 #ifdef __APPLE__
00035 #include <sys/syslimits.h>
00036 #endif
00037 
00038 #include "jslist.h"
00039 #include "JackCompilerDeps.h"
00040 #include "JackError.h"
00041 
00042 #include <string>
00043 #include <algorithm>
00044 #include <vector>
00045 #include <iostream>
00046 #include <fstream>
00047 
00048 namespace Jack
00049 {
00050 
00055     struct SERVER_EXPORT JackTools
00056     {
00057         static int GetPID();
00058         static int GetUID();
00059 
00060         static void KillServer();
00061 
00062         static int MkDir(const char* path);
00063         static char* UserDir();
00064         static char* ServerDir(const char* server_name, char* server_dir);
00065         static const char* DefaultServerName();
00066         static void CleanupFiles(const char* server_name);
00067         static int GetTmpdir();
00068         static void RewriteName(const char* name, char* new_name);
00069         static void ThrowJackNetException();
00070 
00071         // For OSX only
00072         static int ComputationMicroSec(int buffer_size)
00073         {
00074             if (buffer_size < 128) {
00075                 return 500;
00076             } else if (buffer_size < 256) {
00077                 return 300;
00078             } else {
00079                 return 100;
00080             }
00081         }
00082     };
00083 
00101     template <class T> class JackGnuPlotMonitor
00102     {
00103         private:
00104             uint32_t fMeasureCnt;
00105             uint32_t fMeasurePoints;
00106             uint32_t fMeasureId;
00107             T* fCurrentMeasure;
00108             T** fMeasureTable;
00109             uint32_t fTablePos;
00110             std::string fName;
00111 
00112         public:
00113             JackGnuPlotMonitor(uint32_t measure_cnt, uint32_t measure_points, std::string name)
00114             {
00115                     jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
00116 
00117                     fMeasureCnt = measure_cnt;
00118                     fMeasurePoints = measure_points;
00119                     fTablePos = 0;
00120                     fName = name;
00121                     fCurrentMeasure = new T[fMeasurePoints];
00122                     fMeasureTable = new T*[fMeasureCnt];
00123                     for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00124                     {
00125                             fMeasureTable[cnt] = new T[fMeasurePoints];
00126                             std::fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
00127                     }
00128             }
00129 
00130             ~JackGnuPlotMonitor()
00131             {
00132                     jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
00133 
00134                     for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00135                             delete[] fMeasureTable[cnt];
00136                     delete[] fMeasureTable;
00137                     delete[] fCurrentMeasure;
00138             }
00139 
00140             T AddNew(T measure_point)
00141             {
00142                     fMeasureId = 0;
00143                     return fCurrentMeasure[fMeasureId++] = measure_point;
00144             }
00145 
00146             uint32_t New()
00147             {
00148                     return fMeasureId = 0;
00149             }
00150 
00151             T Add(T measure_point)
00152             {
00153                     return fCurrentMeasure[fMeasureId++] = measure_point;
00154             }
00155 
00156             uint32_t AddLast(T measure_point)
00157             {
00158                     fCurrentMeasure[fMeasureId] = measure_point;
00159                     fMeasureId = 0;
00160                     return Write();
00161             }
00162 
00163             uint32_t Write()
00164             {
00165                     for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00166                             fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
00167                     if ( ++fTablePos == fMeasureCnt )
00168                             fTablePos = 0;
00169                     return fTablePos;
00170             }
00171 
00172             int Save(std::string name = std::string ( "" ))
00173             {
00174                     std::string filename = ( name.empty() ) ? fName : name;
00175                     filename += ".log";
00176 
00177                     jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
00178 
00179                     std::ofstream file ( filename.c_str() );
00180 
00181                     for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00182                     {
00183                             for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00184                                     file << fMeasureTable[cnt][point] << " \t";
00185                             file << std::endl;
00186                     }
00187 
00188                     file.close();
00189                     return 0;
00190             }
00191 
00192             int SetPlotFile(std::string* options_list, uint32_t options_number,
00193                             std::string* field_names, uint32_t field_number,
00194                             std::string name = std::string ( "" ))
00195             {
00196                     std::string title = ( name.empty() ) ? fName : name;
00197                     std::string plot_filename = title + ".plt";
00198                     std::string data_filename = title + ".log";
00199 
00200                     std::ofstream file ( plot_filename.c_str() );
00201 
00202                     file << "set multiplot" << std::endl;
00203                     file << "set grid" << std::endl;
00204                     file << "set title \"" << title << "\"" << std::endl;
00205 
00206                     for ( uint32_t i = 0; i < options_number; i++ )
00207                             file << options_list[i] << std::endl;
00208 
00209                     file << "plot ";
00210                     for ( uint32_t row = 1; row <= field_number; row++ )
00211                     {
00212                             file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
00213                             file << ( ( row < field_number ) ? ", " : "\n" );
00214                     }
00215 
00216                     jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
00217 
00218                     file.close();
00219                     return 0;
00220             }
00221 
00222     };
00223 
00224     void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
00225     void PrintLoadError(const char* so_name);
00226 
00227 }
00228 
00229 #endif