4 * Copyright 2015 Eddie Berrisford-Lynch <dev@fun2be.me>.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 package uk.ac.ntu.n0521366.wsyd.libs.net;
26 import java.io.IOException;
27 import java.net.InetSocketAddress;
28 import java.net.ServerSocket;
29 import java.net.Socket;
30 import java.net.SocketException;
31 import java.net.SocketTimeoutException;
32 import java.text.MessageFormat;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import uk.ac.ntu.n0521366.wsyd.libs.message.MessageNetworkStream;
40 * Dual-use multithreading network TCP server that can be used stand-alone
41 * or in a Swing GUI application as a background worker thread.
43 * @see NetworkServerAbstract
44 * @author Eddie Berrisford-Lynch <dev@fun2be.me>
46 public class NetworkServerTCP extends NetworkServerAbstract {
48 ServerSocket _serverSocket;
50 NetworkStreamManager _streamManager;
52 protected ArrayList<ConnectionEstablishedEventListener> _ConnectionEstablishedEventListeners;
55 * Construct the server with a Logger.
57 * No socket is opened.
59 * @param socketAddress The socket to listen on
60 * @param title source identifier for use in log messages and sent NetworkMessage objects
61 * @param serviceToHostMap the map object used for host <> InetSocketAddress lookups
63 * @param logger An instance of Logger to be used by all objects of this class
65 public NetworkServerTCP(WSYD_SocketAddress socketAddress, String title, ServiceAddressMap serviceToHostMap, NetworkStreamManager manager, Logger logger) {
66 super(socketAddress, title, serviceToHostMap, logger);
67 this._streamManager = manager;
68 this._ConnectionEstablishedEventListeners = new ArrayList<>();
72 * Construct the server without a Logger.
74 * No socket is opened.
76 * @param socketAddress The socket to listen on
77 * @param title source identifier for use in log messages and sent NetworkMessage objects
78 * @param serviceToHostMap the map object used for host <> InetSocketAddress lookups
81 public NetworkServerTCP(WSYD_SocketAddress socketAddress, String title, ServiceAddressMap serviceToHostMap, NetworkStreamManager manager) {
82 super(socketAddress, title, serviceToHostMap);
83 this._streamManager = manager;
87 * Open the socket ready for accepting connections.
89 * It should also set a reasonable socket timeout with a call to setSoTimeout()
91 * @throws SocketException
94 public void serverOpen() throws SocketException {
96 _serverSocket = new ServerSocket(_socketAddress.getPort(), 50, _socketAddress.getAddress());
97 } catch (IOException ex) {
98 throw new SocketException(ex.getMessage());
100 _serverSocket.setSoTimeout(100);
102 if (_socketAddress.getPort() == Network.PORTS_EPHEMERAL) {
103 // reflect the actual port in use if an ephermal port was requested
104 InetSocketAddress actualSA = (InetSocketAddress)_serverSocket.getLocalSocketAddress();
105 _socketAddress.setAddress(actualSA.getAddress());
106 _socketAddress.setPort(actualSA.getPort());
108 //log(Level.FINEST, _title, MessageFormat.format("Connection from {0}:{1}", _socketAddress.getAddress().getCanonicalHostName(), Integer.toString(_socketAddress.getPort())));
109 // TODO: Complete this implementation
113 * Send an unsolicited message to a remote service.
115 * This method is called by the main worker loop if there is a message to
118 * @param message must have its _serviceTarget parameter set
119 * @return true if the message was sent
122 protected boolean serverSend(NetworkMessage message) {
123 boolean result = false;
131 * @throws SocketException
134 public void serverClose() throws SocketException {
135 if (this._serverSocket != null)
137 this._serverSocket.close();
138 } catch (IOException ex) {
139 throw new SocketException(ex.getMessage());
144 * Accept connection from remote hosts.
146 * This method should wait for a single incoming connection.
148 * @return true if the server should continue listening
151 public boolean serverListen() {
152 boolean result = false;
155 //System.err.println("Before");
156 Socket connectionSocket = _serverSocket.accept();
157 NetworkStream newStream = new NetworkStream(connectionSocket, _streamManager);
158 long userID = _streamManager.addStream(0, newStream);
159 // workaround to enable firing a ConnectionEstablishedEvent in process() on the owner thread
160 NetworkMessage temp = new NetworkMessage("triggerConnectionEstablishedEvent", null, new MessageNetworkStream(newStream));
162 log(Level.INFO, _title, MessageFormat.format("Incoming connection from {0}:{1}", connectionSocket.getInetAddress().getCanonicalHostName(), Integer.toString(connectionSocket.getPort())));
164 // add or update the last-seen time of the Sender host in the known services map
165 ServiceAddressMap.LastSeenHost host = new ServiceAddressMap.LastSeenHost((InetSocketAddress)connectionSocket.getRemoteSocketAddress());
166 this._serviceToHostMap.put(Long.toString(userID), host);
167 log(Level.INFO, _title, MessageFormat.format("Added \"{0}\" to service map", userID));
168 System.err.println("Added new TCP stream successfully");
170 catch (SocketTimeoutException e) {
173 catch (IOException e) {
174 // Nothing to be done
175 System.err.println("Incoming connection caused exception...");
181 private void fireConnectionEstablishedEvent(NetworkStream stream) {
182 ConnectionEstablishedEvent event = new ConnectionEstablishedEvent(this, stream);
183 for (ConnectionEstablishedEventListener listener: this._ConnectionEstablishedEventListeners) {
184 listener.connectionEstablished(event);
188 public synchronized void addConnectionEstablisedEventListener(ConnectionEstablishedEventListener listener) {
189 _ConnectionEstablishedEventListeners.add(listener);
192 public synchronized void removeConnectionEstablishedEventListener(ConnectionEstablishedEventListener listener) {
193 for (ConnectionEstablishedEventListener el : _ConnectionEstablishedEventListeners)
194 if (el == listener) {
195 _ConnectionEstablishedEventListeners.remove(listener);
201 /* XXX: Methods below here all execute on the GUI Event Dispatch Thread */
204 * Clean up after doInBackground() has returned.
206 * This method will run on the GUI Event Dispatch Thread so must complete quickly.
209 protected void done() {
214 protected NetworkMessage sendMessage() {
219 protected void process(List<NetworkMessage> list) {
220 System.err.println("process()");
221 for (NetworkMessage nm: list) {
222 MessageNetworkStream message = (MessageNetworkStream)nm.getMessage();
223 System.err.println("ConnectionEstablished dispatched for " + message._stream._key);
224 fireConnectionEstablishedEvent(message._stream);