blob: f46c61daa77293154dd39e56dc7bcb02de3a1939 (
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
|
/*
*
* 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.client;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
public final class ChannelToSessionMap
{
private final Map<Integer, AMQSession> _sessionMap = new ConcurrentHashMap<>();
private AtomicInteger _idFactory = new AtomicInteger(0);
private int _maxChannelID;
private int _minChannelID;
public AMQSession get(int channelId)
{
return _sessionMap.get(channelId);
}
public void put(int channelId, AMQSession session)
{
_sessionMap.put(channelId, session);
}
public void remove(int channelId)
{
_sessionMap.remove(channelId);
}
public Collection<AMQSession> values()
{
return new ArrayList<>(_sessionMap.values());
}
public int size()
{
return _sessionMap.size();
}
public void clear()
{
_sessionMap.clear();
}
/*
* Synchronized on whole method so that we don't need to consider the
* increment-then-reset path in too much detail
*/
public synchronized int getNextChannelId()
{
int id = _minChannelID;
boolean done = false;
while (!done)
{
id = _idFactory.getAndIncrement();
if (id == _maxChannelID)
{
//go back to the start
_idFactory.set(_minChannelID);
}
done = (!_sessionMap.keySet().contains(id));
}
return id;
}
public void setMaxChannelID(int maxChannelID)
{
_maxChannelID = maxChannelID;
}
public void setMinChannelID(int minChannelID)
{
_minChannelID = minChannelID;
_idFactory.set(_minChannelID);
}
}
|