1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel.socket.nio;
17
18 import static org.jboss.netty.channel.Channels.*;
19
20 import java.io.IOException;
21 import java.net.InetSocketAddress;
22 import java.nio.channels.ServerSocketChannel;
23
24 import org.jboss.netty.channel.AbstractServerChannel;
25 import org.jboss.netty.channel.ChannelException;
26 import org.jboss.netty.channel.ChannelFactory;
27 import org.jboss.netty.channel.ChannelPipeline;
28 import org.jboss.netty.channel.ChannelSink;
29 import org.jboss.netty.channel.socket.DefaultServerSocketChannelConfig;
30 import org.jboss.netty.channel.socket.ServerSocketChannelConfig;
31 import org.jboss.netty.logging.InternalLogger;
32 import org.jboss.netty.logging.InternalLoggerFactory;
33
34 class NioServerSocketChannel extends AbstractServerChannel
35 implements org.jboss.netty.channel.socket.ServerSocketChannel {
36
37 private static final InternalLogger logger =
38 InternalLoggerFactory.getInstance(NioServerSocketChannel.class);
39
40 final ServerSocketChannel socket;
41 final Boss boss;
42 final WorkerPool<NioWorker> workerPool;
43
44 private final ServerSocketChannelConfig config;
45
46 NioServerSocketChannel(
47 ChannelFactory factory,
48 ChannelPipeline pipeline,
49 ChannelSink sink, Boss boss, WorkerPool<NioWorker> workerPool) {
50
51 super(factory, pipeline, sink);
52 this.boss = boss;
53 this.workerPool = workerPool;
54 try {
55 socket = ServerSocketChannel.open();
56 } catch (IOException e) {
57 throw new ChannelException(
58 "Failed to open a server socket.", e);
59 }
60
61 try {
62 socket.configureBlocking(false);
63 } catch (IOException e) {
64 try {
65 socket.close();
66 } catch (IOException e2) {
67 if (logger.isWarnEnabled()) {
68 logger.warn(
69 "Failed to close a partially initialized socket.", e2);
70 }
71 }
72
73 throw new ChannelException("Failed to enter non-blocking mode.", e);
74 }
75
76 config = new DefaultServerSocketChannelConfig(socket.socket());
77
78 fireChannelOpen(this);
79 }
80
81 public ServerSocketChannelConfig getConfig() {
82 return config;
83 }
84
85 public InetSocketAddress getLocalAddress() {
86 return (InetSocketAddress) socket.socket().getLocalSocketAddress();
87 }
88
89 public InetSocketAddress getRemoteAddress() {
90 return null;
91 }
92
93 public boolean isBound() {
94 return isOpen() && socket.socket().isBound();
95 }
96
97 @Override
98 protected boolean setClosed() {
99 return super.setClosed();
100 }
101 }