summaryrefslogtreecommitdiff
path: root/java/amqp-1-0-common/src/main/java/org/apache/qpid/amqp_1_0/framing/ConnectionHandler.java
blob: 78bed8a71e5a94b7334347b13e48005f6810d657 (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
/*
 *
 * 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.amqp_1_0.framing;

import org.apache.qpid.amqp_1_0.codec.FrameWriter;
import org.apache.qpid.amqp_1_0.codec.ProtocolHandler;
import org.apache.qpid.amqp_1_0.codec.ProtocolHeaderHandler;
import org.apache.qpid.amqp_1_0.codec.ValueHandler;
import org.apache.qpid.amqp_1_0.codec.ValueWriter;
import org.apache.qpid.amqp_1_0.transport.BytesProcessor;
import org.apache.qpid.amqp_1_0.transport.ConnectionEndpoint;

import org.apache.qpid.amqp_1_0.transport.FrameOutputHandler;
import org.apache.qpid.amqp_1_0.type.AmqpErrorException;
import org.apache.qpid.amqp_1_0.type.Binary;
import org.apache.qpid.amqp_1_0.type.transport.Open;
import org.apache.qpid.amqp_1_0.type.Symbol;
import org.apache.qpid.amqp_1_0.type.UnsignedShort;
import org.apache.qpid.amqp_1_0.type.codec.AMQPDescribedTypeRegistry;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConnectionHandler
{
    private final ConnectionEndpoint _connection;
    private ProtocolHandler _delegate;

    private static final Logger FRAME_LOGGER = Logger.getLogger("FRM");
    private static final Logger RAW_LOGGER = Logger.getLogger("RAW");

    public ConnectionHandler(final ConnectionEndpoint connection)
    {
        _connection = connection;
        _delegate = new ProtocolHeaderHandler(connection);
    }


    public boolean parse(ByteBuffer in)
    {

        while(in.hasRemaining() && !isDone())
        {
            _delegate = _delegate.parse(in);

        }
        return isDone();
    }

    public boolean isDone()
    {
        return _delegate.isDone();
    }


    // ----------------------------------------------------------------

    public static class FrameOutput<T> implements FrameOutputHandler<T>, FrameSource
    {

        private static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.wrap(new byte[0]);
        private final BlockingQueue<AMQFrame<T>> _queue = new ArrayBlockingQueue<AMQFrame<T>>(100);
        private ConnectionEndpoint _conn;

        private final AMQFrame<T> _endOfFrameMarker = new AMQFrame<T>(null)
        {
            @Override public short getChannel()
            {
                throw new UnsupportedOperationException();
            }

            @Override public byte getFrameType()
            {
                throw new UnsupportedOperationException();
            }
        };

        private boolean _setForClose;
        private boolean _closed;

        public FrameOutput(final ConnectionEndpoint conn)
        {
            _conn = conn;
        }

        public boolean canSend()
        {
            return _queue.remainingCapacity() != 0;
        }

        public void send(AMQFrame<T> frame)
        {
            send(frame, null);
        }

        public void send(final AMQFrame<T> frame, final ByteBuffer payload)
        {
            synchronized(_conn.getLock())
            {
                try
                {
// TODO HACK - check frame length
                    int size = _conn.getDescribedTypeRegistry()
                            .getValueWriter(frame.getFrameBody()).writeToBuffer(EMPTY_BYTEBUFFER) + 8;

                    if(size > _conn.getMaxFrameSize())
                    {
                        throw new OversizeFrameException(frame, size);
                    }

                    while(!_queue.offer(frame))
                    {
                        _conn.getLock().wait(1000L);

                    }
                    _conn.getLock().notifyAll();
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        }


        public void close()
        {
            synchronized (_conn.getLock())
            {
                if(!_queue.offer(_endOfFrameMarker))
                {
                    _setForClose = true;
                }
                _conn.getLock().notifyAll();
            }
        }

        public AMQFrame<T> getNextFrame(final boolean wait)
        {
            synchronized(_conn.getLock())
            {
                try
                {
                    AMQFrame frame = null;
                    while(!closed() && (frame = _queue.poll()) == null && wait)
                    {
                        _conn.getLock().wait();
                    }

                    if(frame == _endOfFrameMarker)
                    {
                        _closed = true;
                        frame = null;
                    }
                    else if(_setForClose && frame != null)
                    {
                        _setForClose = !_queue.offer(_endOfFrameMarker);
                    }


                    if(frame != null && FRAME_LOGGER.isLoggable(Level.FINE))
                    {
                        FRAME_LOGGER.fine("SEND[" + _conn.getRemoteAddress() + "|" + frame.getChannel() + "] : " + frame.getFrameBody());
                    }

                    _conn.getLock().notifyAll();

                    return frame;
                }
                catch (InterruptedException e)
                {
                    _conn.setClosedForOutput(true);
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    return null;
                }
            }
        }

        public boolean closed()
        {
            return _closed;
        }
    }

    public static interface FrameSource<T>
    {
        AMQFrame<T> getNextFrame(boolean wait);
        boolean closed();
    }


    public static interface BytesSource
    {
        void getBytes(BytesProcessor processor, boolean wait);
        boolean closed();
    }

    public static class FrameToBytesSourceAdapter implements BytesSource
    {

        private final FrameSource _frameSource;
        private final FrameWriter _writer;
        private static final int BUF_SIZE = 1<<16;
        private final byte[] _bytes = new byte[BUF_SIZE];
        private final ByteBuffer _buffer = ByteBuffer.wrap(_bytes);

        public FrameToBytesSourceAdapter(final FrameSource frameSource, ValueWriter.Registry registry)
        {
            _frameSource = frameSource;
            _writer =  new FrameWriter(registry);
        }

        public void getBytes(final BytesProcessor processor, final boolean wait)
        {

            AMQFrame frame;

            if(_buffer.position() == 0 && !_frameSource.closed())
            {
                if(!_writer.isComplete())
                {
                    _writer.writeToBuffer(_buffer);
                }

                while(_buffer.hasRemaining())
                {

                    if((frame = _frameSource.getNextFrame(wait && _buffer.position()==0)) != null)
                    {
                        _writer.setValue(frame);

                        try
                        {
                        _writer.writeToBuffer(_buffer);
                        }
                        catch(RuntimeException e)
                        {
                            e.printStackTrace();
                            throw e;
                        }
                        catch(Error e)
                        {
                            e.printStackTrace();
                            throw e;
                        }


                    }
                    else
                    {
                        break;
                    }
                }
                _buffer.flip();
            }
            if(_buffer.limit() != 0)
            {
                processor.processBytes(_buffer);
                if(_buffer.remaining() == 0)
                {
                    _buffer.clear();
                }
            }
        }

        public boolean closed()
        {
            return _buffer.position() == 0 && _frameSource.closed();
        }
    }


    public static class HeaderBytesSource implements BytesSource
    {

        private final ByteBuffer _buffer;
        private ConnectionEndpoint _conn;

        public HeaderBytesSource(ConnectionEndpoint conn, byte... headerBytes)
        {
            _conn = conn;
            _buffer = ByteBuffer.wrap(headerBytes);
        }

        public void getBytes(final BytesProcessor processor, final boolean wait)
        {
            if(!_conn.closedForOutput())
            {
                processor.processBytes(_buffer);
            }
        }

        public boolean closed()
        {
            return !_conn.closedForOutput() && !_buffer.hasRemaining();
        }
    }

    public static class SequentialBytesSource implements BytesSource
    {
        private Queue<BytesSource> _sources = new LinkedList<BytesSource>();

        public SequentialBytesSource(BytesSource... sources)
        {
            _sources.addAll(Arrays.asList(sources));
        }

        public synchronized void addSource(BytesSource source)
        {
            _sources.add(source);
        }

        public synchronized void getBytes(final BytesProcessor processor, final boolean wait)
        {
            BytesSource src = _sources.peek();
            while (src != null && src.closed())
            {
                _sources.poll();
                src = _sources.peek();
            }

            if(src != null)
            {
                src.getBytes(processor, wait);
            }
        }

        public boolean closed()
        {
            return _sources.isEmpty();
        }
    }


    public static class BytesOutputHandler implements Runnable, BytesProcessor
    {

        private final OutputStream _outputStream;
        private BytesSource _bytesSource;
        private boolean _closed;
        private ConnectionEndpoint _conn;

        public BytesOutputHandler(OutputStream outputStream, BytesSource source, ConnectionEndpoint conn)
            {
                _outputStream = outputStream;
                _bytesSource = source;
                _conn = conn;
            }

            public void run()
            {

                final BytesSource bytesSource = _bytesSource;

                while(!(_closed || bytesSource.closed()))
                {
                    _bytesSource.getBytes(this, true);
                }

            }

        public void processBytes(final ByteBuffer buf)
        {
            try
            {
                if(RAW_LOGGER.isLoggable(Level.FINE))
                {
                    Binary bin = new Binary(buf.array(),buf.arrayOffset()+buf.position(), buf.limit()-buf.position());
                    RAW_LOGGER.fine("SEND["+ _conn.getRemoteAddress() +"] : " + bin.toString());
                }
                _outputStream.write(buf.array(),buf.arrayOffset()+buf.position(), buf.limit()-buf.position());
                buf.position(buf.limit());
            }
            catch (IOException e)
            {
                _closed = true;
                e.printStackTrace();  //TODO
            }
        }
    }


    public static class OutputHandler implements Runnable
    {



        private final OutputStream _outputStream;
        private FrameSource _frameSource;

        private static final int BUF_SIZE = 1<<16;
        private ValueWriter.Registry _registry;


        public OutputHandler(OutputStream outputStream, FrameSource source, ValueWriter.Registry registry)
        {
            _outputStream = outputStream;
            _frameSource = source;
            _registry = registry;
        }

        public void run()
        {
            int i=0;


            try
            {

                byte[] buffer = new byte[BUF_SIZE];
                ByteBuffer buf = ByteBuffer.wrap(buffer);

                buf.put((byte)'A');
                buf.put((byte)'M');
                buf.put((byte)'Q');
                buf.put((byte)'P');
                buf.put((byte) 0);
                buf.put((byte) 1);
                buf.put((byte) 0);
                buf.put((byte) 0);



                final FrameSource frameSource = _frameSource;

                AMQFrame frame;
                FrameWriter writer =  new FrameWriter(_registry);

                while(!frameSource.closed())
                {

                    if(!writer.isComplete())
                    {
                        writer.writeToBuffer(buf);
                    }

                    while(buf.hasRemaining())
                    {

                        if((frame = frameSource.getNextFrame(buf.position()==0)) != null)
                        {
                            writer.setValue(frame);

                            int size = writer.writeToBuffer(buf);

                        }
                        else
                        {
                            break;
                        }
                    }

                    if(buf.limit() != 0)
                    {
                        _outputStream.write(buffer,0, buf.position());
                        buf.clear();
                    }
                }

            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) throws AmqpErrorException
    {
        byte[] buffer = new byte[76];
        ByteBuffer buf = ByteBuffer.wrap(buffer);
        AMQPDescribedTypeRegistry registry = AMQPDescribedTypeRegistry.newInstance()
                .registerTransportLayer()
                .registerMessagingLayer()
                .registerTransactionLayer();

        Open open = new Open();
        // Open(container_id="venture", channel_max=10, hostname="foo", offered_capabilities=[Symbol("one"), Symbol("two"), Symbol("three")])
        open.setContainerId("venture");
        open.setChannelMax(UnsignedShort.valueOf((short) 10));
        open.setHostname("foo");
        open.setOfferedCapabilities(new Symbol[] {Symbol.valueOf("one"),Symbol.valueOf("two"),Symbol.valueOf("three")});

        ValueWriter<Open> writer = registry.getValueWriter(open);

        System.out.println("------ Encode (time in ms for 1 million opens)");
        Long myLong = Long.valueOf(32);
        ValueWriter<Long> writer2 = registry.getValueWriter(myLong);
        Double myDouble = Double.valueOf(3.14159265359);
        ValueWriter<Double> writer3 = registry.getValueWriter(myDouble);
        for(int n = 0; n < 1/*00*/; n++)
        {
            long startTime = System.currentTimeMillis();
            for(int i = 1/*000000*/; i !=0; i--)
            {
                buf.position(0);
                writer.setValue(open);
                writer.writeToBuffer(buf);
                writer2.setValue(myLong);
                writer.writeToBuffer(buf);
                writer3.setValue(myDouble);
                writer3.writeToBuffer(buf);


            }
            long midTime = System.currentTimeMillis();
            System.out.println((midTime - startTime));

        }


        ValueHandler handler = new ValueHandler(registry);
        System.out.println("------ Decode (time in ms for 1 million opens)");
        for(int n = 0; n < 100; n++)
        {
            long startTime = System.currentTimeMillis();
            for(int i = 1000000; i !=0; i--)
            {
                buf.flip();
                handler.parse(buf);
                handler.parse(buf);
                handler.parse(buf);

            }
            long midTime = System.currentTimeMillis();
            System.out.println((midTime - startTime));
        }


    }

}