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 namespace ICQ2000 {
00030
00031 /* fd -> DirectClient cache
00032 *
00033 * Where the party is at. Once a DirectClient object is added to the
00034 * cache MM for it is assumed on the cache. Also lookups for a
00035 * Contact are done in linear time now, it was just too much of a
00036 * head-ache maintaining the uin map in Client.
00037 */
00038 class DCCache : public Cache<int, DirectClient*> {
00039 public:
00040 DCCache() { }
00041 ~DCCache()
00042 {
00043 removeAll();
00044 }
00045
00046 void removeItem(const DCCache::literator& l) {
00047 delete ((*l).getValue());
00048 Cache<int, DirectClient*>::removeItem(l);
00049 }
00050
00051 void expireItem(const DCCache::literator& l) {
00052 expired.emit( (*l).getValue() );
00053 Cache<int, DirectClient*>::expireItem(l);
00054 /* this will removeItem(..), which'll delete the DirectClient
00055 * (see above).
00056 */
00057 }
00058
00059 void removeContact(const ContactRef& c) {
00060 literator curr = m_list.begin();
00061 literator next = curr;
00062 while ( curr != m_list.end() ) {
00063 DirectClient *dc = (*curr).getValue();
00064 ++next;
00065 if ( dc->getContact().get() != NULL
00066 /* Direct Connections won't have a contact associated
00067 * with them initially just after having been accepted as
00068 * an incoming connection (we don't know who they are
00069 * yet) - so Contact could be a NULL ref.
00070 */
00071 && dc->getContact()->getUIN() == c->getUIN() ) {
00072 removeItem(curr);
00073 }
00074 curr = next;
00075 }
00076 }
00077
00078 DirectClient* getByContact(const ContactRef& c)
00079 {
00080 // linear lookup
00081 literator curr = m_list.begin();
00082 while ( curr != m_list.end() ) {
00083 DirectClient *dc = (*curr).getValue();
00084 if ( dc->getContact().get() != NULL
00085 /* Direct Connections won't have a contact associated
00086 * with them initially just after having been accepted as
00087 * an incoming connection (we don't know who they are
00088 * yet) - so Contact could be a NULL ref.
00089 */
00090 && dc->getContact()->getUIN() == c->getUIN() )
00091 return dc; // found it
00092
00093 ++curr;
00094 }
00095
00096 return NULL; // not found
00097 }
00098
00099 void clearoutMessagesPoll() {
00100 literator curr = m_list.begin();
00101 while ( curr != m_list.end() ) {
00102 DirectClient *dc = (*curr).getValue();
00103 dc->clearoutMessagesPoll();
00104 ++curr;
00105 }
00106 }
00107
00108 SigC::Signal1<void,DirectClient*> expired;
00109 };
00110
00111 }
00112
00113 #endif
1.2.16