f@0
|
1 /*
|
f@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
f@0
|
3
|
f@0
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
f@0
|
5
|
f@0
|
6 This program is free software: you can redistribute it and/or modify
|
f@0
|
7 it under the terms of the GNU General Public License as published by
|
f@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
f@0
|
9 (at your option) any later version.
|
f@0
|
10
|
f@0
|
11 This program is distributed in the hope that it will be useful,
|
f@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@0
|
14 GNU General Public License for more details.
|
f@0
|
15
|
f@0
|
16 You should have received a copy of the GNU General Public License
|
f@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@0
|
18 */
|
f@0
|
19
|
f@0
|
20 package uk.ac.qmul.eecs.ccmi.network;
|
f@0
|
21
|
f@0
|
22 import java.awt.geom.Point2D;
|
f@0
|
23 import java.awt.geom.Rectangle2D;
|
f@0
|
24 import java.io.IOException;
|
f@0
|
25 import java.nio.channels.SocketChannel;
|
f@0
|
26 import java.util.Queue;
|
f@0
|
27 import java.util.Set;
|
f@0
|
28
|
f@0
|
29 import javax.swing.tree.TreeNode;
|
f@0
|
30
|
f@0
|
31 import uk.ac.qmul.eecs.ccmi.diagrammodel.CollectionModel;
|
f@0
|
32 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramElement;
|
f@0
|
33 import uk.ac.qmul.eecs.ccmi.diagrammodel.DiagramTreeNode;
|
f@0
|
34 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
|
f@0
|
35 import uk.ac.qmul.eecs.ccmi.diagrammodel.TreeModel;
|
f@0
|
36 import uk.ac.qmul.eecs.ccmi.gui.Diagram;
|
f@0
|
37 import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource;
|
f@0
|
38 import uk.ac.qmul.eecs.ccmi.gui.DiagramModelUpdater;
|
f@0
|
39 import uk.ac.qmul.eecs.ccmi.gui.Edge;
|
f@0
|
40 import uk.ac.qmul.eecs.ccmi.gui.GraphElement;
|
f@0
|
41 import uk.ac.qmul.eecs.ccmi.gui.Lock;
|
f@0
|
42 import uk.ac.qmul.eecs.ccmi.gui.Node;
|
f@0
|
43 import uk.ac.qmul.eecs.ccmi.gui.awareness.AwarenessPanel;
|
f@0
|
44 import uk.ac.qmul.eecs.ccmi.gui.persistence.PrototypePersistenceDelegate;
|
f@0
|
45 import uk.ac.qmul.eecs.ccmi.network.ClientConnectionManager.LockAnswer;
|
f@0
|
46 import uk.ac.qmul.eecs.ccmi.utils.ExceptionHandler;
|
f@0
|
47
|
f@0
|
48 /**
|
f@0
|
49 *
|
f@0
|
50 * A NetDiagram is a diagram that is shared by connecting to a server on either a remote or local host.
|
f@0
|
51 * That means that other users from other computers can modify the diagram model through the server.
|
f@0
|
52 * A NetDiagram is created by wrapping a local diagram (a diagram open in the local editor) into a NetDiagram class.
|
f@0
|
53 * The wrapped diagram works as a delegate. What Really changes between a local diagram and a network diagram is
|
f@0
|
54 * that the modelUpdater will directly affect the diagram model for the former and exchange messages with the server
|
f@0
|
55 * for the latter. In the case of a network diagram the changes to the model are actually made by a {@link ClientConnectionManager}
|
f@0
|
56 * thread upon receiving a message from the server.
|
f@0
|
57 *
|
f@0
|
58 */
|
f@0
|
59 public abstract class NetDiagram extends Diagram {
|
f@0
|
60
|
f@0
|
61 private NetDiagram(Diagram delegateDiagram){
|
f@0
|
62 this.delegateDiagram = delegateDiagram;
|
f@0
|
63 innerModelUpdater = new InnerModelUpdater();
|
f@0
|
64 }
|
f@0
|
65
|
f@0
|
66 public static NetDiagram wrapRemoteHost(Diagram diagram, ClientConnectionManager connectionManager,SocketChannel channel){
|
f@0
|
67 return new RemoteHostDiagram(diagram,connectionManager,channel);
|
f@0
|
68 }
|
f@0
|
69
|
f@0
|
70 public static NetDiagram wrapLocalHost(Diagram diagram, SocketChannel channel, Queue<DiagramElement> dElements, ExceptionHandler handler){
|
f@0
|
71 return new LocalHostDiagram(diagram,channel,dElements,handler);
|
f@0
|
72 }
|
f@0
|
73
|
f@0
|
74 @Override
|
f@0
|
75 public String getName(){
|
f@0
|
76 return delegateDiagram.getName();
|
f@0
|
77 }
|
f@0
|
78
|
f@0
|
79 @Override
|
f@0
|
80 public String toString(){
|
f@0
|
81 return getName();
|
f@0
|
82 }
|
f@0
|
83
|
f@0
|
84 @Override
|
f@0
|
85 public PrototypePersistenceDelegate getPrototypePersistenceDelegate(){
|
f@0
|
86 return delegateDiagram.getPrototypePersistenceDelegate();
|
f@0
|
87 }
|
f@0
|
88
|
f@0
|
89 @Override
|
f@0
|
90 public TreeModel<Node,Edge> getTreeModel(){
|
f@0
|
91 return delegateDiagram.getTreeModel();
|
f@0
|
92 }
|
f@0
|
93
|
f@0
|
94 @Override
|
f@0
|
95 public CollectionModel<Node,Edge> getCollectionModel(){
|
f@0
|
96 return delegateDiagram.getCollectionModel();
|
f@0
|
97 }
|
f@0
|
98
|
f@0
|
99 @Override
|
f@0
|
100 public void setName(String name) {
|
f@0
|
101 delegateDiagram.setName(name);
|
f@0
|
102 }
|
f@0
|
103
|
f@0
|
104 @Override
|
f@0
|
105 public Node[] getNodePrototypes() {
|
f@0
|
106 return delegateDiagram.getNodePrototypes();
|
f@0
|
107 }
|
f@0
|
108
|
f@0
|
109 @Override
|
f@0
|
110 public Edge[] getEdgePrototypes() {
|
f@0
|
111 return delegateDiagram.getEdgePrototypes();
|
f@0
|
112 }
|
f@0
|
113
|
f@0
|
114 @Override
|
f@0
|
115 public DiagramModelUpdater getModelUpdater(){
|
f@0
|
116 return innerModelUpdater;
|
f@0
|
117 }
|
f@0
|
118
|
f@0
|
119 public Diagram getDelegate(){
|
f@0
|
120 return delegateDiagram;
|
f@0
|
121 }
|
f@0
|
122
|
f@0
|
123 public abstract void enableAwareness(AwarenessPanel panel);
|
f@0
|
124
|
f@0
|
125 public abstract void disableAwareness(AwarenessPanel panel);
|
f@0
|
126
|
f@0
|
127 public abstract SocketChannel getSocketChannel();
|
f@0
|
128
|
f@0
|
129 protected abstract void send(Command cmd, DiagramElement element);
|
f@0
|
130
|
f@0
|
131 protected abstract void send(Command cmd, DiagramTreeNode treeNode);
|
f@0
|
132
|
f@0
|
133 protected abstract void send(LockMessage lockMessage);
|
f@0
|
134
|
f@0
|
135 protected abstract void send(AwarenessMessage awMsg);
|
f@0
|
136
|
f@0
|
137 protected abstract boolean receiveLockAnswer();
|
f@0
|
138
|
f@0
|
139 private Diagram delegateDiagram;
|
f@0
|
140 private InnerModelUpdater innerModelUpdater;
|
f@0
|
141 public static String LOCALHOST_STRING = " @ localhost";
|
f@0
|
142
|
f@0
|
143 private class InnerModelUpdater implements DiagramModelUpdater {
|
f@0
|
144 @Override
|
f@0
|
145 public boolean getLock(DiagramTreeNode treeNode, Lock lock, DiagramEventActionSource actionSource) {
|
f@0
|
146 try {
|
f@0
|
147 sendLockMessage(treeNode,lock,true,actionSource);
|
f@0
|
148 }catch(IllegalArgumentException iae){
|
f@0
|
149 return false;
|
f@0
|
150 }
|
f@0
|
151 return receiveLockAnswer();
|
f@0
|
152 }
|
f@0
|
153
|
f@0
|
154 @Override
|
f@0
|
155 public void yieldLock(DiagramTreeNode treeNode, Lock lock, DiagramEventActionSource actionSource) {
|
f@0
|
156 try {
|
f@0
|
157 sendLockMessage(treeNode,lock,false,actionSource);
|
f@0
|
158 }catch(IllegalArgumentException iae) {}
|
f@0
|
159 }
|
f@0
|
160
|
f@0
|
161 @Override
|
f@0
|
162 public void sendAwarenessMessage(AwarenessMessage.Name awMsgName, Object source){
|
f@0
|
163 if(source instanceof DiagramEventActionSource)
|
f@0
|
164 send(new AwarenessMessage(awMsgName,getName(),(DiagramEventActionSource)source));
|
f@0
|
165 else if(source instanceof String){
|
f@0
|
166 send(new AwarenessMessage(awMsgName,getName(),(String)source));
|
f@0
|
167 }
|
f@0
|
168 }
|
f@0
|
169
|
f@0
|
170 private void sendLockMessage(DiagramTreeNode treeNode, Lock lock, boolean isGettingLock, DiagramEventActionSource source){
|
f@0
|
171 TreeNode[] path = treeNode.getPath();
|
f@0
|
172 Object[] args = new Object[path.length-1];
|
f@0
|
173 if(args.length == 0 && !treeNode.isRoot())
|
f@0
|
174 throw new IllegalArgumentException("it's a node no longer connected with the tree");
|
f@0
|
175 for(int i=0;i<path.length-1;i++){
|
f@0
|
176 args[i] = delegateDiagram.getTreeModel().getIndexOfChild(path[i], path[i+1]);
|
f@0
|
177 }
|
f@0
|
178 send(new LockMessage(
|
f@0
|
179 LockMessageConverter.getLockMessageNamefromLock(lock,isGettingLock),
|
f@0
|
180 delegateDiagram.getName(),
|
f@0
|
181 args,
|
f@0
|
182 source
|
f@0
|
183 ));
|
f@0
|
184 }
|
f@0
|
185
|
f@0
|
186 @Override
|
f@0
|
187 public void insertInCollection(DiagramElement element,DiagramEventSource source) {
|
f@0
|
188 boolean isNode = false;
|
f@0
|
189 if(element instanceof Node)
|
f@0
|
190 isNode = true;
|
f@0
|
191
|
f@0
|
192 Command cmd = null;
|
f@0
|
193 if(isNode){
|
f@0
|
194 Rectangle2D bounds = ((Node)element).getBounds();
|
f@0
|
195 cmd = new Command(
|
f@0
|
196 Command.Name.INSERT_NODE,
|
f@0
|
197 delegateDiagram.getName(),
|
f@0
|
198 new Object[] {element.getType(),bounds.getX(),bounds.getY()},
|
f@0
|
199 makeRemote(source)
|
f@0
|
200 );
|
f@0
|
201 }else{
|
f@0
|
202 Edge edge = (Edge)element;
|
f@0
|
203 Object args[] = new Object[1+edge.getNodesNum()];
|
f@0
|
204 args[0] = edge.getType();
|
f@0
|
205 /* the args of the command will be the id's of the connected edges */
|
f@0
|
206 for(int i = 1; i< args.length; i++)
|
f@0
|
207 args[i] = edge.getNodeAt(i-1).getId();
|
f@0
|
208 cmd = new Command(
|
f@0
|
209 Command.Name.INSERT_EDGE,
|
f@0
|
210 delegateDiagram.getName(),
|
f@0
|
211 args,
|
f@0
|
212 makeRemote(source)
|
f@0
|
213 );
|
f@0
|
214 }
|
f@0
|
215 send(cmd,element);
|
f@0
|
216 }
|
f@0
|
217
|
f@0
|
218 @Override
|
f@0
|
219 public void insertInTree(DiagramElement element) {
|
f@0
|
220 insertInCollection(element,DiagramEventSource.TREE);
|
f@0
|
221 }
|
f@0
|
222
|
f@0
|
223 @Override
|
f@0
|
224 public void takeOutFromCollection(DiagramElement element,DiagramEventSource source) {
|
f@0
|
225 boolean isNode = false;
|
f@0
|
226 if(element instanceof Node)
|
f@0
|
227 isNode = true;
|
f@0
|
228 Command cmd = new Command(
|
f@0
|
229 isNode ? Command.Name.REMOVE_NODE : Command.Name.REMOVE_EDGE,
|
f@0
|
230 delegateDiagram.getName(),
|
f@0
|
231 new Object[] {element.getId()},
|
f@0
|
232 makeRemote(source)
|
f@0
|
233 );
|
f@0
|
234 send(cmd,element);
|
f@0
|
235 }
|
f@0
|
236
|
f@0
|
237 @Override
|
f@0
|
238 public void takeOutFromTree(DiagramElement element) {
|
f@0
|
239 takeOutFromCollection(element,DiagramEventSource.TREE);
|
f@0
|
240 }
|
f@0
|
241
|
f@0
|
242 @Override
|
f@0
|
243 public void setName(DiagramElement element, String name, DiagramEventSource source) {
|
f@0
|
244 send(new Command(
|
f@0
|
245 element instanceof Node ? Command.Name.SET_NODE_NAME : Command.Name.SET_EDGE_NAME,
|
f@0
|
246 delegateDiagram.getName(),
|
f@0
|
247 new Object[] {element.getId(), name},
|
f@0
|
248 makeRemote(source)),
|
f@0
|
249 element);
|
f@0
|
250 }
|
f@0
|
251
|
f@0
|
252 @Override
|
f@0
|
253 public void setNotes(DiagramTreeNode treeNode, String notes, DiagramEventSource source) {
|
f@0
|
254 TreeNode[] path = treeNode.getPath();
|
f@0
|
255 Object[] args = new Object[path.length];
|
f@0
|
256 for(int i=0;i<path.length-1;i++){
|
f@0
|
257 args[i] = delegateDiagram.getTreeModel().getIndexOfChild(path[i], path[i+1]);
|
f@0
|
258 }
|
f@0
|
259 args[args.length-1] = notes;
|
f@0
|
260 Command cmd = new Command(Command.Name.SET_NOTES, delegateDiagram.getName(),args,makeRemote(source));
|
f@0
|
261 send(cmd,treeNode);
|
f@0
|
262 }
|
f@0
|
263
|
f@0
|
264 @Override
|
f@0
|
265 public void setProperty(Node node, String type, int index, String value, DiagramEventSource source) {
|
f@0
|
266 send(new Command(Command.Name.SET_PROPERTY,
|
f@0
|
267 delegateDiagram.getName(),
|
f@0
|
268 new Object[] {node.getId(),type,index,value},
|
f@0
|
269 makeRemote(source)),
|
f@0
|
270 node
|
f@0
|
271 );
|
f@0
|
272 }
|
f@0
|
273
|
f@0
|
274 @Override
|
f@0
|
275 public void setProperties(Node node, NodeProperties properties, DiagramEventSource source) {
|
f@0
|
276 send(new Command(Command.Name.SET_PROPERTIES,
|
f@0
|
277 delegateDiagram.getName(),
|
f@0
|
278 new Object[] {node.getId(),properties.toString()},
|
f@0
|
279 makeRemote(source)),
|
f@0
|
280 node
|
f@0
|
281 );
|
f@0
|
282
|
f@0
|
283 }
|
f@0
|
284
|
f@0
|
285 @Override
|
f@0
|
286 public void clearProperties(Node node, DiagramEventSource source) {
|
f@0
|
287 send(new Command(Command.Name.CLEAR_PROPERTIES,
|
f@0
|
288 delegateDiagram.getName(),
|
f@0
|
289 new Object[] {node.getId()},
|
f@0
|
290 makeRemote(source)),
|
f@0
|
291 node
|
f@0
|
292 );
|
f@0
|
293 }
|
f@0
|
294
|
f@0
|
295 @Override
|
f@0
|
296 public void addProperty(Node node, String type, String value, DiagramEventSource source) {
|
f@0
|
297 send(new Command(Command.Name.ADD_PROPERTY,
|
f@0
|
298 delegateDiagram.getName(),
|
f@0
|
299 new Object[] {node.getId(),type,value},
|
f@0
|
300 makeRemote(source)),
|
f@0
|
301 node
|
f@0
|
302 );
|
f@0
|
303 }
|
f@0
|
304
|
f@0
|
305 @Override
|
f@0
|
306 public void removeProperty(Node node, String type, int index, DiagramEventSource source) {
|
f@0
|
307 send(new Command(Command.Name.REMOVE_PROPERTY,
|
f@0
|
308 delegateDiagram.getName(),
|
f@0
|
309 new Object[] {node.getId(),type,index},
|
f@0
|
310 makeRemote(source)),
|
f@0
|
311 node
|
f@0
|
312 );
|
f@0
|
313 }
|
f@0
|
314
|
f@0
|
315 @Override
|
f@0
|
316 public void setModifiers(Node node, String type, int index,
|
f@0
|
317 Set<Integer> modifiers, DiagramEventSource source) {
|
f@0
|
318 Object args[] = new Object[modifiers.size()+3];
|
f@0
|
319 args[0] = node.getId();
|
f@0
|
320 args[1] = type;
|
f@0
|
321 args[2] = index;
|
f@0
|
322 int i = 0;
|
f@0
|
323 for(Integer I : modifiers){
|
f@0
|
324 args[i+3] = I;
|
f@0
|
325 i++;
|
f@0
|
326 }
|
f@0
|
327 send(new Command(Command.Name.SET_MODIFIERS, delegateDiagram.getName(),args,makeRemote(source)),node);
|
f@0
|
328 }
|
f@0
|
329
|
f@0
|
330 @Override
|
f@0
|
331 public void setEndLabel(Edge edge, Node node, String label, DiagramEventSource source) {
|
f@0
|
332 send(new Command(Command.Name.SET_ENDLABEL, delegateDiagram.getName(),
|
f@0
|
333 new Object[] {edge.getId(), node.getId(), label},makeRemote(source)),
|
f@0
|
334 edge
|
f@0
|
335 );
|
f@0
|
336 }
|
f@0
|
337
|
f@0
|
338 @Override
|
f@0
|
339 public void setEndDescription(Edge edge, Node node, int index, DiagramEventSource source) {
|
f@0
|
340 send(new Command(Command.Name.SET_ENDDESCRIPTION, delegateDiagram.getName(),
|
f@0
|
341 new Object[] {edge.getId(), node.getId(), index},makeRemote(source)),
|
f@0
|
342 edge
|
f@0
|
343 );
|
f@0
|
344 }
|
f@0
|
345
|
f@0
|
346 @Override
|
f@0
|
347 public void translate(GraphElement ge, Point2D p, double dx, double dy, DiagramEventSource source) {
|
f@0
|
348 double px = 0;
|
f@0
|
349 double py = 0;
|
f@0
|
350 if(p != null){
|
f@0
|
351 px = p.getX();
|
f@0
|
352 py = p.getY();
|
f@0
|
353 }
|
f@0
|
354 if(ge instanceof Node){
|
f@0
|
355 Node n = (Node)ge;
|
f@0
|
356 send(new Command(Command.Name.TRANSLATE_NODE, delegateDiagram.getName(),
|
f@0
|
357 new Object[] {n.getId(), px, py, dx,dy},makeRemote(source)
|
f@0
|
358 ),n);
|
f@0
|
359 }else{
|
f@0
|
360 Edge e = (Edge)ge;
|
f@0
|
361 send(new Command(Command.Name.TRANSLATE_EDGE, delegateDiagram.getName(),
|
f@0
|
362 new Object[] {e.getId(), px, py, dx,dy},makeRemote(source)
|
f@0
|
363 ),e);
|
f@0
|
364 }
|
f@0
|
365 }
|
f@0
|
366
|
f@0
|
367 @Override
|
f@0
|
368 public void startMove(GraphElement ge, Point2D p, DiagramEventSource source) {
|
f@0
|
369 /* Store internally the point the motion started from and send a unique message *
|
f@0
|
370 * to the server when the edge is actually bended. This is because the lock will be *
|
f@0
|
371 * asked only when the mouse motion actually starts, whereas this call is done when *
|
f@0
|
372 * the edge is clicked down. So this variable is non null only when the first *
|
f@0
|
373 * bend-message is sent */
|
f@0
|
374 edgeStartMovePoint = p;
|
f@0
|
375 }
|
f@0
|
376
|
f@0
|
377 @Override
|
f@0
|
378 public void bend(Edge edge, Point2D p, DiagramEventSource source) {
|
f@0
|
379 /* send informations about the starting point only at the first time */
|
f@0
|
380 if(edgeStartMovePoint == null)
|
f@0
|
381 send(new Command(Command.Name.BEND, delegateDiagram.getName(),
|
f@0
|
382 new Object[] {edge.getId(),p.getX(),p.getY()},
|
f@0
|
383 makeRemote(source)),
|
f@0
|
384 edge);
|
f@0
|
385 else{
|
f@0
|
386 send(new Command(Command.Name.BEND, delegateDiagram.getName(),
|
f@0
|
387 new Object[] {edge.getId(),p.getX(),p.getY(),
|
f@0
|
388 edgeStartMovePoint.getX(),edgeStartMovePoint.getY()},
|
f@0
|
389 makeRemote(source)),
|
f@0
|
390 edge);
|
f@0
|
391 edgeStartMovePoint = null;
|
f@0
|
392 }
|
f@0
|
393 }
|
f@0
|
394
|
f@0
|
395 @Override
|
f@0
|
396 public void stopMove(GraphElement ge, DiagramEventSource source) {
|
f@0
|
397 if(ge instanceof Node){
|
f@0
|
398 Node n = (Node)ge;
|
f@0
|
399 send(new Command(Command.Name.STOP_NODE_MOVE, delegateDiagram.getName(),
|
f@0
|
400 new Object[] {n.getId()},makeRemote(source)),
|
f@0
|
401 n);
|
f@0
|
402 }else{
|
f@0
|
403 Edge e = (Edge)ge;
|
f@0
|
404 send(new Command(Command.Name.STOP_EDGE_MOVE, delegateDiagram.getName(),
|
f@0
|
405 new Object[] {e.getId()},makeRemote(source)),
|
f@0
|
406 e);
|
f@0
|
407 }
|
f@0
|
408 }
|
f@0
|
409
|
f@0
|
410 /* source passed as argument to the updater methods have are local and with no id
|
f@0
|
411 * since this source has to be sent to the server, it must be set as non local
|
f@0
|
412 * (constructor will do) and the is must be set as well
|
f@0
|
413 */
|
f@0
|
414 private DiagramEventSource makeRemote(DiagramEventSource src){
|
f@0
|
415 return new DiagramEventSource(src);
|
f@0
|
416 }
|
f@0
|
417
|
f@0
|
418 private Point2D edgeStartMovePoint;
|
f@0
|
419 }
|
f@0
|
420
|
f@0
|
421 private static class RemoteHostDiagram extends NetDiagram{
|
f@0
|
422 /**
|
f@0
|
423 * This class wraps an existing diagram into a network diagram.
|
f@0
|
424 * The network diagrams returns a TreeModelNetWrap and a CollectionModelNetWrap
|
f@0
|
425 * when the relative getters are called
|
f@0
|
426 *
|
f@0
|
427 * @param diagram the diagram to wrap
|
f@0
|
428 * @param connectionManager a connected socket channel
|
f@0
|
429 */
|
f@0
|
430 private RemoteHostDiagram(Diagram diagram, ClientConnectionManager connectionManager,SocketChannel channel){
|
f@0
|
431 super(diagram);
|
f@0
|
432 this.channel = channel;
|
f@0
|
433 this.connectionManager = connectionManager;
|
f@0
|
434 }
|
f@0
|
435
|
f@0
|
436 @Override
|
f@0
|
437 protected void send(Command cmd, DiagramElement element) {
|
f@0
|
438 connectionManager.addRequest(new ClientConnectionManager.SendCmdRequest(cmd, channel, element ));
|
f@0
|
439 }
|
f@0
|
440
|
f@0
|
441 @Override
|
f@0
|
442 protected void send(Command cmd, DiagramTreeNode treeNode) {
|
f@0
|
443 connectionManager.addRequest(new ClientConnectionManager.SendTreeCmdRequest(cmd, channel, treeNode ));
|
f@0
|
444 }
|
f@0
|
445
|
f@0
|
446 @Override
|
f@0
|
447 protected void send(LockMessage lockMessage){
|
f@0
|
448 connectionManager.addRequest(new ClientConnectionManager.SendLockRequest(channel, lockMessage));
|
f@0
|
449 }
|
f@0
|
450
|
f@0
|
451 @Override
|
f@0
|
452 protected void send(AwarenessMessage awMsg){
|
f@0
|
453 connectionManager.addRequest(new ClientConnectionManager.SendAwarenessRequest(channel,awMsg));
|
f@0
|
454 }
|
f@0
|
455
|
f@0
|
456 @Override
|
f@0
|
457 protected boolean receiveLockAnswer(){
|
f@0
|
458 ClientConnectionManager.Answer answer = connectionManager.getAnswer();
|
f@0
|
459 /* diagram has been reverted while waiting for a lock answer : the answer is gonna be yes *
|
f@0
|
460 * then, as the client is no longer connected to the server and there is no more locking in place */
|
f@0
|
461 if(answer instanceof ClientConnectionManager.RevertedDiagramAnswer)
|
f@0
|
462 return true;
|
f@0
|
463 LockMessage.Name name = ((LockAnswer)answer).message.getName();
|
f@0
|
464 switch(name){
|
f@0
|
465 case YES_L :
|
f@0
|
466 return true;
|
f@0
|
467 case NO_L :
|
f@0
|
468 return false;
|
f@0
|
469 default :
|
f@0
|
470 throw new RuntimeException("message not recognized: "+name.toString());
|
f@0
|
471 }
|
f@0
|
472 }
|
f@0
|
473
|
f@0
|
474 @Override
|
f@0
|
475 public String getLabel(){
|
f@0
|
476 return new StringBuilder(getName())
|
f@0
|
477 .append(' ').append('@').append(' ')
|
f@0
|
478 .append(channel.socket().getInetAddress().getHostAddress())
|
f@0
|
479 .toString();
|
f@0
|
480 }
|
f@0
|
481
|
f@0
|
482 @Override
|
f@0
|
483 public SocketChannel getSocketChannel(){
|
f@0
|
484 return channel;
|
f@0
|
485 }
|
f@0
|
486
|
f@0
|
487 @Override
|
f@0
|
488 public void enableAwareness(AwarenessPanel panel){
|
f@0
|
489 connectionManager.getAwarenessPanelEditor().addAwarenessPanel(panel);
|
f@0
|
490 }
|
f@0
|
491
|
f@0
|
492 @Override
|
f@0
|
493 public void disableAwareness(AwarenessPanel panel){
|
f@0
|
494 connectionManager.getAwarenessPanelEditor().removeAwarenessPanel(panel);
|
f@0
|
495 }
|
f@0
|
496
|
f@0
|
497 @Override
|
f@0
|
498 public Object clone(){
|
f@0
|
499 throw new UnsupportedOperationException();
|
f@0
|
500 }
|
f@0
|
501
|
f@0
|
502 private SocketChannel channel;
|
f@0
|
503 private ClientConnectionManager connectionManager;
|
f@0
|
504 }
|
f@0
|
505
|
f@0
|
506 private static class LocalHostDiagram extends NetDiagram {
|
f@0
|
507
|
f@0
|
508 private LocalHostDiagram(Diagram diagram, SocketChannel channel, Queue<DiagramElement> diagramElements, ExceptionHandler handler) {
|
f@0
|
509 super(diagram);
|
f@0
|
510 this.channel = channel;
|
f@0
|
511 this.diagramElements = diagramElements;
|
f@0
|
512 this.exceptionHandler = handler;
|
f@0
|
513 this.protocol = ProtocolFactory.newInstance();
|
f@0
|
514 }
|
f@0
|
515
|
f@0
|
516 @Override
|
f@0
|
517 protected void send(Command cmd, DiagramElement element){
|
f@0
|
518 switch(cmd.getName()){
|
f@0
|
519 case INSERT_NODE :
|
f@0
|
520 case INSERT_EDGE :
|
f@0
|
521 case REMOVE_NODE :
|
f@0
|
522 case REMOVE_EDGE :
|
f@0
|
523 diagramElements.add(element);
|
f@0
|
524 break;
|
f@0
|
525 }
|
f@0
|
526 try{
|
f@0
|
527 protocol.send(channel, cmd);
|
f@0
|
528 }catch(IOException ioe){
|
f@0
|
529 switch(cmd.getName()){
|
f@0
|
530 case INSERT_NODE :
|
f@0
|
531 case INSERT_EDGE :
|
f@0
|
532 case REMOVE_NODE :
|
f@0
|
533 case REMOVE_EDGE :
|
f@0
|
534 diagramElements.remove(element);
|
f@0
|
535 break;
|
f@0
|
536 }
|
f@0
|
537 exceptionHandler.handleException(ioe);
|
f@0
|
538 }
|
f@0
|
539 }
|
f@0
|
540
|
f@0
|
541 @Override
|
f@0
|
542 protected void send(LockMessage lockMessage) {
|
f@0
|
543 try {
|
f@0
|
544 protocol.send(channel, lockMessage);
|
f@0
|
545 } catch (IOException ioe) {
|
f@0
|
546 exceptionHandler.handleException(ioe);
|
f@0
|
547 }
|
f@0
|
548 }
|
f@0
|
549
|
f@0
|
550 @Override
|
f@0
|
551 protected void send(AwarenessMessage awMsg){
|
f@0
|
552 try {
|
f@0
|
553 protocol.send(channel, awMsg);
|
f@0
|
554 } catch (IOException ioe) {
|
f@0
|
555 exceptionHandler.handleException(ioe);
|
f@0
|
556 }
|
f@0
|
557 }
|
f@0
|
558
|
f@0
|
559 @Override
|
f@0
|
560 protected boolean receiveLockAnswer(){
|
f@0
|
561 LockMessage answer;
|
f@0
|
562 try {
|
f@0
|
563 answer = protocol.receiveLockMessage(channel);
|
f@0
|
564 } catch (IOException ioe) {
|
f@0
|
565 exceptionHandler.handleException(ioe);
|
f@0
|
566 return false;
|
f@0
|
567 }
|
f@0
|
568 switch((LockMessage.Name)answer.getName()){
|
f@0
|
569 case YES_L :
|
f@0
|
570 return true;
|
f@0
|
571 case NO_L :
|
f@0
|
572 return false;
|
f@0
|
573 default :
|
f@0
|
574 throw new RuntimeException("message not recognized: "+answer.getName().toString());
|
f@0
|
575 }
|
f@0
|
576 }
|
f@0
|
577
|
f@0
|
578 @Override
|
f@0
|
579 protected void send(Command cmd , DiagramTreeNode treeNode){
|
f@0
|
580 try {
|
f@0
|
581 protocol.send(channel, cmd);
|
f@0
|
582 } catch (IOException ioe) {
|
f@0
|
583 exceptionHandler.handleException(ioe);
|
f@0
|
584 }
|
f@0
|
585 }
|
f@0
|
586
|
f@0
|
587 @Override
|
f@0
|
588 public String getLabel(){
|
f@0
|
589 return getName()+LOCALHOST_STRING;
|
f@0
|
590 }
|
f@0
|
591
|
f@0
|
592 @Override
|
f@0
|
593 public SocketChannel getSocketChannel(){
|
f@0
|
594 return channel;
|
f@0
|
595 }
|
f@0
|
596
|
f@0
|
597 @Override
|
f@0
|
598 public void enableAwareness(AwarenessPanel panel){
|
f@0
|
599 Server.getServer().getAwarenessPanelEditor().addAwarenessPanel(panel);
|
f@0
|
600 }
|
f@0
|
601
|
f@0
|
602 @Override
|
f@0
|
603 public void disableAwareness(AwarenessPanel panel){
|
f@0
|
604 Server.getServer().getAwarenessPanelEditor().removeAwarenessPanel(panel);
|
f@0
|
605 }
|
f@0
|
606
|
f@0
|
607 private SocketChannel channel;
|
f@0
|
608 private Queue<DiagramElement> diagramElements;
|
f@0
|
609 private Protocol protocol;
|
f@0
|
610 private ExceptionHandler exceptionHandler;
|
f@0
|
611 }
|
f@0
|
612 }
|