0
|
1 package rep;
|
|
2
|
|
3 import java.nio.channels.SocketChannel;
|
|
4 import java.util.LinkedList;
|
130
|
5
|
|
6 import rep.channel.REPSocketChannel;
|
41
|
7 import rep.xml.SessionXMLEncoder;
|
39
|
8
|
0
|
9 public class SessionList {
|
|
10
|
41
|
11 // Hashtable<Integer, Session> session3 = new Hashtable<Integer, Session>();
|
56
|
12 LinkedList<Session> sessionLinkedList = new LinkedList<Session>();
|
38
|
13 LinkedList<String> stringlist = new LinkedList<String>();
|
1
|
14 private int sessionID = 0;
|
|
15 private int editorCount = 0;
|
0
|
16
|
2
|
17 private boolean channelequals = false;
|
31
|
18 private LinkedList<Session> tmpSessionList = new LinkedList<Session>();
|
80
|
19 private String maxHost;
|
122
|
20 private LinkedList<Integer> temp = new LinkedList<Integer>();
|
31
|
21
|
120
|
22 //private TranslaterImp1 translater;
|
|
23
|
31
|
24 public SessionList(){
|
|
25 makeDefaultSession();
|
|
26 }
|
0
|
27
|
130
|
28 public void add(REPSocketChannel<REPCommand> channel) {
|
0
|
29
|
|
30 }
|
31
|
31
|
|
32 public void makeDefaultSession(){
|
41
|
33 // session3.put(0, new Session(0, null, null));
|
43
|
34 // session4.add(0, new Session(0, null, null));
|
31
|
35 }
|
0
|
36
|
|
37 public int getEditorNumber() {
|
|
38 return 0;
|
|
39
|
|
40 }
|
|
41
|
130
|
42 public void add(REPSocketChannel<REPCommand> channel, int sid) {
|
0
|
43
|
|
44 }
|
|
45
|
130
|
46 public int addSession(REPSocketChannel<REPCommand> channel, String string) {
|
0
|
47 sessionID++;
|
41
|
48 // session3.put(sessionID, new Session(sessionID, string, channel));
|
56
|
49 sessionLinkedList.add(new Session(sessionID, string, channel));
|
0
|
50 return sessionID;
|
|
51
|
|
52 }
|
|
53
|
130
|
54 public void addEditor(REPSocketChannel<REPCommand> channel, int sid, REPCommand repCmd) {
|
1
|
55 int editorID = repCmd.eid;
|
122
|
56 // if(session3.get(sid) == null) System.out.println("ぬるぽ!");
|
41
|
57 // session3.get(sid).addEditor(editorID, channel);
|
122
|
58 sessionLinkedList.get(sid-1).addEditor(editorID, channel); //本当はforループで検索しないといけないよ。
|
0
|
59 }
|
|
60
|
120
|
61 // public int getSessionID(SocketChannel channel) {
|
|
62 // return 0;
|
|
63 // }
|
0
|
64
|
|
65 public int getNumberOfEditor() {
|
|
66 editorCount++;
|
|
67 return editorCount;
|
|
68 }
|
130
|
69
|
|
70 public void sendCmd(REPSocketChannel<REPCommand> channel2, REPCommand repCmd) {
|
41
|
71 // LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList();
|
122
|
72 LinkedList <Editor> editorList = sessionLinkedList.get(repCmd.sid).getEditorList(); //ここもforループで検索しないといけないよ。
|
1
|
73 for(Editor editor : editorList){
|
130
|
74 REPSocketChannel<REPCommand> channel = editor.getChannel();
|
0
|
75 if(channel.equals(channel2)) {
|
|
76 System.out.println("equals");
|
|
77 continue;
|
|
78 }
|
|
79 REPPacketSend repSend = new REPPacketSend(channel);
|
|
80 repSend.send(repCmd);
|
|
81 }
|
|
82 }
|
2
|
83
|
130
|
84 public void sendCmd(REPSocketChannel<REPCommand> channel, REPCommand repCmd, boolean ring){
|
41
|
85 // LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList();
|
122
|
86 LinkedList <Editor> editorList = sessionLinkedList.get(repCmd.sid).getEditorList(); //ここもforループで検索しないといけないよ。
|
2
|
87 REPPacketSend repsend = null;
|
|
88 for(Editor editor : editorList){
|
130
|
89 REPSocketChannel<REPCommand> channel2 = editor.getChannel();
|
2
|
90 if(channel.equals(channel2)){
|
9
|
91 System.out.println("equals");
|
2
|
92 channelequals = true;
|
|
93 }else if(channelequals){
|
|
94 repsend = new REPPacketSend(channel2);
|
|
95 repsend.send(repCmd);
|
|
96 }
|
|
97 }
|
|
98 }
|
0
|
99
|
1
|
100 public void addSessionManager(SocketChannel channel, REPCommand repCmd) {
|
|
101
|
|
102 }
|
|
103
|
41
|
104 // public void sendAddedSession(REPCommand repCmd) {
|
|
105 // repCmd.setCMD(REP.SMCMD_SM_JOIN);
|
|
106 // REPPacketSend repSend = new REPPacketSend(smchannel);
|
|
107 // }
|
1
|
108
|
3
|
109 public String getSessionList() {
|
41
|
110 // if (session3 != null) {
|
|
111 // System.out.println(session3.toString());
|
|
112 // return session3.toString();
|
|
113 // }
|
56
|
114 if (sessionLinkedList != null) {
|
|
115 System.out.println(sessionLinkedList.toString());
|
|
116 return sessionLinkedList.toString();
|
3
|
117 }
|
|
118 return "{}";
|
|
119 }
|
|
120
|
130
|
121 public void sendToNextEditor(REPSocketChannel<REPCommand> channel, REPCommand repCmd) {
|
8
|
122 Editor editor2 = null;
|
|
123 Editor editor3;
|
41
|
124 // LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList();
|
120
|
125 // this.getSession(sid);
|
122
|
126 LinkedList <Editor> editorList = sessionLinkedList.get(repCmd.sid-1).getEditorList(); //ここもforループで検索しないといけないよ。
|
8
|
127 for(Editor editor : editorList){
|
132
|
128 REPSocketChannel<REPCommand> channel2 = editor.getChannel();
|
8
|
129 if(channel.equals(channel2)){
|
|
130 editor2 = editor;
|
|
131 }
|
|
132 }
|
|
133 int size = editorList.size();
|
|
134 Editor lastEditor = (Editor) editorList.getLast();
|
|
135 if(size == 0){
|
|
136 editor3 = null;
|
|
137 }else if(editor2 == lastEditor){
|
|
138 editor3 = editorList.get(0);
|
|
139 }else{
|
|
140 editor3 = editorList.get(editorList.indexOf(editor2)+1);
|
|
141 }
|
9
|
142 if(editor3 != null){
|
122
|
143 //temp.add(repCmd.cmd);
|
|
144 REPCommand getUndoCommand = new REPCommand();
|
|
145 getUndoCommand.setCMD(REP.SMCMD_GET_UNDO);
|
|
146 getUndoCommand.lineno = repCmd.lineno;
|
|
147 editor3.send(getUndoCommand);
|
|
148 editor3.setKindOfUndoCmd(reverseCmd(repCmd.cmd));
|
120
|
149 editor3.send(repCmd);
|
|
150 //REPPacketSend send = new REPPacketSend(editor3.getChannel());
|
|
151 //send.send(repCmd);
|
9
|
152 }
|
8
|
153 }
|
|
154
|
122
|
155 private int reverseCmd(int cmd) {
|
|
156 // TODO Auto-generated method stub
|
|
157 int kindOfCmd = 0;
|
|
158 switch(cmd){
|
|
159 case REP.REPCMD_INSERT:
|
|
160 kindOfCmd = REP.REPCMD_DELETE;
|
|
161 break;
|
|
162 case REP.REPCMD_DELETE:
|
|
163 kindOfCmd = REP.REPCMD_INSERT;
|
|
164 break;
|
|
165 case REP.REPCMD_REPLACE:
|
|
166 kindOfCmd = REP.REPCMD_REPLACE;
|
|
167 break;
|
|
168 }
|
|
169 return kindOfCmd;
|
|
170 }
|
|
171
|
130
|
172 public void addEditor(REPSocketChannel<REPCommand> editorChannel, int sid, int eid) {
|
41
|
173 // session3.get(sid).addEditor(eid, editorChannel);
|
56
|
174 sessionLinkedList.get(sid-1).addEditor(eid, editorChannel);
|
8
|
175 }
|
|
176
|
|
177 public void sendSelect(int sid) {
|
41
|
178 // SocketChannel channel = session3.get(sid).getMaster().getChannel();
|
130
|
179 REPSocketChannel<REPCommand> channel = sessionLinkedList.get(sid-1).getMaster().getChannel();
|
8
|
180 REPPacketSend send = new REPPacketSend(channel);
|
|
181 send.send(new REPCommand(REP.SMCMD_SELECT, sid, 0, 0, 0, 0, ""));
|
|
182 }
|
|
183
|
132
|
184 public void addSession(REPSocketChannel<REPCommand> channel, String string, int i) {
|
31
|
185 tmpSessionList.add(new Session(0, string, channel));
|
|
186 }
|
|
187
|
39
|
188 // public String getXML() {
|
122
|
189 ///* SessionListをXMLに書き出す。ときの形式
|
39
|
190 // * <Session>
|
|
191 // * <editor>
|
|
192 // * <sid/>
|
|
193 // * <host/>
|
|
194 // * <port/>
|
|
195 // * <filename/>
|
|
196 // * </editor>
|
|
197 // * <editor>
|
|
198 // * <sid/>
|
|
199 // * <host/>
|
|
200 // * <port/>
|
|
201 // * </editor>
|
|
202 // * </Session>
|
|
203 // * <SessionManager>
|
|
204 // * <host/>
|
|
205 // * <port/>
|
|
206 // * </SessionManager>
|
|
207 // * <Session>
|
|
208 // * </Session>
|
41
|
209 // *
|
39
|
210 // StringWriter str = null;
|
|
211 // try {
|
|
212 // str = new StringWriter();
|
|
213 // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
214 // DocumentBuilder builder = factory.newDocumentBuilder();
|
|
215 // Document doc = builder.newDocument();
|
|
216 // Element root = doc.getDocumentElement();
|
|
217 // root = doc.createElement("SessionList");
|
|
218 // for(Session session : session4){
|
|
219 // Element element = doc.createElement("Session");
|
|
220 // root.appendChild(element);
|
|
221 // Element element2 = doc.createElement("editor");
|
|
222 // element.appendChild(element2);
|
|
223 // Editor editor = session.getMaster();
|
|
224 // Element element3 = doc.createElement(editor.getHost());
|
|
225 // Element element4 = doc.createElement(editor.getPort());
|
|
226 // Element element5 = doc.createElement(editor.getName());
|
|
227 // element5 = doc.createElement(session.getName());
|
|
228 // element2.appendChild(element3);
|
|
229 // element2.appendChild(element4);
|
|
230 // element2.appendChild(element5);
|
|
231 //
|
|
232 // }
|
|
233 // doc.appendChild(root);
|
38
|
234 //
|
39
|
235 // TransformerFactory tfactory = TransformerFactory.newInstance();
|
|
236 // Transformer transformer = tfactory.newTransformer();
|
|
237 // StreamResult result = new StreamResult(str);
|
|
238 // //transformer.transform(new DOMSource(doc), new StreamResult(System.out));
|
|
239 // transformer.transform(new DOMSource(doc), result);
|
|
240 // System.out.println(str);
|
|
241 ////
|
|
242 //// XMLEncoder e = new XMLEncoder(System.out);
|
|
243 //// e.writeObject(session4);
|
|
244 //// e.writeObject(stringlist);
|
|
245 //// e.close();
|
|
246 //
|
|
247 // //System.out.println(doc);
|
|
248 // }catch (Exception e){
|
|
249 // e.printStackTrace();
|
|
250 // }
|
|
251 // return str.toString();
|
41
|
252 // }*/
|
39
|
253
|
|
254 public LinkedList<Session> getList() {
|
56
|
255 return sessionLinkedList;
|
39
|
256 }
|
|
257
|
|
258 public String toXML() {
|
56
|
259 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionLinkedList);
|
39
|
260 encoder.sessionListToXML();
|
|
261 return null;
|
|
262 }
|
|
263
|
|
264 public int addSession(Session session) {
|
|
265 sessionID++;
|
41
|
266 // session3.put(sessionID, session);
|
56
|
267 session.setSID(sessionID);
|
|
268 sessionLinkedList.add(session);
|
39
|
269 return sessionID;
|
38
|
270 }
|
|
271
|
56
|
272 public void addToRoutingTable(SocketChannel channel) {
|
|
273
|
|
274 }
|
|
275
|
66
|
276 public Session getSession(int sid) {
|
120
|
277 for(Session session : sessionLinkedList){
|
|
278 if(session.getSID() == sid){
|
|
279 return session;
|
|
280 }
|
|
281 }
|
66
|
282 return sessionLinkedList.get(sid - 1);
|
|
283 }
|
|
284
|
80
|
285 public void setMaxHost(String myHost) {
|
77
|
286 // TODO Auto-generated method stub
|
80
|
287 maxHost = myHost;
|
77
|
288 }
|
|
289
|
82
|
290 public String getMaxHost() {
|
77
|
291 // TODO Auto-generated method stub
|
80
|
292 return maxHost;
|
77
|
293 }
|
|
294
|
|
295 public void setList(LinkedList<Session> list) {
|
|
296 // TODO Auto-generated method stub
|
|
297 sessionLinkedList = list;
|
|
298 }
|
|
299
|
130
|
300 public void update(REPSocketChannel<REPCommand> channel, SessionList receivedSessionList) {
|
83
|
301 LinkedList<Session> list = receivedSessionList.getList();
|
|
302 int i = 0;
|
|
303 for(Session session : list){
|
101
|
304 session.getEditorList().get(0).setChannel(channel);
|
83
|
305 ++i;
|
|
306 int sid = addSession(session);
|
|
307 setTranslationTable(channel, i, sid);
|
|
308 }
|
|
309 }
|
|
310
|
135
|
311 private void setTranslationTable(REPSocketChannel<REPCommand> channel, int i, int sid) {
|
83
|
312
|
|
313 }
|
77
|
314
|
0
|
315 }
|