comparison test/channeltest/testSeMa.java @ 157:f0d80a64aba0

*** empty log message ***
author kono
date Thu, 28 Aug 2008 16:20:17 +0900
parents fafbaaa0abd0
children 5b4be02e7243
comparison
equal deleted inserted replaced
156:31334767e65d 157:f0d80a64aba0
1 package test.channeltest; 1 package test.channeltest;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.net.InetSocketAddress;
5 import java.net.SocketAddress;
6 import java.nio.channels.SelectionKey;
4 import java.util.LinkedList; 7 import java.util.LinkedList;
5 8
6 import pathfinder.mergetest.channels.*; 9 import rep.channel.REPLogger;
10 import rep.channel.REPSelector;
11 import rep.channel.REPServerSocketChannel;
12 import rep.channel.REPSocketChannel;
13 import sun.nio.ch.SocketOpts.IP;
7 14
8 15
9 public class testSeMa extends Thread{ 16 public class testSeMa extends Thread{
10 17
11 int IP; 18 SocketAddress IP;
12 boolean running=true; 19 boolean running=true;
13 NetworkSimulator<String> ns; 20 REPLogger ns;
14 LinkedList<ChannelSimulator<String>> channels; 21 LinkedList<REPSocketChannel<String>> channels;
15 22
16 public testSeMa(NetworkSimulator<String> _ns, String name, int ip){ 23 public testSeMa(REPLogger _ns, String name, String host, int port){
17 super(name); 24 super(name);
18 IP = ip; 25 IP = new InetSocketAddress(host,port);
19 ns = _ns; 26 ns = _ns;
20 channels = new LinkedList<ChannelSimulator<String>>(); 27 channels = new LinkedList<REPSocketChannel<String>>();
21 } 28 }
22 public void init(){ 29 public void init(){
23 30
24 } 31 }
25 32
33 @SuppressWarnings("unchecked")
26 public void run() { 34 public void run() {
27 SelectorSimulator<String> selector = new SelectorSimulator<String>(); 35 REPSelector selector = new REPSelector(null);
28 ServerChannelSimulator<String> scs = new ServerChannelSimulator<String>(ns, selector); 36 REPServerSocketChannel<String> scs;
29 scs.bind(IP); 37 try {
30 selector.register(scs, SelectionKeySimulator.OP_ACCEPT); 38 scs = REPServerSocketChannel.<String>open();
39 scs.socket().setReuseAddress(true);
40 scs.socket().bind(IP);
41 scs.configureBlocking(false);
42 selector.register(scs, SelectionKey.OP_ACCEPT, null);
43 } catch (IOException e1) {
44 }
45
31 ns.writeLog("SessionManager starts mainroutin.", 1); 46 ns.writeLog("SessionManager starts mainroutin.", 1);
32 47
33 /* Main Loop */ 48 /* Main Loop */
34 while(running){ 49 while(running){
35 50
36 try { selector.select(); } 51 try { selector.select(); }
37 catch (IOException e) { e.printStackTrace();} 52 catch (IOException e) { e.printStackTrace();}
38 53
39 for(SelectionKeySimulator<String> key : selector.selectedKeys()){ 54 for(SelectionKey key : selector.selectedKeys()){
40 55
41 if(key.isAcceptable()){ 56 if(key.isAcceptable()){
42 ns.writeLog(this, "gets acceptable channel", 1); 57 ns.writeLog(this, "gets acceptable channel", 1);
43 ServerChannelSimulator<String> sc = (ServerChannelSimulator<String>) key.channel(); 58 REPServerSocketChannel<String> sc = (REPServerSocketChannel<String>) key.channel();
44 ChannelSimulator<String> channel = sc.accept(); 59 REPSocketChannel<String> channel;
45 selector.register(channel, SelectionKeySimulator.OP_READ); 60 try {
61 channel = sc.accept1();
62 selector.register(channel, SelectionKey.OP_READ, null);
63 } catch (IOException e) {
64 }
46 ns.writeLog(this, "accepts a client.", 1); 65 ns.writeLog(this, "accepts a client.", 1);
47 66
48 }else if(key.isReadable()){ 67 }else if(key.isReadable()){
49 ns.writeLog(this, "gets readable channel", 1); 68 ns.writeLog(this, "gets readable channel", 1);
50 //SelectableChannelSimulator<String> channel = key.channel(); 69 //SelectableChannelSimulator<String> channel = key.channel();
51 ChannelSimulator<String> channel = (ChannelSimulator<String>) key.channel(); 70 REPSocketChannel<String> channel = (REPSocketChannel<String>) key.channel();
52 String packet = channel.read(); 71 try {
53 ns.writeLog(this, "receives String==> `"+packet+"\'", 1); 72 String packet;
73 packet = channel.read();
74 ns.writeLog(this, "receives String==> `"+packet+"\'", 1);
75 } catch (IOException e) {
76 }
54 channel.write("from SeMa"+this.getName()+": world"); 77 channel.write("from SeMa"+this.getName()+": world");
55 } 78 }
56 } 79 }
57 } 80 }
58 81