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