View Javadoc
1   package com.ericsson.research.transport;
2   
3   /*
4    * ##_BEGIN_LICENSE_##
5    * Transport Abstraction Package (trap)
6    * ----------
7    * Copyright (C) 2014 Ericsson AB
8    * ----------
9    * Redistribution and use in source and binary forms, with or without modification,
10   * are permitted provided that the following conditions are met:
11   * 
12   * 1. Redistributions of source code must retain the above copyright notice, this
13   *    list of conditions and the following disclaimer.
14   * 
15   * 2. Redistributions in binary form must reproduce the above copyright notice,
16   *    this list of conditions and the following disclaimer in the documentation
17   *    and/or other materials provided with the distribution.
18   * 
19   * 3. Neither the name of the Ericsson AB nor the names of its contributors
20   *    may be used to endorse or promote products derived from this software without
21   *    specific prior written permission.
22   * 
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26   * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32   * OF THE POSSIBILITY OF SUCH DAMAGE.
33   * ##_END_LICENSE_##
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 	/* (non-Javadoc)
159 	 * @see java.lang.Object#finalize()
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 }