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