forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxSession.java
More file actions
executable file
·214 lines (184 loc) · 4.9 KB
/
mxSession.java
File metadata and controls
executable file
·214 lines (184 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* $Id: mxSession.java,v 1.15 2012/01/13 11:07:37 david Exp $
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.sharing;
import org.w3c.dom.Node;
import com.mxgraph.sharing.mxSharedState.mxDiagramChangeListener;
import com.mxgraph.util.mxUtils;
/**
* Implements a session that may be attached to a shared diagram. The session
* contains a synchronized buffer which is used to hold the pending edits which
* are to be sent to a specific client. The update mechnism between the server
* and the client uses HTTP requests (polling). The request is kept on the server
* for an amount of time or wakes up / returns immediately if the buffer is no
* longer empty.
*/
public class mxSession implements mxDiagramChangeListener
{
/**
* Default timeout is 10000 ms.
*/
public static int DEFAULT_TIMEOUT = 10000;
/**
* Holds the session ID.
*/
protected String id;
/**
* Reference to the shared diagram.
*/
protected mxSharedState diagram;
/**
* Holds the send buffer for this session.
*/
protected StringBuffer buffer = new StringBuffer();
/**
* Holds the last active time millis.
*/
protected long lastTimeMillis = 0;
/**
* Constructs a new session with the given ID.
*
* @param id Specifies the session ID to be used.
* @param diagram Reference to the shared diagram.
*/
public mxSession(String id, mxSharedState diagram)
{
this.id = id;
this.diagram = diagram;
this.diagram.addDiagramChangeListener(this);
lastTimeMillis = System.currentTimeMillis();
}
/**
* Returns the session ID.
*/
public String getId()
{
return id;
}
/**
* Initializes the session buffer and returns a string that represents the
* state of the session.
*
* @return Returns the initial state of the session.
*/
public synchronized String init()
{
synchronized (this)
{
buffer = new StringBuffer();
notify();
}
return getInitialMessage();
}
/**
* Returns an XML string that represents the current state of the session
* and the shared diagram. A globally unique ID is used as the session's
* namespace, which is used on the client side to prefix IDs of newly
* created cells.
*/
public String getInitialMessage()
{
String ns = mxUtils.getMd5Hash(id);
StringBuffer result = new StringBuffer("<message namespace=\"" + ns
+ "\">");
result.append("<state>");
result.append(diagram.getState());
result.append("</state>");
result.append("<delta>");
result.append(diagram.getDelta());
result.append("</delta>");
result.append("</message>");
return result.toString();
}
/**
* Posts the change represented by the given XML string to the shared diagram.
*
* @param message XML that represents the change.
*/
public void receive(Node message)
{
//System.out.println(getId() + ": " + mxUtils.getPrettyXml(message));
Node child = message.getFirstChild();
while (child != null)
{
if (child.getNodeName().equals("delta"))
{
diagram.processDelta(this, child);
}
child = child.getNextSibling();
}
/*System.out.println(mxUtils.getPrettyXml(new mxCodec()
.encode(((mxSharedGraphModel) diagram).getModel())));*/
}
/**
* Returns the changes received by other sessions for the shared diagram.
* The method returns an empty XML node if no change was received within
* 10 seconds.
*
* @return Returns a string representing the changes to the shared diagram.
*/
public String poll() throws InterruptedException
{
return poll(DEFAULT_TIMEOUT);
}
/**
* Returns the changes received by other sessions for the shared diagram.
* The method returns an empty XML node if no change was received within
* the given timeout.
*
* @param timeout Time in milliseconds to wait for changes.
* @return Returns a string representing the changes to the shared diagram.
*/
public String poll(long timeout) throws InterruptedException
{
lastTimeMillis = System.currentTimeMillis();
StringBuffer result = new StringBuffer("<message>");
synchronized (this)
{
if (buffer.length() == 0)
{
wait(timeout);
}
if (buffer.length() > 0)
{
result.append("<delta>");
result.append(buffer.toString());
result.append("</delta>");
buffer = new StringBuffer();
}
notify();
}
result.append("</message>");
return result.toString();
}
/*
* (non-Javadoc)
* @see com.mxgraph.sharing.mxSharedDiagram.mxDiagramChangeListener#diagramChanged(java.lang.Object, org.w3c.dom.Node)
*/
public synchronized void diagramChanged(Object sender, String edits)
{
if (sender != this)
{
synchronized (this)
{
buffer.append(edits);
notify();
}
}
}
/**
* Returns the number of milliseconds this session has been inactive.
*/
public long inactiveTimeMillis()
{
return System.currentTimeMillis() - lastTimeMillis;
}
/**
* Destroys the session and removes its listener from the shared diagram.
*/
public void destroy()
{
diagram.removeDiagramChangeListener(this);
}
}