Jack2  1.9.8
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 "driver_interface.h"
00040 #include "JackCompilerDeps.h"
00041 #include "JackError.h"
00042 #include "JackException.h"
00043 
00044 #include <string>
00045 #include <algorithm>
00046 #include <vector>
00047 #include <iostream>
00048 #include <fstream>
00049 
00050 namespace Jack
00051 {
00052 
00057     struct SERVER_EXPORT JackTools
00058     {
00059         static int GetPID();
00060         static int GetUID();
00061 
00062         static void KillServer();
00063 
00064         static int MkDir(const char* path);
00065         static char* UserDir();
00066         static char* ServerDir(const char* server_name, char* server_dir);
00067         static const char* DefaultServerName();
00068         static void CleanupFiles(const char* server_name);
00069         static int GetTmpdir();
00070         static void RewriteName(const char* name, char* new_name);
00071         static void ThrowJackNetException();
00072 
00073         // For OSX only
00074         static int ComputationMicroSec(int buffer_size)
00075         {
00076             if (buffer_size < 128) {
00077                 return 500;
00078             } else if (buffer_size < 256) {
00079                 return 300;
00080             } else {
00081                 return 100;
00082             }
00083         }
00084     };
00085 
00103     template <class T> class JackGnuPlotMonitor
00104     {
00105         private:
00106             uint32_t fMeasureCnt;
00107             uint32_t fMeasurePoints;
00108             uint32_t fMeasureId;
00109             T* fCurrentMeasure;
00110             T** fMeasureTable;
00111             uint32_t fTablePos;
00112             std::string fName;
00113 
00114         public:
00115             JackGnuPlotMonitor ( uint32_t measure_cnt = 512, uint32_t measure_points = 5, std::string name = std::string ( "default" ) )
00116             {
00117                 jack_log ( "JackGnuPlotMonitor::JackGnuPlotMonitor %u measure points - %u measures", measure_points, measure_cnt );
00118 
00119                 fMeasureCnt = measure_cnt;
00120                 fMeasurePoints = measure_points;
00121                 fTablePos = 0;
00122                 fName = name;
00123                 fCurrentMeasure = new T[fMeasurePoints];
00124                 fMeasureTable = new T*[fMeasureCnt];
00125                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00126                 {
00127                     fMeasureTable[cnt] = new T[fMeasurePoints];
00128                     fill_n ( fMeasureTable[cnt], fMeasurePoints, 0 );
00129                 }
00130             }
00131 
00132             ~JackGnuPlotMonitor()
00133             {
00134                 jack_log ( "JackGnuPlotMonitor::~JackGnuPlotMonitor" );
00135 
00136                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00137                     delete[] fMeasureTable[cnt];
00138                 delete[] fMeasureTable;
00139                 delete[] fCurrentMeasure;
00140             }
00141 
00142             T AddNew ( T measure_point )
00143             {
00144                                 fMeasureId = 0;
00145                                 return fCurrentMeasure[fMeasureId++] = measure_point;
00146             }
00147 
00148                         uint32_t New()
00149                         {
00150                                 return fMeasureId = 0;
00151                         }
00152 
00153             T Add ( T measure_point )
00154             {
00155                 return fCurrentMeasure[fMeasureId++] = measure_point;
00156             }
00157 
00158             uint32_t AddLast ( T measure_point )
00159             {
00160                 fCurrentMeasure[fMeasureId] = measure_point;
00161                 fMeasureId = 0;
00162                 return Write();
00163             }
00164 
00165             uint32_t Write()
00166             {
00167                 for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00168                     fMeasureTable[fTablePos][point] = fCurrentMeasure[point];
00169                 if ( ++fTablePos == fMeasureCnt )
00170                     fTablePos = 0;
00171                 return fTablePos;
00172             }
00173 
00174             int Save ( std::string name = std::string ( "" ) )
00175             {
00176                 std::string filename = ( name.empty() ) ? fName : name;
00177                 filename += ".log";
00178 
00179                 jack_log ( "JackGnuPlotMonitor::Save filename %s", filename.c_str() );
00180 
00181                 std::ofstream file ( filename.c_str() );
00182 
00183                 for ( uint32_t cnt = 0; cnt < fMeasureCnt; cnt++ )
00184                 {
00185                     for ( uint32_t point = 0; point < fMeasurePoints; point++ )
00186                         file << fMeasureTable[cnt][point] << " \t";
00187                     file << std::endl;
00188                 }
00189 
00190                 file.close();
00191                 return 0;
00192             }
00193 
00194             int SetPlotFile ( std::string* options_list = NULL, uint32_t options_number = 0,
00195                               std::string* field_names = NULL, uint32_t field_number = 0,
00196                               std::string name = std::string ( "" ) )
00197             {
00198                 std::string title = ( name.empty() ) ? fName : name;
00199                 std::string plot_filename = title + ".plt";
00200                 std::string data_filename = title + ".log";
00201 
00202                 std::ofstream file ( plot_filename.c_str() );
00203 
00204                 file << "set multiplot" << std::endl;
00205                 file << "set grid" << std::endl;
00206                 file << "set title \"" << title << "\"" << std::endl;
00207 
00208                 for ( uint32_t i = 0; i < options_number; i++ )
00209                     file << options_list[i] << std::endl;
00210 
00211                 file << "plot ";
00212                 for ( uint32_t row = 1; row <= field_number; row++ )
00213                 {
00214                     file << "\"" << data_filename << "\" using " << row << " title \"" << field_names[row-1] << "\" with lines";
00215                     file << ( ( row < field_number ) ? ", " : "\n" );
00216                 }
00217 
00218                 jack_log ( "JackGnuPlotMonitor::SetPlotFile - Save GnuPlot file to '%s'", plot_filename.c_str() );
00219 
00220                 file.close();
00221                 return 0;
00222             }
00223     };
00224 
00225     void BuildClientPath(char* path_to_so, int path_len, const char* so_name);
00226     void PrintLoadError(const char* so_name);
00227 
00228 }
00229 
00230 #endif