001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.applications.whiteboard.net;
020:
021: import java.io.IOException;
022: import java.net.Socket;
023: import java.net.UnknownHostException;
024: import java.util.ArrayList;
025:
026: import org.jgraph.graph.GraphModel;
027: import org.lucane.applications.whiteboard.BoardAction;
028: import org.lucane.applications.whiteboard.GraphGui;
029: import org.lucane.applications.whiteboard.operations.GraphOperation;
030: import org.lucane.common.net.ObjectConnection;
031: import org.lucane.common.net.ObjectListener;
032:
033: public class BoardClient implements ObjectListener {
034: private String host;
035: private int port;
036:
037: private boolean connected;
038: private ObjectConnection connection;
039: private GraphGui gui;
040:
041: public BoardClient(String host, int port) {
042: this .host = host;
043: this .port = port;
044: this .connected = false;
045: }
046:
047: public void setGui(GraphGui gui) {
048: this .gui = gui;
049: }
050:
051: public void start() throws UnknownHostException, IOException {
052: Socket server = new Socket(host, port);
053: this .connection = new ObjectConnection(server);
054: this .connection.addObjectListener(this );
055: this .connection.listen();
056: this .connected = true;
057: }
058:
059: public void stop() {
060: this .connected = false;
061: this .connection.close();
062: }
063:
064: public boolean isConnected() {
065: return this .connected;
066: }
067:
068: //-- user management
069:
070: /**
071: * Join the board session
072: * @param user the user name
073: */
074: public void joinBoard(String user) throws IOException {
075: BoardAction action = new BoardAction(BoardAction.JOIN_BOARD,
076: user);
077: sendToServer(action);
078: }
079:
080: /**
081: * Leave the board session
082: * @param user the user name
083: */
084: public void leaveBoard(String user) throws IOException {
085: BoardAction action = new BoardAction(BoardAction.LEAVE_BOARD,
086: user);
087: sendToServer(action);
088: stop();
089: }
090:
091: //-- pen management
092:
093: /**
094: * Take the pen (or get queued)
095: * @param user the user
096: */
097: public void takePen(String user) throws IOException {
098: BoardAction action = new BoardAction(BoardAction.TAKE_PEN, user);
099: sendToServer(action);
100: }
101:
102: /**
103: * Release the pen (or remove from the queue)
104: * @param user the user
105: */
106: public void releasePen(String user) throws IOException {
107: BoardAction action = new BoardAction(BoardAction.RELEASE_PEN,
108: user);
109: sendToServer(action);
110: }
111:
112: //-- graph changes
113:
114: /**
115: * Broadcast a graph operation to everyone but me
116: *
117: * @param drawingUser the user that made the operation
118: * @param operation the graph operation
119: */
120: public void broadcastOperation(String drawingUser,
121: GraphOperation operation) throws IOException {
122: BoardAction action = new BoardAction(
123: BoardAction.BROADCAST_OPERATION, drawingUser, operation);
124: sendToServer(action);
125: }
126:
127: /**
128: * Broadcast a graph model to everyone but me
129: *
130: * @param drawingUser the user that made the operation
131: * @param model the graph model
132: */
133: public void broadcastModel(String drawingUser, GraphModel model)
134: throws IOException {
135: BoardAction action = new BoardAction(
136: BoardAction.BROADCAST_MODEL, drawingUser, model);
137: sendToServer(action);
138: }
139:
140: //-- reception
141:
142: private void userListChanged(ArrayList users) {
143: gui.changeUserList(users);
144: }
145:
146: private void penOwnerChanged(String user, int queueSize) {
147: gui.changePenOwner(user, queueSize);
148: }
149:
150: private void graphModelChanged(GraphModel model) {
151: gui.setModel(model);
152: }
153:
154: private void serverExit() {
155: gui.serverExit();
156: stop();
157: }
158:
159: private void graphChanged(GraphOperation operation) {
160: try {
161: gui.applyGraphOperation(operation);
162: } catch (Exception e) {
163: e.printStackTrace();
164: }
165: }
166:
167: //-- utilities
168:
169: private void sendToServer(BoardAction action) throws IOException {
170: if (this .connected)
171: this .connection.write(action);
172: }
173:
174: public void objectRead(Object o) {
175: BoardAction action = (BoardAction) o;
176: if (action.getAction() == BoardAction.SEND_COMPLETE_GRAPH)
177: graphModelChanged(action.getGraphModel());
178: else if (action.getAction() == BoardAction.BROADCAST_MODEL)
179: graphModelChanged(action.getGraphModel());
180: else if (action.getAction() == BoardAction.SEND_USER_LIST)
181: userListChanged(action.getUserList());
182: else if (action.getAction() == BoardAction.GIVE_PEN)
183: penOwnerChanged(action.getUser(), action.getQueueSize());
184: else if (action.getAction() == BoardAction.SERVER_EXIT)
185: serverExit();
186: else if (action.getAction() == BoardAction.BROADCAST_OPERATION)
187: graphChanged(action.getOperation());
188: }
189: }
|