bes  Updated for version 3.19.1
BESStreamResponseHandler.cc
1 // BESStreamResponseHandler.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundatiion; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <unistd.h>
34 
35 #include <cerrno>
36 #include <iostream>
37 #include <fstream>
38 #include <string>
39 #include <cstring>
40 
41 using std::ifstream;
42 using std::ios;
43 using std::endl;
44 using std::string;
45 
46 #include "BESStreamResponseHandler.h"
47 #include "BESRequestHandlerList.h"
48 #include "BESNotFoundError.h"
49 #include "BESInternalError.h"
50 #include "BESDataNames.h"
51 #include "BESContainer.h"
52 
53 #define BES_STREAM_BUFFER_SIZE 4096
54 
55 BESStreamResponseHandler::BESStreamResponseHandler(const string &name) :
56  BESResponseHandler(name)
57 {
58 }
59 
60 BESStreamResponseHandler::~BESStreamResponseHandler()
61 {
62 }
63 
64 extern volatile int bes_timeout; // defined in BESInterface. jhrg 1/24/17
65 
77 {
78  _response = 0;
79 
80  // Hack. We put this here because the bes timeout period should not
81  // include the time it takes to send data for a file transfer response.
82  //
83  // An alternative would be to implement a BESTransmitter for the "get stream"
84  // operation and have that run from the call to BESInterface::transmist_data().
85  // pcw talks about that below.
86  // jhrg 1/24/17
87  if (bes_timeout != 0) {
88  bes_timeout = 0;
89  alarm(bes_timeout);
90  }
91 
92  // What if there is a special way to stream back a data file?
93  // Should we pass this off to the request handlers and put
94  // this code into a different class for reuse? For now
95  // just keep it here. pcw 10/11/06
96 
97  // I thought about putting this in the transmit method below
98  // but decided that this is like executing a non-buffered
99  // request, so kept it here. Plus the idea expressed above
100  // led me to leave the code in the execute method.
101  // pcw 10/11/06
102  if (dhi.containers.size() != 1) {
103  string err = (string) "Unable to stream file: " + "no container specified";
104  throw BESInternalError(err, __FILE__, __LINE__);
105  }
106 
107  dhi.first_container();
108  BESContainer *container = dhi.container;
109  string filename = container->access();
110  if (filename.empty()) {
111  string err = (string) "Unable to stream file: " + "filename not specified";
112  throw BESInternalError(err, __FILE__, __LINE__);
113  }
114 
115  int bytes = 0;
116  ifstream os;
117  os.open(filename.c_str(), ios::in);
118  int myerrno = errno;
119  if (!os) {
120  string serr = (string) "Unable to stream file: " + "cannot open file " + filename + ": ";
121  char *err = strerror(myerrno);
122  if (err)
123  serr += err;
124  else
125  serr += "Unknown error";
126 
127  throw BESNotFoundError(err, __FILE__, __LINE__);
128  }
129 
130  int nbytes;
131  char block[BES_STREAM_BUFFER_SIZE];
132  os.read(block, sizeof block);
133  nbytes = os.gcount();
134  while (nbytes) {
135  bytes += nbytes;
136  dhi.get_output_stream().write((char*) block, nbytes);
137 
138  os.read(block, sizeof block);
139  nbytes = os.gcount();
140  }
141 
142  os.close();
143 }
144 
153 {
154  // The Data is transmitted when it is read, dumped to stdout, so there is nothing
155  // to transmit here.
156 }
157 
164 void BESStreamResponseHandler::dump(ostream &strm) const
165 {
166  strm << BESIndent::LMarg << "BESStreamResponseHandler::dump - (" << (void *) this << ")" << endl;
167  BESIndent::Indent();
169  BESIndent::UnIndent();
170 }
171 
173 BESStreamResponseHandler::BESStreamResponseBuilder(const string &name)
174 {
175  return new BESStreamResponseHandler(name);
176 }
177 
error thrown if the resource requested cannot be found
exception thrown if inernal error encountered
virtual void execute(BESDataHandlerInterface &r)
executes the command &#39;get file <filename>;&#39; by streaming the specified file
virtual string access()=0
returns the true name of this container
virtual void dump(ostream &strm) const
dumps information about this object
handler object that knows how to create a specific response object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &r)
transmit the file, streaming it back to the client
Structure storing information used by the BES to handle the request.
virtual void dump(ostream &strm) const
dumps information about this object
void first_container()
set the container pointer to the first container in the containers list
A container is something that holds data. I.E. a netcdf file or a database entry. ...
Definition: BESContainer.h:60
BESContainer * container
pointer to current container in this interface