Asynch_Queued_Message.cpp

Go to the documentation of this file.
00001 #include "tao/Asynch_Queued_Message.h"
00002 #include "tao/debug.h"
00003 #include "tao/ORB_Core.h"
00004 
00005 #include "ace/OS_Memory.h"
00006 #include "ace/OS_NS_string.h"
00007 #include "ace/os_include/sys/os_uio.h"
00008 #include "ace/Log_Msg.h"
00009 #include "ace/Message_Block.h"
00010 #include "ace/Malloc_Base.h"
00011 
00012 
00013 ACE_RCSID (tao,
00014            Asynch_Queued_Message,
00015            "Asynch_Queued_Message.cpp,v 1.21 2006/05/16 12:28:40 jwillemsen Exp")
00016 
00017 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00018 
00019 TAO_Asynch_Queued_Message::TAO_Asynch_Queued_Message (
00020   const ACE_Message_Block *contents,
00021   TAO_ORB_Core *oc,
00022   ACE_Allocator *alloc,
00023   bool is_heap_allocated)
00024   : TAO_Queued_Message (oc, alloc, is_heap_allocated)
00025   , size_ (contents->total_length ())
00026   , offset_ (0)
00027 {
00028   // @@ Use a pool for these guys!!
00029   ACE_NEW (this->buffer_, char[this->size_]);
00030 
00031   size_t copy_offset = 0;
00032   for (const ACE_Message_Block *i = contents;
00033        i != 0;
00034        i = i->cont ())
00035     {
00036       ACE_OS::memcpy (this->buffer_ + copy_offset,
00037                       i->rd_ptr (),
00038                       i->length ());
00039       copy_offset += i->length ();
00040     }
00041 }
00042 
00043 TAO_Asynch_Queued_Message::TAO_Asynch_Queued_Message (char *buf,
00044                                                       TAO_ORB_Core *oc,
00045                                                       size_t size,
00046                                                       ACE_Allocator *alloc)
00047   : TAO_Queued_Message (oc, alloc)
00048   , size_ (size)
00049   , offset_ (0)
00050   , buffer_ (buf)
00051 {
00052 }
00053 
00054 TAO_Asynch_Queued_Message::~TAO_Asynch_Queued_Message (void)
00055 {
00056   // @@ Use a pool for these guys!
00057   delete [] this->buffer_;
00058 }
00059 
00060 size_t
00061 TAO_Asynch_Queued_Message::message_length (void) const
00062 {
00063   return this->size_ - this->offset_;
00064 }
00065 
00066 int
00067 TAO_Asynch_Queued_Message::all_data_sent (void) const
00068 {
00069   return this->size_ == this->offset_;
00070 }
00071 
00072 void
00073 TAO_Asynch_Queued_Message::fill_iov (int iovcnt_max,
00074                                      int &iovcnt,
00075                                      iovec iov[]) const
00076 {
00077   ACE_ASSERT (iovcnt_max > iovcnt);
00078   ACE_UNUSED_ARG (iovcnt_max); // not used if ACE_ASSERT() is empty
00079 
00080   iov[iovcnt].iov_base = this->buffer_ + this->offset_;
00081   iov[iovcnt].iov_len  = static_cast<u_long> (this->size_ - this->offset_);
00082   ++iovcnt;
00083 }
00084 
00085 void
00086 TAO_Asynch_Queued_Message::bytes_transferred (size_t &byte_count)
00087 {
00088   this->state_changed_i (TAO_LF_Event::LFS_ACTIVE);
00089 
00090   size_t const remaining_bytes = this->size_ - this->offset_;
00091   if (byte_count > remaining_bytes)
00092     {
00093       this->offset_ = this->size_;
00094       byte_count -= remaining_bytes;
00095       return;
00096     }
00097   this->offset_ += byte_count;
00098   byte_count = 0;
00099 
00100   if (this->all_data_sent ())
00101     this->state_changed (TAO_LF_Event::LFS_SUCCESS,
00102                          this->orb_core_->leader_follower ());
00103 }
00104 
00105 
00106 TAO_Queued_Message *
00107 TAO_Asynch_Queued_Message::clone (ACE_Allocator *alloc)
00108 {
00109   char *buf = 0;
00110 
00111   // @@todo: Need to use a memory pool. But certain things need to
00112   // change a bit in this class for that. Till then.
00113 
00114   // Just allocate and copy data that needs to be sent, no point
00115   // copying the whole buffer.
00116   size_t const sz = this->size_ - this->offset_;
00117 
00118   ACE_NEW_RETURN (buf,
00119                   char[sz],
00120                   0);
00121 
00122   ACE_OS::memcpy (buf,
00123                   this->buffer_ + this->offset_,
00124                   sz);
00125 
00126   TAO_Asynch_Queued_Message *qm = 0;
00127 
00128   if (alloc)
00129     {
00130       ACE_NEW_MALLOC_RETURN (qm,
00131                              static_cast<TAO_Asynch_Queued_Message *> (
00132                                  alloc->malloc (sizeof (TAO_Asynch_Queued_Message))),
00133                              TAO_Asynch_Queued_Message (buf,
00134                                                         this->orb_core_,
00135                                                         sz,
00136                                                         alloc),
00137                              0);
00138     }
00139   else
00140     {
00141       // No allocator, so use the common heap!
00142       if (TAO_debug_level == 4)
00143         {
00144           // This debug is for testing purposes!
00145           ACE_DEBUG ((LM_DEBUG,
00146                       "TAO (%P|%t) - Asynch_Queued_Message::clone\n"
00147                       "Using global pool for allocation \n"));
00148         }
00149 
00150       ACE_NEW_RETURN (qm,
00151                       TAO_Asynch_Queued_Message (buf,
00152                                                  this->orb_core_,
00153                                                  sz),
00154                       0);
00155     }
00156 
00157   // Set the flag to indicate that <qm> is created on the heap.
00158   if (qm)
00159     qm->is_heap_created_ = true;
00160 
00161   return qm;
00162 }
00163 
00164 void
00165 TAO_Asynch_Queued_Message::destroy (void)
00166 {
00167   if (this->is_heap_created_)
00168     {
00169       // If we have an allocator release the memory to the allocator
00170       // pool.
00171       if (this->allocator_)
00172         {
00173           ACE_DES_FREE (this,
00174                         this->allocator_->free,
00175                         TAO_Asynch_Queued_Message);
00176 
00177         }
00178       else // global release..
00179         {
00180           delete this;
00181         }
00182     }
00183 
00184 }
00185 
00186 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 11:54:07 2006 for TAO by doxygen 1.3.6