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;
|
155
|
19
|
|
20 public Editor(){
|
|
21 setHostAndPort(myChannel);
|
|
22 translater = new TranslaterImp1(eid);
|
|
23 sentList = new LinkedList<REPCommand>();
|
|
24 }
|
1
|
25
|
132
|
26 public Editor(int editorNo, REPSocketChannel<REPCommand> channel){
|
23
|
27 this.eid = editorNo;
|
56
|
28 this.myChannel = channel;
|
142
|
29 translater = new TranslaterImp1(eid);
|
|
30 sentList = new LinkedList<REPCommand>();
|
152
|
31 setHostAndPort(myChannel);
|
56
|
32 }
|
142
|
33
|
132
|
34 public Editor(REPSocketChannel<REPCommand> channel) {
|
56
|
35 this.myChannel = channel;
|
|
36 setHostAndPort(myChannel);
|
152
|
37 translater = new TranslaterImp1(eid);
|
|
38 sentList = new LinkedList<REPCommand>();
|
56
|
39 }
|
142
|
40
|
|
41 public LinkedList<REPCommand> translate(REPCommand command){
|
|
42 LinkedList<REPCommand> list = new LinkedList<REPCommand>();
|
|
43 if(command.eid == eid){
|
|
44 if(checkReturnedCommand(command)){
|
152
|
45 //エディタからのコマンドが元のエディタに戻ってきた
|
142
|
46 REPCommand[] cmds = translater.catchOwnCommand(command);
|
|
47 sendMergedCommand(cmds);
|
|
48 return null;
|
|
49 }else{
|
152
|
50 //エディタからの新たな編集コマンド
|
142
|
51 sentList.add(command);
|
|
52 translater.transSendCmd(command);
|
|
53 list.add(command);
|
|
54 }
|
|
55 }else{
|
152
|
56 //他のエディタからの編集コマンド
|
142
|
57 REPCommand[] cmds = translater.transReceiveCmd(command);
|
|
58 for(REPCommand cmd : cmds){
|
|
59 list.add(cmd);
|
|
60 }
|
|
61 }
|
|
62 return list;
|
|
63 }
|
|
64
|
|
65 private void sendMergedCommand(REPCommand[] cmds) {
|
|
66 for(int i = 0; i < cmds.length; i++){
|
|
67 try {
|
|
68 myChannel.write(pack(cmds[i]));
|
|
69 } catch (IOException e) {
|
|
70 e.printStackTrace();
|
|
71 }
|
|
72 }
|
|
73 }
|
56
|
74
|
142
|
75 private boolean checkReturnedCommand(REPCommand command) {
|
|
76 if(sentList.size() > 0){
|
|
77 if(sentList.get(0).seq == command.seq){
|
|
78 sentList.remove(0);
|
|
79 return true;
|
|
80 }else{
|
|
81 System.out.println("error:Editor.checkReturnedCommand()");
|
|
82 }
|
|
83 }
|
|
84 return false;
|
74
|
85 }
|
|
86
|
132
|
87 private void setHostAndPort(REPSocketChannel<REPCommand> myChannel2) {
|
131
|
88 String socketString = myChannel2.socket().getRemoteSocketAddress().toString();
|
56
|
89 String[] split = socketString.split("/");
|
|
90 int length = split.length;
|
|
91 String hostAndPort = split[length-1];
|
|
92 split = hostAndPort.split(":");
|
|
93 host = split[0];
|
|
94 port = split[1];
|
1
|
95 }
|
|
96
|
132
|
97 public REPSocketChannel<REPCommand> getChannel() {
|
56
|
98 return myChannel;
|
1
|
99 }
|
21
|
100
|
|
101 public void setHost(String host){
|
|
102 this.host = host;
|
|
103 }
|
|
104 public void setPort(String port){
|
|
105 this.port = port;
|
|
106 }
|
|
107
|
|
108 public String getHost(){
|
|
109 return host;
|
|
110 }
|
|
111 public String getPort(){
|
|
112 return port;
|
|
113 }
|
|
114
|
23
|
115 public int getEID() {
|
|
116 return eid;
|
|
117 }
|
|
118
|
|
119 public void setEID(int eid) {
|
|
120 this.eid = eid;
|
|
121 }
|
24
|
122 public String toString(){
|
74
|
123 return (host + ":" + port + ":" + file);
|
24
|
124 }
|
23
|
125
|
38
|
126 public String getName() {
|
39
|
127 return file;
|
|
128 }
|
|
129
|
|
130 public void setName(String string) {
|
|
131 file = string;
|
38
|
132 }
|
|
133
|
56
|
134 public void send(REPCommand repCmd) {
|
142
|
135 try {
|
|
136 myChannel.write(pack(repCmd));
|
|
137 } catch (IOException e) {
|
|
138 e.printStackTrace();
|
|
139 }
|
|
140 }
|
|
141
|
|
142 private ByteBuffer pack(REPCommand repCmd) {
|
|
143 // TODO Auto-generated method stub
|
|
144 return null;
|
56
|
145 }
|
|
146
|
131
|
147 public void setChannel(REPSocketChannel channel) {
|
83
|
148 myChannel = channel;
|
|
149 }
|
|
150
|
1
|
151 }
|