View Javadoc
1 /*** 2 * Copyright (c) 2003 held jointly by the individual authors. 3 * 4 * This library is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU Lesser General Public License as published 6 * by the Free Software Foundation; either version 2.1 of the License, or 7 * (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; with out even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this library; if not, write to the Free Software Foundation, 16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 17 * 18 * > http://www.gnu.org/copyleft/lesser.html 19 * > http://www.opensource.org/licenses/lgpl-license.php 20 */ 21 package net.mlw.gfw.server.impl; 22 23 import java.io.IOException; 24 import java.io.ObjectInputStream; 25 import java.io.ObjectOutputStream; 26 import java.net.ServerSocket; 27 import java.net.Socket; 28 import java.util.logging.Logger; 29 30 import net.mlw.gfw.Constants; 31 import net.mlw.gfw.event.Event; 32 import net.mlw.gfw.event.EventHandler; 33 import net.mlw.gfw.event.impl.ConnectEvent; 34 import net.mlw.gfw.server.Server; 35 36 /*** 37 * @author Matthew L. Wilson 38 * @version $Revision: 1.5 $ $Date: 2004/06/29 20:32:54 $ 39 */ 40 public class ServerSocketDecorator extends DefaultServerDecorator 41 { 42 private static Logger LOGGER = Logger.getLogger(ServerSocketDecorator.class.getName()); 43 44 /*** the port the server will listen on. **/ 45 private int port = Constants.DEFAULT_PORT; 46 47 private ServerSocket socket; 48 49 private boolean isListening = false; 50 /*** 51 * @param server 52 */ 53 public ServerSocketDecorator(Server server) 54 { 55 super(server); 56 } 57 58 /*** 59 * @param port the port the server will listen on. 60 */ 61 public void setPort(int port) 62 { 63 this.port = port; 64 } 65 66 public boolean isListening() 67 { 68 return isListening; 69 } 70 71 public void accept() throws IOException 72 { 73 socket = new ServerSocket(port); 74 LOGGER.info("Listening on port " + port); 75 76 while ((isListening = !socket.isClosed())) 77 { 78 SocketRepresentingClient client = new SocketRepresentingClient(socket.accept()); 79 LOGGER.info("Accepted connection: " + client); 80 Thread thread = new Thread(client); 81 thread.setDaemon(true); 82 thread.start(); 83 } 84 } 85 86 public void close() throws IOException 87 { 88 LOGGER.info("Closing server (" + socket + ")"); 89 socket.close(); 90 LOGGER.info("Server closed."); 91 } 92 93 private class SocketRepresentingClient implements EventHandler, Runnable 94 { 95 private String name; 96 private Socket socket; 97 private ObjectInputStream in = null; 98 private ObjectOutputStream out = null; 99 100 public SocketRepresentingClient(Socket socket) throws IOException 101 { 102 this.socket = socket; 103 out = new ObjectOutputStream(socket.getOutputStream()); 104 in = new ObjectInputStream(socket.getInputStream()); 105 } 106 107 /*** 108 * @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event) 109 * 110 * @todo Do something with the IOException 111 */ 112 public void onEvent(Event event) 113 { 114 try 115 { 116 out.writeObject(event); 117 } 118 catch (IOException e) 119 { 120 isListening = false; 121 LOGGER.info(e.getMessage()); 122 } 123 124 } 125 126 /*** 127 * @see java.lang.Runnable#run() 128 * 129 * @todo Check for a unique name! 130 */ 131 public void run() 132 { 133 try 134 { 135 while (socket.isConnected()) 136 { 137 Event event = (Event) in.readObject(); 138 139 //Throw event away till a ConnectionEvent is recieved. 140 if (event instanceof ConnectEvent) 141 { 142 name = ((ConnectEvent) event).getUserName(); 143 getServer().addClient(name, this); 144 } 145 else if (name != null) 146 { 147 getServer().onEvent(event); 148 } 149 } 150 } 151 catch (Exception e) 152 { 153 LOGGER.info(e.getMessage()); 154 getServer().removeClient(name, this); 155 } 156 157 } 158 } 159 }

This page was automatically generated by Maven