KickJava   Java API By Example, From Geeks To Geeks.

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


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.Socket JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import org.jgraph.graph.GraphModel;
27 import org.lucane.applications.whiteboard.BoardAction;
28 import org.lucane.applications.whiteboard.GraphGui;
29 import org.lucane.applications.whiteboard.operations.GraphOperation;
30 import org.lucane.common.net.ObjectConnection;
31 import org.lucane.common.net.ObjectListener;
32
33 public class BoardClient implements ObjectListener
34 {
35     private String JavaDoc host;
36     private int port;
37     
38     private boolean connected;
39     private ObjectConnection connection;
40     private GraphGui gui;
41         
42     public BoardClient(String JavaDoc host, int port)
43     {
44         this.host = host;
45         this.port = port;
46         this.connected = false;
47     }
48     
49     public void setGui(GraphGui gui)
50     {
51         this.gui = gui;
52     }
53     
54     public void start()
55     throws UnknownHostException JavaDoc, IOException JavaDoc
56     {
57         Socket JavaDoc server = new Socket JavaDoc(host, port);
58         this.connection = new ObjectConnection(server);
59         this.connection.addObjectListener(this);
60         this.connection.listen();
61         this.connected = true;
62     }
63     
64     public void stop()
65     {
66         this.connected = false;
67         this.connection.close();
68     }
69     
70     public boolean isConnected()
71     {
72         return this.connected;
73     }
74
75     
76     //-- user management
77

78     /**
79      * Join the board session
80      * @param user the user name
81      */

82     public void joinBoard(String JavaDoc user)
83     throws IOException JavaDoc
84     {
85         BoardAction action = new BoardAction(BoardAction.JOIN_BOARD, user);
86         sendToServer(action);
87     }
88     
89     /**
90      * Leave the board session
91      * @param user the user name
92      */

93     public void leaveBoard(String JavaDoc user)
94     throws IOException JavaDoc
95     {
96         BoardAction action = new BoardAction(BoardAction.LEAVE_BOARD, user);
97         sendToServer(action);
98         stop();
99     }
100     
101     //-- pen management
102

103     /**
104      * Take the pen (or get queued)
105      * @param user the user
106      */

107     public void takePen(String JavaDoc user)
108     throws IOException JavaDoc
109     {
110         BoardAction action = new BoardAction(BoardAction.TAKE_PEN, user);
111         sendToServer(action);
112     }
113     
114     /**
115      * Release the pen (or remove from the queue)
116      * @param user the user
117      */

118     public void releasePen(String JavaDoc user)
119     throws IOException JavaDoc
120     {
121         BoardAction action = new BoardAction(BoardAction.RELEASE_PEN, user);
122         sendToServer(action);
123     }
124     
125     
126     //-- graph changes
127

128     /**
129      * Broadcast a graph operation to everyone but me
130      *
131      * @param drawingUser the user that made the operation
132      * @param operation the graph operation
133      */

134     public void broadcastOperation(String JavaDoc drawingUser, GraphOperation operation)
135     throws IOException JavaDoc
136     {
137         BoardAction action = new BoardAction(BoardAction.BROADCAST_OPERATION, drawingUser, operation);
138         sendToServer(action);
139     }
140     
141     /**
142      * Broadcast a graph model to everyone but me
143      *
144      * @param drawingUser the user that made the operation
145      * @param model the graph model
146      */

147     public void broadcastModel(String JavaDoc drawingUser, GraphModel model)
148     throws IOException JavaDoc
149     {
150         BoardAction action = new BoardAction(BoardAction.BROADCAST_MODEL, drawingUser, model);
151         sendToServer(action);
152     }
153     
154     //-- reception
155

156     private void userListChanged(ArrayList JavaDoc users)
157     {
158         gui.changeUserList(users);
159     }
160     
161     private void penOwnerChanged(String JavaDoc user, int queueSize)
162     {
163         gui.changePenOwner(user, queueSize);
164     }
165     
166     private void graphModelChanged(GraphModel model)
167     {
168         gui.setModel(model);
169     }
170     
171     private void serverExit()
172     {
173         gui.serverExit();
174         stop();
175     }
176     
177     private void graphChanged(GraphOperation operation)
178     {
179         try {
180             gui.applyGraphOperation(operation);
181         } catch(Exception JavaDoc e) {
182             e.printStackTrace();
183         }
184     }
185     
186     //-- utilities
187

188     private void sendToServer(BoardAction action)
189     throws IOException JavaDoc
190     {
191         if(this.connected)
192             this.connection.write(action);
193     }
194     
195     public void objectRead(Object JavaDoc o)
196     {
197         BoardAction action = (BoardAction)o;
198         if(action.getAction() == BoardAction.SEND_COMPLETE_GRAPH)
199             graphModelChanged(action.getGraphModel());
200         else if(action.getAction() == BoardAction.BROADCAST_MODEL)
201             graphModelChanged(action.getGraphModel());
202         else if(action.getAction() == BoardAction.SEND_USER_LIST)
203             userListChanged(action.getUserList());
204         else if(action.getAction() == BoardAction.GIVE_PEN)
205             penOwnerChanged(action.getUser(), action.getQueueSize());
206         else if(action.getAction() == BoardAction.SERVER_EXIT)
207             serverExit();
208         else if(action.getAction() == BoardAction.BROADCAST_OPERATION)
209             graphChanged(action.getOperation());
210     }
211 }
Popular Tags