154
|
1 package test.channeltest;
|
|
2
|
|
3 import java.io.IOException;
|
157
|
4 import java.net.InetSocketAddress;
|
|
5 import java.net.SocketAddress;
|
|
6 import java.nio.channels.SelectionKey;
|
154
|
7 import java.util.LinkedList;
|
196
|
8 import java.util.Set;
|
154
|
9
|
157
|
10 import rep.channel.REPLogger;
|
196
|
11 import rep.channel.REPPack;
|
|
12 import rep.channel.REPSelectionKey;
|
157
|
13 import rep.channel.REPSelector;
|
|
14 import rep.channel.REPServerSocketChannel;
|
|
15 import rep.channel.REPSocketChannel;
|
154
|
16
|
|
17 public class testSeMa extends Thread{
|
|
18
|
157
|
19 SocketAddress IP;
|
154
|
20 boolean running=true;
|
157
|
21 REPLogger ns;
|
|
22 LinkedList<REPSocketChannel<String>> channels;
|
166
|
23
|
161
|
24 public testSeMa(String name, String host, int port){
|
154
|
25 super(name);
|
157
|
26 IP = new InetSocketAddress(host,port);
|
174
|
27 ns = REPLogger.singleton();
|
157
|
28 channels = new LinkedList<REPSocketChannel<String>>();
|
154
|
29 }
|
|
30 public void init(){
|
166
|
31
|
154
|
32 }
|
|
33
|
|
34 public void run() {
|
205
|
35 REPSelector<String> selector=null;
|
171
|
36
|
157
|
37 REPServerSocketChannel<String> scs;
|
205
|
38 REPPack<String> pack = new StringPacker();
|
157
|
39 try {
|
196
|
40 selector = REPSelector.<String>create();
|
|
41 scs = REPServerSocketChannel.<String>open(pack);
|
166
|
42 scs.socket().setReuseAddress(true);
|
|
43 scs.socket().bind(IP);
|
|
44 scs.configureBlocking(false);
|
175
|
45 scs.register(selector, SelectionKey.OP_ACCEPT, null);
|
157
|
46 } catch (IOException e1) {
|
209
|
47 e1.printStackTrace();
|
175
|
48 ns.writeLog("cannot create REPServerSocketChannel!, exit.");
|
166
|
49 return;
|
157
|
50 }
|
175
|
51 ns.writeLog("selector is "+selector.toString());
|
|
52 ns.writeLog("REPssc is "+scs.toString());
|
157
|
53
|
196
|
54 ns.writeLog("SessionManager starts main routine.", 1);
|
154
|
55
|
|
56 /* Main Loop */
|
|
57 while(running){
|
|
58
|
166
|
59 try {
|
248
|
60 selector.select();
|
196
|
61 Set<REPSelectionKey<String>> set = selector.selectedKeys1();
|
|
62 for(REPSelectionKey<String> key : set) {
|
154
|
63
|
166
|
64 if(key.isAcceptable()){
|
196
|
65 REPSocketChannel<String> channel = key.accept(pack);
|
203
|
66 if(channel==null) continue;
|
196
|
67 channel.configureBlocking(false);
|
175
|
68 channel.register(selector, SelectionKey.OP_READ, null);
|
174
|
69 ns.writeLog("accepts a client.", 1);
|
166
|
70
|
|
71 }else if(key.isReadable()){
|
268
|
72 try {
|
308
|
73 REPSocketChannel<String> channel = key.channel1();
|
268
|
74 String packet;
|
|
75 packet = channel.read();
|
|
76 if (packet==null) continue;
|
|
77 ns.writeLog("receives String==> `"+packet+"\'", 1);
|
|
78 channel.write(this.getName()+": get `"+packet+"\'");
|
|
79 }catch (IOException e) {
|
|
80 ns.writeLog("channel "+ns+"closed.");
|
|
81 key.cancel();
|
|
82 }
|
157
|
83 }
|
154
|
84 }
|
|
85 }
|
166
|
86 catch (IOException e) { e.printStackTrace();}
|
154
|
87 }
|
166
|
88
|
154
|
89 }
|
|
90 }
|