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 ~DCCache()
00044 {
00045 removeAll();
00046 }
00047
00048 void removeItem(const DCCache::literator& l) {
00049 delete ((*l).getValue());
00050 Cache<int, DirectClient*>::removeItem(l);
00051 }
00052
00053 void expireItem(const DCCache::literator& l) {
00054 expired.emit( (*l).getValue() );
00055 Cache<int, DirectClient*>::expireItem(l);
00056 /* this will removeItem(..), which'll delete the DirectClient
00057 * (see above).
00058 */
00059 }
00060
00061 void removeContact(const ContactRef& c) {
00062 literator curr = m_list.begin();
00063 literator next = curr;
00064 while ( curr != m_list.end() ) {
00065 DirectClient *dc = (*curr).getValue();
00066 ++next;
00067 if ( dc->getContact().get() != NULL
00068 /* Direct Connections won't have a contact associated
00069 * with them initially just after having been accepted as
00070 * an incoming connection (we don't know who they are
00071 * yet) - so Contact could be a NULL ref.
00072 */
00073 && dc->getContact()->getUIN() == c->getUIN() ) {
00074 removeItem(curr);
00075 }
00076 curr = next;
00077 }
00078 }
00079
00080 DirectClient* getByContact(const ContactRef& c)
00081 {
00082 // linear lookup
00083 literator curr = m_list.begin();
00084 while ( curr != m_list.end() ) {
00085 DirectClient *dc = (*curr).getValue();
00086 if ( dc->getContact().get() != NULL
00087 /* Direct Connections won't have a contact associated
00088 * with them initially just after having been accepted as
00089 * an incoming connection (we don't know who they are
00090 * yet) - so Contact could be a NULL ref.
00091 */
00092 && dc->getContact()->getUIN() == c->getUIN() )
00093 return dc; // found it
00094
00095 ++curr;
00096 }
00097
00098 return NULL; // not found
00099 }
00100
00101 void clearoutMessagesPoll() {
00102 literator curr = m_list.begin();
00103 while ( curr != m_list.end() ) {
00104 DirectClient *dc = (*curr).getValue();
00105 dc->clearoutMessagesPoll();
00106 ++curr;
00107 }
00108 }
00109
00110 Signal1<void,DirectClient*> expired;
00111 };
00112
00113 }
00114
00115 #endif
1.2.16