linked_buffer.cpp

Go to the documentation of this file.
00001 #ifndef LINKED_BUFFER_IMPLEMENTATION_FILE
00002 #define LINKED_BUFFER_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : linked_buffer                                                     *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "buffer.h"
00019 #include "buffer_key.h"
00020 #include "linked_buffer.h"
00021 
00022 #include <basis/byte_array.h>
00023 #include <basis/function.h>
00024 #include <basis/mutex.h>
00025 #include <data_struct/bit_vector.h>
00026 #include <data_struct/span_manager.h>
00027 #include <mechanisms/time_stamp.h>
00028 
00029 linked_buffer::linked_buffer(const buffer_key &key, buffer *real_buffer)
00030 : _the_key(new buffer_key(key)),
00031   _the_buffer(real_buffer),
00032   _lock(new mutex)
00033 {
00034   _the_buffer->add_reference(key.connection);
00035 }
00036 
00037 linked_buffer::~linked_buffer()
00038 {
00039   _the_buffer->remove_reference(_the_key->connection);
00040   if (!_the_buffer->references()) {
00041     // wow, we are the lucky winner that gets to clean up.
00042     WHACK(_the_buffer);
00043   }
00044   _the_buffer = NIL;
00045   WHACK(_the_key);
00046   WHACK(_lock);
00047 }
00048 
00049 // this should be safe since we don't allow the buffer that's linked to be
00050 // changed during lifetime of linked_buffer object.
00051 buffer &linked_buffer::access_buffer() { return *_the_buffer; }
00052 
00053 buffer_key linked_buffer::key() const
00054 {
00055   auto_synchronizer l(*_lock);
00056   return *_the_key;
00057 }
00058 
00059 void linked_buffer::set_key(const buffer_key &to_set)
00060 {
00061   auto_synchronizer l(*_lock);
00062   *_the_key = to_set;
00063 }
00064 
00065 istring linked_buffer::text_form() const
00066 {
00067   auto_synchronizer l(*_lock);
00068   return istring(istring::SPRINTF, "[%d] size=%d alive=%s conn_id=%d "
00069       "msg_id=%d whole?=%s mod_time=%s", _the_key->buff_id.raw_id(),
00070       (whole()? full_length() : (estimate()? estimate() : -1) ),
00071       (alive()? "true" : "false"), _the_key->connection, _the_key->msg_id,
00072       (whole()? "true":"false"),
00073       _the_buffer->last_add().text_form().observe());
00074   // note that -1 is used here as a kludgy way of saying "has not been
00075   // determined yet".
00076 }
00077 
00078 istring linked_buffer::detailed_form(bool really_detailed) const
00079 {
00080   auto_synchronizer l(*_lock);
00081   istring to_return(text_form() + istring(" "));
00082   span_manager buff_status = _the_buffer->buffer_status();
00083   int_array pack_sizes = _the_buffer->packet_sizes();
00084   to_return += buff_status.print_received_list();
00085   if (really_detailed) {
00086     to_return += " lengths: ";
00087     for (int i = 0; i < packets(); i++) {
00088       if (buff_status.vector().on(i))
00089         to_return += istring(istring::SPRINTF, "[%d=%d] ", i, pack_sizes[i]);
00090       else to_return += istring(istring::SPRINTF, "[%d=empty] ", i);
00091     }
00092   }
00093   return to_return;
00094 }
00095 
00096 // re-routing bank.
00097 bool linked_buffer::alive() const { return _the_buffer->alive(); }
00098 int linked_buffer::packets() const  { return _the_buffer->packets(); }
00099 outcome linked_buffer::store_packet(int packet_number, const byte_array &buffer)
00100 { return _the_buffer->store_packet(packet_number, buffer); }
00101 byte_array linked_buffer::get_packet(int packet_number) const
00102 { return _the_buffer->get_packet(packet_number); }
00103 int linked_buffer::packet_length(int packet_number) const
00104 { return _the_buffer->packet_length(packet_number); }
00105 bool linked_buffer::whole() const { return _the_buffer->whole(); }
00106 bool linked_buffer::empty() const { return _the_buffer->empty(); }
00107 int linked_buffer::full_length(int offset) const
00108 { return _the_buffer->full_length(offset); }
00109 int linked_buffer::guess_size(int offset)
00110 { return _the_buffer->guess_size(offset); }
00111 bool linked_buffer::did_estimate() const
00112 { return _the_buffer->did_estimate(); }
00113 void linked_buffer::reset_estimate() { _the_buffer->reset_estimate(); }
00114 int linked_buffer::estimate() const { return _the_buffer->estimate(); }
00115 outcome linked_buffer::dump(byte_array &storage_location, int offset)
00116 { return _the_buffer->dump(storage_location, offset); }
00117 void linked_buffer::release(int packet_number, release_style status_handling)
00118 { _the_buffer->release(packet_number, status_handling); }
00119 void linked_buffer::release() { _the_buffer->release(); }
00120 time_stamp linked_buffer::last_add() const { return _the_buffer->last_add(); }
00121 void linked_buffer::set_last_add(const time_stamp &new_last_add)
00122 { _the_buffer->set_last_add(new_last_add); }
00123 bool linked_buffer::owner_alerted() const
00124 { return _the_buffer->owner_alerted(); }
00125 void linked_buffer::set_alerted(bool owner_alerted)
00126 { _the_buffer->set_alerted(owner_alerted); }
00127 void linked_buffer::get_attachment(byte_array &storage) const
00128 { _the_buffer->get_attachment(storage); }
00129 void linked_buffer::set_attachment(const byte_array &new_attachment)
00130 { _the_buffer->set_attachment(new_attachment); }
00131 span_manager linked_buffer::buffer_status() const
00132 { return _the_buffer->buffer_status(); }
00133 int_array linked_buffer::packet_sizes() const
00134 { return _the_buffer->packet_sizes(); }
00135 
00136 
00137 #endif //LINKED_BUFFER_IMPLEMENTATION_FILE
00138 

Generated on Fri Nov 28 04:28:53 2008 for HOOPLE Libraries by  doxygen 1.5.1