annotate rep/Session.java @ 217:4deaaaa6354e

*** empty log message ***
author kono
date Sun, 31 Aug 2008 00:26:35 +0900
parents 3a2c6c2f9761
children 6b0dd92b8e45
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
1 package rep;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
2
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
3 import java.util.LinkedList;
131
617a47cb0150 *** empty log message ***
pin
parents: 96
diff changeset
4 import rep.channel.REPSocketChannel;
617a47cb0150 *** empty log message ***
pin
parents: 96
diff changeset
5
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
6 public class Session {
38
de8638eb0edd *** empty log message ***
pin
parents: 9
diff changeset
7 private Editor masterEditor;
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
8 private int sessionID;
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
9 private String sessionName;
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
10 private LinkedList<Editor> editorList = new LinkedList<Editor>();
56
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
11 private LinkedList<REPNode> routingTable = new LinkedList<REPNode>();
67
13e4c3f2c6f9 *** empty log message ***
pin
parents: 66
diff changeset
12 private boolean isOwner = false;
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 3
diff changeset
13
179
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
14 public Session(int sessionID, String string, REPSocketChannel<REPCommand> channel) {
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
15 masterEditor = new Editor(sessionID, channel);
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
16 this.sessionID = sessionID;
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
17 this.sessionName = string;
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
18 }
39
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
19 public Session(Editor editor) {
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
20 masterEditor = editor;
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
21 this.sessionID = 0;
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
22 this.sessionName = editor.getName();
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
23 }
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
24
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
25 public Session(int sid, Editor editor) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
26 sessionID = sid;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
27 masterEditor = editor;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
28 editorList.add(editor);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
29 }
179
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
30 public void addEditor(int editorID, REPSocketChannel<REPCommand> channel) {
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
31 editorList.add(new Editor(editorID, channel));
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
32 }
39
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
33 public LinkedList<Editor> getEditorList() {
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
34 if(editorList == null) System.out.println("null!");
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
35 return editorList;
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
36 }
3
f31fcac5a949 *** empty log message ***
pin
parents: 2
diff changeset
37 public String toString(){
f31fcac5a949 *** empty log message ***
pin
parents: 2
diff changeset
38 return sessionName;
f31fcac5a949 *** empty log message ***
pin
parents: 2
diff changeset
39 }
56
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
40 public int getSID() {
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
41 return sessionID;
38
de8638eb0edd *** empty log message ***
pin
parents: 9
diff changeset
42 }
158
5cc8cd48bded *** empty log message ***
pin
parents: 144
diff changeset
43 public Editor getOwner() {
38
de8638eb0edd *** empty log message ***
pin
parents: 9
diff changeset
44 return masterEditor;
de8638eb0edd *** empty log message ***
pin
parents: 9
diff changeset
45 }
de8638eb0edd *** empty log message ***
pin
parents: 9
diff changeset
46 public String getName() {
de8638eb0edd *** empty log message ***
pin
parents: 9
diff changeset
47 return sessionName;
de8638eb0edd *** empty log message ***
pin
parents: 9
diff changeset
48 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
49 public void addEditor(Editor editor) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
50 int eid = editorList.size();
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
51 editor.setEID(eid);
39
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
52 editorList.add(editor);
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
53 }
56
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
54 public void setSID(int sessionID2) {
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
55 sessionID = sessionID2;
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
56 }
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
57 public void addToRoutingTable(Editor editor) {
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
58 routingTable.add(new REPNode(editor));
6ead43b2475e *** empty log message ***
pin
parents: 39
diff changeset
59 }
158
5cc8cd48bded *** empty log message ***
pin
parents: 144
diff changeset
60 public boolean hasOwner() {
67
13e4c3f2c6f9 *** empty log message ***
pin
parents: 66
diff changeset
61 return isOwner;
13e4c3f2c6f9 *** empty log message ***
pin
parents: 66
diff changeset
62 }
158
5cc8cd48bded *** empty log message ***
pin
parents: 144
diff changeset
63 public void hasOwner(boolean b) {
67
13e4c3f2c6f9 *** empty log message ***
pin
parents: 66
diff changeset
64 isOwner = true;
66
99196780bbf5 *** empty log message ***
pin
parents: 56
diff changeset
65 }
70
248d774be113 *** empty log message ***
pin
parents: 67
diff changeset
66 public void sendToEditor(REPCommand repCmd) {
248d774be113 *** empty log message ***
pin
parents: 67
diff changeset
67 for(Editor editor : editorList){
181
3a2c6c2f9761 *** empty log message ***
kent
parents: 179
diff changeset
68 editor.getChannel().write(repCmd);
70
248d774be113 *** empty log message ***
pin
parents: 67
diff changeset
69 }
248d774be113 *** empty log message ***
pin
parents: 67
diff changeset
70 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
71 public Editor getEditor(REPSocketChannel<REPCommand> channel) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
72 for(Editor editor : editorList){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
73 if(editor.getChannel() == channel) return editor;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
74 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
75 return null;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
76 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
77 public void translate(REPSocketChannel<REPCommand> channel, REPCommand command) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
78 Editor editor = getEditor(channel);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
79 LinkedList<REPCommand> commandList = editor.translate(command);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
80 Editor nextEditor = getNextEditor(editor);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
81
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
82 for(REPCommand cmd: commandList){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
83 nextEditor.send(cmd);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
84 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
85 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
86 private Editor getNextEditor(Editor editor) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
87 int eid = editor.getEID();
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
88 int neid = (eid+1)%editorList.size();
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
89 Editor nextEditor = editorList.get(neid);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
90 return nextEditor;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 131
diff changeset
91 }
167
3dc194f5e28f *** empty log message ***
pin
parents: 158
diff changeset
92 public Editor getPrevEditor(Editor editor) {
3dc194f5e28f *** empty log message ***
pin
parents: 158
diff changeset
93 int eid = editor.getEID();
3dc194f5e28f *** empty log message ***
pin
parents: 158
diff changeset
94 int peid = (eid + editorList.size() - 1)%editorList.size();
3dc194f5e28f *** empty log message ***
pin
parents: 158
diff changeset
95 Editor prevEditor = editorList.get(peid);
3dc194f5e28f *** empty log message ***
pin
parents: 158
diff changeset
96 return prevEditor;
3dc194f5e28f *** empty log message ***
pin
parents: 158
diff changeset
97 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
98 }