ESA JPIP server  0.1
databin_server.h
Go to the documentation of this file.
1 #ifndef _JPIP_DATABIN_SERVER_H_
2 #define _JPIP_DATABIN_SERVER_H_
3 
4 
5 //#define SHOW_TRACES
6 
7 
8 #include <utility>
9 #include "trace.h"
10 #include "data/file.h"
11 #include "jpip/woi.h"
12 #include "jpip/request.h"
13 #include "jpip/cache_model.h"
14 #include "jpip/woi_composer.h"
15 #include "jpip/databin_writer.h"
16 #include "jpeg2000/range.h"
17 #include "jpeg2000/image_index.h"
18 
19 
20 namespace jpip
21 {
22 
23  using namespace std;
24  using namespace data;
25  using namespace jpeg2000;
26 
27 
34  {
35  private:
36  WOI woi;
37  int pending;
39  bool has_woi;
40  bool metareq;
41  bool end_woi_;
44 
49  bool eof;
50 
52  vector<File::Ptr> files;
56 
57  enum {
58  MINIMUM_SPACE = 60
59  };
60 
72  template<int BIN_CLASS> int WriteSegment(int num_codestream, int id, FileSegment segment, int offset = 0, bool last = true)
73  {
74  int cached = cache_model.GetDataBin<BIN_CLASS>(num_codestream, id);
75  int res = 1, seg_cached = cached - offset;
76 
77  if((cached != INT_MAX) && (((int)segment.length - seg_cached) >= 0)) {
78  int free = data_writer.GetFree() - MINIMUM_SPACE;
79 
80  if(free <= 0) {
81  eof = true;
82  res = 0;
83 
84  } else {
85  segment.RemoveFirst(seg_cached);
86 
87  if((int)segment.length > free) {
88  segment.length = free;
89  last = false;
90  res = 0;
91  }
92 
93  data_writer.SetCodestream(num_codestream);
94  data_writer.SetDataBinClass(BIN_CLASS);
95 
96  File::Ptr file_ptr;
97  if(BIN_CLASS == DataBinClass::META_DATA) file_ptr = file;
98  else {
99  int idx = range.GetIndex(num_codestream);
100  if((int)files.size() <= idx) return -1;
101  file_ptr = files[idx];
102  }
103 
104  if(!data_writer.Write(id, cached, *file_ptr, segment, last)) res = -1;
105  else cache_model.AddToDataBin<BIN_CLASS>(num_codestream, id, segment.length, last);
106  }
107  }
108 
109  return res;
110  }
111 
123  int WritePlaceHolder(int num_codestream, int id, const PlaceHolder& place_holder, int offset = 0, bool last = false)
124  {
125  int cached = cache_model.GetDataBin<DataBinClass::META_DATA>(num_codestream, id);
126  int res = 1, seg_cached = cached - offset;
127 
128  if((cached != INT_MAX) && (((int)place_holder.length() - seg_cached) > 0)) {
129  int free = data_writer.GetFree() - MINIMUM_SPACE - place_holder.length();
130 
131  if(free <= 0) {
132  eof = true;
133  res = 0;
134 
135  } else {
136  data_writer.SetCodestream(num_codestream);
137  data_writer.SetDataBinClass(DataBinClass::META_DATA);
138 
139  if(!data_writer.WritePlaceHolder(id, cached, *file, place_holder, last)) res = -1;
140  else cache_model.AddToDataBin<DataBinClass::META_DATA>(num_codestream, id, place_holder.length(), last);
141 
142  }
143  }
144 
145  return res;
146  }
147 
148  public:
153  {
154  pending = 0;
155  has_woi = false;
156  end_woi_ = false;
157  metareq = false;
158  current_idx = 0;
159  }
160 
165  bool end_woi() const
166  {
167  return end_woi_;
168  }
169 
176  bool Reset(const ImageIndex::Ptr image_index);
177 
184  bool SetRequest(const Request& req);
185 
197  bool GenerateChunk(char *buff, int *len, bool *last);
198 
199  virtual ~DataBinServer()
200  {
201  }
202  };
203 
204 }
205 
206 #endif /* _JPIP_DATABIN_SERVER_H_ */
Range range
Range of codestreams.
Definition: databin_server.h:38
Class derived from the HTTP Request class that contains the required code for properly analyzing a JP...
Definition: request.h:31
WOI woi
Current WOI.
Definition: databin_server.h:36
File::Ptr file
Pointer to the associated image file.
Definition: databin_server.h:42
bool metareq
true if the last request contained a "metareq"
Definition: databin_server.h:40
The cache model of a JPIP client is handled using this class.
Definition: cache_model.h:38
DataBinWriter data_writer
Data-bin writer for generating the chunks.
Definition: databin_server.h:55
Class that identifies a WOI (Window Of Interest).
Definition: woi.h:24
Identifies a data segment of a file.
Definition: file_segment.h:20
FileSegment & RemoveFirst(int count)
Removes the first bytes of the segment.
Definition: file_segment.h:80
Class used to generate data-bin segments and write them into a memory buffer.
Definition: databin_writer.h:28
Contains the information of a place-holder.
Definition: place_holder.h:18
bool end_woi() const
Returns true if the end of the WOI has been reached, that is, there is not more associated packets to...
Definition: databin_server.h:165
bool end_woi_
true if the WOI has been completely sent
Definition: databin_server.h:41
virtual ~DataBinServer()
Definition: databin_server.h:199
int pending
Number of pending bytes.
Definition: databin_server.h:37
ImageIndex::Ptr im_index
Pointer to the associated image index.
Definition: databin_server.h:54
DataBinServer()
Initializes the obect.
Definition: databin_server.h:152
Class identifier for meta-data data-bins.
Definition: jpip.h:58
int WritePlaceHolder(int num_codestream, int id, const PlaceHolder &place_holder, int offset=0, bool last=false)
Writes a new place-holder segment, only if it is possible to write it completely. ...
Definition: databin_server.h:123
Represents a range of integer values, defined by two values, first and last, which are assumed to be ...
Definition: range.h:20
WOIComposer woi_composer
WOI composer for determining the packets.
Definition: databin_server.h:53
int length() const
Returns the length of the place-holder.
Definition: place_holder.h:91
list< ImageIndex >::iterator Ptr
Pointer of an object of this class.
Definition: image_index.h:118
Contains the core functionality of a (JPIP) data-bin server, which maintains a cache model and is cap...
Definition: databin_server.h:33
bool eof
true if the end has been reached and the last write operation could not be completed.
Definition: databin_server.h:49
CacheModel cache_model
Cache model of the client.
Definition: databin_server.h:51
vector< File::Ptr > files
List of files (for hyperlinked JPX files)
Definition: databin_server.h:52
By means of this class it is possible to find out the which packets of an image are associated to a W...
Definition: woi_composer.h:27
bool has_woi
true if the last request contained a WOI
Definition: databin_server.h:39
int WriteSegment(int num_codestream, int id, FileSegment segment, int offset=0, bool last=true)
Writes a new data-bin segment or a part of it that is not already cached.
Definition: databin_server.h:72
SHARED_PTR< BaseFile< LockedAccess > > Ptr
Safe pointer to this class.
Definition: file.h:114
uint64_t length
Length of the data segment.
Definition: file_segment.h:24
int current_idx
Current codestream index.
Definition: databin_server.h:43