summaryrefslogtreecommitdiff
path: root/qpid/wcf/src/Apache/Qpid/Interop/AmqpSession.cpp
blob: ac7c777d1fcf34e4cd7db08de4d0e11bbb9617a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <windows.h>
#include <msclr\lock.h>
#include <oletx2xa.h>

#include "qpid/client/AsyncSession.h"
#include "qpid/client/SubscriptionManager.h"
#include "qpid/client/Connection.h"
#include "qpid/client/SessionImpl.h"
#include "qpid/client/SessionBase_0_10Access.h"
#include "qpid/client/Message.h"
#include "qpid/framing/MessageTransferBody.h"
#include "qpid/client/Future.h"
#include "qpid/framing/Xid.h"

#include "AmqpConnection.h"
#include "AmqpSession.h"
#include "AmqpMessage.h"
#include "MessageBodyStream.h"
#include "InputLink.h"
#include "OutputLink.h"
#include "QpidMarshal.h"
#include "QpidException.h"
#include "XaTransaction.h"
#include "DtxResourceManager.h"

namespace Apache {
namespace Qpid {
namespace Interop {

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Transactions;
using namespace msclr;

using namespace qpid::client;
using namespace std;


AmqpSession::AmqpSession(AmqpConnection^ conn, qpid::client::Connection* qpidConnectionp) :
    connection(conn),
    sessionp(NULL),
    sessionImplp(NULL),
    subs_mgrp(NULL),
    helperRunning(false),
    openCount(0),
    syncCount(0),
    closing(false),
    dtxEnabled(false)    
{
    bool success = false;
    try {
	sessionp = new qpid::client::AsyncSession;
	*sessionp = qpidConnectionp->newSession();
	subs_mgrp = new SubscriptionManager (*sessionp);
	waiters = gcnew Collections::Generic::List<CompletionWaiter^>();
	sessionLock = waiters;  // waiters convenient and not publicly visible
	openCloseLock = gcnew Object();
	success = true;
    } finally {
        if (!success) {
 	    Cleanup();
	    // TODO: include inner exception information
	    throw gcnew QpidException ("session creation failure");
	}
    }
}


void AmqpSession::Cleanup()
{
    bool connected = connection->IsOpen;

    if (subs_mgrp != NULL) {
	if (connected)
	    subs_mgrp->stop();
	delete subs_mgrp;
	subs_mgrp = NULL;
    }

    if (sessionp != NULL) {
	if (connected) {
	    sessionp->close();
	}
	delete sessionp;
	sessionp = NULL;
	sessionImplp = NULL;
    }
}


static qpid::framing::Xid& getXid(XaTransaction^ xaTx)
{
    return *((qpid::framing::Xid *)xaTx->XidHandle.ToPointer());
}


void AmqpSession::CheckOpen()
{
    if (closing)
	throw gcnew ObjectDisposedException("AmqpSession");
}


// Called by the parent AmqpConnection

void AmqpSession::ConnectionClosed()
{
    lock l(sessionLock);

    if (closing)
	return;

    closing = true;

    if (connection->IsOpen) {
	// send closing handshakes...

	if (dtxEnabled) {
	    // session may close before all its transactions complete, at least force the phase 0 flush
	    if (pendingTransactions->Count > 0) {
		array<XaTransaction^>^ txArray = pendingTransactions->ToArray();
		l.release();
		for each (XaTransaction^ xaTx in txArray) {
			//xaTx->SessionClosing(this);
			xaTx->WaitForCompletion();
		}
		l.acquire();
	    }
	}
	    
	WaitLastSync (%l);
	// Assert pendingTransactions->Count == 0

	if (openXaTransaction != nullptr) {
	    // send final dtxend
	    sessionp->dtxEnd(getXid(openXaTransaction), false, true, false);
	    openXaTransaction = nullptr;
	    openSystemTransaction = nullptr;
	    // this operation will complete by the time Cleanup() returns
	}
    }

    Cleanup();
}

InputLink^ AmqpSession::CreateInputLink(System::String^ sourceQueue)
{
    return CreateInputLink(sourceQueue, true, false, nullptr, nullptr);
}

InputLink^ AmqpSession::CreateInputLink(System::String^ sourceQueue, bool exclusive, bool temporary, 
					System::String^ filterKey, System::String^ exchange)
{
    lock ocl(openCloseLock);
    lock l(sessionLock);
    CheckOpen();

    InputLink^ link = gcnew InputLink (this, sourceQueue, sessionp, subs_mgrp, exclusive, temporary, filterKey, exchange);
    {
	if (openCount == 0) {
	    l.release();
	    connection->NotifyBusy();
	}
	openCount++;
    }
    return link;
}

OutputLink^ AmqpSession::CreateOutputLink(System::String^ targetQueue)
{
    lock ocl(openCloseLock);
    lock l(sessionLock);
    CheckOpen();

    OutputLink^ link = gcnew OutputLink (this, targetQueue);

    if (sessionImplp == NULL) {
	// not needed unless sending messages
	SessionBase_0_10Access sa(*sessionp);
	boost::shared_ptr<SessionImpl> sip = sa.get();
	sessionImplp = sip.get();
    }

    if (openCount == 0) {
	l.release();
	connection->NotifyBusy();
    }
    openCount++;

    return link;
}


// called whenever a child InputLink or OutputLink is closed or finalized
void AmqpSession::NotifyClosed()
{
    lock ocl(openCloseLock);
    openCount--;
    if (openCount == 0) {
	connection->NotifyIdle();
    }
}    


CompletionWaiter^ AmqpSession::SendMessage (System::String^ queue, MessageBodyStream ^mbody, TimeSpan timeout, bool async, AsyncCallback^ callback, Object^ state)
{
    lock l(sessionLock);

    // delimit with session dtx commands depending on the transaction context
    UpdateTransactionState(%l);

    CheckOpen();

    bool syncPending = false;

    // create an AMQP message.transfer command to use with the partial frameset from the MessageBodyStream

    std::string exname = QpidMarshal::ToNative(queue);
    FrameSet *framesetp = (FrameSet *) mbody->GetFrameSet().ToPointer();
    uint8_t acceptMode=1;
    uint8_t acquireMode=0;
    MessageTransferBody mtcmd(ProtocolVersion(0,10), exname, acceptMode, acquireMode);
    // ask for a command completion
    mtcmd.setSync(true);

    //send it

    Future *futurep = NULL;
    try {
	futurep = new Future(sessionImplp->send(mtcmd, *framesetp));

	CompletionWaiter^ waiter = nullptr;
	if (async || (timeout != TimeSpan::MaxValue)) {
	    waiter = gcnew CompletionWaiter(this, timeout, (IntPtr) futurep, callback, state);
	    // waiter is responsible for releasing the Future native resource
	    futurep = NULL;
	    addWaiter(waiter);
	    return waiter;
	}

	// synchronous send with no timeout: no need to involve the asyncHelper thread

	IncrementSyncs();
	syncPending = true;
	l.release();
	internalWaitForCompletion((IntPtr) futurep);
    }
    finally {
	if (syncPending) {
	    if (!l.is_locked())
		l.acquire();
	    DecrementSyncs();
	}
	if (futurep != NULL)
	    delete (futurep);
    }
    return nullptr;
}
    

void AmqpSession::Bind(System::String^ queue, System::String^ exchange, System::String^ filterKey)
{
    lock l(sessionLock);
    CheckOpen();

    sessionp->exchangeBind(arg::queue=QpidMarshal::ToNative(queue),
			       arg::exchange=QpidMarshal::ToNative(exchange),
			       arg::bindingKey=QpidMarshal::ToNative(filterKey));    

}


void AmqpSession::internalWaitForCompletion(IntPtr fp)
{
    Debug::Assert(syncCount > 0, "sync counter mismatch");

    // Qpid native lib call to wait for the command completion
    ((Future *)fp.ToPointer())->wait(*sessionImplp);
}

// call with lock held
void AmqpSession::addWaiter(CompletionWaiter^ waiter)
{
    IncrementSyncs();
    waiters->Add(waiter);
    if (!helperRunning) {
	helperRunning = true;
	ThreadPool::QueueUserWorkItem(gcnew WaitCallback(this, &AmqpSession::asyncHelper));
    }
}


void AmqpSession::removeWaiter(CompletionWaiter^ waiter)
{
    // a waiter can be removed from anywhere in the list if timed out

    lock l(sessionLock);
    int idx = waiters->IndexOf(waiter);
    if (idx == -1) {
	// TODO: assert or log
    }
    else {
	waiters->RemoveAt(idx);
	DecrementSyncs();
    }
}


// process CompletionWaiter list one at a time.

void AmqpSession::asyncHelper(Object ^unused)
{
    lock l(sessionLock);

    while (true) {
	if (waiters->Count == 0) {
	    helperRunning = false;
	    return;
	}

	CompletionWaiter^ waiter = waiters[0];
	l.release();
	// can block, but for short time
	// the waiter removes itself from the list, possibly as the timer thread on timeout
	waiter->Run();
	l.acquire();
    }
}

bool AmqpSession::MessageStop(std::string &name)
{
    lock l(sessionLock);

    if (closing)
	return false;

    sessionp->messageStop(name, true);
    return true;
}

void AmqpSession::AcceptAndComplete(SequenceSet& transfers, bool browsing)
{
    lock l(sessionLock);

    if (!browsing) {
	// delimit with session dtx commands depending on the transaction context
	UpdateTransactionState(%l);
    }

    CheckOpen();

    sessionp->markCompleted(transfers, false);
    if (!browsing)
	sessionp->messageAccept(transfers, false);
}


// call with session lock held

void AmqpSession::UpdateTransactionState(lock^ slock)
{
    Transaction^ currentTx = Transaction::Current;
    if ((currentTx == nullptr) && !dtxEnabled) {
	// no transaction scope and no previous dtx work to monitor
	return;
    }

    if (currentTx == openSystemTransaction) {
	// no change
	return;
    }

    if (!dtxEnabled) {
	// AMQP requires that this be the first dtx-related command on the session
	sessionp->dtxSelect(false);
	dtxEnabled = true;
	pendingTransactions = gcnew Collections::Generic::List<XaTransaction^>();
    }
 
    bool notify = false; // unless the System.Transaction is no longer active
    XaTransaction^ oldXaTx = openXaTransaction;
    if (openSystemTransaction != nullptr) {
	// The application may start a new transaction before the phase0 on rollback
	try {
	    if (openSystemTransaction->TransactionInformation->Status != TransactionStatus::Active) {
		notify = true;
	    }
	} catch (System::ObjectDisposedException^) {
	    notify = true;
	}
    }

    slock->release();
    // only use stack variables until lock re-acquired

    if (notify) {
	// will do call back to all enlisted sessions.  call with session lock released.
	// If NotifyPhase0() wins the race to start phase 0, openXaTransaction will be null
	oldXaTx->NotifyPhase0();
    }
    
    XaTransaction^ newXaTx = nullptr;
    if (currentTx != nullptr) {
	// This must be called with locks released.  The DTC and System.Transactions methods that
	// will be called hold locks that interfere with the ITransactionResourceAsync callbacks.
	newXaTx = DtxResourceManager::GetXaTransaction(this, currentTx);
    }

    slock->acquire();

    if (closing)
	return;

    if (openSystemTransaction != nullptr) {
	// some other transaction has the dtx window open
	// close the XID window, suspend = true... in case it is used again
	sessionp->dtxEnd(getXid(openXaTransaction), false, true, false);
	openSystemTransaction = nullptr;
	openXaTransaction = nullptr;
    }


    // Call enlist with session lock held.  The XaTransaction will call DtxStart before returning.
    if (newXaTx != nullptr) {
	if (!pendingTransactions->Contains(newXaTx)) {
	    pendingTransactions->Add(newXaTx);
	}

	newXaTx->Enlist(this);
    }

    openXaTransaction = newXaTx;
    openSystemTransaction = currentTx;
}


typedef TypedResult<qpid::framing::XaResult> XaResultCompletion;


// send the required closing dtx.End before Phase 1

IntPtr AmqpSession::BeginPhase0Flush(XaTransaction ^xaTx) {
    
    lock l(sessionLock);
    IntPtr completionp = IntPtr::Zero;
    try {
	if (sessionp != NULL) {

	    // proceed even if "closing == true", the phase 0 is part of the transition from closing to closed

	    if (xaTx != openXaTransaction) {
		// a different transaction (or none) is in scope, so xaTx was previously suspended.
		// must re-open it to close it properly
		if (openXaTransaction != nullptr) {
		    // suspend the session's current pending transaction
		    // it wil be reopened in a future enlistment or phase 0 flush.
		    sessionp->dtxEnd(getXid(openXaTransaction), false, true, false);
		}
		// resuming
		sessionp->dtxStart(getXid(xaTx), false, true, false);
	    }
	
	    // the closing (i.e. non-suspended) dtxEnd happens here (exactly once for a given transaction)
	    // set the sync bit since phase0 is a precondition to prepare or abort
	    completionp = (IntPtr) new XaResultCompletion(sessionp->dtxEnd(getXid(xaTx), false, false, true));
	    IncrementSyncs();
	}
    }
    catch (System::Exception^ ) {
	// all the caller wants to know is if completionp is non-null
    }

    openXaTransaction = nullptr;
    openSystemTransaction = nullptr;
    return completionp;
}


void AmqpSession::EndPhase0Flush(XaTransaction ^xaTx, IntPtr intptr) {
    XaResultCompletion *completionp = (XaResultCompletion *) intptr.ToPointer();
    lock l(sessionLock);

    if (completionp != NULL) {
	try {
	    l.release();
	    completionp->wait();
	    pendingTransactions->Remove(xaTx);
	}
	catch (System::Exception^) {
	    // connection closed or network drop
	}
	finally {
	    l.acquire();
	    DecrementSyncs();
	    delete completionp;
	}
    }
}


IntPtr AmqpSession::DtxStart(IntPtr ip, bool join, bool resume) {
    // called with session lock held (as a callback from the Enlist())
    // The XaTransaction knows if this should be the originating dtxStart, or a join/resume
    IntPtr rv = IntPtr::Zero;
    qpid::framing::Xid* xidp = (qpid::framing::Xid *) ip.ToPointer();
    if (join || resume) {
	sessionp->dtxStart(*xidp, join, resume, false);
    }
    else {
	// The XaTransaction needs to track when the first dtxStart completes to safely request a join
	IncrementSyncs();  // caller must use ReleaseCompletion() for corresponding DecrementSyncs
	rv = (IntPtr) new XaResultCompletion(sessionp->dtxStart(*xidp, join, resume, false));
    }

    return rv;
}


IntPtr AmqpSession::DtxPrepare(IntPtr ip) {
    qpid::framing::Xid* xidp = (qpid::framing::Xid *) ip.ToPointer();
    lock l(sessionLock);

    if (closing)
	return IntPtr::Zero;

    IncrementSyncs();  // caller must use ReleaseCompletion() for corresponding DecrementSyncs
    return (IntPtr) new XaResultCompletion(sessionp->dtxPrepare(*xidp, true));
}


IntPtr AmqpSession::DtxCommit(IntPtr ip, bool onePhase) {
    qpid::framing::Xid* xidp = (qpid::framing::Xid *) ip.ToPointer();
    lock l(sessionLock);

    if (closing)
	return IntPtr::Zero;

    IncrementSyncs();  // caller must use ReleaseCompletion() for corresponding DecrementSyncs
    return (IntPtr) new XaResultCompletion(sessionp->dtxCommit(*xidp, onePhase, true));
}


IntPtr AmqpSession::DtxRollback(IntPtr ip) {
    qpid::framing::Xid* xidp = (qpid::framing::Xid *) ip.ToPointer();
    lock l(sessionLock);
    if (closing)
	return IntPtr::Zero;

    IncrementSyncs();  // caller must use ReleaseCompletion() for corresponding DecrementSyncs

    return (IntPtr) new XaResultCompletion(sessionp->dtxRollback(*xidp, true));
}


//call with lock held
void AmqpSession::IncrementSyncs() {
    syncCount++;
}


//call with lock held
void AmqpSession::DecrementSyncs() {
    syncCount--;
    Debug::Assert(syncCount >= 0, "sync counter underrun");
    if (syncCount == 0) {
	if (closeWaitHandle != nullptr) {
	    // now OK to move from closing to closed
	    closeWaitHandle->Set();
	}
    }
}


// call with lock held
void AmqpSession::WaitLastSync(lock ^l) {
    if (syncCount == 0)
	return;
    if (AppDomain::CurrentDomain->IsFinalizingForUnload()) {
	// a wait would be a hang.  No more syncs coming
	return;
    }
    if (closeWaitHandle == nullptr)
	closeWaitHandle = gcnew ManualResetEvent(false);
    l->release();
    closeWaitHandle->WaitOne();
    l->acquire();
}


void AmqpSession::ReleaseCompletion(IntPtr completion) {
    lock l(sessionLock);
    DecrementSyncs();
    delete completion.ToPointer();
}


// Non-exclusive borrowing for a "brief" period.  I.e. several synced
// commands (address resolution)

IntPtr AmqpSession::BorrowNativeSession() {
    lock l(sessionLock);
    if (closing)
	return IntPtr::Zero;

    IncrementSyncs();
    return (IntPtr) sessionp;
}

void AmqpSession::ReturnNativeSession() {
    lock l(sessionLock);
    DecrementSyncs();
}

}}} // namespace Apache::Qpid::Cli