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
38
39
40
41
42
43
44
45 class OioServerSocketChannel extends AbstractServerChannel
46 implements ServerSocketChannel {
47
48 private static final InternalLogger logger =
49 InternalLoggerFactory.getInstance(OioServerSocketChannel.class);
50
51 final ServerSocket socket;
52 final Lock shutdownLock = new ReentrantLock();
53 private final ServerSocketChannelConfig config;
54
55 OioServerSocketChannel(
56 ChannelFactory factory,
57 ChannelPipeline pipeline,
58 ChannelSink sink) {
59
60 super(factory, pipeline, sink);
61
62 try {
63 socket = new ServerSocket();
64 } catch (IOException e) {
65 throw new ChannelException(
66 "Failed to open a server socket.", e);
67 }
68
69 try {
70 socket.setSoTimeout(1000);
71 } catch (IOException e) {
72 try {
73 socket.close();
74 } catch (IOException e2) {
75 logger.warn(
76 "Failed to close a partially initialized socket.", e2);
77 }
78 throw new ChannelException(
79 "Failed to set the server socket timeout.", e);
80 }
81
82 config = new DefaultServerSocketChannelConfig(socket);
83
84 fireChannelOpen(this);
85 }
86
87 public ServerSocketChannelConfig getConfig() {
88 return config;
89 }
90
91 public InetSocketAddress getLocalAddress() {
92 return (InetSocketAddress) socket.getLocalSocketAddress();
93 }
94
95 public InetSocketAddress getRemoteAddress() {
96 return null;
97 }
98
99 public boolean isBound() {
100 return isOpen() && socket.isBound();
101 }
102
103 @Override
104 protected boolean setClosed() {
105 return super.setClosed();
106 }
107 }