1 19 package org.lucane.applications.slideshow.net; 20 21 import java.awt.Image ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import javax.swing.ImageIcon ; 29 30 31 import org.lucane.applications.slideshow.SlideShow; 32 import org.lucane.client.Client; 33 import org.lucane.client.Communicator; 34 import org.lucane.common.ConnectInfo; 35 import org.lucane.common.net.ObjectConnection; 36 import org.lucane.common.net.ObjectListener; 37 38 41 public class SlideServer 42 implements ObjectListener 43 { 44 private SlideShow plugin; 46 private HashMap connections; 47 private HashMap acceptedConnections; 48 49 54 public SlideServer(SlideShow plugin) 55 { 56 this.plugin = plugin; 57 this.connections = new HashMap (); 58 this.acceptedConnections = new HashMap (); 59 } 60 61 66 public void sendInvitations(ConnectInfo[] friends) 67 { 68 sendUserList(); 69 70 for(int i=0;i<friends.length;i++) 71 { 72 ObjectConnection oc = 73 Communicator.getInstance().sendMessageTo(friends[i], plugin.getName(), ""); 74 oc.addObjectListener(this); 75 oc.listen(); 76 connections.put(friends[i].getName(), oc); 77 } 78 } 79 80 83 private void sendUserList() 84 { 85 String myName = Client.getInstance().getMyInfos().getName(); 86 87 ArrayList list = new ArrayList (acceptedConnections.size()+1); 88 list.add(myName); 89 list.addAll(acceptedConnections.keySet()); 90 91 Object [] userNames = list.toArray(); 92 plugin.getStarterWindow().setUsers(userNames); 93 94 SlideAction action = new SlideAction(SlideAction.SEND_USER_LIST, userNames); 95 sendToEveryone(action, false); 96 } 97 98 103 public void sendImage(Image image) 104 { 105 plugin.getStarterWindow().initStatus(plugin.tr("msg.broadcastingImage")); 106 SlideAction action = new SlideAction(SlideAction.SEND_IMAGE, new ImageIcon (image)); 107 sendToEveryone(action, true); 108 } 109 110 113 public void disconnect() 114 { 115 SlideAction action = new SlideAction(SlideAction.DISCONNECT, null); 116 sendToEveryone(action, false); 117 Iterator connections = this.connections.values().iterator(); 118 while(connections.hasNext()) 119 { 120 ObjectConnection oc = (ObjectConnection)connections.next(); 121 oc.close(); 122 } 123 } 124 125 130 private void sendToEveryone(final SlideAction action, final boolean newThread) 131 { 132 Iterator connections = acceptedConnections.values().iterator(); 133 while(connections.hasNext()) 134 { 135 final ObjectConnection oc = (ObjectConnection)connections.next(); 136 Runnable sender = new Runnable () { 137 public void run() { 138 if(newThread) 139 plugin.getStarterWindow().doProgress(); 140 141 try { 142 oc.write(action); 143 } catch(IOException ioe) { 144 closeConnection(oc); 145 } 146 147 if(newThread) 148 plugin.getStarterWindow().doProgress(); 149 } 150 }; 151 152 if(newThread) 153 new Thread (sender).start(); 154 else 155 sender.run(); 156 } 157 158 } 159 160 165 private void closeConnection(ObjectConnection oc) 166 { 167 oc.close(); 168 removeFromMap(acceptedConnections, oc); 169 removeFromMap(connections, oc); 170 sendUserList(); 171 } 172 173 179 private void removeFromMap(Map map, Object value) 180 { 181 synchronized(map) 182 { 183 Iterator entries = map.entrySet().iterator(); 184 while(entries.hasNext()) 185 { 186 Map.Entry entry = (Map.Entry )entries.next(); 187 if(entry.getValue() == value) 188 map.remove(entry.getKey()); 189 } 190 } 191 } 192 193 195 public void objectRead(Object o) 196 { 197 SlideAction action = (SlideAction)o; 198 String user = (String )action.getParam(); 199 200 switch(action.getAction()) 201 { 202 case SlideAction.ACCEPT_SHOW: 203 acceptedConnections.put(user, connections.get(user)); 204 sendUserList(); 205 break; 206 207 case SlideAction.REJECT_SHOW: 208 closeConnection((ObjectConnection)connections.get(user)); 209 break; 210 211 case SlideAction.DISCONNECT: 212 closeConnection((ObjectConnection)connections.get(user)); 213 sendUserList(); 214 break; 215 } 216 } 217 } | Popular Tags |