KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > whiteboard > net > BoardServer


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.whiteboard.net;
20
21 import java.io.IOException JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
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 JavaDoc
34 {
35     private WhiteBoard parent;
36     
37     //server socket
38
private ServerSocket JavaDoc socket;
39     private boolean listening;
40     
41     //connected users
42
private ArrayList JavaDoc slaves;
43     
44     //users waiting for the pen
45
private ArrayList JavaDoc penQueue;
46     
47     
48     public BoardServer(WhiteBoard parent)
49     {
50         this.parent = parent;
51         this.slaves = new ArrayList JavaDoc();
52         this.penQueue = new ArrayList JavaDoc();
53     }
54
55     public void listen()
56     throws IOException JavaDoc
57     {
58         this.socket = new ServerSocket JavaDoc(0);
59         this.listening = true;
60         new Thread JavaDoc(this).start();
61     }
62     
63     public int getPort()
64     {
65         return this.socket.getLocalPort();
66     }
67     
68     public void stop()
69     throws IOException JavaDoc
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 JavaDoc client = socket.accept();
81                 BoardSlave slave = new BoardSlave(this, client);
82                 slaves.add(slave);
83                 slave.start();
84             } catch(IOException JavaDoc ioe) {
85                 ioe.printStackTrace();
86             }
87         }
88     }
89     
90     //-- user management
91

92     /**
93      * A user is joining the board session
94      * @param connection the connection
95      * @param user the user name
96      */

97     public void joinBoard(ObjectConnection connection, String JavaDoc 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 JavaDoc 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 JavaDoc drawer = (String JavaDoc)this.penQueue.get(0);
114                 BoardAction action = new BoardAction(BoardAction.GIVE_PEN, drawer);
115                 connection.write(action);
116             }
117         } catch(IOException JavaDoc ioe) {
118             Logging.getLogger().warning("Unable to send drawing user to '" + user
119                     + "' : " + ioe);
120         }
121     }
122     
123     /**
124      * A user is leaving the board session
125      * @param user the user name
126      */

127     public void leaveBoard(String JavaDoc 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     /**
145      * Send the userlist to every connected user
146      */

147     private void sendUserList()
148     {
149         //extract user list
150
ArrayList JavaDoc users = new ArrayList JavaDoc(slaves.size());
151         Iterator JavaDoc 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         //send it to everyone
160
BoardAction action = new BoardAction(BoardAction.SEND_USER_LIST, users);
161         sendToEveryone(action);
162     }
163     
164     //-- pen management
165

166     /**
167      * A user wants to take the pen
168      * @param user the user
169      */

170     public void takePen(String JavaDoc user)
171     {
172         if(!this.penQueue.contains(user))
173         {
174             this.penQueue.add(user);
175             givePen();
176         }
177     }
178     
179     /**
180      * A user releases the pen
181      * @param user the user
182      */

183     public void releasePen(String JavaDoc user)
184     {
185         this.penQueue.remove(user);
186         givePen();
187     }
188     
189     /**
190      * Give the pen to the first user in the queue (or null for nobody)
191      */

192     private void givePen()
193     {
194         //get the next user to have the pen
195
String JavaDoc user = null;
196         if(this.penQueue.size() > 0)
197             user = (String JavaDoc)this.penQueue.get(0);
198         
199         //send it to everyone
200
BoardAction action = new BoardAction(BoardAction.GIVE_PEN, user, penQueue.size());
201         sendToEveryone(action);
202     }
203     
204     //-- other
205

206     /**
207      * Broadcast a graph operation to everyone but the drawing user
208      *
209      * @param drawingUser the user that made the operation
210      * @param action the action containing the graph operation
211      */

212     public void broadcast(String JavaDoc drawingUser, BoardAction action)
213     {
214         Iterator JavaDoc i = slaves.iterator();
215         while(i.hasNext())
216         {
217             BoardSlave slave = (BoardSlave)i.next();
218         
219             //don't send to drawing user or unconnected ones
220
if(slave.getUser() == null || slave.getUser().equals(drawingUser))
221                 continue;
222                 
223             try {
224                 slave.write(action);
225             } catch(IOException JavaDoc ioe) {
226                 Logging.getLogger().info("Unable to reach user, disconnecting : " + slave.getUser());
227                 slave.stop();
228                 sendUserList();
229             }
230         }
231     }
232     
233     /**
234      * Send an action to everyone
235      * @param action the action to send
236      */

237     private void sendToEveryone(BoardAction action)
238     {
239         Iterator JavaDoc i = slaves.iterator();
240         while(i.hasNext())
241         {
242             BoardSlave slave = (BoardSlave)i.next();
243             
244             //don't send to unconnected users
245
if(slave.getUser() == null)
246                 continue;
247             
248             try {
249                 slave.write(action);
250             } catch(IOException JavaDoc ioe) {
251                 Logging.getLogger().info("Unable to reach user, disconnecting : " + slave.getUser());
252                 slave.stop();
253                 sendUserList();
254             }
255         }
256     }
257 }
Popular Tags