Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 38bbe2d756c237c808267e1f162f383958bea1d2 (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
/***************************************************************************
 * 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.internal.net4j.transport.tcp;

import org.eclipse.net4j.transport.Buffer;
import org.eclipse.net4j.util.concurrent.Synchronizer;
import org.eclipse.net4j.util.concurrent.SynchronizingCorrelator;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.internal.net4j.bundle.Net4j;
import org.eclipse.internal.net4j.transport.BufferUtil;
import org.eclipse.internal.net4j.transport.ChannelImpl;

import java.nio.ByteBuffer;

/**
 * @author Eike Stepper
 */
public final class ControlChannelImpl extends ChannelImpl
{
  public static final short CONTROL_CHANNEL_ID = -1;

  public static final long REGISTRATION_TIMEOUT = 500000;

  public static final byte OPCODE_REGISTRATION = 1;

  public static final byte OPCODE_REGISTRATION_ACK = 2;

  public static final byte OPCODE_DEREGISTRATION = 3;

  public static final byte SUCCESS = 1;

  public static final byte FAILURE = 0;

  private static final ContextTracer TRACER = new ContextTracer(Net4j.DEBUG_CHANNEL,
      ControlChannelImpl.class);

  private SynchronizingCorrelator<Short, Boolean> registrations = new SynchronizingCorrelator();

  public ControlChannelImpl(AbstractTCPConnector connector)
  {
    super(connector.getReceiveExecutor());
    setChannelIndex(CONTROL_CHANNEL_ID);
    setConnector(connector);
  }

  public boolean registerChannel(short channelIndex, String protocolID)
  {
    assertValidChannelIndex(channelIndex);
    Synchronizer<Boolean> registration = registrations.correlate(channelIndex);

    Buffer buffer = provideBuffer();
    ByteBuffer byteBuffer = buffer.startPutting(CONTROL_CHANNEL_ID);
    byteBuffer.put(OPCODE_REGISTRATION);
    byteBuffer.putShort(channelIndex);
    BufferUtil.putUTF8(byteBuffer, protocolID);
    handleBuffer(buffer);

    return registration.get(REGISTRATION_TIMEOUT);
  }

  public void deregisterChannel(short channelIndex)
  {
    assertValidChannelIndex(channelIndex);

    Buffer buffer = provideBuffer();
    ByteBuffer byteBuffer = buffer.startPutting(CONTROL_CHANNEL_ID);
    byteBuffer.put(OPCODE_DEREGISTRATION);
    byteBuffer.putShort(channelIndex);
    handleBuffer(buffer);
  }

  private void assertValidChannelIndex(short channelIndex)
  {
    if (channelIndex <= CONTROL_CHANNEL_ID)
    {
      throw new IllegalArgumentException("channelIndex <= CONTROL_CHANNEL_ID"); //$NON-NLS-1$
    }
  }

  public void handleBufferFromMultiplexer(Buffer buffer)
  {
    try
    {
      ByteBuffer byteBuffer = buffer.getByteBuffer();
      byte opcode = byteBuffer.get();
      switch (opcode)
      {
      case OPCODE_REGISTRATION:
      {
        short channelIndex = byteBuffer.getShort();
        assertValidChannelIndex(channelIndex);
        boolean success = true;

        try
        {
          byte[] handlerFactoryUTF8 = BufferUtil.getByteArray(byteBuffer);
          String protocolID = BufferUtil.fromUTF8(handlerFactoryUTF8);
          ChannelImpl channel = ((AbstractTCPConnector)getConnector()).createChannel(channelIndex,
              protocolID);
          if (channel != null)
          {
            channel.activate();
          }
          else
          {
            success = false;
          }
        }
        catch (Exception ex)
        {
          Net4j.LOG.error(ex);
          success = false;
        }

        sendStatus(OPCODE_REGISTRATION_ACK, channelIndex, success);
        break;
      }

      case OPCODE_REGISTRATION_ACK:
      {
        short channelIndex = byteBuffer.getShort();
        boolean success = byteBuffer.get() == SUCCESS;
        registrations.put(channelIndex, success);
        break;
      }

      case OPCODE_DEREGISTRATION:
      {
        short channelIndex = byteBuffer.getShort();
        assertValidChannelIndex(channelIndex);

        try
        {
          ChannelImpl channel = ((AbstractTCPConnector)getConnector()).getChannel(channelIndex);
          if (channel != null)
          {
            channel.deactivate();
          }
          else
          {
            if (TRACER.isEnabled())
            {
              TRACER.trace(this, "Invalid channel id: " + channelIndex); //$NON-NLS-1$
            }
          }
        }
        catch (Exception ex)
        {
          Net4j.LOG.error(ex);
        }

        break;
      }

      default:
        Net4j.LOG.error("Invalid opcode: " + opcode); //$NON-NLS-1$
        ((AbstractTCPConnector)getConnector()).deactivate();
      }
    }
    finally
    {
      buffer.release();
    }
  }

  private void sendStatus(byte opcode, short channelIndex, boolean status)
  {
    Buffer buffer = provideBuffer();
    ByteBuffer byteBuffer = buffer.startPutting(CONTROL_CHANNEL_ID);
    byteBuffer.put(opcode);
    byteBuffer.putShort(channelIndex);
    byteBuffer.put(status ? SUCCESS : FAILURE);
    handleBuffer(buffer);
  }
}

Back to the top