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