Mercurial > hg > RemoteEditor > REPSessionManager
annotate rep/Session.java @ 361:65c6d12a5835
*** empty log message ***
author | kono |
---|---|
date | Sun, 19 Oct 2008 22:50:20 +0900 |
parents | b25f832f875d |
children | f0bd158dace6 |
rev | line source |
---|---|
0 | 1 package rep; |
2 | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
3 import java.util.Collection; |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
4 import java.util.HashMap; |
1 | 5 import java.util.LinkedList; |
131 | 6 import rep.channel.REPSocketChannel; |
7 | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
8 public class Session extends HashMap<Integer,EditorPlus> { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
9 /** |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
10 * |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
11 */ |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
12 private static final long serialVersionUID = 1L; |
316 | 13 private Forwarder masterEditor; |
1 | 14 private int sessionID; |
15 private String sessionName; | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
16 // isOnwer means this session has active channels(forwarders). |
67 | 17 private boolean isOwner = false; |
319 | 18 private Forwarder firstForwarder; |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
19 private Forwarder last; |
9 | 20 |
319 | 21 public Session(int sid, String name, Forwarder editor) { |
227 | 22 this(sid, editor); |
23 sessionName = name; | |
1 | 24 } |
320 | 25 |
39 | 26 |
319 | 27 public Session(int sid, Forwarder editor) { |
144 | 28 sessionID = sid; |
320 | 29 if (editor!=null) { |
30 // we have a master | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
31 masterEditor = last = editor; |
320 | 32 editor.setSID(sid); |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
33 put(editor.eid,editor); |
323 | 34 if(editor.channel!=null) { |
35 firstForwarder = editor; | |
36 masterEditor.setNext(masterEditor); | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
37 isOwner = true; |
323 | 38 } |
320 | 39 } |
1 | 40 } |
319 | 41 |
42 public void addForwarder(Forwarder forwarder) { | |
320 | 43 // add a forwarder and connect this to the session |
319 | 44 forwarder.setNext(last.getNextForwarder()); |
45 last.setNext(forwarder); | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
46 last = forwarder; |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
47 put(forwarder.eid,forwarder); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
48 isOwner = true; |
319 | 49 if(firstForwarder==null) firstForwarder = forwarder; |
50 } | |
51 | |
320 | 52 public void addEditor(Editor editor) { |
53 // add a not-connected editor in a sassion | |
54 // the editor is outside of this manager | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
55 put(editor.eid,editor); |
320 | 56 } |
57 | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
58 public Collection<EditorPlus> getEditorList() { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
59 return values(); |
1 | 60 } |
319 | 61 |
56 | 62 public int getSID() { |
63 return sessionID; | |
38 | 64 } |
316 | 65 public Forwarder getOwner() { |
38 | 66 return masterEditor; |
67 } | |
68 public String getName() { | |
69 return sessionName; | |
70 } | |
319 | 71 |
274 | 72 |
73 public boolean deleteEditor(REPSocketChannel<REPCommand> channel) { | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
74 LinkedList<EditorPlus> toBeRemoved = new LinkedList<EditorPlus>(); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
75 for (EditorPlus e:values() ) { |
275 | 76 if (e.getChannel()==channel) { |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
77 unconnect((Forwarder)e); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
78 toBeRemoved.add(e); |
274 | 79 } |
80 } | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
81 for(EditorPlus e:toBeRemoved) { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
82 remove(e); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
83 } |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
84 return !toBeRemoved.isEmpty(); |
274 | 85 } |
86 | |
358 | 87 public boolean deleteForwarder(Forwarder editor) { |
88 unconnect(editor); | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
89 return remove(editor)!=null; |
358 | 90 } |
91 | |
319 | 92 private void unconnect(Forwarder e) { |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
93 boolean hasOwner = false; |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
94 for(EditorPlus e1:values()) { |
319 | 95 Forwarder f = (Forwarder)e1; |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
96 if(f.next==e) { |
319 | 97 f.next=e.next; |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
98 } else { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
99 if (f.channel!=null) hasOwner=true; |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
100 } |
319 | 101 } |
102 if(firstForwarder==e) firstForwarder=null; | |
103 if(masterEditor==e) masterEditor=null; | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
104 isOwner = hasOwner; |
319 | 105 } |
106 | |
227 | 107 public void setSID(int sid) { |
108 sessionID = sid; | |
56 | 109 } |
274 | 110 |
158 | 111 public boolean hasOwner() { |
67 | 112 return isOwner; |
113 } | |
319 | 114 |
115 | |
144 | 116 public Editor getEditor(REPSocketChannel<REPCommand> channel) { |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
117 for(EditorPlus editor : values()){ |
319 | 118 if(editor.getChannel() == channel) { |
119 return (Editor)editor; | |
120 } | |
144 | 121 } |
122 return null; | |
123 } | |
315 | 124 |
125 | |
316 | 126 Forwarder getNextEditor(Forwarder editor) { |
319 | 127 return editor.getNextForwarder(); |
167 | 128 } |
319 | 129 |
259 | 130 public void closeSession() { |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
131 Forwarder first = firstForwarder; |
319 | 132 REPCommand command = new REPCommand(REP.REPCMD_CLOSE, sessionID, REP.SM_EID.id, 0, 0, ""); |
133 if (first!=null) | |
134 first.send(command); | |
300 | 135 } |
316 | 136 |
319 | 137 |
316 | 138 |
319 | 139 public Forwarder getFirstForwarder() { |
140 return firstForwarder; | |
316 | 141 } |
358 | 142 |
143 | |
144 public void remove(SessionManager manager) { | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
145 for(EditorPlus editor : values()){ |
358 | 146 if(editor.getChannel() !=null) |
147 unconnect((Forwarder)editor); | |
148 manager.editorList.remove(editor); | |
149 } | |
150 } | |
359
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
151 |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
152 |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
153 public void merge(Session s) { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
154 for(EditorPlus editor : s.values()){ |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
155 EditorPlus mine = get(editor.eid); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
156 if (mine==null) { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
157 put(editor.eid,editor); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
158 } else { |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
159 mine.merge(editor); |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
160 } |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
161 } |
fa041bae35f1
all code written for distributed session except gather.
kono
parents:
358
diff
changeset
|
162 } |
319 | 163 |
360 | 164 @Override |
165 public String toString() { | |
166 String s = super.toString(); | |
167 return s+" sid="+sessionID+" "+sessionName+" master="+ | |
168 ((masterEditor==null)?"null":masterEditor.toString()); | |
169 } | |
170 | |
171 | |
172 public void setName(String string) { | |
173 sessionName = string; | |
174 } | |
320 | 175 |
361 | 176 |
177 public void setFirstForwarder(Forwarder f) { | |
178 put(f.eid,f); | |
179 firstForwarder = last = f; | |
180 } | |
181 | |
182 | |
183 | |
0 | 184 } |