Back to index

lightning-sunbird  0.9+nobinonly
tmTransactionManager.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Transaction Manager.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2003
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   John Gaunt <jgaunt@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "plstr.h"
00039 #include <stdlib.h>
00040 #include "tmQueue.h"
00041 #include "tmTransactionManager.h"
00042 #include "tmTransaction.h"
00043 #include "tmUtils.h"
00044 
00046 // Constructors & Destructor & Initializer
00047 
00048 tmTransactionManager::~tmTransactionManager() {
00049 
00050   PRUint32 size = mQueues.Size();
00051   tmQueue *queue = nsnull;
00052   for (PRUint32 index = 0; index < size; index++) {
00053      queue = (tmQueue *)mQueues[index];
00054      if (queue) {
00055        delete queue;
00056      }
00057   }
00058 }
00059 
00060 PRInt32
00061 tmTransactionManager::Init() {
00062   return mQueues.Init();
00063 }
00064 
00066 // public transaction module methods
00067 
00068 void
00069 tmTransactionManager::HandleTransaction(tmTransaction *aTrans) {
00070 
00071   PRUint32 action = aTrans->GetAction();
00072   PRUint32 ownerID = aTrans->GetOwnerID();
00073   tmQueue *queue = nsnull;
00074 
00075   // get the right queue -- attaches do it differently
00076   if (action == TM_ATTACH) {
00077     const char *name = (char*) aTrans->GetMessage(); // is qName for Attaches
00078     queue = GetQueue(name);  
00079     if (!queue) {
00080       PRInt32 index = AddQueue(name);
00081       if (index >= 0)
00082         queue = GetQueue(index); // GetQueue may return nsnull
00083     }
00084   }
00085   else  // all other trans should have a valid queue ID already
00086     queue = GetQueue(aTrans->GetQueueID());
00087 
00088   if (queue) {
00089     // All possible actions should have a case, default is not valid
00090     //   delete trans when done with them, let the queue own the trans
00091     //   that are posted to them.
00092     PRInt32 result = 0;
00093     switch (action) {
00094     case TM_ATTACH:
00095       queue->AttachClient(ownerID);
00096       break;
00097     case TM_POST:
00098       result = queue->PostTransaction(aTrans);
00099       if (result >= 0) // post failed, aTrans cached in a tmQueue
00100         return;
00101       break;
00102     case TM_FLUSH:
00103       queue->FlushQueue(ownerID);
00104       break;
00105     case TM_DETACH:
00106       if (queue->DetachClient(ownerID) == TM_SUCCESS_DELETE_QUEUE) {
00107         // the last client has been removed, remove the queue
00108         RemoveQueue(aTrans->GetQueueID()); // this _could_ be out of bounds
00109       }
00110       break;
00111     default:
00112       PR_NOT_REACHED("bad action in the transaction");
00113     }
00114   }
00115   delete aTrans;
00116 }
00117 
00119 // Protected member functions
00120 
00121 //
00122 // Queue Handling
00123 //
00124 
00125 tmQueue*
00126 tmTransactionManager::GetQueue(const char *aQueueName) {
00127 
00128   PRUint32 size = mQueues.Size();
00129   tmQueue *queue = nsnull;
00130   for (PRUint32 index = 0; index < size; index++) {
00131     queue = (tmQueue*) mQueues[index];
00132     if (queue && strcmp(queue->GetName(), aQueueName) == 0)
00133       return queue;
00134   }
00135   return nsnull;
00136 }
00137 
00138 // if successful the nsresult contains the index of the added queue
00139 PRInt32
00140 tmTransactionManager::AddQueue(const char *aQueueName) {
00141 
00142   tmQueue* queue = new tmQueue();
00143   if (!queue)
00144     return -1;
00145   PRInt32 index = mQueues.Append(queue);
00146   if (index < 0)
00147     delete queue;
00148   else
00149     queue->Init(aQueueName, index, this);
00150   return index;
00151 }
00152 
00153 void
00154 tmTransactionManager::RemoveQueue(PRUint32 aQueueID) {
00155   PR_ASSERT(aQueueID <= mQueues.Size());
00156 
00157   tmQueue *queue = (tmQueue*)mQueues[aQueueID];
00158   if (queue) {
00159     mQueues.RemoveAt(aQueueID);
00160     delete queue;
00161   }
00162 }