152
|
1 package pathfinder.mergetest;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.List;
|
|
5
|
|
6 import remoteeditor.command.REPCommand;
|
|
7 import sample.merge.TranslaterImp1;
|
|
8
|
|
9 public class EditorObject<P> {
|
|
10
|
|
11 int eid;
|
|
12 private ChannelSimulator<P> channel;
|
|
13 private TranslaterImp1 translater;
|
|
14 private List<REPCommand> sentList;
|
|
15
|
|
16 public EditorObject(int i, ChannelSimulator<P> cs) {
|
|
17 // TODO Auto-generated constructor stub
|
|
18 eid = i;
|
|
19 channel = cs;
|
|
20 }
|
|
21
|
|
22 public EditorObject(int i, ChannelSimulator<P> cs, TranslaterImp1 imp1) {
|
|
23 // TODO Auto-generated constructor stub
|
|
24 eid = i;
|
|
25 channel = cs;
|
|
26 translater = imp1;
|
|
27 sentList = new ArrayList<REPCommand>();
|
|
28 }
|
|
29
|
|
30 public ChannelSimulator<P> getChannel() {
|
|
31 // TODO Auto-generated method stub
|
|
32 return channel;
|
|
33 }
|
|
34
|
|
35 public int getEID() {
|
|
36 // TODO Auto-generated method stub
|
|
37 return eid;
|
|
38 }
|
|
39
|
|
40 public REPCommand receive(REPCommand command) {
|
|
41 // TODO Auto-generated method stub
|
|
42
|
|
43 if(command.eid == eid){
|
|
44 if(checkReturnCommand(command)){
|
|
45 REPCommand[] cmds = translater.catchOwnCommand(command);
|
|
46 sendMergedCommand(cmds);
|
|
47 return null;
|
|
48 }else{
|
|
49 sentList.add(command);
|
|
50 }
|
|
51 }
|
|
52
|
|
53 return command;
|
|
54 }
|
|
55
|
|
56 private boolean checkReturnCommand(REPCommand command) {
|
|
57 // TODO Auto-generated method stub
|
|
58
|
|
59 if(sentList.size() > 0){
|
|
60 if(sentList.get(0).seq == command.seq){
|
|
61 return true;
|
|
62 }
|
|
63 }
|
|
64
|
|
65 return false;
|
|
66 }
|
|
67
|
|
68 private void sendMergedCommand(REPCommand[] cmds) {
|
|
69 // TODO Auto-generated method stub
|
|
70 for(int i = 0; i < cmds.length; i++){
|
|
71 channel.write((P) cmds[i]);
|
|
72 }
|
|
73 }
|
|
74
|
|
75 public void send(REPCommand command) {
|
|
76 // TODO Auto-generated method stub
|
|
77 if(command !=null){
|
|
78 channel.write((P) command);
|
|
79 }
|
|
80 }
|
|
81
|
|
82 }
|