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.Socket; 27 import java.util.logging.Logger; 28 29 import net.mlw.gfw.Constants; 30 import net.mlw.gfw.event.Event; 31 import net.mlw.gfw.event.EventHandler; 32 import net.mlw.gfw.event.impl.ConnectEvent; 33 import net.mlw.gfw.server.Server; 34 35 /*** 36 * @author Matthew L. Wilson 37 * @version $Revision: 1.4 $ $Date: 2004/06/29 20:32:54 $ 38 */ 39 public class RemoteServer implements Server 40 { 41 private static Logger LOGGER = Logger.getLogger(RemoteServer.class.getName()); 42 43 protected Socket socket; 44 protected ObjectInputStream in; 45 protected ObjectOutputStream out; 46 47 /*** The port to connect to. **/ 48 private int port = Constants.DEFAULT_PORT; 49 /*** The host to connect to. **/ 50 private String host = Constants.DEFAULT_HOST; 51 52 private EventHandler client; 53 54 private boolean isListening = false; 55 56 /*** 57 */ 58 public RemoteServer() 59 { 60 } 61 62 /*** 63 * @param port 64 * @param host 65 */ 66 public RemoteServer(String host, int port) 67 { 68 this.port = port; 69 this.host = host; 70 } 71 72 /*** Sets the port to connect to. 73 * @param port The port to connect to. 74 */ 75 public void setPort(int port) 76 { 77 this.port = port; 78 } 79 80 /*** Sets the host to connect to. 81 * @param host The host to connect to. 82 */ 83 public void setHost(String host) 84 { 85 this.host = host; 86 } 87 88 public boolean isListening() 89 { 90 return isListening; 91 } 92 93 public Thread spawnThreadAndlisten() 94 { 95 Thread clientThread = new Thread(new Runnable() 96 { 97 public void run() 98 { 99 try 100 { 101 listen(); 102 } 103 catch (Exception e) 104 { 105 e.printStackTrace(); 106 } 107 } 108 }); 109 clientThread.setDaemon(true); 110 clientThread.start(); 111 112 while (!isListening()) 113 { 114 try 115 { 116 Thread.sleep(10); 117 } 118 catch (InterruptedException ignore) 119 { 120 ignore.printStackTrace(); 121 } 122 } 123 124 return clientThread; 125 } 126 127 /*** Connect to the real remote server. 128 * @throws IOException 129 */ 130 public void listen() throws IOException, ClassNotFoundException 131 { 132 socket = new Socket(host, port); 133 LOGGER.info("Connecting to (" + host + ") on port " + port); 134 out = new ObjectOutputStream(socket.getOutputStream()); 135 in = new ObjectInputStream(socket.getInputStream()); 136 137 while (isListening = socket.isConnected()) 138 { 139 Event event = (Event) in.readObject(); 140 if (client != null) 141 { 142 client.onEvent(event); 143 } 144 } 145 } 146 147 /*** 148 * @see net.mlw.gfw.server.Server#addClient(java.lang.String, net.mlw.gfw.event.EventHandler) 149 */ 150 public void addClient(String name, EventHandler client) 151 { 152 if (this.client != null) 153 { 154 throw new RuntimeException("Only one connection is allowed from :" + this); 155 } 156 157 this.client = client; 158 onEvent(new ConnectEvent(name)); 159 } 160 161 /*** 162 * @see net.mlw.gfw.server.Server#removeClient(java.lang.String, net.mlw.gfw.event.EventHandler) 163 * 164 * @todo Send an event to notify server! 165 */ 166 public void removeClient(String name, EventHandler client) 167 { 168 if (this.client != null && this.client.equals(client)) 169 { 170 this.client = null; 171 } 172 } 173 174 /*** 175 * @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event) 176 * 177 * @todo Do something with the IOException 178 */ 179 public void onEvent(Event event) 180 { 181 try 182 { 183 LOGGER.info("Sending event to remote server: " + event); 184 out.writeObject(event); 185 } 186 catch (IOException e) 187 { 188 LOGGER.info(e.getMessage()); 189 } 190 } 191 192 }

This page was automatically generated by Maven