summaryrefslogtreecommitdiff
path: root/java/newclient/src/main/java/org/apache/qpid/nclient/amqp/qpid/QpidAMQPDtxDemarcation.java
blob: eb2cdb4d0101cf750f5ff73e65393f4770af6966 (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
package org.apache.qpid.nclient.amqp.qpid;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.log4j.Logger;
import org.apache.qpid.framing.AMQMethodBody;
import org.apache.qpid.framing.DtxDemarcationEndBody;
import org.apache.qpid.framing.DtxDemarcationEndOkBody;
import org.apache.qpid.framing.DtxDemarcationSelectBody;
import org.apache.qpid.framing.DtxDemarcationSelectOkBody;
import org.apache.qpid.framing.DtxDemarcationStartBody;
import org.apache.qpid.framing.DtxDemarcationStartOkBody;
import org.apache.qpid.nclient.amqp.AMQPDtxDemarcation;
import org.apache.qpid.nclient.amqp.event.AMQPMethodEvent;
import org.apache.qpid.nclient.amqp.event.AMQPMethodListener;
import org.apache.qpid.nclient.amqp.state.AMQPState;
import org.apache.qpid.nclient.amqp.state.AMQPStateChangedEvent;
import org.apache.qpid.nclient.amqp.state.AMQPStateMachine;
import org.apache.qpid.nclient.amqp.state.AMQPStateManager;
import org.apache.qpid.nclient.amqp.state.AMQPStateType;
import org.apache.qpid.nclient.config.ClientConfiguration;
import org.apache.qpid.nclient.core.AMQPException;
import org.apache.qpid.nclient.core.Phase;
import org.apache.qpid.nclient.core.QpidConstants;
import org.apache.qpid.nclient.util.AMQPValidator;

public class QpidAMQPDtxDemarcation extends AMQPStateMachine implements AMQPMethodListener, AMQPDtxDemarcation
{
	private static final Logger _logger = Logger.getLogger(QpidAMQPDtxDemarcation.class);

	// the channelId that will be used for transactions
	private int _channelId;

	private Phase _phase;

	private AMQPState _currentState;

	private AMQPStateManager _stateManager;

	private final AMQPState[] _validEndStates = new AMQPState[]
	{ AMQPState.DTX_STARTED };

	private final AMQPState[] _validStartStates = new AMQPState[]
	{ AMQPState.DTX_NOT_STARTED, AMQPState.DTX_END };

	// The wait period until a server sends a respond
	private long _serverTimeOut = 1000;

	private final Lock _lock = new ReentrantLock();
	
	private final Condition _dtxNotSelected = _lock.newCondition();

	private final Condition _channelNotClosed = _lock.newCondition();
	
	private DtxDemarcationSelectOkBody _dtxDemarcationSelectOkBody;
	
	protected QpidAMQPDtxDemarcation(int channelId, Phase phase, AMQPStateManager stateManager)
	{
		_channelId = channelId;
		_phase = phase;
		_stateManager = stateManager;
		_currentState = AMQPState.DTX_CHANNEL_NOT_SELECTED;
		_serverTimeOut = ClientConfiguration.get().getLong(QpidConstants.SERVER_TIMEOUT_IN_MILLISECONDS);
	}	

	/**
	 * ------------------------------------------- 
	 * API Methods
	 *  --------------------------------------------
	 */
	public DtxDemarcationSelectOkBody select(DtxDemarcationSelectBody dtxDemarcationSelectBody) throws AMQPException
	{
		_lock.lock();
		try
		{
			_dtxDemarcationSelectOkBody = null;
			checkIfValidStateTransition(AMQPState.DTX_CHANNEL_NOT_SELECTED, _currentState, AMQPState.DTX_NOT_STARTED);
			AMQPMethodEvent msg = new AMQPMethodEvent(_channelId, _dtxDemarcationSelectOkBody, QpidConstants.EMPTY_CORRELATION_ID);
			_phase.messageSent(msg);

			//_channelNotOpend.await(_serverTimeOut, TimeUnit.MILLISECONDS);
			_dtxNotSelected.await();
			AMQPValidator.throwExceptionOnNull(_dtxDemarcationSelectOkBody, "The broker didn't send the DtxDemarcationSelectOkBody in time");
			notifyState(AMQPState.CHANNEL_OPENED);
			_currentState = AMQPState.CHANNEL_OPENED;
			return _dtxDemarcationSelectOkBody;
		}
		catch (Exception e)
		{
			throw new AMQPException("Error in dtx.select", e);
		}
		finally
		{
			_lock.unlock();
		}
	}

	public DtxDemarcationStartOkBody start(DtxDemarcationStartBody dtxDemarcationStartBody) throws AMQPException
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	public DtxDemarcationEndOkBody end(DtxDemarcationEndBody dtxDemarcationEndBody) throws AMQPException
	{
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * ------------------------------------------- 
	 * AMQPMethodListener methods
	 * --------------------------------------------
	 */
	public <B extends AMQMethodBody> boolean methodReceived(AMQPMethodEvent<B> evt) throws AMQPException
	{
		return true;
	}

	private void notifyState(AMQPState newState) throws AMQPException
	{
		_stateManager.notifyStateChanged(new AMQPStateChangedEvent(_currentState, newState,AMQPStateType.CHANNEL_STATE));
	}
}