Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cc48fbe5d68c843de15588346dc09b483fafb09e (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
/***************************************************************************
 * Copyright (c) 2004-2007 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.tcp;

import org.eclipse.net4j.tcp.TCPAcceptor;
import org.eclipse.net4j.tcp.TCPSelector;
import org.eclipse.net4j.tcp.TCPSelectorListener;
import org.eclipse.net4j.tcp.TCPUtil;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.internal.net4j.bundle.Net4j;
import org.eclipse.internal.net4j.transport.AbstractAcceptor;
import org.eclipse.internal.net4j.transport.DescriptionUtil;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.MessageFormat;

/**
 * @author Eike Stepper
 */
public class TCPAcceptorImpl extends AbstractAcceptor implements TCPAcceptor, TCPSelectorListener.Passive
{
  private static final ContextTracer TRACER = new ContextTracer(Net4j.DEBUG_ACCEPTOR, TCPAcceptorImpl.class);

  private static final String DEFAULT_ADDRESS = "0.0.0.0";

  private TCPSelector selector;

  private ServerSocketChannel serverSocketChannel;

  private String address;

  private int port;

  public TCPAcceptorImpl()
  {
  }

  public String getAddress()
  {
    return address;
  }

  public int getPort()
  {
    return port;
  }

  public TCPSelector getSelector()
  {
    return selector;
  }

  public void setSelector(TCPSelector selector)
  {
    this.selector = selector;
  }

  public void handleAccept(TCPSelector selector, ServerSocketChannel serverSocketChannel)
  {
    try
    {
      SocketChannel socketChannel = serverSocketChannel.accept();
      if (socketChannel != null)
      {
        if (TRACER.isEnabled())
        {
          TRACER.trace("Accepted socketChannel " + socketChannel); //$NON-NLS-1$
        }

        socketChannel.configureBlocking(false);
        ServerTCPConnectorImpl connector = createConnector(socketChannel);
        addConnector(connector);
      }
    }
    catch (ClosedChannelException ex)
    {
      deactivate();
    }
    catch (Exception ex)
    {
      if (isActive())
      {
        Net4j.LOG.error(ex);
      }

      deactivate();
    }
  }

  @Override
  public String toString()
  {
    return MessageFormat.format("TCPAcceptor[{0}]", getDescription()); //$NON-NLS-1$ 
  }

  protected ServerTCPConnectorImpl createConnector(SocketChannel socketChannel)
  {
    String description = createConnectorDescription(socketChannel);

    ServerTCPConnectorImpl connector = new ServerTCPConnectorImpl();
    connector.setDescription(description);
    connector.setSocketChannel(socketChannel);
    connector.setReceiveExecutor(getReceiveExecutor());
    connector.setProtocolFactoryRegistry(getProtocolFactoryRegistry());
    connector.setBufferProvider(getBufferProvider());
    connector.setSelector(selector);
    return connector;
  }

  @Override
  protected void onAboutToActivate() throws Exception
  {
    super.onAboutToActivate();
    if (getDescription() == null)
    {
      throw new IllegalStateException("description == null"); //$NON-NLS-1$
    }
    else
    {
      String[] elements = DescriptionUtil.getElements(getDescription());
      address = elements[1];
      if (address.length() == 0)
      {
        address = DEFAULT_ADDRESS;
      }

      port = Integer.parseInt(elements[2]);
    }

    if (selector == null)
    {
      throw new IllegalStateException("selector == null");
    }
  }

  @Override
  protected void onActivate() throws Exception
  {
    super.onActivate();
    InetSocketAddress addr = null;
    if (address != null)
    {
      addr = new InetSocketAddress(InetAddress.getByName(address), port);
    }

    serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(false);

    ServerSocket socket = serverSocketChannel.socket();
    socket.bind(addr);

    if (address == null)
    {
      address = socket.getInetAddress().toString();
      if (address.startsWith("/"))
      {
        address = address.substring(1);
      }

      int colon = address.indexOf(':');
      if (colon != -1)
      {
        port = Integer.parseInt(address.substring(colon + 1));
        address = address.substring(0, colon);
      }
    }

    selector.registerAsync(serverSocketChannel, this);
  }

  @Override
  protected void onDeactivate() throws Exception
  {
    serverSocketChannel.close();
    super.onDeactivate();
  }

  private String createConnectorDescription(SocketChannel socketChannel)
  {
    SocketAddress addr = socketChannel.socket().getRemoteSocketAddress();
    String host = addr.toString();
    if (host.startsWith("/"))
    {
      host = host.substring(1);
    }

    int port = 0;
    int colon = host.indexOf(':');
    if (colon != -1)
    {
      port = Integer.parseInt(host.substring(colon + 1));
      host = host.substring(0, colon);
    }

    return TCPUtil.createConnectorDescription(host, port);
  }
}

Back to the top