Requests.h 6.34 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//###########################################################################
// 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/>.
//###########################################################################
#include <string>
#include <map>
#include <vector>

#include "eigerapi/CurlLoop.h"

namespace eigerapi
{
  class Requests
  {
  public:
    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);
70
71
72
73
      Value get_min(double timeout = CurlLoop::FutureRequest::TIMEOUT,
		    bool lock = true);
      Value get_max(double timeout = CurlLoop::FutureRequest::TIMEOUT,
		    bool lock = true);
74
75
      Value get_allowed_values(double timeout = CurlLoop::FutureRequest::TIMEOUT,
			       bool lock = true);
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    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&);

      virtual void _request_finished();

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

91
92
      Value _get(double timeout,bool lock,const char*);
      
93
94
95
96
97
98
99
100
101
102
103
104
105
      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,
106
107
108
109
	       const std::string& url,
	       const std::string& target_path,
	       bool delete_after_transfer = true,
	       int buffer_write_size = 64 * 1024);
110
111
112
113
114
115
116
117
118
      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;
119
      void*	m_buffer;
120
121
    };

122
123
    enum COMMAND_NAME {INITIALIZE,ARM, DISARM,TRIGGER,CANCEL,ABORT,
		       FILEWRITER_CLEAR};
124
125
126
127
128
129
130
131
132
133
134
    enum PARAM_NAME {TEMP,
		     HUMIDITY,
		     DETECTOR_STATUS,
		     PIXELDEPTH,
		     X_PIXEL_SIZE,
		     Y_PIXEL_SIZE,
		     DETECTOR_WITDH,
		     DETECTOR_HEIGHT,
		     DESCRIPTION,
		     DETECTOR_NUMBER,
		     DETECTOR_READOUT_TIME,
135
136
             DATA_COLLECTION_DATE,
             SOFTWARE_VERSION,
137
138
139
140
141
142
143
144
145
146
		     EXPOSURE,
		     FRAME_TIME,
		     TRIGGER_MODE,
		     COUNTRATE_CORRECTION,
		     FLATFIELD_CORRECTION,
		     EFFICIENCY_CORRECTION,
		     PIXEL_MASK,
		     THRESHOLD_ENERGY,
		     VIRTUAL_PIXEL_CORRECTION,
		     PHOTON_ENERGY,
147
 		     NIMAGES,
148
		     NTRIGGER,
149
150
151
152
153
154
155
156
157
158
		     AUTO_SUMMATION,
		     FILEWRITER_MODE,
		     FILEWRITER_COMPRESSION,
		     FILEWRITER_NAME_PATTERN,
		     NIMAGES_PER_FILE,
		     FILEWRITER_STATUS,
		     FILEWRITER_ERROR,
		     FILEWRITER_TIME,
		     FILEWRITER_BUFFER_FREE,
		     FILEWRITER_LS,
159
		     FILEWRITER_LS2,
160
161
		     STREAM_MODE,
		     STREAM_HEADER_DETAIL,
162
163
		     HEADER_BEAM_CENTER_X,
		     HEADER_BEAM_CENTER_Y,
164
165
		     HEADER_CHI_INCREMENT,
		     HEADER_CHI_START,
166
		     HEADER_DETECTOR_DISTANCE,
167
168
169
170
171
172
		     HEADER_KAPPA_INCREMENT,
		     HEADER_KAPPA_START,
		     HEADER_OMEGA_INCREMENT,
		     HEADER_OMEGA_START,
		     HEADER_PHI_INCREMENT,
		     HEADER_PHI_START,
173
		     HEADER_WAVELENGTH,
174
		     COMPRESSION_TYPE,
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
    };

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

    std::shared_ptr<Command> get_command(COMMAND_NAME);
    std::shared_ptr<Param> get_param(PARAM_NAME);
    std::shared_ptr<Param> get_param(PARAM_NAME,bool&);
    std::shared_ptr<Param> get_param(PARAM_NAME,double&);
    std::shared_ptr<Param> get_param(PARAM_NAME,int&);
    std::shared_ptr<Param> get_param(PARAM_NAME,unsigned int&);
    std::shared_ptr<Param> get_param(PARAM_NAME,std::string&);

    std::shared_ptr<Param> set_param(PARAM_NAME,bool);
    std::shared_ptr<Param> set_param(PARAM_NAME,double);
    std::shared_ptr<Param> set_param(PARAM_NAME,int);
    std::shared_ptr<Param> set_param(PARAM_NAME,unsigned int);
    std::shared_ptr<Param> set_param(PARAM_NAME,const std::string&);
    std::shared_ptr<Param> set_param(PARAM_NAME,const char*);

    std::shared_ptr<Transfer> start_transfer(const std::string& src_filename,
					       const std::string& target_path,
					       bool delete_after_transfer = true);
    std::shared_ptr<CurlLoop::FutureRequest> delete_file(const std::string& filename,
							 bool full_url = false);
    
    void cancel(std::shared_ptr<CurlLoop::FutureRequest> request);
  private:
    std::shared_ptr<Param> _create_get_param(PARAM_NAME);
    template <class T>
    std::shared_ptr<Param> _set_param(PARAM_NAME,const T&);


    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;
  };
}