1
|
1 package rep;
|
|
2
|
142
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
122
|
5 import java.util.LinkedList;
|
142
|
6 import java.util.List;
|
1
|
7
|
131
|
8 import rep.channel.REPSocketChannel;
|
142
|
9 import rep.translater.TranslaterImp1;
|
131
|
10
|
1
|
11 public class Editor {
|
23
|
12 private int eid;
|
132
|
13 private REPSocketChannel<REPCommand> myChannel;
|
21
|
14 private String host;
|
|
15 private String port;
|
39
|
16 private String file;
|
142
|
17 private TranslaterImp1 translater;
|
|
18 private List<REPCommand> sentList;
|
1
|
19
|
132
|
20 public Editor(int editorNo, REPSocketChannel<REPCommand> channel){
|
23
|
21 this.eid = editorNo;
|
56
|
22 this.myChannel = channel;
|
142
|
23 translater = new TranslaterImp1(eid);
|
|
24 sentList = new LinkedList<REPCommand>();
|
56
|
25 }
|
142
|
26
|
132
|
27 public Editor(REPSocketChannel<REPCommand> channel) {
|
56
|
28 this.myChannel = channel;
|
|
29 setHostAndPort(myChannel);
|
|
30 }
|
142
|
31
|
|
32 public LinkedList<REPCommand> translate(REPCommand command){
|
|
33 LinkedList<REPCommand> list = new LinkedList<REPCommand>();
|
|
34 if(command.eid == eid){
|
|
35 if(checkReturnedCommand(command)){
|
|
36 REPCommand[] cmds = translater.catchOwnCommand(command);
|
|
37 sendMergedCommand(cmds);
|
|
38 return null;
|
|
39 }else{
|
|
40 sentList.add(command);
|
|
41 translater.transSendCmd(command);
|
|
42 list.add(command);
|
|
43 }
|
|
44 }else{
|
|
45 REPCommand[] cmds = translater.transReceiveCmd(command);
|
|
46 for(REPCommand cmd : cmds){
|
|
47 list.add(cmd);
|
|
48 }
|
|
49 }
|
|
50 return list;
|
|
51 }
|
|
52
|
|
53 private void sendMergedCommand(REPCommand[] cmds) {
|
|
54 for(int i = 0; i < cmds.length; i++){
|
|
55 try {
|
|
56 myChannel.write(pack(cmds[i]));
|
|
57 } catch (IOException e) {
|
|
58 e.printStackTrace();
|
|
59 }
|
|
60 }
|
|
61 }
|
56
|
62
|
142
|
63 private boolean checkReturnedCommand(REPCommand command) {
|
|
64 if(sentList.size() > 0){
|
|
65 if(sentList.get(0).seq == command.seq){
|
|
66 sentList.remove(0);
|
|
67 return true;
|
|
68 }else{
|
|
69 System.out.println("error:Editor.checkReturnedCommand()");
|
|
70 }
|
|
71 }
|
|
72 return false;
|
74
|
73 }
|
|
74
|
132
|
75 private void setHostAndPort(REPSocketChannel<REPCommand> myChannel2) {
|
131
|
76 String socketString = myChannel2.socket().getRemoteSocketAddress().toString();
|
56
|
77 String[] split = socketString.split("/");
|
|
78 int length = split.length;
|
|
79 String hostAndPort = split[length-1];
|
|
80 split = hostAndPort.split(":");
|
|
81 host = split[0];
|
|
82 port = split[1];
|
1
|
83 }
|
|
84
|
132
|
85 public REPSocketChannel<REPCommand> getChannel() {
|
|
86
|
56
|
87 return myChannel;
|
1
|
88 }
|
21
|
89
|
|
90 public void setHost(String host){
|
|
91 this.host = host;
|
|
92 }
|
|
93 public void setPort(String port){
|
|
94 this.port = port;
|
|
95 }
|
|
96
|
|
97 public String getHost(){
|
|
98 return host;
|
|
99 }
|
|
100 public String getPort(){
|
|
101 return port;
|
|
102 }
|
|
103
|
23
|
104 public int getEID() {
|
|
105 return eid;
|
|
106 }
|
|
107
|
|
108 public void setEID(int eid) {
|
|
109 this.eid = eid;
|
|
110 }
|
24
|
111 public String toString(){
|
74
|
112 return (host + ":" + port + ":" + file);
|
|
113 //return ("eid:" + eid + ":" + myChannel.socket().getLocalSocketAddress().toString());
|
24
|
114 }
|
23
|
115
|
38
|
116 public String getName() {
|
39
|
117 return file;
|
|
118 }
|
|
119
|
|
120 public void setName(String string) {
|
|
121 file = string;
|
38
|
122 }
|
|
123
|
56
|
124 public void send(REPCommand repCmd) {
|
142
|
125 try {
|
|
126 myChannel.write(pack(repCmd));
|
|
127 } catch (IOException e) {
|
|
128 e.printStackTrace();
|
|
129 }
|
|
130 }
|
|
131
|
|
132 private ByteBuffer pack(REPCommand repCmd) {
|
|
133 // TODO Auto-generated method stub
|
|
134 return null;
|
56
|
135 }
|
|
136
|
131
|
137 public void setChannel(REPSocketChannel channel) {
|
83
|
138 myChannel = channel;
|
|
139 }
|
|
140
|
1
|
141 }
|