0
|
1 package rep;
|
|
2
|
275
|
3 import java.util.Iterator;
|
1
|
4 import java.util.LinkedList;
|
131
|
5 import rep.channel.REPSocketChannel;
|
|
6
|
0
|
7 public class Session {
|
316
|
8 private Forwarder masterEditor;
|
1
|
9 private int sessionID;
|
|
10 private String sessionName;
|
319
|
11 private LinkedList<EditorPlus> editorList = new LinkedList<EditorPlus>();
|
67
|
12 private boolean isOwner = false;
|
319
|
13 private Forwarder firstForwarder;
|
9
|
14
|
319
|
15 public Session(int sid, String name, Forwarder editor) {
|
227
|
16 this(sid, editor);
|
|
17 sessionName = name;
|
1
|
18 }
|
320
|
19
|
39
|
20
|
319
|
21 public Session(int sid, Forwarder editor) {
|
144
|
22 sessionID = sid;
|
320
|
23 if (editor!=null) {
|
|
24 // we have a master
|
|
25 masterEditor = editor;
|
|
26 editor.setSID(sid);
|
|
27 editorList.add(editor);
|
323
|
28 if(editor.channel!=null) {
|
|
29 firstForwarder = editor;
|
|
30 masterEditor.setNext(masterEditor);
|
|
31 }
|
320
|
32 }
|
1
|
33 }
|
319
|
34
|
|
35 public void addForwarder(Forwarder forwarder) {
|
320
|
36 // add a forwarder and connect this to the session
|
319
|
37 Forwarder last = (Forwarder)editorList.getLast();
|
|
38 forwarder.setNext(last.getNextForwarder());
|
|
39 last.setNext(forwarder);
|
|
40 editorList.add(forwarder);
|
|
41 if(firstForwarder==null) firstForwarder = forwarder;
|
|
42 }
|
|
43
|
320
|
44 public void addEditor(Editor editor) {
|
|
45 // add a not-connected editor in a sassion
|
|
46 // the editor is outside of this manager
|
|
47 editorList.add(editor);
|
|
48 }
|
|
49
|
319
|
50 public LinkedList<EditorPlus> getEditorList() {
|
1
|
51 return editorList;
|
|
52 }
|
319
|
53
|
3
|
54 public String toString(){
|
|
55 return sessionName;
|
|
56 }
|
56
|
57 public int getSID() {
|
|
58 return sessionID;
|
38
|
59 }
|
316
|
60 public Forwarder getOwner() {
|
38
|
61 return masterEditor;
|
|
62 }
|
|
63 public String getName() {
|
|
64 return sessionName;
|
|
65 }
|
319
|
66
|
274
|
67
|
|
68 public boolean deleteEditor(REPSocketChannel<REPCommand> channel) {
|
|
69 boolean flag = false;
|
275
|
70
|
319
|
71 for (Iterator<EditorPlus> it = editorList.iterator();it.hasNext(); ) {
|
|
72 Forwarder e = (Forwarder)it.next();
|
275
|
73 if (e.getChannel()==channel) {
|
319
|
74 unconnect(e);
|
275
|
75 it.remove(); // to avoid concurrent modification
|
274
|
76 flag = true;
|
|
77 }
|
|
78 }
|
|
79 return flag;
|
|
80 }
|
|
81
|
319
|
82 private void unconnect(Forwarder e) {
|
|
83 for(EditorPlus e1:editorList) {
|
|
84 Forwarder f = (Forwarder)e1;
|
|
85 if(f.next==e)
|
|
86 f.next=e.next;
|
|
87 }
|
|
88 if(firstForwarder==e) firstForwarder=null;
|
|
89 if(masterEditor==e) masterEditor=null;
|
|
90 // if no masterEditor we should delete session also...
|
|
91 }
|
|
92
|
227
|
93 public void setSID(int sid) {
|
|
94 sessionID = sid;
|
56
|
95 }
|
274
|
96
|
158
|
97 public boolean hasOwner() {
|
67
|
98 return isOwner;
|
|
99 }
|
319
|
100
|
158
|
101 public void hasOwner(boolean b) {
|
320
|
102 isOwner = b;
|
66
|
103 }
|
319
|
104
|
144
|
105 public Editor getEditor(REPSocketChannel<REPCommand> channel) {
|
319
|
106 for(EditorPlus editor : editorList){
|
|
107 if(editor.getChannel() == channel) {
|
|
108 return (Editor)editor;
|
|
109 }
|
144
|
110 }
|
|
111 return null;
|
|
112 }
|
315
|
113
|
|
114
|
316
|
115 Forwarder getNextEditor(Forwarder editor) {
|
319
|
116 return editor.getNextForwarder();
|
167
|
117 }
|
319
|
118
|
259
|
119 public void closeSession() {
|
319
|
120 EditorPlus e = editorList.getFirst();
|
|
121 Forwarder first = (Forwarder)e;
|
|
122 REPCommand command = new REPCommand(REP.REPCMD_CLOSE, sessionID, REP.SM_EID.id, 0, 0, "");
|
|
123 if (first!=null)
|
|
124 first.send(command);
|
300
|
125 }
|
316
|
126
|
319
|
127
|
|
128 public boolean deleteForwarder(Forwarder editor) {
|
|
129 unconnect(editor);
|
|
130 return editorList.remove(editor);
|
316
|
131 }
|
|
132
|
319
|
133 public Forwarder getFirstForwarder() {
|
|
134 return firstForwarder;
|
316
|
135 }
|
319
|
136
|
320
|
137
|
0
|
138 }
|