entity_data_bin.h

Go to the documentation of this file.
00001 #ifndef ENTITY_DATA_BIN_CLASS
00002 #define ENTITY_DATA_BIN_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : entity_data_bin                                                   *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2002-$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 <basis/contracts.h>
00019 #include <basis/definitions.h>
00020 #include <basis/mutex.h>
00021 #include <structures/set.h>
00022 
00023 namespace octopi {
00024 
00025 // forward.
00026 class basketcase;
00027 class entity_basket;
00028 class entity_item_hash;
00029 class infoton;
00030 class infoton_list;
00031 class octopus_entity;
00032 class octopus_request_id;
00033 
00035 
00036 class entity_data_bin
00037 {
00038 public:
00039   entity_data_bin(int max_bytes_per_entity);
00041 
00043   virtual ~entity_data_bin();
00044 
00045   DEFINE_CLASS_NAME("entity_data_bin");
00046 
00047   int max_bytes_per_entity() const { return _max_per_ent; }
00048     // reports the maximum size allowed per entity for storage.
00049   void max_bytes_per_entity(int max_bytes_per) { _max_per_ent = max_bytes_per; }
00050     // allows resetting of the storage size allowed per entity.  note that if
00051     // this value is made smaller and the bin is already holding more than
00052     // the new limit, then no additional stores will be allowed until some of
00053     // the data is removed.
00054 
00055   int entities() const;
00056     // returns the number of entities that currently possess storage bins.
00057     // this is a very costly call.
00058 
00059   int items_held() const { return _items_held; }
00060     // returns the number of items held here, if any.  this is a very
00061     // inexpensive call that should be used prior to checking for data.
00062     // it's safe to check this at any time, since it's just an int.  there's
00063     // every likelihood that the number might change by the time one acquires
00064     // the lock on the bin, but if it's zero then that's a good reason to
00065     // avoid looking for data yet.
00066 
00067   bool get_sizes(const octopus_entity &id, int &items, int &bytes);
00068     // finds the storage for "id".  if there is any there, true is returned
00069     // and "items" is set to the number of pending items and "bytes" is set
00070     // to the number of bytes for those items.
00071 
00072   bool add_item(infoton *to_add, const octopus_request_id &id);
00073     // stores an item "to_add" for an entity listed in "id".  if the item
00074     // cannot be stored due to space constraints, false is returned and
00075     // "to_add" is deleted.
00076 
00077   infoton *acquire_for_identifier(const octopus_request_id &id);
00078     // locates an item for the specific "id".  this will generally be a
00079     // response to a previous request.  if no object can be found that matches
00080     // the "id", then NIL is returned.
00081 
00082   infoton *acquire_for_entity(const octopus_entity &requester,
00083           octopus_request_id &id);
00084     // this returns an infoton for the "requester", if any are available.  call
00085     // this function repeatedly to ensure that all available items have
00086     // been provided.  the "original_id" is a copy of the "item_id" that was
00087     // originally passed to evaluate_request().  the returned object must
00088     // eventually be destroyed if non-NIL.
00089 
00090   int acquire_for_entity(const octopus_entity &requester,
00091           infoton_list &items, int maximum_size);
00092     // retrieves up to "maximum_size" in bytes of pending items for the
00093     // "requester" into "items".  the number of items found is returned.
00094 
00095   infoton *acquire_for_any(octopus_request_id &id);
00096     // acquires an infoton for any random entity.  if no items are ready at
00097     // all, then NIL is returned.
00098 
00099   basis::astring text_form() const;
00100     // returns a textual list of what's held here.
00101 
00102   void clean_out_deadwood(int decay_interval = 4 * basis::MINUTE_ms);
00103     // gets rid of any items that haven't been picked up in a timely manner.
00104     // note that this should be called periodically by the controlling object.
00105     // it will not be called automatically.
00106 
00107 private:
00108   entity_item_hash *_table;  // our main storage object.
00109   basis::mutex *_ent_lock;  // protects our structures.
00110   int _action_count;
00111     // used for debugging; tracks how many acquires have occurred since the
00112     // last dump of item count.
00113   int _max_per_ent;  // the maximum size allowed per entity.
00114   int _items_held;  // the number of items in residence.
00115 
00116   friend class monk_the_detective;  // eerie supernatural powers, for testing.
00117 
00118   int scramble_counter();  // counts the number of items used.
00119 
00120   // not available.
00121   entity_data_bin(const entity_data_bin &);
00122   entity_data_bin &operator =(const entity_data_bin &);
00123 };
00124 
00125 } //namespace.
00126 
00127 #endif
00128 
Generated on Sat Jan 28 04:22:42 2012 for hoople2 project by  doxygen 1.6.3