2 // Ossama Othman <ossama@uci.edu>
4 #include "ace/FILE_Connector.h"
5 #include "ace/Log_Msg.h"
6 #include "Callback_Handler.h"
8 Callback_Handler::Callback_Handler (const char *pathname
,
9 Web_Server::Callback_ptr client_callback
)
10 : file_ (ACE_TEXT_CHAR_TO_TCHAR(pathname
)),
12 callback_ (Web_Server::Callback::_duplicate (client_callback
)),
14 chunk_ (new Web_Server::Chunk_Type
),
21 Callback_Handler::~Callback_Handler ()
27 Callback_Handler::next_chunk ()
29 if (this->last_chunk_
== 1)
36 // Allocate a buffer for the file being read.
38 Web_Server::Chunk_Type::allocbuf (BUFSIZ
);
41 throw CORBA::NO_MEMORY ();
43 ssize_t bytes_read
= this->file_io_
.recv (buf
,
47 Web_Server::Chunk_Type::freebuf (buf
);
49 throw CORBA::INTERNAL ();
51 else if (bytes_read
== 0)
53 Web_Server::Chunk_Type::freebuf (buf
);
54 this->last_chunk_
= 1;
61 ACE_TEXT ("Sending chunk %d from %s of size <%d>\n"),
63 this->file_
.get_path_name (),
66 // Place the contents of the buffer into the outgoing octet
67 // sequence. Only replace the amount of data actually read.
68 this->chunk_
->replace (bytes_read
,
71 1); // The sequence releases memory for us.
74 this->callback_
->sendc_next_chunk (this->ami_handler_
.in (),
80 Callback_Handler::next_chunk_excep
81 (::Messaging::ExceptionHolder
*excep_holder
)
83 this->last_chunk_
= 1;
89 excep_holder
->raise_exception ();
91 catch (const CORBA::Exception
& ex
)
93 ex
._tao_print_exception (
94 ACE_TEXT ("Exception occurred during ")
95 ACE_TEXT ("sendc_next_chunk() call:"));
100 Callback_Handler::run ()
102 // Open the file to be downloaded
105 // Activate this Reply Handler.
106 this->ami_handler_
= this->_this ();
108 // Begin the asynchronous invocation. Note that the AMI
109 // "sendc_next_chunk()" call is done within the following call,
110 // since data must first be read into the Chunk.
115 Callback_Handler::open_file ()
117 // Create a temporary file to store the retrieved data.
118 ACE_FILE_Connector connector
;
120 if (connector
.connect (this->file_io_
,
126 // HTTP 1.1 "Not Found"
127 throw Web_Server::Error_Result (404);
131 Callback_Handler::deactivate ()
133 // Close the file that was sent to the client.
134 (void) this->file_io_
.close ();
136 // Get the POA used when activating the Reply Handler object.
137 PortableServer::POA_var poa
= this->_default_POA ();
139 // Get the object ID associated with this servant.
140 PortableServer::ObjectId_var oid
=
141 poa
->servant_to_id (this);
143 // Now deactivate the AMI_CallbackHandler object.
144 poa
->deactivate_object (oid
.in ());