Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

DCCache.h

00001 /*
00002  * DCCache
00003  *
00004  * Copyright (C) 2001 Barnaby Gray <barnaby@beedesign.co.uk>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef DCCACHE_H
00023 #define DCCACHE_H
00024 
00025 #include <libicq2000/Cache.h>
00026 
00027 #include <sigc++/signal_system.h>
00028 
00029 using SigC::Signal1;
00030 
00031 namespace ICQ2000 {
00032 
00033   /* fd -> DirectClient cache
00034    *
00035    * Where the party is at. Once a DirectClient object is added to the
00036    * cache MM for it is assumed on the cache. Also lookups for a
00037    * Contact are done in linear time now, it was just too much of a
00038    * head-ache maintaining the uin map in Client.
00039    */
00040   class DCCache : public Cache<int, DirectClient*> {
00041    public:
00042     DCCache() { }
00043 
00044     void removeItem(const DCCache::literator& l) {
00045       delete ((*l).getValue());
00046       Cache<int, DirectClient*>::removeItem(l);
00047     }
00048 
00049     void expireItem(const DCCache::literator& l) {
00050       expired.emit( (*l).getValue() );
00051       Cache<int, DirectClient*>::expireItem(l);
00052       /* this will removeItem(..), which'll delete the DirectClient
00053        * (see above).
00054        */
00055     }
00056 
00057     void removeContact(const ContactRef& c) {
00058       literator curr = m_list.begin();
00059       literator next = curr;
00060       while ( curr != m_list.end() ) {
00061         DirectClient *dc = (*curr).getValue();
00062         ++next;
00063         if ( dc->getContact().get() != NULL
00064              /* Direct Connections won't have a contact associated
00065               * with them initially just after having been accepted as
00066               * an incoming connection (we don't know who they are
00067               * yet) - so Contact could be a NULL ref.
00068               */
00069              && dc->getContact()->getUIN() == c->getUIN() ) {
00070           removeItem(curr);
00071         }
00072         curr = next;
00073       }
00074     }
00075 
00076     DirectClient* getByContact(const ContactRef& c)
00077     {
00078       // linear lookup
00079       literator curr = m_list.begin();
00080       while ( curr != m_list.end() ) {
00081         DirectClient *dc = (*curr).getValue();
00082         if (  dc->getContact().get() != NULL
00083              /* Direct Connections won't have a contact associated
00084               * with them initially just after having been accepted as
00085               * an incoming connection (we don't know who they are
00086               * yet) - so Contact could be a NULL ref.
00087               */
00088              && dc->getContact()->getUIN() == c->getUIN() )
00089           return dc; // found it
00090 
00091         ++curr;
00092       }
00093 
00094       return NULL; // not found
00095     }
00096 
00097     void clearoutMessagesPoll() {
00098       literator curr = m_list.begin();
00099       while ( curr != m_list.end() ) {
00100         DirectClient *dc = (*curr).getValue();
00101         dc->clearoutMessagesPoll();
00102         ++curr;
00103       }
00104     }
00105 
00106     Signal1<void,DirectClient*> expired;
00107   };
00108   
00109 }
00110 
00111 #endif

Generated on Fri Apr 26 23:48:14 2002 for libicq2000 by doxygen1.2.15