Requests.h 6.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2015
// 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/>.
//###########################################################################
22
23
24
#ifndef EIGERAPI_REQUESTS_H
#define EIGERAPI_REQUESTS_H

25
26
27
28
29
30
31
32
#include <string>
#include <map>
#include <vector>

#include "eigerapi/CurlLoop.h"

namespace eigerapi
{
33
34
35
36
37
38
39
  template <typename T>
  struct HeapDeleter {
    void operator()(T *p) { ::free(p); };
  };
  template <typename T>
  using HeapPtr = std::unique_ptr<T, HeapDeleter<T>>;

40
41
42
  class Requests
  {
  public:
Alejandro Homs Puron's avatar
Alejandro Homs Puron committed
43
    typedef CurlLoop::CurlReq CurlReq;
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
    class Command : public CurlLoop::FutureRequest
    {
      friend class Requests;
    public:
      Command(const std::string& url);
      virtual ~Command();

      int get_serie_id();
    private:
      static size_t _write_callback(char *ptr,size_t size,size_t nmemb,Command*);
      void _fill_request();
      char m_data[128];
    };
  
    class Param : public CurlLoop::FutureRequest
    {
      friend class Requests;
    public:
      enum VALUE_TYPE {BOOL,DOUBLE,INT,UNSIGNED,STRING,STRING_ARRAY};
      struct Value
      {
	VALUE_TYPE type;
	union
	{
	  bool bool_val;
	  int  int_val;
	  unsigned int unsigned_val;
	  double double_val;
	}data;
	std::string string_val;
	std::vector<std::string> string_array;
      };
	
      Param(const std::string& url);
      virtual ~Param();
      Value get(double timeout = CurlLoop::FutureRequest::TIMEOUT,
		bool lock = true);
82
83
84
85
      Value get_min(double timeout = CurlLoop::FutureRequest::TIMEOUT,
		    bool lock = true);
      Value get_max(double timeout = CurlLoop::FutureRequest::TIMEOUT,
		    bool lock = true);
86
87
      Value get_allowed_values(double timeout = CurlLoop::FutureRequest::TIMEOUT,
			       bool lock = true);
88
89
90
91
92
93
94
95
96
97
    private:
      void _fill_get_request();
      template <class T>
      void _fill_set_request(const T& value);

      void _set_return_value(bool&);
      void _set_return_value(double&);
      void _set_return_value(int&);
      void _set_return_value(unsigned int&);
      void _set_return_value(std::string&);
98
      void _set_return_value(std::vector<std::string>&);
99
100
101
102
103

      virtual void _request_finished();

      static size_t _write_callback(char*, size_t, size_t, void*);

104
105
      Value _get(double timeout,bool lock,const char*);
      
106
107
108
109
110
111
112
113
114
115
116
117
118
      char*			m_data_buffer;
      int			m_data_size;
      int			m_data_memorysize;
      struct curl_slist*	m_headers;
      VALUE_TYPE		m_return_type;
      void*			m_return_value;
    };
    
    class Transfer : public CurlLoop::FutureRequest
    {
      friend class Requests;
    public:
      Transfer(Requests& requests,
119
120
121
122
	       const std::string& url,
	       const std::string& target_path,
	       bool delete_after_transfer = true,
	       int buffer_write_size = 64 * 1024);
123
124
125
126
127
128
129
130
131
      virtual ~Transfer();
    private:
      static size_t _write(void *ptr, size_t size, size_t nmemb,Transfer*);
      virtual void _request_finished();
      
      Requests&	m_requests;
      bool	m_delete_after_transfer;
      long	m_download_size;
      FILE*	m_target_file;
132
      HeapPtr<void> m_buffer;
133
134
    };

135
136
137
138
    typedef std::shared_ptr<Command> CommandReq;
    typedef std::shared_ptr<Param> ParamReq;
    typedef std::shared_ptr<Transfer> TransferReq;

139
140
    enum COMMAND_NAME {INITIALIZE,ARM, DISARM,TRIGGER,CANCEL,ABORT,
		       FILEWRITER_CLEAR};
141
142
143
144
145
146
147
148
149
150
151
    enum PARAM_NAME {TEMP,
		     HUMIDITY,
		     DETECTOR_STATUS,
		     PIXELDEPTH,
		     X_PIXEL_SIZE,
		     Y_PIXEL_SIZE,
		     DETECTOR_WITDH,
		     DETECTOR_HEIGHT,
		     DESCRIPTION,
		     DETECTOR_NUMBER,
		     DETECTOR_READOUT_TIME,
152
153
             DATA_COLLECTION_DATE,
             SOFTWARE_VERSION,
154
155
156
157
158
159
160
161
162
163
		     EXPOSURE,
		     FRAME_TIME,
		     TRIGGER_MODE,
		     COUNTRATE_CORRECTION,
		     FLATFIELD_CORRECTION,
		     EFFICIENCY_CORRECTION,
		     PIXEL_MASK,
		     THRESHOLD_ENERGY,
		     VIRTUAL_PIXEL_CORRECTION,
		     PHOTON_ENERGY,
164
 		     NIMAGES,
165
		     NTRIGGER,
166
167
168
169
170
171
172
173
174
175
		     AUTO_SUMMATION,
		     FILEWRITER_MODE,
		     FILEWRITER_COMPRESSION,
		     FILEWRITER_NAME_PATTERN,
		     NIMAGES_PER_FILE,
		     FILEWRITER_STATUS,
		     FILEWRITER_ERROR,
		     FILEWRITER_TIME,
		     FILEWRITER_BUFFER_FREE,
		     FILEWRITER_LS,
176
		     FILEWRITER_LS2,
177
178
		     STREAM_MODE,
		     STREAM_HEADER_DETAIL,
179
180
		     HEADER_BEAM_CENTER_X,
		     HEADER_BEAM_CENTER_Y,
181
182
		     HEADER_CHI_INCREMENT,
		     HEADER_CHI_START,
183
		     HEADER_DETECTOR_DISTANCE,
184
185
186
187
188
189
		     HEADER_KAPPA_INCREMENT,
		     HEADER_KAPPA_START,
		     HEADER_OMEGA_INCREMENT,
		     HEADER_OMEGA_START,
		     HEADER_PHI_INCREMENT,
		     HEADER_PHI_START,
190
		     HEADER_WAVELENGTH,
191
		     COMPRESSION_TYPE,
192
193
194
195
196
    };

    Requests(const std::string& address);
    ~Requests();

197
198
199
200
201
202
203
    CommandReq get_command(COMMAND_NAME);
    ParamReq get_param(PARAM_NAME);
    ParamReq get_param(PARAM_NAME,bool&);
    ParamReq get_param(PARAM_NAME,double&);
    ParamReq get_param(PARAM_NAME,int&);
    ParamReq get_param(PARAM_NAME,unsigned int&);
    ParamReq get_param(PARAM_NAME,std::string&);
204
    ParamReq get_param(PARAM_NAME,std::vector<std::string>&);
205

206
207
208
209
210
211
    ParamReq set_param(PARAM_NAME,bool);
    ParamReq set_param(PARAM_NAME,double);
    ParamReq set_param(PARAM_NAME,int);
    ParamReq set_param(PARAM_NAME,unsigned int);
    ParamReq set_param(PARAM_NAME,const std::string&);
    ParamReq set_param(PARAM_NAME,const char*);
212

213
214
215
216
    TransferReq start_transfer(const std::string& src_filename,
			       const std::string& target_path,
			       bool delete_after_transfer = true);
    CurlReq delete_file(const std::string& filename, bool full_url = false);
217
    
218
    void cancel(CurlReq request);
219
  private:
220
    ParamReq _create_get_param(PARAM_NAME);
221
    template <class T>
222
    ParamReq _set_param(PARAM_NAME,const T&);
223
224
225
226
227
228
229
230
231


    typedef std::map<int,std::string> CACHE_TYPE;
    CurlLoop	m_loop;
    CACHE_TYPE	m_cmd_cache_url;
    CACHE_TYPE	m_param_cache_url;
    std::string m_address;
  };
}
232
233

#endif // EIGERAPI_REQUESTS_H