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 {
|
38
|
8 private Editor masterEditor;
|
1
|
9 private int sessionID;
|
|
10 private String sessionName;
|
|
11 private LinkedList<Editor> editorList = new LinkedList<Editor>();
|
67
|
12 private boolean isOwner = false;
|
9
|
13
|
227
|
14 public Session(int sid, String name, Editor editor) {
|
|
15 this(sid, editor);
|
|
16 sessionName = name;
|
1
|
17 }
|
39
|
18 public Session(Editor editor) {
|
|
19 masterEditor = editor;
|
|
20 this.sessionID = 0;
|
|
21 this.sessionName = editor.getName();
|
|
22 }
|
|
23
|
144
|
24 public Session(int sid, Editor editor) {
|
|
25 sessionID = sid;
|
|
26 masterEditor = editor;
|
|
27 editorList.add(editor);
|
|
28 }
|
179
|
29 public void addEditor(int editorID, REPSocketChannel<REPCommand> channel) {
|
1
|
30 editorList.add(new Editor(editorID, channel));
|
|
31 }
|
39
|
32 public LinkedList<Editor> getEditorList() {
|
|
33 if(editorList == null) System.out.println("null!");
|
1
|
34 return editorList;
|
|
35 }
|
3
|
36 public String toString(){
|
|
37 return sessionName;
|
|
38 }
|
56
|
39 public int getSID() {
|
|
40 return sessionID;
|
38
|
41 }
|
158
|
42 public Editor getOwner() {
|
38
|
43 return masterEditor;
|
|
44 }
|
|
45 public String getName() {
|
|
46 return sessionName;
|
|
47 }
|
144
|
48 public void addEditor(Editor editor) {
|
|
49 int eid = editorList.size();
|
|
50 editor.setEID(eid);
|
281
|
51 editor.setSID(sessionID);
|
39
|
52 editorList.add(editor);
|
|
53 }
|
274
|
54
|
|
55 public boolean deleteEditor(REPSocketChannel<REPCommand> channel) {
|
|
56 boolean flag = false;
|
275
|
57
|
|
58 for (Iterator<Editor> it = editorList.iterator();it.hasNext(); ) {
|
|
59 Editor e = it.next();
|
|
60 if (e.getChannel()==channel) {
|
|
61 it.remove(); // to avoid concurrent modification
|
274
|
62 flag = true;
|
|
63 }
|
|
64 }
|
|
65 return flag;
|
|
66 }
|
|
67
|
227
|
68 public void setSID(int sid) {
|
|
69 sessionID = sid;
|
56
|
70 }
|
274
|
71
|
158
|
72 public boolean hasOwner() {
|
67
|
73 return isOwner;
|
|
74 }
|
158
|
75 public void hasOwner(boolean b) {
|
67
|
76 isOwner = true;
|
66
|
77 }
|
70
|
78 public void sendToEditor(REPCommand repCmd) {
|
|
79 for(Editor editor : editorList){
|
181
|
80 editor.getChannel().write(repCmd);
|
70
|
81 }
|
|
82 }
|
144
|
83 public Editor getEditor(REPSocketChannel<REPCommand> channel) {
|
|
84 for(Editor editor : editorList){
|
|
85 if(editor.getChannel() == channel) return editor;
|
|
86 }
|
|
87 return null;
|
|
88 }
|
315
|
89
|
|
90
|
|
91 Editor getNextEditor(Editor editor) {
|
144
|
92 int eid = editor.getEID();
|
|
93 int neid = (eid+1)%editorList.size();
|
|
94 Editor nextEditor = editorList.get(neid);
|
|
95 return nextEditor;
|
|
96 }
|
167
|
97 public Editor getPrevEditor(Editor editor) {
|
|
98 int eid = editor.getEID();
|
|
99 int peid = (eid + editorList.size() - 1)%editorList.size();
|
|
100 Editor prevEditor = editorList.get(peid);
|
|
101 return prevEditor;
|
|
102 }
|
259
|
103 public void closeSession() {
|
260
|
104 REPCommand command = new REPCommand();
|
|
105 command.setCMD(REP.REPCMD_CLOSE);
|
|
106 for(Editor editor : editorList){
|
|
107 command.setEID(editor.getEID());
|
|
108 editor.send(command);
|
|
109 }
|
259
|
110 }
|
300
|
111 public void sendToNextEditor(REPSocketChannel<REPCommand>channel,REPCommand command) {
|
|
112 Editor next = getNextEditor(getEditor(channel));
|
|
113 next.send(command);
|
|
114 }
|
0
|
115 }
|