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
|
122
|
121 private int reverseCmd(int cmd) {
|
|
122 // TODO Auto-generated method stub
|
|
123 int kindOfCmd = 0;
|
|
124 switch(cmd){
|
|
125 case REP.REPCMD_INSERT:
|
|
126 kindOfCmd = REP.REPCMD_DELETE;
|
|
127 break;
|
|
128 case REP.REPCMD_DELETE:
|
|
129 kindOfCmd = REP.REPCMD_INSERT;
|
|
130 break;
|
|
131 case REP.REPCMD_REPLACE:
|
|
132 kindOfCmd = REP.REPCMD_REPLACE;
|
|
133 break;
|
|
134 }
|
|
135 return kindOfCmd;
|
|
136 }
|
|
137
|
130
|
138 public void addEditor(REPSocketChannel<REPCommand> editorChannel, int sid, int eid) {
|
41
|
139 // session3.get(sid).addEditor(eid, editorChannel);
|
56
|
140 sessionLinkedList.get(sid-1).addEditor(eid, editorChannel);
|
8
|
141 }
|
|
142
|
|
143 public void sendSelect(int sid) {
|
41
|
144 // SocketChannel channel = session3.get(sid).getMaster().getChannel();
|
158
|
145 REPSocketChannel<REPCommand> channel = sessionLinkedList.get(sid-1).getOwner().getChannel();
|
8
|
146 REPPacketSend send = new REPPacketSend(channel);
|
|
147 send.send(new REPCommand(REP.SMCMD_SELECT, sid, 0, 0, 0, 0, ""));
|
|
148 }
|
|
149
|
132
|
150 public void addSession(REPSocketChannel<REPCommand> channel, String string, int i) {
|
31
|
151 tmpSessionList.add(new Session(0, string, channel));
|
|
152 }
|
|
153
|
39
|
154 // public String getXML() {
|
122
|
155 ///* SessionListをXMLに書き出す。ときの形式
|
39
|
156 // * <Session>
|
|
157 // * <editor>
|
|
158 // * <sid/>
|
|
159 // * <host/>
|
|
160 // * <port/>
|
|
161 // * <filename/>
|
|
162 // * </editor>
|
|
163 // * <editor>
|
|
164 // * <sid/>
|
|
165 // * <host/>
|
|
166 // * <port/>
|
|
167 // * </editor>
|
|
168 // * </Session>
|
|
169 // * <SessionManager>
|
|
170 // * <host/>
|
|
171 // * <port/>
|
|
172 // * </SessionManager>
|
|
173 // * <Session>
|
|
174 // * </Session>
|
41
|
175 // *
|
39
|
176 // StringWriter str = null;
|
|
177 // try {
|
|
178 // str = new StringWriter();
|
|
179 // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
180 // DocumentBuilder builder = factory.newDocumentBuilder();
|
|
181 // Document doc = builder.newDocument();
|
|
182 // Element root = doc.getDocumentElement();
|
|
183 // root = doc.createElement("SessionList");
|
|
184 // for(Session session : session4){
|
|
185 // Element element = doc.createElement("Session");
|
|
186 // root.appendChild(element);
|
|
187 // Element element2 = doc.createElement("editor");
|
|
188 // element.appendChild(element2);
|
|
189 // Editor editor = session.getMaster();
|
|
190 // Element element3 = doc.createElement(editor.getHost());
|
|
191 // Element element4 = doc.createElement(editor.getPort());
|
|
192 // Element element5 = doc.createElement(editor.getName());
|
|
193 // element5 = doc.createElement(session.getName());
|
|
194 // element2.appendChild(element3);
|
|
195 // element2.appendChild(element4);
|
|
196 // element2.appendChild(element5);
|
|
197 //
|
|
198 // }
|
|
199 // doc.appendChild(root);
|
38
|
200 //
|
39
|
201 // TransformerFactory tfactory = TransformerFactory.newInstance();
|
|
202 // Transformer transformer = tfactory.newTransformer();
|
|
203 // StreamResult result = new StreamResult(str);
|
|
204 // //transformer.transform(new DOMSource(doc), new StreamResult(System.out));
|
|
205 // transformer.transform(new DOMSource(doc), result);
|
|
206 // System.out.println(str);
|
|
207 ////
|
|
208 //// XMLEncoder e = new XMLEncoder(System.out);
|
|
209 //// e.writeObject(session4);
|
|
210 //// e.writeObject(stringlist);
|
|
211 //// e.close();
|
|
212 //
|
|
213 // //System.out.println(doc);
|
|
214 // }catch (Exception e){
|
|
215 // e.printStackTrace();
|
|
216 // }
|
|
217 // return str.toString();
|
41
|
218 // }*/
|
39
|
219
|
|
220 public LinkedList<Session> getList() {
|
56
|
221 return sessionLinkedList;
|
39
|
222 }
|
|
223
|
|
224 public String toXML() {
|
56
|
225 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionLinkedList);
|
39
|
226 encoder.sessionListToXML();
|
|
227 return null;
|
|
228 }
|
|
229
|
|
230 public int addSession(Session session) {
|
|
231 sessionID++;
|
41
|
232 // session3.put(sessionID, session);
|
56
|
233 session.setSID(sessionID);
|
|
234 sessionLinkedList.add(session);
|
39
|
235 return sessionID;
|
38
|
236 }
|
|
237
|
56
|
238 public void addToRoutingTable(SocketChannel channel) {
|
|
239
|
|
240 }
|
|
241
|
66
|
242 public Session getSession(int sid) {
|
120
|
243 for(Session session : sessionLinkedList){
|
|
244 if(session.getSID() == sid){
|
|
245 return session;
|
|
246 }
|
|
247 }
|
66
|
248 return sessionLinkedList.get(sid - 1);
|
|
249 }
|
|
250
|
80
|
251 public void setMaxHost(String myHost) {
|
77
|
252 // TODO Auto-generated method stub
|
80
|
253 maxHost = myHost;
|
77
|
254 }
|
|
255
|
82
|
256 public String getMaxHost() {
|
77
|
257 // TODO Auto-generated method stub
|
80
|
258 return maxHost;
|
77
|
259 }
|
|
260
|
|
261 public void setList(LinkedList<Session> list) {
|
|
262 // TODO Auto-generated method stub
|
|
263 sessionLinkedList = list;
|
|
264 }
|
|
265
|
130
|
266 public void update(REPSocketChannel<REPCommand> channel, SessionList receivedSessionList) {
|
83
|
267 LinkedList<Session> list = receivedSessionList.getList();
|
|
268 int i = 0;
|
|
269 for(Session session : list){
|
101
|
270 session.getEditorList().get(0).setChannel(channel);
|
83
|
271 ++i;
|
|
272 int sid = addSession(session);
|
|
273 setTranslationTable(channel, i, sid);
|
|
274 }
|
|
275 }
|
|
276
|
135
|
277 private void setTranslationTable(REPSocketChannel<REPCommand> channel, int i, int sid) {
|
83
|
278
|
|
279 }
|
77
|
280
|
0
|
281 }
|