Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / TAO / examples / Content_Server / AMI_Observer / Callback_Handler.cpp
blob643fa24c04e96c152de4a982da05dbef0cc2c0a6
1 // -*- C++ -*-
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)),
11 file_io_ (),
12 callback_ (Web_Server::Callback::_duplicate (client_callback)),
13 ami_handler_ (),
14 chunk_ (new Web_Server::Chunk_Type),
15 chunk_index_ (0),
16 last_chunk_ (0)
18 // Nothing else
21 Callback_Handler::~Callback_Handler ()
23 // Nothing else
26 void
27 Callback_Handler::next_chunk ()
29 if (this->last_chunk_ == 1)
31 this->deactivate ();
33 return;
36 // Allocate a buffer for the file being read.
37 CORBA::Octet *buf =
38 Web_Server::Chunk_Type::allocbuf (BUFSIZ);
40 if (buf == 0)
41 throw CORBA::NO_MEMORY ();
43 ssize_t bytes_read = this->file_io_.recv (buf,
44 BUFSIZ);
45 if (bytes_read == -1)
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;
56 else
58 this->chunk_index_++;
60 ACE_DEBUG ((LM_DEBUG,
61 ACE_TEXT ("Sending chunk %d from %s of size <%d>\n"),
62 this->chunk_index_,
63 this->file_.get_path_name (),
64 bytes_read));
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,
69 bytes_read,
70 buf,
71 1); // The sequence releases memory for us.
74 this->callback_->sendc_next_chunk (this->ami_handler_.in (),
75 this->chunk_.in (),
76 this->last_chunk_);
79 void
80 Callback_Handler::next_chunk_excep
81 (::Messaging::ExceptionHolder *excep_holder)
83 this->last_chunk_ = 1;
85 try
87 this->deactivate ();
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:"));
99 void
100 Callback_Handler::run ()
102 // Open the file to be downloaded
103 this->open_file ();
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.
111 this->next_chunk ();
114 void
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_,
121 this->file_,
123 ACE_Addr::sap_any,
125 O_RDONLY) == -1)
126 // HTTP 1.1 "Not Found"
127 throw Web_Server::Error_Result (404);
130 void
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 ());