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 }
73 throw new ChannelException(
74 "Failed to set the server socket timeout.", e);
75 }
76
77 config = new DefaultServerSocketChannelConfig(socket);
78
79 fireChannelOpen(this);
80 }
81
82 public ServerSocketChannelConfig getConfig() {
83 return config;
84 }
85
86 public InetSocketAddress getLocalAddress() {
87 return (InetSocketAddress) socket.getLocalSocketAddress();
88 }
89
90 public InetSocketAddress getRemoteAddress() {
91 return null;
92 }
93
94 public boolean isBound() {
95 return isOpen() && socket.isBound();
96 }
97
98 @Override
99 protected boolean setClosed() {
100 return super.setClosed();
101 }
102 }