1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel.socket.oio;
17
18 import static org.jboss.netty.channel.Channels.*;
19
20 import java.io.IOException;
21 import java.net.InetSocketAddress;
22 import java.net.ServerSocket;
23 import java.util.concurrent.locks.Lock;
24 import java.util.concurrent.locks.ReentrantLock;
25
26 import org.jboss.netty.channel.AbstractServerChannel;
27 import org.jboss.netty.channel.ChannelException;
28 import org.jboss.netty.channel.ChannelFactory;
29 import org.jboss.netty.channel.ChannelPipeline;
30 import org.jboss.netty.channel.ChannelSink;
31 import org.jboss.netty.channel.socket.DefaultServerSocketChannelConfig;
32 import org.jboss.netty.channel.socket.ServerSocketChannel;
33 import org.jboss.netty.channel.socket.ServerSocketChannelConfig;
34 import org.jboss.netty.logging.InternalLogger;
35 import org.jboss.netty.logging.InternalLoggerFactory;
36
37 class OioServerSocketChannel extends AbstractServerChannel
38 implements ServerSocketChannel {
39
40 private static final InternalLogger logger =
41 InternalLoggerFactory.getInstance(OioServerSocketChannel.class);
42
43 final ServerSocket socket;
44 final Lock shutdownLock = new ReentrantLock();
45 private final ServerSocketChannelConfig config;
46
47 OioServerSocketChannel(
48 ChannelFactory factory,
49 ChannelPipeline pipeline,
50 ChannelSink sink) {
51
52 super(factory, pipeline, sink);
53
54 try {
55 socket = new ServerSocket();
56 } catch (IOException e) {
57 throw new ChannelException(
58 "Failed to open a server socket.", e);
59 }
60
61 try {
62 socket.setSoTimeout(1000);
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 throw new ChannelException(
73 "Failed to set the server socket timeout.", e);
74 }
75
76 config = new DefaultServerSocketChannelConfig(socket);
77
78 fireChannelOpen(this);
79 }
80
81 public ServerSocketChannelConfig getConfig() {
82 return config;
83 }
84
85 public InetSocketAddress getLocalAddress() {
86 return (InetSocketAddress) socket.getLocalSocketAddress();
87 }
88
89 public InetSocketAddress getRemoteAddress() {
90 return null;
91 }
92
93 public boolean isBound() {
94 return isOpen() && socket.isBound();
95 }
96
97 @Override
98 protected boolean setClosed() {
99 return super.setClosed();
100 }
101 }