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