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