HwSavingCtrlObj.cpp 5.89 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
//###########################################################################
// 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 "HwSavingCtrlObj.h"
#include "Exceptions.h"
24
25
#include <deque>
#include <algorithm>
26
27

using namespace lima;
28
29
30
const char* HwSavingCtrlObj::RAW_FORMAT_STR = "RAW"; ///< Raw format (no header)
const char* HwSavingCtrlObj::EDF_FORMAT_STR = "EDF"; ///< EDF format (Esrf Data Format)
const char* HwSavingCtrlObj::CBF_FORMAT_STR = "CBF"; ///< CBF format
31
#ifdef __linux__
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
class HwSavingCtrlObj::DirectoryCallback : public DirectoryEvent::Callback
{
public:
  DirectoryCallback(HwSavingCtrlObj& saving) : 
    m_saving(saving),
    m_start_file_number(0)
  {
  }
  void prepare(const DirectoryEvent::Parameters& params)
  {
    m_start_file_number = params.next_file_number_expected;
  }

  void clear()
  {
    m_image_ids.clear();
  }
  virtual bool nextFileExpected(int file_number,
				const char*,
				int &next_file_number_expected) throw()
  {
    int image_id = file_number - m_start_file_number;

    bool continueFlag = m_saving.m_callback ? 
      m_saving.m_callback->newFrameWritten(image_id) : false;

    int next_file_number = file_number + 1;

    std::sort(m_image_ids.begin(),m_image_ids.end());
    while(continueFlag &&
	  !m_image_ids.empty() && m_image_ids.front() == next_file_number)
      {
	image_id = next_file_number - m_start_file_number;
	continueFlag = m_saving.m_callback->newFrameWritten(image_id);
	++next_file_number;
	m_image_ids.pop_front();
      }
    next_file_number_expected = next_file_number_expected;
    return continueFlag;
  }

  virtual bool newFile(int file_number,const char*) throw()
  {
    m_image_ids.push_back(file_number);
    return true;
  }
private:
  HwSavingCtrlObj& 	m_saving;
  int 			m_start_file_number;
  std::deque<int> 	m_image_ids;
};
83
#endif
84
85
86

HwSavingCtrlObj::HwSavingCtrlObj(int capabilities) :
  m_caps(capabilities),
87
  m_active(false),
88
89
90
  m_callback(NULL)
#ifdef __linux__
  ,m_dir_cbk(new HwSavingCtrlObj::DirectoryCallback(*this)),
91
  m_dir_event(true,*m_dir_cbk)
92
#endif
93
94
95
96
97
{
}

HwSavingCtrlObj::~HwSavingCtrlObj()
{
98
#ifdef __linux__
99
  delete m_dir_cbk;
100
#endif
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}

void HwSavingCtrlObj::setActive(bool flag)
{
  _setActive(flag);
  m_active = flag;
}

bool HwSavingCtrlObj::isActive() const
{
  return m_active;
}
void HwSavingCtrlObj::setDirectory(const std::string& directory)
{
  m_directory = directory;
}

void HwSavingCtrlObj::setPrefix(const std::string& prefix)
{
  m_prefix = prefix;
}
void HwSavingCtrlObj::setSuffix(const std::string& suffix)
{
  m_suffix = suffix;
}
void HwSavingCtrlObj::setNextNumber(long number)
{
  m_next_number = number;
}
void HwSavingCtrlObj::setIndexFormat(const std::string& indexFormat)
{
  m_index_format = indexFormat;
133
}
134
135
136
137
138
void HwSavingCtrlObj::setSaveFormat(const std::string &format)
{
  m_file_format = format;
}

139
140
141
142
143
144
145
146

/** @brief write manualy a frame
 */
void HwSavingCtrlObj::writeFrame(int,int)
{
  DEB_MEMBER_FUNCT();
  THROW_HW_ERROR(NotSupported) << "No available for this Hardware";
}
147
148
149
150
151
152
153
/** @brief write manualy a frame
 */
void HwSavingCtrlObj::readFrame(HwFrameInfoType&,int)
{
  DEB_MEMBER_FUNCT();
  THROW_HW_ERROR(NotSupported) << "No available for this Hardware";
}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

/** @brief set frames' common header
 */
void HwSavingCtrlObj::setCommonHeader(const HeaderMap&)
{
  DEB_MEMBER_FUNCT();
  THROW_HW_ERROR(NotSupported) << "No available for this Hardware";
}

/** @brief clear common header
 */
void HwSavingCtrlObj::resetCommonHeader()
{
  DEB_MEMBER_FUNCT();
  THROW_HW_ERROR(NotSupported) << "No available for this Hardware";
}

171
172
173
174
175
176
177
void HwSavingCtrlObj::prepare()
{
  DEB_MEMBER_FUNCT();

  if(m_active)
    {
      _prepare();
178
#ifdef __linux__
179
180
181
182
183
184
185
186
187
188
      DirectoryEvent::Parameters params;
      params.watch_path = m_directory;
      params.file_pattern = m_prefix;
      params.file_pattern += m_index_format;
      params.file_pattern += m_suffix;
      params.next_file_number_expected = m_next_number;
      m_dir_event.prepare(params);

      if(m_callback)
	m_callback->prepare(params);
189
#endif
190
191
192
193
194
195
196
197
198
199
    }
}

void HwSavingCtrlObj::start()
{
  DEB_MEMBER_FUNCT();

  if(m_active)
    {
      _start();
200
#ifdef __linux__
201
      m_dir_event.start();
202
#endif
203
204
205
206
    }
}
void HwSavingCtrlObj::stop()
{
207
#ifdef __linux__
208
  m_dir_event.stop();
209
#endif
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
int HwSavingCtrlObj::getCapabilities() const
{
  return m_caps;
}

void HwSavingCtrlObj::registerCallback(HwSavingCtrlObj::Callback *cbk)
{
  DEB_MEMBER_FUNCT();

  if(m_callback)
    THROW_HW_ERROR(Error) << "Callback is already registered";
  m_callback = cbk;
}

void HwSavingCtrlObj::unregisterCallback(HwSavingCtrlObj::Callback *cbk)
{
  DEB_MEMBER_FUNCT();

  if(m_callback != cbk)
    THROW_HW_ERROR(Error) << "Try the unregister wrong callback object";
  m_callback = NULL;
}

235
std::string HwSavingCtrlObj::_getFullPath(int image_number) const
236
{
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  char nbBuffer[32];
  snprintf(nbBuffer,sizeof(nbBuffer),
	   m_index_format.c_str(),image_number);
#ifdef __unix
  const char SEPARATOR = '/';
#else	 // WINDOW
  const char SEPARATOR = '\\';
#endif
  std::string fullpath;
  fullpath = m_directory + SEPARATOR;
  fullpath += m_prefix;
  fullpath += nbBuffer;
  fullpath += m_suffix;
  return fullpath;
251
}