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 java.io.IOException;
19 import java.net.InetSocketAddress;
20 import java.net.SocketAddress;
21
22 import org.jboss.netty.channel.AbstractChannel;
23 import org.jboss.netty.channel.Channel;
24 import org.jboss.netty.channel.ChannelFactory;
25 import org.jboss.netty.channel.ChannelFuture;
26 import org.jboss.netty.channel.ChannelPipeline;
27 import org.jboss.netty.channel.ChannelSink;
28 import org.jboss.netty.channel.socket.Worker;
29
30 abstract class AbstractOioChannel extends AbstractChannel {
31 private volatile InetSocketAddress localAddress;
32 volatile InetSocketAddress remoteAddress;
33 volatile Thread workerThread;
34 volatile Worker worker;
35
36 final Object interestOpsLock = new Object();
37
38 AbstractOioChannel(
39 Channel parent,
40 ChannelFactory factory,
41 ChannelPipeline pipeline,
42 ChannelSink sink) {
43 super(parent, factory, pipeline, sink);
44 }
45
46 @Override
47 protected boolean setClosed() {
48 return super.setClosed();
49 }
50
51 @Override
52 protected void setInterestOpsNow(int interestOps) {
53 super.setInterestOpsNow(interestOps);
54 }
55
56 @Override
57 public ChannelFuture write(Object message, SocketAddress remoteAddress) {
58 if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) {
59 return super.write(message, null);
60 } else {
61 return super.write(message, remoteAddress);
62 }
63 }
64
65
66 public boolean isBound() {
67 return isOpen() && isSocketBound();
68 }
69
70
71 public boolean isConnected() {
72 return isOpen() && isSocketConnected();
73 }
74
75
76
77 public InetSocketAddress getLocalAddress() {
78 InetSocketAddress localAddress = this.localAddress;
79 if (localAddress == null) {
80 try {
81 this.localAddress = localAddress = getLocalSocketAddress();
82 } catch (Throwable t) {
83
84 return null;
85 }
86 }
87 return localAddress;
88 }
89
90
91 public InetSocketAddress getRemoteAddress() {
92 InetSocketAddress remoteAddress = this.remoteAddress;
93 if (remoteAddress == null) {
94 try {
95 this.remoteAddress = remoteAddress =
96 getRemoteSocketAddress();
97 } catch (Throwable t) {
98
99 return null;
100 }
101 }
102 return remoteAddress;
103 }
104
105 abstract boolean isSocketBound();
106
107 abstract boolean isSocketConnected();
108
109 abstract boolean isSocketClosed();
110
111 abstract InetSocketAddress getLocalSocketAddress() throws Exception;
112
113 abstract InetSocketAddress getRemoteSocketAddress() throws Exception;
114
115 abstract void closeSocket() throws IOException;
116
117 }