Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 905f8cb7fbedba52fc2a12840bd370a61ac5003e (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
/***************************************************************************
 * Copyright (c) 2004, 2005, 2006 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.signal;

import org.eclipse.net4j.transport.Buffer;
import org.eclipse.net4j.transport.BufferProvider;
import org.eclipse.net4j.transport.Channel;
import org.eclipse.net4j.transport.util.BufferInputStream;
import org.eclipse.net4j.transport.util.ChannelOutputStream;

import org.eclipse.internal.net4j.transport.AbstractProtocol;
import org.eclipse.internal.net4j.transport.BufferUtil;
import org.eclipse.internal.net4j.transport.ChannelImpl;

import java.nio.ByteBuffer;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;

/**
 * @author Eike Stepper
 */
public abstract class SignalProtocol extends AbstractProtocol
{
  public static final long NO_TIMEOUT = BufferInputStream.NO_TIMEOUT;

  private static final int MIN_CORRELATION_ID = 1;

  private static final int MAX_CORRELATION_ID = Integer.MAX_VALUE;

  private ExecutorService executorService;

  private Map<Integer, Signal> signals = new ConcurrentHashMap();

  private int nextCorrelationID = MIN_CORRELATION_ID;

  protected SignalProtocol(Channel channel, ExecutorService executorService)
  {
    super(channel);

    if (executorService == null)
    {
      throw new IllegalArgumentException("executorService == null");
    }

    this.executorService = executorService;
  }

  protected SignalProtocol(Channel channel)
  {
    this(channel, ((ChannelImpl)channel).getReceiveExecutor());
  }

  public void handleBuffer(Buffer buffer)
  {
    ByteBuffer byteBuffer = buffer.getByteBuffer();
    int correlationID = byteBuffer.getInt();
    System.out.println(toString() + ": Received buffer for correlation " + correlationID);

    Signal signal;
    if (correlationID > 0)
    {
      // Incoming indication
      signal = signals.get(-correlationID);
      if (signal == null)
      {
        short signalID = byteBuffer.getShort();
        System.out.println(toString() + ": Got signal id " + signalID);

        signal = createSignalReactor(signalID);
        signal.setProtocol(this);
        signal.setCorrelationID(-correlationID);
        signal.setInputStream(new SignalInputStream(getInputStreamTimeout()));
        signal.setOutputStream(new SignalOutputStream(-correlationID, signalID, false));
        signals.put(-correlationID, signal);
        executorService.execute(signal);
      }
    }
    else
    {
      // Incoming confirmation
      signal = signals.get(-correlationID);
      if (signal == null)
      {
        System.out.println(toString() + ": Discarding buffer");
        buffer.release();
      }
    }

    signal.getInputStream().handleBuffer(buffer);
  }

  public long getInputStreamTimeout()
  {
    return NO_TIMEOUT;
  }

  @Override
  public String toString()
  {
    return "SignalProtocol[" + getProtocolID() + ", " + getChannel() + "]";
  }

  protected abstract SignalReactor createSignalReactor(short signalID);

  void startSignal(SignalActor signalActor, long timeout)
  {
    if (signalActor.getProtocol() != this)
    {
      throw new IllegalArgumentException("signalActor.getProtocol() != this");
    }

    short signalID = signalActor.getSignalID();
    int correlationID = signalActor.getCorrelationID();
    signalActor.setInputStream(new SignalInputStream(timeout));
    signalActor.setOutputStream(new SignalOutputStream(correlationID, signalID, true));
    signals.put(correlationID, signalActor);

    signalActor.run();
  }

  void stopSignal(Signal signal)
  {
    int correlationID = signal.getCorrelationID();
    signals.remove(correlationID);
  }

  int getNextCorrelationID()
  {
    int correlationID = nextCorrelationID;
    if (nextCorrelationID == MAX_CORRELATION_ID)
    {
      System.out.println(toString() + ": Correlation wrap around");
      nextCorrelationID = MIN_CORRELATION_ID;
    }
    else
    {
      ++nextCorrelationID;
    }

    return correlationID;
  }

  class SignalInputStream extends BufferInputStream
  {
    private long timeout;

    public SignalInputStream(long timeout)
    {
      this.timeout = timeout;
    }

    @Override
    public long getMillisBeforeTimeout()
    {
      return timeout;
    }
  }

  class SignalOutputStream extends ChannelOutputStream
  {
    public SignalOutputStream(final int correlationID, final short signalID,
        final boolean addSignalID)
    {
      super(getChannel(), new BufferProvider()
      {
        private BufferProvider delegate = BufferUtil.getBufferProvider(getChannel());

        private boolean firstBuffer = addSignalID;

        public short getBufferCapacity()
        {
          return delegate.getBufferCapacity();
        }

        public Buffer provideBuffer()
        {
          Buffer buffer = delegate.provideBuffer();
          ByteBuffer byteBuffer = buffer.startPutting(getChannel().getChannelID());

          System.out.println("Providing buffer for correlation " + correlationID);
          byteBuffer.putInt(correlationID);
          if (firstBuffer)
          {
            System.out.println(SignalProtocol.this.toString() + ": Put signal id " + signalID);
            byteBuffer.putShort(signalID);
          }

          firstBuffer = false;
          return buffer;
        }

        public void retainBuffer(Buffer buffer)
        {
          delegate.retainBuffer(buffer);
        }
      });
    }
  }
}

Back to the top