22
|
1 package rep;
|
|
2
|
23
|
3 import java.nio.channels.SocketChannel;
|
|
4 import java.util.LinkedList;
|
|
5
|
131
|
6 import rep.channel.REPSocketChannel;
|
|
7
|
22
|
8 public class EditorList {
|
|
9
|
23
|
10 private int numberOfEditor;
|
|
11 private LinkedList<Editor> editorList = new LinkedList<Editor>();
|
|
12
|
22
|
13 public void sendJoinAck(REPCommand repCmd) {
|
23
|
14 Editor editor = null;
|
|
15 for(Editor editor2 : editorList){
|
24
|
16 error(String.valueOf(editor2.getEID()), String.valueOf(repCmd.eid));
|
23
|
17 if(editor2.getEID() == repCmd.eid){
|
|
18 editor = editor2;
|
24
|
19 break;
|
23
|
20 }
|
|
21 }
|
24
|
22 error(editor);
|
23
|
23 REPPacketSend send = new REPPacketSend(editor.getChannel());
|
|
24 send.send(repCmd);
|
22
|
25 }
|
|
26
|
24
|
27
|
137
|
28 public void sendJoinAck(REPSocketChannel channel, REPCommand repCmd) {
|
23
|
29 REPCommand command = repCmd;
|
|
30 command.setCMD(REP.SMCMD_JOIN_ACK);
|
22
|
31
|
23
|
32 REPPacketSend send = new REPPacketSend(channel);
|
|
33 send.send(command);
|
|
34 }
|
|
35
|
132
|
36 public int addEditor(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
|
23
|
37 numberOfEditor++;
|
|
38 editorList.add(new Editor(numberOfEditor, channel));
|
|
39 return numberOfEditor;
|
|
40 }
|
|
41
|
132
|
42 public void addEditor(REPSocketChannel<REPCommand> channel) {
|
23
|
43 editorList.add(new Editor(0, channel));
|
|
44 }
|
|
45
|
|
46 public void setEID(REPCommand repCmd) {
|
|
47 for(Editor editor : editorList){
|
|
48 if(editor.getEID() == 0){
|
|
49 editor.setEID(repCmd.eid);
|
|
50 break;
|
|
51 }
|
|
52 }
|
22
|
53 }
|
24
|
54
|
|
55 private void error(Object obj) {
|
|
56 if(obj == null){
|
|
57 System.out.println("null!");
|
|
58 }
|
|
59 }
|
|
60 private void error(String str1, String str2){
|
|
61 if(str1.equals(str2)){
|
|
62 return;
|
|
63 }else{
|
|
64 System.out.println("Not equals! str1:str2");
|
|
65 }
|
|
66 }
|
22
|
67
|
31
|
68
|
132
|
69 public void sendPutAck(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
|
31
|
70 REPPacketSend send = new REPPacketSend(channel);
|
|
71 send.send(repCmd);
|
|
72 }
|
|
73
|
132
|
74 public void send(REPSocketChannel<REPCommand> channel, REPCommand command){
|
31
|
75 REPPacketSend send = new REPPacketSend(channel);
|
|
76 send.send(command);
|
|
77 }
|
|
78
|
76
|
79
|
|
80 public void setHost(String myHost) {
|
|
81 // TODO Auto-generated method stub
|
|
82 for(Editor editor : editorList) {
|
|
83 editor.setHost(myHost);
|
|
84 }
|
|
85 }
|
|
86
|
85
|
87
|
|
88 public Editor getEditor(String hostport) {
|
|
89 // TODO Auto-generated method stub
|
|
90 for(Editor editor : editorList){
|
86
|
91 String[] splited = hostport.split(":");
|
87
|
92 System.out.println(splited[0] + "," + editor.getHost());
|
86
|
93 if(splited[0].equals(editor.getHost())){
|
92
|
94 return editor;
|
85
|
95 }
|
92
|
96
|
86
|
97 //String hostandport = editor.getHost() + ":" + editor.getPort();
|
|
98 //if(hostport.equals(hostandport)){
|
|
99 // return editor;
|
|
100 //}
|
85
|
101 }
|
|
102 return null;
|
|
103 }
|
|
104
|
91
|
105
|
106
|
106 public int addEditor(Editor editor) {
|
|
107 numberOfEditor++;
|
91
|
108 editorList.add(editor);
|
106
|
109 return numberOfEditor;
|
91
|
110 }
|
|
111
|
122
|
112
|
137
|
113 public Editor getEditor(REPSocketChannel channel) {
|
122
|
114 // TODO Auto-generated method stub
|
|
115 Editor editor1 = null;
|
|
116 for(Editor editor: editorList){
|
|
117 if(channel == editor.getChannel()){
|
|
118 editor1 = editor;
|
|
119 }
|
|
120 }
|
|
121 return editor1;
|
|
122 }
|
|
123
|
22
|
124 }
|