]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/interface/test/testAliHLTExternalInterface.C
major change in the external component interface: redesigned and moved to libHLTinter...
[u/mrichter/AliRoot.git] / HLT / BASE / interface / test / testAliHLTExternalInterface.C
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   testAliHLTExternalInterface.C
20     @author Matthias Richter
21     @date   
22     @brief  Test program for the external wrapper interface
23  */
24
25 #include "AliHLTProcessor.h"
26 #include "AliHLTModuleAgent.h"
27 #include "AliHLTExternalInterface.h"
28 #include <cstring>
29 #include <dlfcn.h>
30
31 const char* gDummy="dummy";
32 AliHLTUInt32_t gRunNo=kAliHLTVoidRunNo;
33 const char* gChainId="<void>";
34 AliHLTComponentDataType gInputDt=kAliHLTVoidDataType;
35 const char* gBasePath="../.libs";
36
37 class TestProcessor : public AliHLTProcessor
38 {
39 public:
40   TestProcessor();
41   ~TestProcessor();
42
43   const char* GetComponentID() {return "TestProcessor";}
44
45   void GetInputDataTypes( vector<AliHLTComponentDataType>& list)
46   {list.push_back(kAliHLTAnyDataType);}
47
48   AliHLTComponentDataType GetOutputDataType() {return kAliHLTAnyDataType;}
49
50   void GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
51   {constBase=100; inputMultiplier=2.0;}
52   
53   AliHLTComponent* Spawn() {return new TestProcessor;}
54 private:
55   int DoInit( int argc, const char** argv ) {
56     if (fState!=kCreated) {
57       HLTError("wrong state (%d): component already initialized", fState);
58       return -EBUSY;
59     }
60
61     if (argc>0 && argv) {
62     }
63     gRunNo=GetRunNo();
64     gChainId=GetChainId();
65
66     fState=kInitialized;
67     return 0;
68   }
69   int DoDeinit() {
70     if (fState!=kInitialized) {
71       HLTError("wrong state (%d): required %d kInitialized", fState, kInitialized);
72       return -ENODEV;
73     }
74
75     return 0;
76   }
77
78   int DoEvent( const AliHLTComponentEventData& /*evtData*/, AliHLTComponentTriggerData& /*trigData*/) {
79     int iResult=0;
80     if (fState!=kInitialized) {
81       HLTError("wrong state (%d): component not initialized", fState);
82       return -ENODEV;
83     }
84
85     HLTInfo("processing event");
86     for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock();
87          pBlock!=NULL; 
88          pBlock=GetNextInputBlock()) {
89       gInputDt=pBlock->fDataType;
90       AliHLTComponentDataType dt;
91       SetDataType(dt, "-OUTPUT-", "MYCO");
92       iResult=PushBack(pBlock->fPtr, pBlock->fSize/2, dt, ~pBlock->fSpecification);
93       Forward();
94     }
95
96     return iResult;
97   }
98
99   enum {
100     kCreated =0,
101     kInitialized,
102     kProcessing,
103   };
104
105   int fState; //!transient
106 };
107
108 TestProcessor::TestProcessor()
109   : 
110   AliHLTProcessor(),
111   fState(kCreated)
112  
113 {
114 }
115
116 TestProcessor::~TestProcessor()
117 {
118 }
119
120 class TestAgent : public AliHLTModuleAgent
121 {
122 public:
123   TestAgent() : AliHLTModuleAgent("TEST") {}
124   ~TestAgent() {}
125
126   int RegisterComponents(AliHLTComponentHandler* pHandler) const
127   {
128     pHandler->AddComponent(new TestProcessor);
129     return 0;
130   }
131   
132 };
133
134 TestAgent gAgent;
135
136 int Logging( void* /*param*/, 
137              AliHLTComponentLogSeverity severity,
138              const char* origin,
139              const char* keyword,
140              const char* message)
141 {
142   cout << "Logging: "<< severity << " " << origin << " " << keyword << " " << message << endl;
143   return 0;
144 }
145
146 void* AllocMemory( void* param, unsigned long size )
147 {
148   if (param!=&gDummy) {
149     cerr << "AllocMemoryFunc callback with wrong parameter " << endl;
150     abort();
151   }
152   if (size==0) return NULL;
153   return new AliHLTUInt8_t[size];
154 }
155
156 int main(int /*argc*/, const char** /*argv*/)
157 {
158   int iResult=0;
159
160   string libraryPath=gBasePath;
161   libraryPath+="/";
162   libraryPath+=ALIHLTANALYSIS_INTERFACE_LIBRARY;
163
164   void* libHandle=dlopen(libraryPath.c_str(), RTLD_NOW);
165   if (!libHandle) {
166     cerr << "error: can not load library " << libraryPath.c_str() << endl;
167     return -1;
168   }
169
170   AliHLTAnalysisFctGetInterfaceCall fctGetSystemCall=(AliHLTAnalysisFctGetInterfaceCall)dlsym(libHandle, ALIHLTANALYSIS_FCT_GETINTERFACECALL);
171   if (!fctGetSystemCall) {
172     cerr << "error: can not find function '" << ALIHLTANALYSIS_FCT_GETINTERFACECALL << "' in " << libraryPath.c_str() << endl;
173     return -1;
174   }
175
176   AliHLTAnalysisEnvironment environment;
177   memset(&environment, 0, sizeof(environment));
178   environment.fStructSize=sizeof(environment);
179   environment.fAllocMemoryFunc=AllocMemory;
180   environment.fLoggingFunc=Logging;
181
182   AliHLTExtFctInitSystem fctInitSystem=(AliHLTExtFctInitSystem)fctGetSystemCall("int AliHLTAnalysisInitSystem(unsigned long,AliHLTAnalysisEnvironment*,unsigned long,const char*)");
183   if (!fctInitSystem) {
184     cerr << "error: missing AliHLTAnalysisInitSystem call" << endl;
185     return -1;
186   }
187
188   if ((iResult=fctInitSystem( ALIHLT_DATA_TYPES_VERSION, &environment, 0xbeef, "dummy-run" ))<0) {
189     cerr << "InitSystem failed with " << iResult << endl;
190     return iResult;
191   }
192
193   AliHLTExtFctLoadLibrary fctLoadLibrary=(AliHLTExtFctLoadLibrary)fctGetSystemCall("int AliHLTAnalysisLoadLibrary(const char*)");
194   if (!fctLoadLibrary) {
195     cerr << "error: missing LoadLibrary call" << endl;
196     return -1;
197   }
198
199   if ((iResult=fctLoadLibrary("../../util/.libs/libAliHLTUtil.so"))<0) {
200     cerr << "error: AliHLTAnalysisLoadLibrary failed with " << iResult << endl;
201     return iResult;
202   }
203
204   AliHLTExtFctCreateComponent fctCreateComponent=(AliHLTExtFctCreateComponent)fctGetSystemCall("int AliHLTAnalysisCreateComponent(const char*,void*,int,const char**,AliHLTComponentHandle*,const char*)");
205   if (!fctCreateComponent) {
206     cerr << "error: missing CreateComponent call" << endl;
207     return -1;
208   }
209
210   AliHLTComponentHandle handle;
211   if ((iResult=fctCreateComponent("TestProcessor", &gDummy, 0, NULL, &handle, "chainid=test" ))<0) {
212     cerr << "error: AliHLTAnalysisCreateComponent failed with " << iResult << endl;
213     return iResult;
214   }
215
216   if (gRunNo!=0xbeef) {
217     cerr << "error: propagation of run number failed " << hex << gRunNo << " vs. 0xbeef" << endl;
218     return -1;
219   }
220
221   // can be used in the new interface again
222   if (strcmp(gChainId, "test")) {
223     cerr << "propagation of chain id failed: '" << gChainId << "' vs. test" << endl;
224     return -1;
225   }
226
227   AliHLTExtFctProcessEvent fctProcessEvent=(AliHLTExtFctProcessEvent)fctGetSystemCall("int AliHLTAnalysisProcessEvent(AliHLTComponentHandle,const AliHLTComponentEventData*,const AliHLTComponentBlockData*,AliHLTComponentTriggerData*,AliHLTUInt8_t*,AliHLTUInt32_t*,AliHLTUInt32_t*,AliHLTComponentBlockData**,AliHLTComponentEventDoneData**)");
228   if (!fctProcessEvent) {
229     cerr << "error: missing ProcessEvent call" << endl;
230     return -1;
231   }
232
233   const char* inputData="some data to be copied";
234   AliHLTComponentBlockData inputBlock;
235   memset(&inputBlock, 0, sizeof(inputBlock));
236   inputBlock.fStructSize=sizeof(inputBlock);
237   inputBlock.fPtr=(void*)inputData;
238   inputBlock.fSize=strlen(inputData);
239   inputBlock.fDataType=kAliHLTDataTypeDDLRaw;
240   inputBlock.fSpecification=0xdead;
241
242   AliHLTComponentEventData evtData;
243   memset(&evtData, 0, sizeof(evtData));
244   evtData.fStructSize=sizeof(evtData);
245   evtData.fBlockCnt=1;
246
247   AliHLTComponentTriggerData trigData;
248   memset(&trigData, 0, sizeof(trigData));
249   trigData.fStructSize=sizeof(trigData);
250
251   AliHLTUInt8_t outputPtr[100];
252   AliHLTUInt32_t size=sizeof(outputPtr);
253
254   AliHLTUInt32_t outputBlockCnt=0;
255   AliHLTComponentBlockData* outputBlocks=NULL;
256   AliHLTComponentEventDoneData* edd=NULL;
257
258   if ((iResult=fctProcessEvent( handle, &evtData, &inputBlock, &trigData, outputPtr,
259                                 &size, &outputBlockCnt, &outputBlocks, &edd ))<0) {
260     cerr << "error: AliHLT_C_Component_ProcessEvent failed with " << iResult << endl;
261     return iResult;
262   }
263
264   if (outputBlockCnt<2) {
265     cerr << "error: mismatch in output block count, expecting >2, got " << outputBlockCnt << endl;
266     return -1;
267   }
268
269   if (outputBlocks==NULL) {
270     cerr << "error: did not get output block array " << endl;
271     return -1;
272   }
273
274   AliHLTComponentDataType outdt;
275   AliHLTComponent::SetDataType(outdt, "-OUTPUT-", "MYCO");
276   bool bHaveForwarded=false;
277   bool bHaveCopied=false;
278   for (unsigned int i=0; i<outputBlockCnt; i++) {
279     if (outputBlocks[i].fDataType==kAliHLTDataTypeDDLRaw) {
280       if (outputBlocks[i].fPtr!=inputData ||
281           outputBlocks[i].fSize!=strlen(inputData)) {
282         cerr << "error: failed comparing forwarded input block" << endl;
283         return -1;
284       }
285       bHaveForwarded=true;
286     } else if (outputBlocks[i].fDataType==outdt) {
287       if (outputBlocks[i].fSize!=strlen(inputData)/2) {
288         cerr << "error: wrong size of copied block" << endl;
289         return -1;
290       }
291       if (memcmp(inputData, outputPtr+outputBlocks[i].fOffset, outputBlocks[i].fSize)) {
292         cerr << "error: failed comparing copied block" << endl;
293         return -1;
294       }
295       bHaveCopied=true;
296     }
297   }
298
299   if (!bHaveForwarded) {
300     cerr << "error: did not get forwarded data block" << endl;
301     return -1;
302   }
303
304   if (!bHaveCopied) {
305     cerr << "error: did not get copied data block" << endl;
306     return -1;
307   }
308
309   AliHLTExtFctDestroyComponent fctDestroyComponent=(AliHLTExtFctDestroyComponent)fctGetSystemCall("int AliHLTAnalysisDestroyComponent(AliHLTComponentHandle)");
310   if (!fctDestroyComponent) {
311     cerr << "error: missing DestroyComponent call" << endl;
312     return -1;
313   }
314
315   fctDestroyComponent(handle);
316
317   AliHLTExtFctDeinitSystem fctDeinitSystem=(AliHLTExtFctDeinitSystem)fctGetSystemCall("int AliHLTAnalysisDeinitSystem()");
318   if (!fctDeinitSystem) {
319     cerr << "error: missing DeinitSystem call" << endl;
320     return -1;
321   }
322
323   if ((iResult=fctDeinitSystem( ))<0) {
324     cerr << "AliHLTAnalysisDeinitSystem failed with " << iResult << endl;
325     return iResult;
326   }
327
328   return 0;
329 }