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.util.Iterator; 24 import java.util.logging.Logger; 25 26 import net.mlw.gfw.event.Event; 27 import net.mlw.gfw.event.EventHandler; 28 import net.mlw.gfw.event.EventHandlerList; 29 import net.mlw.gfw.event.EventHandlerTypeDelegator; 30 import net.mlw.gfw.event.EventHandlerTypeDelegator.EventProcessingMode; 31 import net.mlw.gfw.event.impl.ConnectEvent; 32 import net.mlw.gfw.event.impl.DisconnectEvent; 33 import net.mlw.gfw.ext.basic.event.ErrorEvent; 34 import net.mlw.gfw.server.Server; 35 import net.mlw.gfw.server.ServerContext; 36 import net.mlw.gfw.server.ServerEventHandler; 37 38 /*** 39 * @author Matthew L. Wilson 40 * @version $Revision: 1.8 $ $Date: 2004/07/01 14:31:41 $ 41 */ 42 public class DefaultServer implements Server 43 { 44 private static Logger LOGGER = Logger.getLogger(DefaultServer.class.getName()); 45 46 /*** The server context that is passed to the consumers. * */ 47 private ServerContext context; 48 49 /*** A Map of all the consumers. * */ 50 private EventHandlerTypeDelegator consumers; 51 52 private EventHandlerList eventHandlerList = new EventHandlerList(); 53 54 /*** 55 * 56 * 57 */ 58 public DefaultServer() 59 { 60 context = new DefaultServerContext(this); 61 /*** All events that are not processed will be redirected to the clients. * */ 62 consumers = new EventHandlerTypeDelegator(context.getClients()); 63 } 64 65 /*** 66 * 67 * @param serverEventHandler 68 */ 69 public DefaultServer(ServerEventHandlerTypeDelegator serverEventHandler) 70 { 71 context = new DefaultServerContext(this); 72 /*** All events that are not processed will be redirected to the clients. * */ 73 74 if (serverEventHandler != null) 75 { 76 serverEventHandler.init(context); 77 consumers = serverEventHandler; 78 } 79 else 80 { 81 consumers = new EventHandlerTypeDelegator(context.getClients()); 82 } 83 84 } 85 86 /*** 87 * @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event) 88 */ 89 public synchronized void onEvent(Event event) 90 { 91 LOGGER.info("recieved event: " + event); 92 eventHandlerList.onEvent(event); 93 consumers.onEvent(event); 94 } 95 96 /*** 97 * @see net.mlw.gfw.event.EventHandlerTypeDelegator#addEventHandler( Class eventClass, EventHandler eventHandler ) 98 */ 99 public void addEventHandler(Class eventType, EventHandler consumer) 100 { 101 if (consumer instanceof ServerEventHandler) 102 { 103 ((ServerEventHandler) consumer).init(context); 104 } 105 consumers.addEventHandler(eventType, consumer); 106 } 107 108 /*** 109 * 110 * @param consumer 111 */ 112 public void addEventHandler(EventHandler consumer) 113 { 114 if (consumer instanceof ServerEventHandler) 115 { 116 ((ServerEventHandler) consumer).init(context); 117 } 118 eventHandlerList.addEventHandler(consumer); 119 } 120 121 /*** 122 * 123 * @param mode 124 */ 125 public void setEventProcessingMode(EventProcessingMode mode) 126 { 127 consumers.setEventProcessingMode(mode); 128 } 129 130 /*** 131 * @see net.mlw.gfw.server.Server#addClient(java.lang.String, net.mlw.gfw.event.EventHandler) 132 */ 133 public void addClient(String name, EventHandler eventHandler) 134 { 135 LOGGER.info("A client (" + name + ") has connected: " + eventHandler); 136 137 if (context.getClients().getEventHandler(name) != null) 138 { 139 eventHandler.onEvent(new ErrorEvent("Name taken.", "Name already taken on server.")); 140 return; 141 } 142 143 //Send all the people that are already joined. 144 for (Iterator iter = context.getClients().getKeys().iterator(); iter.hasNext();) 145 { 146 eventHandler.onEvent(new ConnectEvent((String) iter.next())); 147 } 148 149 context.getClients().addEventHandler(name, eventHandler); 150 onEvent(new ConnectEvent(name)); 151 } 152 153 /*** 154 * @see net.mlw.gfw.server.Server#removeClient(java.lang.String) 155 */ 156 public void removeClient(String name, EventHandler eventHandler) 157 { 158 context.getClients().removeEventHandler(name, eventHandler); 159 context.getClients().onEvent(new DisconnectEvent(name)); 160 } 161 162 /*** 163 * @return Returns the context. 164 */ 165 public ServerContext getContext() 166 { 167 return context; 168 } 169 170 /*** 171 * @return Returns the consumers. 172 */ 173 public EventHandlerTypeDelegator getConsumers() 174 { 175 return consumers; 176 } 177 178 }

This page was automatically generated by Maven