KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > slideshow > net > SlideServer


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.slideshow.net;
20
21 import java.awt.Image JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.swing.ImageIcon JavaDoc;
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 /**
39  * Server side implementation for slideshow
40  */

41 public class SlideServer
42 implements ObjectListener
43 {
44     //-- attributes
45
private SlideShow plugin;
46     private HashMap JavaDoc connections;
47     private HashMap JavaDoc acceptedConnections;
48
49     /**
50      * Constructor
51      *
52      * @param plugin the plugin instance
53      */

54     public SlideServer(SlideShow plugin)
55     {
56         this.plugin = plugin;
57         this.connections = new HashMap JavaDoc();
58         this.acceptedConnections = new HashMap JavaDoc();
59     }
60     
61     /**
62      * Send invitation to friends
63      *
64      * @param friends the connect infos
65      */

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     /**
81      * Send the user list (used on new accept/disconnect)
82      */

83     private void sendUserList()
84     {
85         String JavaDoc myName = Client.getInstance().getMyInfos().getName();
86         
87         ArrayList JavaDoc list = new ArrayList JavaDoc(acceptedConnections.size()+1);
88         list.add(myName);
89         list.addAll(acceptedConnections.keySet());
90         
91         Object JavaDoc[] 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     /**
99      * Send an image
100      *
101      * @param image the image to send
102      */

103     public void sendImage(Image JavaDoc image)
104     {
105         plugin.getStarterWindow().initStatus(plugin.tr("msg.broadcastingImage"));
106         SlideAction action = new SlideAction(SlideAction.SEND_IMAGE, new ImageIcon JavaDoc(image));
107         sendToEveryone(action, true);
108     }
109     
110     /**
111      * Disconnect
112      */

113     public void disconnect()
114     {
115         SlideAction action = new SlideAction(SlideAction.DISCONNECT, null);
116         sendToEveryone(action, false);
117         Iterator JavaDoc connections = this.connections.values().iterator();
118         while(connections.hasNext())
119         {
120             ObjectConnection oc = (ObjectConnection)connections.next();
121             oc.close();
122         }
123     }
124     
125     /**
126      * Send an action to every accepted connection
127      *
128      * @param action the action to send
129      */

130     private void sendToEveryone(final SlideAction action, final boolean newThread)
131     {
132         Iterator JavaDoc connections = acceptedConnections.values().iterator();
133         while(connections.hasNext())
134         {
135             final ObjectConnection oc = (ObjectConnection)connections.next();
136             Runnable JavaDoc sender = new Runnable JavaDoc() {
137                 public void run() {
138                     if(newThread)
139                         plugin.getStarterWindow().doProgress();
140
141                     try {
142                         oc.write(action);
143                     } catch(IOException JavaDoc ioe) {
144                         closeConnection(oc);
145                     }
146                     
147                     if(newThread)
148                         plugin.getStarterWindow().doProgress();
149                 }
150             };
151             
152             if(newThread)
153                 new Thread JavaDoc(sender).start();
154             else
155                 sender.run();
156         }
157         
158     }
159     
160     /**
161      * Close a connection and remove it from the maps
162      *
163      * @param oc the connection to close
164      */

165     private void closeConnection(ObjectConnection oc)
166     {
167         oc.close();
168         removeFromMap(acceptedConnections, oc);
169         removeFromMap(connections, oc);
170         sendUserList();
171     }
172     
173     /**
174      * Remove entries from a map, based on the value instead of the key
175      *
176      * @param map the map
177      * @param value the value to remove
178      */

179     private void removeFromMap(Map JavaDoc map, Object JavaDoc value)
180     {
181         synchronized(map)
182         {
183             Iterator JavaDoc entries = map.entrySet().iterator();
184             while(entries.hasNext())
185             {
186                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
187                 if(entry.getValue() == value)
188                     map.remove(entry.getKey());
189             }
190         }
191     }
192     
193     //-- ObjectListener interface
194

195     public void objectRead(Object JavaDoc o)
196     {
197         SlideAction action = (SlideAction)o;
198         String JavaDoc user = (String JavaDoc)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