Back to index

lightning-sunbird  0.9+nobinonly
nsLDAPService.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * 
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the mozilla.org LDAP XPCOM SDK.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2000
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Leif Hedstrom <leif@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsLDAP.h"
00041 #include "ldap.h"
00042 #include "nsString.h"
00043 #include "nsCOMArray.h"
00044 #include "nsHashtable.h"
00045 #include "nsILDAPService.h"
00046 #include "nsILDAPMessage.h"
00047 #include "nsILDAPMessageListener.h"
00048 #include "nsCOMPtr.h"
00049 #include "nsILDAPServer.h"
00050 #include "nsILDAPConnection.h"
00051 #include "nsILDAPMessage.h"
00052 
00053 
00054 // 6a89ae33-7a90-430d-888c-0dede53a951a 
00055 //
00056 #define NS_LDAPSERVICE_CID \
00057 { \
00058   0x6a89ae33, 0x7a90, 0x430d, \
00059   {0x88, 0x8c, 0x0d, 0xed, 0xe5, 0x3a, 0x95, 0x1a} \
00060 }
00061 
00062 // This is a little "helper" class, we use to store information
00063 // related to one Service entry (one LDAP server).
00064 //
00065 class nsLDAPServiceEntry
00066 {
00067   public:
00068     nsLDAPServiceEntry();
00069     virtual ~nsLDAPServiceEntry();
00070     PRBool Init();
00071 
00072     inline PRUint32 GetLeases();
00073     inline void IncrementLeases();
00074     inline PRBool DecrementLeases();
00075 
00076     inline PRTime GetTimestamp();
00077     inline void SetTimestamp();
00078 
00079     inline already_AddRefed<nsILDAPServer> GetServer();
00080     inline PRBool SetServer(nsILDAPServer *aServer);
00081 
00082     inline already_AddRefed<nsILDAPConnection> GetConnection();
00083     inline void SetConnection(nsILDAPConnection *aConnection);
00084 
00085     inline already_AddRefed<nsILDAPMessage> GetMessage();
00086     inline void SetMessage(nsILDAPMessage *aMessage);
00087 
00088     inline already_AddRefed<nsILDAPMessageListener> PopListener();
00089     inline PRBool PushListener(nsILDAPMessageListener *);
00090 
00091     inline PRBool IsRebinding();
00092     inline void SetRebinding(PRBool);
00093 
00094     inline PRBool DeleteEntry();
00095 
00096   protected:
00097     PRUint32 mLeases;         // The number of leases currently granted
00098     PRTime mTimestamp;        // Last time this server was "used"
00099     PRBool mDelete;           // This entry is due for deletion
00100     PRBool mRebinding;        // Keep state if we are rebinding or not
00101 
00102     nsCOMPtr<nsILDAPServer> mServer;
00103     nsCOMPtr<nsILDAPConnection> mConnection;
00104     nsCOMPtr<nsILDAPMessage> mMessage;
00105 
00106     // Array holding all the pending callbacks (listeners) for this entry
00107     nsCOMArray<nsILDAPMessageListener> mListeners;  
00108 };
00109 
00110 // This is the interface we're implementing.
00111 //
00112 class nsLDAPService : public nsILDAPService, public nsILDAPMessageListener
00113 {
00114   public: 
00115     // interface decls
00116     //
00117     NS_DECL_ISUPPORTS
00118     NS_DECL_NSILDAPSERVICE
00119     NS_DECL_NSILDAPMESSAGELISTENER
00120 
00121     // constructor and destructor
00122     //
00123     nsLDAPService();
00124     virtual ~nsLDAPService();
00125     
00126     nsresult Init();
00127 
00128   protected:
00129     nsresult EstablishConnection(nsLDAPServiceEntry *,
00130                                  nsILDAPMessageListener *);
00131 
00132     // kinda like strtok_r, but with iterators.  for use by 
00133     // createFilter
00134     //
00135     char *NextToken(nsReadingIterator<char> & aIter,
00136                     nsReadingIterator<char> & aIterEnd);
00137 
00138     // count how many tokens are in this string; for use by
00139     // createFilter; note that unlike with NextToken, these params
00140     // are copies, not references.
00141     //
00142     PRUint32 CountTokens(nsReadingIterator<char> aIter,
00143                          nsReadingIterator<char> aIterEnd);
00144                    
00145     
00146     PRLock *mLock;              // Lock mechanism
00147     nsHashtable *mServers;      // Hash table holding server entries
00148     nsHashtable *mConnections;  // Hash table holding "reverse"
00149                                 // lookups from connection to server
00150 };