1 package com.ericsson.research.transport;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 import java.io.IOException;
37 import java.net.InetAddress;
38 import java.net.InetSocketAddress;
39 import java.net.ServerSocket;
40 import java.nio.channels.SelectionKey;
41 import java.nio.channels.ServerSocketChannel;
42
43 public class ManagedServerSocket implements NioEndpoint
44 {
45
46 public enum State { NOT_LISTENING, LISTENING }
47
48 private State mState;
49 private NioManager mChannelManager;
50 private ManagedServerSocketClient mClient;
51 private SelectionKey key;
52
53 public ManagedServerSocket() {
54 this.mState = State.NOT_LISTENING;
55 }
56
57 public void registerClient(ManagedServerSocketClient client) {
58 if (this.mClient != null)
59 return;
60
61 if (this.mChannelManager == null)
62 this.mChannelManager = NioManager.instance();
63
64 this.mClient = client;
65 }
66
67 public State getState() {
68 return this.mState;
69 }
70
71 public InetSocketAddress getInetAddress() {
72 ServerSocket socket = ((ServerSocketChannel) this.key.channel()).socket();
73 return new InetSocketAddress(socket.getInetAddress(), socket.getLocalPort());
74 }
75
76 public void listen(int port) throws IOException {
77 this.listen("localhost", port);
78 }
79
80 public void listen(InetAddress host, int port) {
81 this.listen(new InetSocketAddress(host, port));
82 }
83
84 public void listen(String addr, int port) throws IOException {
85 this.listen(new InetSocketAddress(InetAddress.getByName(addr), port));
86 }
87
88 public void listen(InetSocketAddress address) {
89
90 if (this.mState != State.NOT_LISTENING)
91 throw new IllegalStateException("already listening");
92
93 this.mState = State.LISTENING;
94
95 this.mChannelManager.bind(this, address, true);
96 }
97
98 public synchronized void close() {
99
100 if (this.mState != State.LISTENING)
101 return;
102
103 this.mChannelManager.close(this.key);
104
105 while (this.mState == State.LISTENING)
106 {
107 try
108 {
109 this.wait();
110 }
111 catch (InterruptedException e)
112 {}
113 }
114 }
115
116 public boolean canAccept()
117 {
118 return true;
119 }
120
121 public NioEndpoint createAcceptChild() throws UnsupportedOperationException
122 {
123 return new ManagedSocket(true);
124 }
125
126 public void notifyAccepted(NioEndpoint endpoint)
127 {
128 this.mClient.notifyAccept((ManagedSocket)endpoint);
129 }
130
131 public synchronized void notifyClosed()
132 {
133 this.mState = State.NOT_LISTENING;
134 this.notify();
135 }
136
137 public void notifyConnected()
138 {
139 this.mClient.notifyBound(this);
140 }
141
142 public void notifyError(Exception e)
143 {
144 this.mClient.notifyError(e);
145 }
146
147 public void receive(byte[] data, int size)
148 {
149
150 }
151
152 public void setNioManager(NioManager nioManager, SelectionKey key)
153 {
154 this.mChannelManager = nioManager;
155 this.key = key;
156 }
157
158
159
160
161 protected void finalize() throws Throwable
162 {
163
164 this.mChannelManager.close(this.key);
165 super.finalize();
166 }
167
168 public SelectionKey getKey()
169 {
170 return this.key;
171 }
172
173 }