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