1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is Mozilla Transaction Manager.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * Netscape Communications Corp.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2003
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * John Gaunt <[email protected]>
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "plstr.h"
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include "tmQueue.h"
|
---|
41 | #include "tmTransactionManager.h"
|
---|
42 | #include "tmTransaction.h"
|
---|
43 | #include "tmUtils.h"
|
---|
44 |
|
---|
45 | ///////////////////////////////////////////////////////////////////////////////
|
---|
46 | // Constructors & Destructor & Initializer
|
---|
47 |
|
---|
48 | tmTransactionManager::~tmTransactionManager() {
|
---|
49 |
|
---|
50 | PRUint32 size = mQueues.Size();
|
---|
51 | tmQueue *queue = nsnull;
|
---|
52 | for (PRUint32 index = 0; index < size; index++) {
|
---|
53 | queue = (tmQueue *)mQueues[index];
|
---|
54 | if (queue) {
|
---|
55 | delete queue;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | PRInt32
|
---|
61 | tmTransactionManager::Init() {
|
---|
62 | return mQueues.Init();
|
---|
63 | }
|
---|
64 |
|
---|
65 | ///////////////////////////////////////////////////////////////////////////////
|
---|
66 | // public transaction module methods
|
---|
67 |
|
---|
68 | void
|
---|
69 | tmTransactionManager::HandleTransaction(tmTransaction *aTrans) {
|
---|
70 |
|
---|
71 | PRUint32 action = aTrans->GetAction();
|
---|
72 | PRUint32 ownerID = aTrans->GetOwnerID();
|
---|
73 | tmQueue *queue = nsnull;
|
---|
74 |
|
---|
75 | // get the right queue -- attaches do it differently
|
---|
76 | if (action == TM_ATTACH) {
|
---|
77 | const char *name = (char*) aTrans->GetMessage(); // is qName for Attaches
|
---|
78 | queue = GetQueue(name);
|
---|
79 | if (!queue) {
|
---|
80 | PRInt32 index = AddQueue(name);
|
---|
81 | if (index >= 0)
|
---|
82 | queue = GetQueue(index); // GetQueue may return nsnull
|
---|
83 | }
|
---|
84 | }
|
---|
85 | else // all other trans should have a valid queue ID already
|
---|
86 | queue = GetQueue(aTrans->GetQueueID());
|
---|
87 |
|
---|
88 | if (queue) {
|
---|
89 | // All possible actions should have a case, default is not valid
|
---|
90 | // delete trans when done with them, let the queue own the trans
|
---|
91 | // that are posted to them.
|
---|
92 | PRInt32 result = 0;
|
---|
93 | switch (action) {
|
---|
94 | case TM_ATTACH:
|
---|
95 | queue->AttachClient(ownerID);
|
---|
96 | break;
|
---|
97 | case TM_POST:
|
---|
98 | result = queue->PostTransaction(aTrans);
|
---|
99 | if (result >= 0) // post failed, aTrans cached in a tmQueue
|
---|
100 | return;
|
---|
101 | break;
|
---|
102 | case TM_FLUSH:
|
---|
103 | queue->FlushQueue(ownerID);
|
---|
104 | break;
|
---|
105 | case TM_DETACH:
|
---|
106 | if (queue->DetachClient(ownerID) == TM_SUCCESS_DELETE_QUEUE) {
|
---|
107 | // the last client has been removed, remove the queue
|
---|
108 | RemoveQueue(aTrans->GetQueueID()); // this _could_ be out of bounds
|
---|
109 | }
|
---|
110 | break;
|
---|
111 | default:
|
---|
112 | PR_NOT_REACHED("bad action in the transaction");
|
---|
113 | }
|
---|
114 | }
|
---|
115 | delete aTrans;
|
---|
116 | }
|
---|
117 |
|
---|
118 | ///////////////////////////////////////////////////////////////////////////////
|
---|
119 | // Protected member functions
|
---|
120 |
|
---|
121 | //
|
---|
122 | // Queue Handling
|
---|
123 | //
|
---|
124 |
|
---|
125 | tmQueue*
|
---|
126 | tmTransactionManager::GetQueue(const char *aQueueName) {
|
---|
127 |
|
---|
128 | PRUint32 size = mQueues.Size();
|
---|
129 | tmQueue *queue = nsnull;
|
---|
130 | for (PRUint32 index = 0; index < size; index++) {
|
---|
131 | queue = (tmQueue*) mQueues[index];
|
---|
132 | if (queue && strcmp(queue->GetName(), aQueueName) == 0)
|
---|
133 | return queue;
|
---|
134 | }
|
---|
135 | return nsnull;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // if successful the nsresult contains the index of the added queue
|
---|
139 | PRInt32
|
---|
140 | tmTransactionManager::AddQueue(const char *aQueueName) {
|
---|
141 |
|
---|
142 | tmQueue* queue = new tmQueue();
|
---|
143 | if (!queue)
|
---|
144 | return -1;
|
---|
145 | PRInt32 index = mQueues.Append(queue);
|
---|
146 | if (index < 0)
|
---|
147 | delete queue;
|
---|
148 | else
|
---|
149 | queue->Init(aQueueName, index, this);
|
---|
150 | return index;
|
---|
151 | }
|
---|
152 |
|
---|
153 | void
|
---|
154 | tmTransactionManager::RemoveQueue(PRUint32 aQueueID) {
|
---|
155 | PR_ASSERT(aQueueID <= mQueues.Size());
|
---|
156 |
|
---|
157 | tmQueue *queue = (tmQueue*)mQueues[aQueueID];
|
---|
158 | if (queue) {
|
---|
159 | mQueues.RemoveAt(aQueueID);
|
---|
160 | delete queue;
|
---|
161 | }
|
---|
162 | }
|
---|