Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 71ed9bcb284428dc4f066f3caefff5a563178099 (plain) (blame)
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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.internal.http;

import org.eclipse.net4j.buffer.IBuffer;
import org.eclipse.net4j.channel.IChannel;
import org.eclipse.net4j.connector.ConnectorException;
import org.eclipse.net4j.http.IHTTPConnector;
import org.eclipse.net4j.internal.http.bundle.OM;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.eclipse.net4j.protocol.IProtocol;
import org.eclipse.net4j.util.io.ExtendedDataInputStream;
import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
import org.eclipse.net4j.util.security.INegotiationContext;

import org.eclipse.internal.net4j.buffer.Buffer;
import org.eclipse.internal.net4j.channel.Channel;
import org.eclipse.internal.net4j.connector.Connector;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author Eike Stepper
 */
public abstract class HTTPConnector extends Connector implements IHTTPConnector
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, HTTPConnector.class);

  private static final byte OPERATION_NONE = 0;

  private static final byte OPERATION_OPEN = 1;

  private static final byte OPERATION_OPEN_ACK = 2;

  private static final byte OPERATION_CLOSE = 3;

  private static final byte OPERATION_BUFFER = 4;

  private String connectorID;

  private transient Queue<ChannelOperation> outputOperations = new ConcurrentLinkedQueue<ChannelOperation>();

  private transient long lastTraffic;

  public HTTPConnector()
  {
    markLastTraffic();
  }

  public String getConnectorID()
  {
    return connectorID;
  }

  public void setConnectorID(String connectorID)
  {
    this.connectorID = connectorID;
  }

  public Queue<ChannelOperation> getOutputQueue()
  {
    return outputOperations;
  }

  public long getLastTraffic()
  {
    return lastTraffic;
  }

  private void markLastTraffic()
  {
    lastTraffic = System.currentTimeMillis();
  }

  public void multiplexChannel(IChannel channel)
  {
    IBuffer buffer;
    long outputOperationCount;

    HTTPChannel httpChannel = (HTTPChannel)channel;
    synchronized (httpChannel)
    {
      Queue<IBuffer> channelQueue = httpChannel.getSendQueue();
      buffer = channelQueue.poll();
      outputOperationCount = httpChannel.getOutputOperationCount();
      httpChannel.increaseOutputOperationCount();
    }

    if (TRACER.isEnabled())
    {
      TRACER.format("Multiplexing {0} (count={1})", ((Buffer)buffer).formatContent(true), outputOperationCount);
    }

    outputOperations.add(new BufferChannelOperation(httpChannel.getChannelIndex(), outputOperationCount, buffer));
  }

  /**
   * Writes operations from the {@link #outputOperations} to the passed stream. After each written operation
   * {@link #writeMoreOperations()} is asked whether to send more operations.
   * 
   * @return <code>true</code> if more operations are in the {@link #outputOperations}, <code>false</code> otherwise.
   */
  public boolean writeOutputOperations(ExtendedDataOutputStream out) throws IOException
  {
    do
    {
      ChannelOperation operation = outputOperations.poll();
      if (operation == null && pollAgain())
      {
        operation = outputOperations.poll();
      }

      if (operation == null)
      {
        break;
      }

      operation.write(out);
      markLastTraffic();
    } while (writeMoreOperations());

    out.writeByte(OPERATION_NONE);
    return !outputOperations.isEmpty();
  }

  public void readInputOperations(ExtendedDataInputStream in) throws IOException
  {
    for (;;)
    {
      ChannelOperation operation;
      byte code = in.readByte();
      switch (code)
      {
      case OPERATION_OPEN:
        operation = new OpenChannelOperation(in);
        break;

      case OPERATION_OPEN_ACK:
        operation = new OpenAckChannelOperation(in);
        break;

      case OPERATION_CLOSE:
        operation = new CloseChannelOperation(in);
        break;

      case OPERATION_BUFFER:
        operation = new BufferChannelOperation(in);
        break;

      case OPERATION_NONE:
        return;

      default:
        throw new IOException("Invalid operation code: " + code);
      }

      markLastTraffic();
      operation.execute();
    }
  }

  @Override
  protected INegotiationContext createNegotiationContext()
  {
    throw new UnsupportedOperationException();
  }

  @Override
  protected Channel createChannelInstance()
  {
    return new HTTPChannel();
  }

  @Override
  protected void registerChannelWithPeer(final int channelID, final short channelIndex, final IProtocol protocol)
      throws ConnectorException
  {
    ChannelOperation operation = new OpenChannelOperation(channelIndex, channelID, protocol.getType());
    outputOperations.add(operation);

    HTTPChannel channel = (HTTPChannel)getChannel(channelIndex);
    channel.waitForOpenAck();
  }

  @Override
  public boolean removeChannel(IChannel channel)
  {
    if (super.removeChannel(channel))
    {
      HTTPChannel httpChannel = (HTTPChannel)channel;
      if (!httpChannel.isInverseRemoved())
      {
        ChannelOperation operation = new CloseChannelOperation(httpChannel);
        outputOperations.add(operation);
      }

      return true;
    }

    return false;
  }

  protected boolean pollAgain()
  {
    return false;
  }

  protected boolean writeMoreOperations()
  {
    return true;
  }

  /**
   * @author Eike Stepper
   */
  public abstract class ChannelOperation
  {
    private short channelIndex;

    private long operationCount;

    public ChannelOperation(short channelIndex, long operationCount)
    {
      this.channelIndex = channelIndex;
      this.operationCount = operationCount;
    }

    public ChannelOperation(ExtendedDataInputStream in) throws IOException
    {
      System.out.println("READING " + getClass().getName());
      channelIndex = in.readShort();
      operationCount = in.readLong();
    }

    public void write(ExtendedDataOutputStream out) throws IOException
    {
      System.out.println("WRITING " + getClass().getName());
      out.writeByte(getOperation());
      out.writeShort(channelIndex);
      out.writeLong(operationCount);
    }

    public abstract byte getOperation();

    public short getChannelIndex()
    {
      return channelIndex;
    }

    public long getOperationCount()
    {
      return operationCount;
    }

    public void execute()
    {
      HTTPChannel channel = (HTTPChannel)getChannel(getChannelIndex());
      long operationCount = getOperationCount();
      synchronized (channel)
      {
        // Execute preceding operations if necessary
        while (operationCount < channel.getInputOperationCount())
        {
          ChannelOperation operation = channel.getQuarantinedInputOperation(channel.getInputOperationCount());
          if (operation != null)
          {
            operation.doEexecute(channel);
            channel.increaseInputOperationCount();
          }
          else
          {
            break;
          }
        }

        if (operationCount == channel.getInputOperationCount())
        {
          // Execute operation if possible
          doEexecute(channel);
          channel.increaseInputOperationCount();

          // Execute following operations if possible
          for (;;)
          {
            ChannelOperation operation = channel.getQuarantinedInputOperation(++operationCount);
            if (operation != null)
            {
              operation.doEexecute(channel);
              channel.increaseInputOperationCount();
            }
            else
            {
              break;
            }
          }
        }
        else
        {
          channel.quarantineInputOperation(operationCount, this);
        }
      }
    }

    public abstract void doEexecute(HTTPChannel channel);

    public void dispose()
    {
    }
  }

  /**
   * @author Eike Stepper
   */
  private final class OpenChannelOperation extends ChannelOperation
  {
    private int channelID;

    private String protocolID;

    public OpenChannelOperation(short channelIndex, int channelID, String protocolID)
    {
      super(channelIndex, 0);
      this.channelID = channelID;
      this.protocolID = protocolID;
    }

    public OpenChannelOperation(ExtendedDataInputStream in) throws IOException
    {
      super(in);
      channelID = in.readInt();
      protocolID = in.readString();
    }

    @Override
    public void write(ExtendedDataOutputStream out) throws IOException
    {
      super.write(out);
      out.writeInt(channelID);
      out.writeString(protocolID);
    }

    @Override
    public byte getOperation()
    {
      return OPERATION_OPEN;
    }

    public int getChannelID()
    {
      return channelID;
    }

    public String getProtocolID()
    {
      return protocolID;
    }

    @Override
    public void execute()
    {
      HTTPChannel channel = (HTTPChannel)createChannel(channelID, getChannelIndex(), protocolID);
      if (channel == null)
      {
        throw new IllegalStateException("Could not open channel");
      }

      channel.increaseInputOperationCount();
      doEexecute(channel);
    }

    @Override
    public void doEexecute(HTTPChannel channel)
    {
      boolean success = false;
      try
      {
        channel.activate();
        success = true;
      }
      finally
      {
        ChannelOperation operation = new OpenAckChannelOperation(getChannelIndex(), success);
        outputOperations.add(operation);
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  private final class OpenAckChannelOperation extends ChannelOperation
  {
    private boolean success;

    public OpenAckChannelOperation(short channelIndex, boolean success)
    {
      super(channelIndex, 0);
      this.success = success;
    }

    public OpenAckChannelOperation(ExtendedDataInputStream in) throws IOException
    {
      super(in);
      success = in.readBoolean();
    }

    @Override
    public void write(ExtendedDataOutputStream out) throws IOException
    {
      super.write(out);
      out.writeBoolean(success);
    }

    @Override
    public byte getOperation()
    {
      return OPERATION_OPEN_ACK;
    }

    @Override
    public void doEexecute(HTTPChannel channel)
    {
      channel.openAck();
    }
  }

  /**
   * @author Eike Stepper
   */
  private final class CloseChannelOperation extends ChannelOperation
  {
    public CloseChannelOperation(HTTPChannel channel)
    {
      super(channel.getChannelIndex(), channel.getOutputOperationCount());
      channel.increaseOutputOperationCount();
    }

    public CloseChannelOperation(ExtendedDataInputStream in) throws IOException
    {
      super(in);
    }

    @Override
    public byte getOperation()
    {
      return OPERATION_CLOSE;
    }

    @Override
    public void doEexecute(HTTPChannel channel)
    {
      // TODO Fix protocol between Channel.close and Connector.removeChannel/inverserRemoveChannel
      channel.setInverseRemoved();
      inverseRemoveChannel(channel.getChannelID(), channel.getChannelIndex());
    }
  }

  /**
   * @author Eike Stepper
   */
  private final class BufferChannelOperation extends ChannelOperation
  {
    private IBuffer buffer;

    public BufferChannelOperation(short channelIndex, long operationCount, IBuffer buffer)
    {
      super(channelIndex, operationCount);
      this.buffer = buffer;
    }

    public BufferChannelOperation(ExtendedDataInputStream in) throws IOException
    {
      super(in);
      int length = in.readShort();

      buffer = getBufferProvider().provideBuffer();
      ByteBuffer byteBuffer = buffer.startPutting(getChannelIndex());
      for (int i = 0; i < length; i++)
      {
        byte b = in.readByte();
        System.out.println("READ " + b);
        byteBuffer.put(b);
      }

      buffer.flip();
    }

    @Override
    public void write(ExtendedDataOutputStream out) throws IOException
    {
      super.write(out);

      buffer.flip();
      ByteBuffer byteBuffer = buffer.getByteBuffer();
      byteBuffer.position(IBuffer.HEADER_SIZE);

      int length = byteBuffer.limit() - byteBuffer.position();
      out.writeShort(length);

      for (int i = 0; i < length; i++)
      {
        byte b = byteBuffer.get();
        System.out.println("WRITE " + b);
        out.writeByte(b);
      }

      buffer.release();
    }

    @Override
    public byte getOperation()
    {
      return OPERATION_BUFFER;
    }

    public IBuffer getBuffer()
    {
      return buffer;
    }

    @Override
    public void doEexecute(HTTPChannel channel)
    {
      channel.handleBufferFromMultiplexer(buffer);
      buffer = null;
    }

    @Override
    public void dispose()
    {
      if (buffer != null)
      {
        buffer.release();
        buffer = null;
      }

      super.dispose();
    }
  }
}

Back to the top