1 19 package org.lucane.applications.whiteboard.net; 20 21 import java.io.IOException ; 22 import java.net.ServerSocket ; 23 import java.net.Socket ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 27 import org.lucane.applications.whiteboard.BoardAction; 28 import org.lucane.applications.whiteboard.WhiteBoard; 29 import org.lucane.client.Client; 30 import org.lucane.common.Logging; 31 import org.lucane.common.net.ObjectConnection; 32 33 public class BoardServer implements Runnable 34 { 35 private WhiteBoard parent; 36 37 private ServerSocket socket; 39 private boolean listening; 40 41 private ArrayList slaves; 43 44 private ArrayList penQueue; 46 47 48 public BoardServer(WhiteBoard parent) 49 { 50 this.parent = parent; 51 this.slaves = new ArrayList (); 52 this.penQueue = new ArrayList (); 53 } 54 55 public void listen() 56 throws IOException 57 { 58 this.socket = new ServerSocket (0); 59 this.listening = true; 60 new Thread (this).start(); 61 } 62 63 public int getPort() 64 { 65 return this.socket.getLocalPort(); 66 } 67 68 public void stop() 69 throws IOException 70 { 71 this.listening = false; 72 this.socket.close(); 73 } 74 75 public void run() 76 { 77 while(this.listening) 78 { 79 try { 80 Socket client = socket.accept(); 81 BoardSlave slave = new BoardSlave(this, client); 82 slaves.add(slave); 83 slave.start(); 84 } catch(IOException ioe) { 85 ioe.printStackTrace(); 86 } 87 } 88 } 89 90 92 97 public void joinBoard(ObjectConnection connection, String user) 98 { 99 sendUserList(); 100 101 try { 102 BoardAction action = new BoardAction(BoardAction.SEND_COMPLETE_GRAPH, 103 parent.getGraphModel()); 104 connection.write(action); 105 } catch(IOException ioe) { 106 Logging.getLogger().warning("Unable to send complete graph to '" + user 107 + "' : " + ioe); 108 } 109 110 try { 111 if(this.penQueue.size() > 0) 112 { 113 String drawer = (String )this.penQueue.get(0); 114 BoardAction action = new BoardAction(BoardAction.GIVE_PEN, drawer); 115 connection.write(action); 116 } 117 } catch(IOException ioe) { 118 Logging.getLogger().warning("Unable to send drawing user to '" + user 119 + "' : " + ioe); 120 } 121 } 122 123 127 public void leaveBoard(String user) 128 { 129 if(penQueue.remove(user)) 130 givePen(); 131 132 if(user.equals(Client.getInstance().getMyInfos().getName())) 133 exitServer(); 134 else 135 sendUserList(); 136 } 137 138 private void exitServer() 139 { 140 BoardAction action = new BoardAction(BoardAction.SERVER_EXIT); 141 sendToEveryone(action); 142 } 143 144 147 private void sendUserList() 148 { 149 ArrayList users = new ArrayList (slaves.size()); 151 Iterator i = slaves.iterator(); 152 while(i.hasNext()) 153 { 154 BoardSlave slave = (BoardSlave)i.next(); 155 if(slave.getUser() != null) 156 users.add(slave.getUser()); 157 } 158 159 BoardAction action = new BoardAction(BoardAction.SEND_USER_LIST, users); 161 sendToEveryone(action); 162 } 163 164 166 170 public void takePen(String user) 171 { 172 if(!this.penQueue.contains(user)) 173 { 174 this.penQueue.add(user); 175 givePen(); 176 } 177 } 178 179 183 public void releasePen(String user) 184 { 185 this.penQueue.remove(user); 186 givePen(); 187 } 188 189 192 private void givePen() 193 { 194 String user = null; 196 if(this.penQueue.size() > 0) 197 user = (String )this.penQueue.get(0); 198 199 BoardAction action = new BoardAction(BoardAction.GIVE_PEN, user, penQueue.size()); 201 sendToEveryone(action); 202 } 203 204 206 212 public void broadcast(String drawingUser, BoardAction action) 213 { 214 Iterator i = slaves.iterator(); 215 while(i.hasNext()) 216 { 217 BoardSlave slave = (BoardSlave)i.next(); 218 219 if(slave.getUser() == null || slave.getUser().equals(drawingUser)) 221 continue; 222 223 try { 224 slave.write(action); 225 } catch(IOException ioe) { 226 Logging.getLogger().info("Unable to reach user, disconnecting : " + slave.getUser()); 227 slave.stop(); 228 sendUserList(); 229 } 230 } 231 } 232 233 237 private void sendToEveryone(BoardAction action) 238 { 239 Iterator i = slaves.iterator(); 240 while(i.hasNext()) 241 { 242 BoardSlave slave = (BoardSlave)i.next(); 243 244 if(slave.getUser() == null) 246 continue; 247 248 try { 249 slave.write(action); 250 } catch(IOException ioe) { 251 Logging.getLogger().info("Unable to reach user, disconnecting : " + slave.getUser()); 252 slave.stop(); 253 sendUserList(); 254 } 255 } 256 } 257 } | Popular Tags |