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
|
157
|
34 @SuppressWarnings("unchecked")
|
154
|
35 public void run() {
|
205
|
36 REPSelector<String> selector=null;
|
171
|
37
|
157
|
38 REPServerSocketChannel<String> scs;
|
205
|
39 REPPack<String> pack = new StringPacker();
|
157
|
40 try {
|
196
|
41 selector = REPSelector.<String>create();
|
|
42 scs = REPServerSocketChannel.<String>open(pack);
|
166
|
43 scs.socket().setReuseAddress(true);
|
|
44 scs.socket().bind(IP);
|
|
45 scs.configureBlocking(false);
|
175
|
46 scs.register(selector, SelectionKey.OP_ACCEPT, null);
|
157
|
47 } catch (IOException e1) {
|
209
|
48 e1.printStackTrace();
|
175
|
49 ns.writeLog("cannot create REPServerSocketChannel!, exit.");
|
166
|
50 return;
|
157
|
51 }
|
175
|
52 ns.writeLog("selector is "+selector.toString());
|
|
53 ns.writeLog("REPssc is "+scs.toString());
|
157
|
54
|
196
|
55 ns.writeLog("SessionManager starts main routine.", 1);
|
154
|
56
|
|
57 /* Main Loop */
|
|
58 while(running){
|
|
59
|
166
|
60 try {
|
248
|
61 selector.select();
|
196
|
62 Set<REPSelectionKey<String>> set = selector.selectedKeys1();
|
|
63 for(REPSelectionKey<String> key : set) {
|
154
|
64
|
166
|
65 if(key.isAcceptable()){
|
196
|
66 REPSocketChannel<String> channel = key.accept(pack);
|
203
|
67 if(channel==null) continue;
|
196
|
68 channel.configureBlocking(false);
|
175
|
69 channel.register(selector, SelectionKey.OP_READ, null);
|
174
|
70 ns.writeLog("accepts a client.", 1);
|
166
|
71
|
|
72 }else if(key.isReadable()){
|
268
|
73 try {
|
273
|
74 REPSocketChannel<String> channel = (REPSocketChannel<String>) key.channel();
|
268
|
75 String packet;
|
|
76 packet = channel.read();
|
|
77 if (packet==null) continue;
|
|
78 ns.writeLog("receives String==> `"+packet+"\'", 1);
|
|
79 channel.write(this.getName()+": get `"+packet+"\'");
|
|
80 }catch (IOException e) {
|
|
81 ns.writeLog("channel "+ns+"closed.");
|
|
82 key.cancel();
|
|
83 }
|
157
|
84 }
|
154
|
85 }
|
|
86 }
|
166
|
87 catch (IOException e) { e.printStackTrace();}
|
154
|
88 }
|
166
|
89
|
154
|
90 }
|
|
91 }
|