summaryrefslogtreecommitdiff
path: root/java/broker-plugins/amqp-1-0-protocol/src/main/java/org/apache/qpid/server/protocol/v1_0/Connection_1_0.java
blob: cae61f9d8004356a18a6539966d28c702691588e (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
/*
 *
 * 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.server.protocol.v1_0;

import java.text.MessageFormat;
import java.util.Collection;
import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint;
import org.apache.qpid.amqp_1_0.transport.ConnectionEventListener;
import org.apache.qpid.amqp_1_0.transport.SessionEndpoint;

import org.apache.qpid.protocol.AMQConstant;
import org.apache.qpid.server.logging.LogSubject;
import org.apache.qpid.server.model.Port;
import org.apache.qpid.server.model.Transport;
import org.apache.qpid.server.protocol.AMQConnectionModel;
import org.apache.qpid.server.protocol.AMQSessionModel;
import org.apache.qpid.server.stats.StatisticsCounter;
import org.apache.qpid.server.util.Action;
import org.apache.qpid.server.virtualhost.VirtualHost;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.apache.qpid.server.logging.subjects.LogSubjectFormat.CONNECTION_FORMAT;

public class Connection_1_0 implements ConnectionEventListener
{

    private final Port _port;
    private VirtualHost _vhost;
    private final Transport _transport;
    private final ConnectionEndpoint _conn;
    private final long _connectionId;
    private final Collection<Session_1_0> _sessions = Collections.synchronizedCollection(new ArrayList<Session_1_0>());
    private final Object _reference = new Object();

    private List<Action<Connection_1_0>> _closeTasks =
            Collections.synchronizedList(new ArrayList<Action<Connection_1_0>>());



    public Connection_1_0(VirtualHost virtualHost,
                          ConnectionEndpoint conn,
                          long connectionId,
                          Port port,
                          Transport transport)
    {
        _vhost = virtualHost;
        _port = port;
        _transport = transport;
        _conn = conn;
        _connectionId = connectionId;
        _vhost.getConnectionRegistry().registerConnection(_model);

    }

    public Object getReference()
    {
        return _reference;
    }

    public void remoteSessionCreation(SessionEndpoint endpoint)
    {
        Session_1_0 session = new Session_1_0(_vhost, this);
        _sessions.add(session);
        endpoint.setSessionEventListener(session);
    }

    void sessionEnded(Session_1_0 session)
    {
        _sessions.remove(session);
    }

    void removeConnectionCloseTask(final Action<Connection_1_0> task)
    {
        _closeTasks.remove( task );
    }

    void addConnectionCloseTask(final Action<Connection_1_0> task)
    {
        _closeTasks.add( task );
    }

    public void closeReceived()
    {
        List<Action<Connection_1_0>> taskCopy;
        synchronized (_closeTasks)
        {
            taskCopy = new ArrayList<Action<Connection_1_0>>(_closeTasks);
        }
        for(Action<Connection_1_0> task : taskCopy)
        {
            task.performAction(this);
        }
        synchronized (_closeTasks)
        {
            _closeTasks.clear();
        }
        _vhost.getConnectionRegistry().deregisterConnection(_model);


    }

    public void closed()
    {
        closeReceived();
    }

    private final AMQConnectionModel _model = new AMQConnectionModel()
    {
        private StatisticsCounter _messageDeliveryStatistics = new StatisticsCounter();
        private StatisticsCounter _messageReceiptStatistics = new StatisticsCounter();
        private StatisticsCounter _dataDeliveryStatistics = new StatisticsCounter();
        private StatisticsCounter _dataReceiptStatistics = new StatisticsCounter();

        private final LogSubject _logSubject = new LogSubject()
        {
            @Override
            public String toLogString()
            {
                return "[" +
                        MessageFormat.format(CONNECTION_FORMAT,
                                             getConnectionId(),
                                             getClientId(),
                                             getRemoteAddressString(),
                                             _vhost.getName())
                     + "] ";

            }
        };

        private volatile boolean _stopped;

        @Override
        public void close(AMQConstant cause, String message)
        {
            _conn.close();
        }

        @Override
        public void block()
        {
            // TODO
        }

        @Override
        public void unblock()
        {
            // TODO
        }

        @Override
        public void closeSession(AMQSessionModel session, AMQConstant cause, String message)
        {
            // TODO
        }

        @Override
        public long getConnectionId()
        {
            return _connectionId;
        }

        @Override
        public List<AMQSessionModel> getSessionModels()
        {
            return new ArrayList<AMQSessionModel>(_sessions);
        }

        @Override
        public LogSubject getLogSubject()
        {
            return _logSubject;
        }

        @Override
        public String getUserName()
        {
            return getPrincipalAsString();
        }

        @Override
        public boolean isSessionNameUnique(byte[] name)
        {
            return true;  // TODO
        }

        @Override
        public String getRemoteAddressString()
        {
            return String.valueOf(_conn.getRemoteAddress());
        }

        @Override
        public String getClientId()
        {
            return _conn.getRemoteContainerId();
        }

        @Override
        public String getClientVersion()
        {
            return "";  //TODO
        }

        @Override
        public String getClientProduct()
        {
            return "";  //TODO
        }

        @Override
        public String getPrincipalAsString()
        {
            return String.valueOf(_conn.getUser());
        }

        @Override
        public long getSessionCountLimit()
        {
            return 0;  // TODO
        }

        @Override
        public long getLastIoTime()
        {
            return 0;  // TODO
        }

        @Override
        public String getVirtualHostName()
        {
            return _vhost == null ? null : _vhost.getName();
        }

        @Override
        public Port getPort()
        {
            return _port;
        }

        @Override
        public Transport getTransport()
        {
            return _transport;
        }

        @Override
        public void stop()
        {
            _stopped = true;
        }

        @Override
        public boolean isStopped()
        {
            return _stopped;
        }

        @Override
        public void initialiseStatistics()
        {
            _messageDeliveryStatistics = new StatisticsCounter("messages-delivered-" + getConnectionId());
            _dataDeliveryStatistics = new StatisticsCounter("data-delivered-" + getConnectionId());
            _messageReceiptStatistics = new StatisticsCounter("messages-received-" + getConnectionId());
            _dataReceiptStatistics = new StatisticsCounter("data-received-" + getConnectionId());
        }

        @Override
        public void registerMessageReceived(long messageSize, long timestamp)
        {
            _messageReceiptStatistics.registerEvent(1L, timestamp);
            _dataReceiptStatistics.registerEvent(messageSize, timestamp);
            _vhost.registerMessageReceived(messageSize,timestamp);

        }

        @Override
        public void registerMessageDelivered(long messageSize)
        {

            _messageDeliveryStatistics.registerEvent(1L);
            _dataDeliveryStatistics.registerEvent(messageSize);
            _vhost.registerMessageDelivered(messageSize);
        }

        @Override
        public StatisticsCounter getMessageDeliveryStatistics()
        {
            return _messageDeliveryStatistics;
        }

        @Override
        public StatisticsCounter getMessageReceiptStatistics()
        {
            return _messageReceiptStatistics;
        }

        @Override
        public StatisticsCounter getDataDeliveryStatistics()
        {
            return _dataDeliveryStatistics;
        }

        @Override
        public StatisticsCounter getDataReceiptStatistics()
        {
            return _dataReceiptStatistics;
        }

        @Override
        public void resetStatistics()
        {
            _dataDeliveryStatistics.reset();
            _dataReceiptStatistics.reset();
            _messageDeliveryStatistics.reset();
            _messageReceiptStatistics.reset();
        }


    };

    AMQConnectionModel getModel()
    {
        return _model;
    }


}