HwFileEventMgr.cpp 8.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
#include <dirent.h>
#include <stdlib.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include "HwFileEventMgr.h"
using namespace lima;

void HwFileEventCallbackHelper::prepare(const DirectoryEvent::Parameters& params)
{
  //Clean all pending Data
  for(DatasPendingType::iterator i = m_pending_frame_infos.begin();
      i != m_pending_frame_infos.end();m_pending_frame_infos.erase(i++))
    if(i->second.buffer_owner_ship != HwFrameInfoType::Managed)
      free(i->second.frame_ptr);

  m_pending_frame_infos.clear();

  m_cbk.prepare(params);
}

bool HwFileEventCallbackHelper::nextFileExpected(int file_number,
						 const char *full_path,
						 int &next_file_number_expected) throw()
{
  HwFrameInfoType aNewFrameInfo;
  bool continueFlag = true;
  try
    {
50
51
52
      continueFlag = m_cbk.getFrameInfo(file_number,full_path,
					Acquisition,aNewFrameInfo);
      bool continueAcq = m_cbk.newFrameReady(aNewFrameInfo);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
      if(continueFlag) continueFlag = continueAcq;
      DatasPendingType::iterator i = m_pending_frame_infos.begin();
      while(i != m_pending_frame_infos.end() && continueFlag)
	{
	  ++file_number;
	  if(i->first == file_number)
	    {
	      next_file_number_expected = file_number;
	      continueFlag = m_cbk.newFrameReady(i->second);
	      m_pending_frame_infos.erase(i);
	    }
	  else
	    break;
	  i = m_pending_frame_infos.begin();
	}
    }
  catch(...)		// File reading problem
    {
      next_file_number_expected = file_number;
    }
  return continueFlag;
}

bool HwFileEventCallbackHelper::newFile(int file_number,const char *full_path) throw()
{
  bool continueFlag = true;
  DatasPendingType::iterator i = m_pending_frame_infos.find(file_number);
  if(i == m_pending_frame_infos.end())
    {

      try
	{
	  HwFrameInfoType aNewFrameInfo;
86
87
88
	  continueFlag = m_cbk.getFrameInfo(file_number,full_path,
					    Acquisition,
					    aNewFrameInfo);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	  m_pending_frame_infos[file_number] = aNewFrameInfo;
	}
      catch(...)
	{
	}
    }
  return continueFlag;
}

/** @brief callback class of HwTmpfsBufferMgr
 */
class HwTmpfsBufferMgr::_CBK : public HwFileEventCallbackHelper::Callback
{
public:
  _CBK(HwTmpfsBufferMgr &cnt) : m_cnt(cnt) {}

  virtual void prepare(const DirectoryEvent::Parameters &params)
  {
    //Clean watch directory
    const char *path = params.watch_path.c_str();
    DIR* aWatchDir = opendir(path);
    if(aWatchDir)
      {
	while(1)
	  {
	    char fullPath[1024];
	    struct dirent aDirentStruct,*result;
	    int status = readdir_r(aWatchDir,
				   &aDirentStruct,&result);
	    if(status || !result)
	      break;
	    snprintf(fullPath,sizeof(fullPath),
		     "%s/%s",path,result->d_name);
	    unlink(fullPath);
	  }
	closedir(aWatchDir);
125
126
      }
    m_cnt.m_cbk.prepare(params);
127
128
  }
  virtual bool getFrameInfo(int image_number,const char* full_path,
129
			    HwFileEventCallbackHelper::CallFrom mode,
130
131
			    HwFrameInfoType &frame_info)
  {
132
    return m_cnt._getFrameInfo(image_number,full_path,mode,frame_info);
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  }
  virtual bool newFrameReady(const HwFrameInfoType &frame_info)
  {
    int imageId2Remove = frame_info.acq_frame_nb - m_cnt.m_keep_nb_images;
    if(imageId2Remove >= 0)
      {
	char aFileName[128];
	snprintf(aFileName,sizeof(aFileName),
		 m_cnt.m_file_pattern.c_str(),imageId2Remove);
	char aFullPathBuffer[1024];
	snprintf(aFullPathBuffer,sizeof(aFullPathBuffer),
		 "%s/%s",m_cnt.m_tmpfs_path.c_str(),aFileName);
	unlink(aFullPathBuffer);
      }
    return m_cnt.newFrameReady(frame_info);
  }
private:
  HwTmpfsBufferMgr& m_cnt;
};
/** @brief Buffer manager for tmp file system.
 *
 *  This manager can be used when you can't get (from API) image from the
 *  detector. The only possible way is to read image back from a file.
 */
HwTmpfsBufferMgr::HwTmpfsBufferMgr(const char* tmpfs_path, 
				   const char* file_pattern,
				   Callback &cbk) :
  m_tmpfs_path(tmpfs_path),
  m_file_pattern(file_pattern),
  m_next_expected_image_number(0),
  m_keep_nb_images(1),
  m_memory_percent(0.5),
  m_internal_cbk(new _CBK(*this)),
  m_directory_cbk(*m_internal_cbk),
  m_directory_event(true,m_directory_cbk),
  m_cbk(cbk)
{
    
}

HwTmpfsBufferMgr::~HwTmpfsBufferMgr()
{
  m_directory_event.stop();
  delete m_internal_cbk;
}

void HwTmpfsBufferMgr::setMemoryPercent(double percent)
{
  DEB_MEMBER_FUNCT();

  if(percent >= 0. && percent <= 1.)
    m_memory_percent = percent;
  else
    THROW_HW_ERROR(Error) << "percent should be between 0. and 1.";
}
/** @brief next image number should we expected
 *  This parameters will be take into account after prepare
 */
void HwTmpfsBufferMgr::setNextExpectedImageNumber(int image_nb)
{
  m_next_expected_image_number = image_nb;
}

void HwTmpfsBufferMgr::prepare()
{
  DirectoryEvent::Parameters params;
  params.watch_path = m_tmpfs_path;
  params.file_pattern = m_file_pattern;
  params.next_file_number_expected = m_next_expected_image_number;
  m_directory_event.prepare(params);
  m_keep_nb_images = _calcNbMaxImages();
}

void HwTmpfsBufferMgr::start()
{
  m_directory_event.start();
  m_start_time = Timestamp::now();
}

void HwTmpfsBufferMgr::stop()
{
  m_directory_event.stop();
}

bool HwTmpfsBufferMgr::isStopped() const
{
  return m_directory_event.isStopped();
}

int HwTmpfsBufferMgr::getLastAcquiredFrame() const
{
  return m_directory_event.getNextFileNumberExpected() - 1;
}

void HwTmpfsBufferMgr::setFrameDim(const FrameDim&) 
{
  DEB_MEMBER_FUNCT();
}
void HwTmpfsBufferMgr::getFrameDim(FrameDim& frame_dim)
{
  m_cbk.getFrameDim(frame_dim);
}

void HwTmpfsBufferMgr::setNbBuffers(int)
{
  DEB_MEMBER_FUNCT();
}

void HwTmpfsBufferMgr::getNbBuffers(int& nb_buffers)
{
  nb_buffers = m_keep_nb_images;
}

void HwTmpfsBufferMgr::setNbConcatFrames(int nb_concat_frames)
{
  DEB_MEMBER_FUNCT();
  if(nb_concat_frames != 1)
    THROW_HW_ERROR(NotSupported) << "Not managed";
}
void HwTmpfsBufferMgr::getNbConcatFrames(int& nb_concat_frames)
{
  nb_concat_frames = 1;
}

void HwTmpfsBufferMgr::getMaxNbBuffers(int& max_nb_buffers)
{
  max_nb_buffers = _calcNbMaxImages();
}

void* HwTmpfsBufferMgr::getBufferPtr(int,int)
{
  return NULL;
}

void* HwTmpfsBufferMgr::getFramePtr(int)
{
  return NULL;
}

void HwTmpfsBufferMgr::getStartTimestamp(Timestamp& start_ts)
{
  start_ts = m_start_time;
}

void HwTmpfsBufferMgr::getFrameInfo(int acq_frame_nb, HwFrameInfoType& info)
{
279
  DEB_MEMBER_FUNCT();
280

281
282
283
284
285
286
287
288
  char filename[1024];
  snprintf(filename,sizeof(filename),
	   m_file_pattern.c_str(),acq_frame_nb);
  std::string fullPath = m_tmpfs_path + "/";
  fullPath += filename;
  m_cbk.getFrameInfo(acq_frame_nb,fullPath.c_str(),
		     HwFileEventCallbackHelper::OnDemand,
		     info);
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
}

void HwTmpfsBufferMgr::registerFrameCallback(HwFrameCallback& frame_cb)
{
  HwFrameCallbackGen::registerFrameCallback(frame_cb);
}

void HwTmpfsBufferMgr::unregisterFrameCallback(HwFrameCallback& frame_cb)
{
  HwFrameCallbackGen::unregisterFrameCallback(frame_cb);
}

int HwTmpfsBufferMgr::_calcNbMaxImages()
{
   DEB_MEMBER_FUNCT();

  struct statvfs fsstat;
  if(statvfs(m_tmpfs_path.c_str(),&fsstat))
    THROW_HW_ERROR(Error) << "Can't figure out what is the size of that filesystem: " 
			  << m_tmpfs_path;


  FrameDim anImageDim;
  getFrameDim(anImageDim);
  int nb_block_for_image = anImageDim.getMemSize() / fsstat.f_bsize;
  int aTotalImage = fsstat.f_blocks / nb_block_for_image;
  return aTotalImage * m_memory_percent;
}

bool HwTmpfsBufferMgr::_getFrameInfo(int image_number,const char* full_path,
319
				     HwFileEventCallbackHelper::CallFrom mode,
320
321
				     HwFrameInfoType &frame_info)
{
322
  return m_cbk.getFrameInfo(image_number,full_path,mode,frame_info);
323
}