]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTHOMERLibManager.cxx
added libAliHLTHOMER dynamic manager
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTHOMERLibManager.cxx
1 // $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
5  * ALICE Experiment at CERN, All rights reserved.                         *
6  *                                                                        *
7  * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8  *                  for The ALICE HLT Project.                            *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /** @file   AliHLTHOMERLibManager.cxx
20     @author Matthias Richter
21     @date   
22     @brief  dynamic HLT HOMER reader/writer generation and destruction.   */
23
24 // see header file for class documentation
25 // or
26 // refer to README to build package
27 // or
28 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
29
30 #include <cerrno>
31 #include <cassert>
32 #include "AliHLTHOMERLibManager.h"
33 #include "AliHLTHOMERReader.h"
34 #include "AliHLTHOMERWriter.h"
35 #include "TString.h"
36 #include "TSystem.h"
37
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTHOMERLibManager)
40
41 AliHLTHOMERLibManager::AliHLTHOMERLibManager()
42   :
43   fLibraryStatus(0),
44   fFctCreateReaderFromBuffer(NULL),
45   fFctDeleteReader(NULL),
46   fFctCreateWriter(NULL),
47   fFctDeleteWriter(NULL)
48 {
49   // see header file for class documentation
50   // or
51   // refer to README to build package
52   // or
53   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
54 }
55
56 AliHLTHOMERLibManager::~AliHLTHOMERLibManager()
57 {
58   // see header file for class documentation
59 }
60
61 AliHLTHOMERReader* AliHLTHOMERLibManager::OpenReader(const AliHLTUInt8_t* pBuffer, int size)
62 {
63   // see header file for class documentation
64   if (fLibraryStatus<0) return NULL;
65
66   if (fLibraryStatus==0) {
67     fLibraryStatus=LoadHOMERLibrary();
68   }
69   
70   AliHLTHOMERReader* pReader=NULL;
71   if (fFctCreateReaderFromBuffer!=NULL && (pReader=(((AliHLTHOMERReaderCreateFromBuffer_t)fFctCreateReaderFromBuffer)(pBuffer, size)))==NULL) {
72     HLTError("can not create instance of HOMER reader (function %p)", fFctCreateReaderFromBuffer);
73   }
74   
75   return pReader;
76 }
77
78 int AliHLTHOMERLibManager::DeleteReader(AliHLTHOMERReader* pReader)
79 {
80   // see header file for class documentation
81   if (fLibraryStatus<0) return fLibraryStatus;
82
83   if (fLibraryStatus==0) {
84     fLibraryStatus=LoadHOMERLibrary();
85   }
86   
87   if (fFctDeleteReader!=NULL) {
88     ((AliHLTHOMERReaderDelete_t)fFctDeleteReader)(pReader);
89   }
90   
91   return 0;
92 }
93
94 AliHLTHOMERWriter* AliHLTHOMERLibManager::OpenWriter()
95 {
96   // see header file for class documentation
97   if (fLibraryStatus<0) return NULL;
98
99   if (fLibraryStatus==0) {
100     fLibraryStatus=LoadHOMERLibrary();
101   }
102   
103   AliHLTHOMERWriter* pWriter=NULL;
104 //   if (fFctCreateWriter!=NULL && (pWriter=(((AliHLTHOMERWriterCreate_t)fFctCreateWriter)()))==NULL) {
105 //     HLTError("can not create instance of HOMER writer (function %p)", fFctCreateWriter);
106 //   }
107   
108   return pWriter;
109 }
110
111 int AliHLTHOMERLibManager::DeleteWriter(AliHLTHOMERWriter* pWriter)
112 {
113   // see header file for class documentation
114   if (fLibraryStatus<0) return fLibraryStatus;
115
116   if (fLibraryStatus==0) {
117     fLibraryStatus=LoadHOMERLibrary();
118   }
119   
120   if (fFctDeleteWriter!=NULL) {
121     ((AliHLTHOMERWriterDelete_t)fFctDeleteWriter)(pWriter);
122   }
123   
124   return 0;
125 }
126
127 int AliHLTHOMERLibManager::LoadHOMERLibrary()
128 {
129   // see header file for class documentation
130   int iResult=-EBADF;
131   const char* libraries[]={"libAliHLTHOMER.so", "libHOMER.so", NULL};
132   const char** library=&libraries[0];
133   do {
134     TString libs = gSystem->GetLibraries();
135     if (libs.Contains(*library) ||
136         (gSystem->Load(*library)) > 0) {
137       HLTDebug("%s (is) loaded", *library);
138       iResult=1;
139       break;
140     }
141   } while (*(++library)!=NULL);
142
143   if (iResult>0 && *library!=NULL) {
144     // print compile info
145     typedef void (*CompileInfo)( char*& date, char*& time);
146     CompileInfo fctInfo=(CompileInfo)gSystem->DynFindSymbol(*library, "CompileInfo");
147     if (fctInfo) {
148       char* date="";
149       char* time="";
150       (*fctInfo)(date, time);
151       if (!date) date="unknown";
152       if (!time) time="unknown";
153       HLTInfo("%s build on %s (%s)", *library, date, time);
154     } else {
155       HLTInfo("no build info available for %s", *library);
156     }
157
158     fFctCreateReaderFromBuffer=gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_CREATE_FROM_BUFFER);
159     fFctDeleteReader=gSystem->DynFindSymbol(*library, ALIHLTHOMERREADER_DELETE);
160     fFctCreateWriter=gSystem->DynFindSymbol(*library, ALIHLTHOMERWRITER_CREATE);
161     fFctDeleteWriter=gSystem->DynFindSymbol(*library, ALIHLTHOMERWRITER_DELETE);
162     if (fFctCreateReaderFromBuffer==NULL || fFctDeleteReader==NULL ||
163         fFctCreateWriter==NULL || fFctDeleteWriter==NULL) {
164       iResult=-ENOSYS;
165     } else {
166       HLTDebug("%s: create fct %p, delete fct %p", *library, fFctCreateReaderFromBuffer, fFctDeleteReader);
167     }
168   }
169   if (iResult<0 || *library==NULL) {
170     fFctCreateReaderFromBuffer=NULL;
171     fFctDeleteReader=NULL;
172     fFctCreateWriter=NULL;
173     fFctDeleteWriter=NULL;
174   }
175
176   return iResult;
177 }