summaryrefslogtreecommitdiff
path: root/java/newclient/src/main/java/org/apache/qpid/nclient/execution/ResponseManager.java
blob: 97d9576a4ee3f88d48e0216569610b5d29124724 (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
/*
 *
 * 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.
 *
 */
package org.apache.qpid.nclient.execution;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;
import org.apache.qpid.framing.AMQFrame;
import org.apache.qpid.framing.AMQMethodBody;
import org.apache.qpid.framing.AMQRequestBody;
import org.apache.qpid.framing.AMQResponseBody;
import org.apache.qpid.framing.RequestResponseMappingException;
import org.apache.qpid.nclient.config.ClientConfiguration;
import org.apache.qpid.nclient.core.QpidConstants;
import org.apache.qpid.nclient.model.AMQPMethodEvent;

public class ResponseManager
{
    private static final Logger logger = Logger.getLogger(ResponseManager.class);

    private int channel;
    
    /**
     * Used for logging and debugging only - allows the context of this instance
     * to be known.
     */
    private boolean serverFlag;
    private long connectionId;

    private int maxAccumulatedResponses = 20; // Default

    /**
     * Request and response frames must have a requestID and responseID which
     * indepenedently increment from 0 on a per-channel basis. These are the
     * counters, and contain the value of the next (not yet used) frame.
     */
    private long responseIdCount;

    /**
     * These keep track of the last requestId and responseId to be received.
     */
    private long lastReceivedRequestId;

    /**
     * Last requestID sent in a response (for batching)
     */
    private long lastSentRequestId;

    private class ResponseStatus implements Comparable<ResponseStatus>
    {
        private long requestId;
        private AMQMethodBody responseMethodBody;

        public ResponseStatus(long requestId)
        {
            this.requestId = requestId;
            responseMethodBody = null;
        }

        public int compareTo(ResponseStatus o)
        {
            return (int)(requestId - o.requestId);
        }
        
        public String toString()
        {
            // Need to define this
        	return "";
        }
    }

    private ConcurrentHashMap<Long, ResponseStatus> responseMap;

    public ResponseManager(long connectionId, int channel, boolean serverFlag)
    {
        this.channel = channel;
        this.serverFlag = serverFlag;
        this.connectionId = connectionId;
        responseIdCount = 1L;
        lastReceivedRequestId = 0L;
        maxAccumulatedResponses = ClientConfiguration.get().getInt(QpidConstants.MAX_ACCUMILATED_RESPONSES);
        responseMap = new ConcurrentHashMap<Long, ResponseStatus>();
    }

    // *** Functions to handle an incoming request ***

    public AMQPMethodEvent requestReceived(AMQRequestBody requestBody) throws Exception
    {
        long requestId = requestBody.getRequestId();
        if (logger.isDebugEnabled())
        {
            logger.debug((serverFlag ? "SRV[" : "CLI[") + connectionId + "," + channel + "] RX REQ: " + 
                requestBody + "; " + requestBody.getMethodPayload());
        }
        long responseMark = requestBody.getResponseMark();
        lastReceivedRequestId = requestId;
        responseMap.put(requestId, new ResponseStatus(requestId));
        
        AMQPMethodEvent methodEvent = new AMQPMethodEvent(channel,
            requestBody.getMethodPayload(), requestId);
        
        return methodEvent;
    }

    public List<AMQFrame> sendResponse(AMQPMethodEvent evt)
        throws RequestResponseMappingException
    {
    	long requestId = evt.getCorrelationId();
    	AMQMethodBody responseMethodBody = evt.getMethod();
    	
        if (logger.isDebugEnabled())
        {
            logger.debug((serverFlag ? "SRV[" : "CLI[") + connectionId + "," + channel +
                "] TX RES: Res[# " + requestId + "]; " + responseMethodBody);
        }
        
        ResponseStatus responseStatus = responseMap.get(requestId);
        if (responseStatus == null)
        {
            throw new RequestResponseMappingException(requestId,
                "Failed to locate requestId " + requestId + " in responseMap." + responseMap);
        }
        if (responseStatus.responseMethodBody != null)
        {
            throw new RequestResponseMappingException(requestId, "RequestId " +
                requestId + " already has a response in responseMap.");
        }       
        responseStatus.responseMethodBody = responseMethodBody;
        return doBatches();
    }

    // *** Management functions ***

    /**
     * Sends batched responses - i.e. all those members of responseMap that have
     * received a response.
     */
    public synchronized List<AMQFrame> doBatches()
    {
        long startRequestId = 0;
        int numAdditionalRequestIds = 0;
        Class responseMethodBodyClass = null;
        List<AMQFrame> frames = new ArrayList<AMQFrame>();
        Iterator<Long> lItr = responseMap.keySet().iterator();
        while (lItr.hasNext())
        {
            long requestId = lItr.next();
            ResponseStatus responseStatus = responseMap.get(requestId);
            if (responseStatus.responseMethodBody != null)
            {
               frames.add(sendResponseBatchFrame(requestId, 0, responseStatus.responseMethodBody));
               lItr.remove();
            }
        }
        
        return frames;
    }

    /**
     * Total number of entries in the responseMap - including both those that
     * are outstanding (i.e. no response has been received) and those that are
     * batched (those for which responses have been received but have not yet
     * been collected together and sent).
     */
    public int responsesMapSize()
    {
        return responseMap.size();
    }

    /**
     * As the responseMap may contain both outstanding responses (those with
     * ResponseStatus.responseMethodBody still null) and responses waiting to
     * be batched (those with ResponseStatus.responseMethodBody not null), we
     * need to count only those in the map with responseMethodBody null.
     */
    public int outstandingResponses()
    {
        int cnt = 0;
        for (Long requestId : responseMap.keySet())
        {
            if (responseMap.get(requestId).responseMethodBody == null)
                cnt++;
        }
        return cnt;
    }

    /**
     * As the responseMap may contain both outstanding responses (those with
     * ResponseStatus.responseMethodBody still null) and responses waiting to
     * be batched (those with ResponseStatus.responseMethodBody not null), we
     * need to count only those in the map with responseMethodBody not null.
     */
    public int batchedResponses()
    {
        int cnt = 0;
        for (Long requestId : responseMap.keySet())
        {
            if (responseMap.get(requestId).responseMethodBody != null)
                cnt++;
        }
        return cnt;
    }

    // *** Private helper functions ***

    private long getNextResponseId()
    {
        return responseIdCount++;
    }

    private AMQFrame sendResponseBatchFrame(long firstRequestId, int numAdditionalRequests,
        AMQMethodBody responseMethodBody)
    {
        long responseId = getNextResponseId(); // Get new response ID
        AMQFrame responseFrame = AMQResponseBody.createAMQFrame(channel, responseId,
            firstRequestId, numAdditionalRequests, responseMethodBody);
        return responseFrame;
    }
}