0
|
1 package rep;
|
|
2
|
|
3 import java.nio.channels.SocketChannel;
|
|
4 import java.util.Hashtable;
|
|
5 import java.util.LinkedList;
|
|
6 import java.util.List;
|
|
7
|
|
8 public class SessionList {
|
|
9
|
|
10 //List<LinkedList<SocketChannel>> sessions = new LinkedList<LinkedList<SocketChannel>>();
|
1
|
11 //Hashtable<Integer, LinkedList<SocketChannel>> sessions2 = new Hashtable<Integer, LinkedList<SocketChannel>>();
|
|
12 Hashtable<Integer, Session> session3 = new Hashtable<Integer, Session>();
|
|
13 private int sessionID = 0;
|
|
14 private int editorCount = 0;
|
0
|
15
|
1
|
16 private SocketChannel smchannel;
|
2
|
17 private boolean channelequals = false;
|
0
|
18
|
|
19 public void add(SocketChannel channel) {
|
|
20
|
|
21 }
|
|
22
|
|
23 public int getEditorNumber() {
|
|
24 return 0;
|
|
25
|
|
26 }
|
|
27
|
|
28 public void add(SocketChannel channel, int sid) {
|
|
29
|
|
30 }
|
|
31
|
|
32 public int addSession(SocketChannel channel, String string) {
|
|
33 sessionID++;
|
1
|
34 //sessions2.put(sessionID, new LinkedList<SocketChannel>());
|
0
|
35 //sessions.add(new LinkedList<SocketChannel>());
|
|
36 //return sessions2.size();
|
1
|
37 session3.put(sessionID, new Session(sessionID, string, channel));
|
9
|
38 //addEditor(channel, sid, command);
|
0
|
39 return sessionID;
|
|
40
|
|
41 }
|
|
42
|
1
|
43 public void addEditor(SocketChannel channel, int sid, REPCommand repCmd) {
|
|
44 int editorID = repCmd.eid;
|
0
|
45 //editorCount++;
|
|
46 //sessions.get(sid-1).add(channel);
|
1
|
47 //sessions2.get(sid).add(channel);
|
9
|
48 if(session3.get(sid) == null) System.out.println("ʂہI");
|
1
|
49 session3.get(sid).addEditor(editorID, channel);
|
0
|
50 }
|
|
51
|
|
52 public int getSessionID(SocketChannel channel) {
|
|
53 return 0;
|
|
54 }
|
|
55
|
|
56 public int getNumberOfEditor() {
|
|
57 editorCount++;
|
|
58 return editorCount;
|
|
59 }
|
2
|
60
|
0
|
61 public void sendCmd(SocketChannel channel2, REPCommand repCmd) {
|
|
62 //int sessionID = repCmd.sid;
|
1
|
63 //LinkedList <SocketChannel> channelList = sessions2.get(repCmd.sid);
|
|
64 LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList();
|
|
65 for(Editor editor : editorList){
|
|
66 SocketChannel channel = editor.getChannel();
|
0
|
67 if(channel.equals(channel2)) {
|
|
68 System.out.println("equals");
|
|
69 continue;
|
|
70 }
|
|
71 REPPacketSend repSend = new REPPacketSend(channel);
|
|
72 repSend.send(repCmd);
|
|
73 }
|
|
74 }
|
2
|
75
|
|
76 public void sendCmd(SocketChannel channel, REPCommand repCmd, boolean ring){
|
|
77 LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList();
|
|
78 REPPacketSend repsend = null;
|
|
79 for(Editor editor : editorList){
|
|
80 SocketChannel channel2 = editor.getChannel();
|
|
81 if(channel.equals(channel2)){
|
9
|
82 System.out.println("equals");
|
2
|
83 channelequals = true;
|
|
84 }else if(channelequals){
|
|
85 repsend = new REPPacketSend(channel2);
|
|
86 repsend.send(repCmd);
|
|
87 }
|
|
88 }
|
|
89 //repsend.send(repCmd);
|
|
90 //repCmd.setCMD(repCmd.cmd + 1);
|
|
91 //repsend.send(repCmd);
|
|
92 }
|
0
|
93
|
1
|
94 public void addSessionManager(SocketChannel channel, REPCommand repCmd) {
|
|
95 smchannel = channel;
|
|
96
|
|
97 }
|
|
98
|
|
99 public void sendAddedSession(REPCommand repCmd) {
|
8
|
100 repCmd.setCMD(REP.SMCMD_SM_JOIN);
|
1
|
101 REPPacketSend repSend = new REPPacketSend(smchannel);
|
|
102 //repSend.send(repCmd);
|
|
103 }
|
|
104
|
3
|
105 public String getSessionList() {
|
|
106 if (session3 != null) {
|
|
107 System.out.println(session3.toString());
|
|
108 return session3.toString();
|
|
109 }
|
|
110 return "{}";
|
|
111 }
|
|
112
|
8
|
113 public void sendToNextEditor(SocketChannel channel, REPCommand repCmd) {
|
|
114 Editor editor2 = null;
|
|
115 Editor editor3;
|
|
116 LinkedList <Editor> editorList = session3.get(repCmd.sid).getEditorList();
|
|
117 for(Editor editor : editorList){
|
|
118 SocketChannel channel2 = editor.getChannel();
|
|
119 if(channel.equals(channel2)){
|
|
120 editor2 = editor;
|
|
121 }
|
|
122 }
|
|
123 int size = editorList.size();
|
|
124 Editor lastEditor = (Editor) editorList.getLast();
|
|
125 if(size == 0){
|
|
126 editor3 = null;
|
|
127 }else if(editor2 == lastEditor){
|
|
128 editor3 = editorList.get(0);
|
|
129 }else{
|
|
130 editor3 = editorList.get(editorList.indexOf(editor2)+1);
|
|
131 }
|
9
|
132 if(editor3 != null){
|
|
133 REPPacketSend send = new REPPacketSend(editor3.getChannel());
|
|
134 send.send(repCmd);
|
|
135 }
|
8
|
136 }
|
|
137
|
|
138 public void addEditor(SocketChannel editorChannel, int sid, int eid) {
|
|
139
|
|
140 session3.get(sid).addEditor(eid, editorChannel);
|
|
141 }
|
|
142
|
|
143 public void sendSelect(int sid) {
|
|
144 SocketChannel channel = session3.get(sid).masterEditor.getChannel();
|
|
145 REPPacketSend send = new REPPacketSend(channel);
|
|
146 send.send(new REPCommand(REP.SMCMD_SELECT, sid, 0, 0, 0, 0, ""));
|
|
147 }
|
|
148
|
0
|
149 }
|